Skip to contentSkip to conversation

hw/mips/loongson3: Remove uses of &first_cpu global

PATCH In review Philippe Mathieu-Daudé 2 participants 25 messages Jan 12, 2025 – Jan 15, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover hw/mips/loongson3: Remove uses of &first_cpu global

Propagate values from machine_init() in order to remove use of globals such &first_cpu and &current_machine.

Philippe Mathieu-Daudé (11):
hw/mips/loongson3_virt: Factor generic_cpu_reset() out hw/mips/loongson3_virt: Keep reference of vCPUs in machine_init() hw/mips/loongson3_virt: Have fw_conf_init() access local loaderparams hw/mips/loongson3_virt: Pass CPU argument to get_cpu_freq_hz() hw/mips/loongson3_bootp: Include missing headers hw/mips/loongson3: Propagate cpu_count to init_loongson_params() hw/mips/loongson3_virt: Propagate cpu_count to init_boot_param() hw/mips/loongson3_bootp: Propagate processor_id to init_cpu_info() hw/mips/loongson3_virt: Propagate processor_id to
init_loongson_params()
hw/mips/loongson3_virt: Propagate %processor_id to init_boot_param() hw/mips/loongson3_bootp: Move to common_ss[]

 hw/mips/loongson3_bootp.h |  1 +
 hw/mips/loongson3_bootp.c | 16 ++++++-----
 hw/mips/loongson3_virt.c  | 57 ++++++++++++++++++++++-----------------
 hw/mips/meson.build       |  3 ++-
 4 files changed, 45 insertions(+), 32 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/11 hw/mips/loongson3_virt: Factor generic_cpu_reset() out

main_cpu_reset() is misleadingly named "main": it resets all vCPUs, with a special case for the first vCPU.

Factor generic_cpu_reset() out of main_cpu_reset(), allowing to remove one &first_cpu use.

---
 hw/mips/loongson3_virt.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
hw/mips/loongson3_virt.c +17 -9
index f3cc7a8376f..47d112981a2 100644
@@ -399,25 +399,33 @@ static uint64_t load_kernel(CPUMIPSState *env)
     return kernel_entry;
 }
 
-static void main_cpu_reset(void *opaque)
+static void generic_cpu_reset(void *opaque)
 {
     MIPSCPU *cpu = opaque;
     CPUMIPSState *env = &cpu->env;
 
     cpu_reset(CPU(cpu));
 
-    /* Loongson-3 reset stuff */
     if (loaderparams.kernel_filename) {
-        if (cpu == MIPS_CPU(first_cpu)) {
-            env->active_tc.gpr[4] = loaderparams.a0;
-            env->active_tc.gpr[5] = loaderparams.a1;
-            env->active_tc.gpr[6] = loaderparams.a2;
-            env->active_tc.PC = loaderparams.kernel_entry;
-        }
         env->CP0_Status &= ~((1 << CP0St_BEV) | (1 << CP0St_ERL));
     }
 }
 
+static void main_cpu_reset(void *opaque)
+{
+    generic_cpu_reset(opaque);
+
+    if (loaderparams.kernel_filename) {
+        MIPSCPU *cpu = opaque;
+        CPUMIPSState *env = &cpu->env;
+
+        env->active_tc.gpr[4] = loaderparams.a0;
+        env->active_tc.gpr[5] = loaderparams.a1;
+        env->active_tc.gpr[6] = loaderparams.a2;
+        env->active_tc.PC = loaderparams.kernel_entry;
+    }
+}
+
 static inline void loongson3_virt_devices_init(MachineState *machine,
                                                DeviceState *pic)
 {
@@ -572,7 +580,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
         /* Init internal devices */
         cpu_mips_irq_init_cpu(cpu);
         cpu_mips_clock_init(cpu);
-        qemu_register_reset(main_cpu_reset, cpu);
+        qemu_register_reset(i ? generic_cpu_reset : main_cpu_reset, cpu);
 
         if (!kvm_enabled()) {
             hwaddr base = ((hwaddr)node << 44) + virt_memmap[VIRT_IPI].base;
Signature · 1 line
2.47.1
Reviewed-by on v1 1/11
#2 lore raw

Re: [PATCH 01/11] hw/mips/loongson3_virt: Factor generic_cpu_reset() out

Quoted 10 lines · Philippe Mathieu-Daudé

main_cpu_reset() is misleadingly named "main": it resets all vCPUs, with a special case for the first vCPU.

Factor generic_cpu_reset() out of main_cpu_reset(), allowing to remove one &first_cpu use.

---
  hw/mips/loongson3_virt.c | 26 +++++++++++++++++---------
  1 file changed, 17 insertions(+), 9 deletions(-)

r~

#3 lore raw

PATCH 2/11 hw/mips/loongson3_virt: Keep reference of vCPUs in machine_init()

In reply to Philippe Mathieu-Daudé

Keep references of all vCPUs created. That allows to directly access the first vCPU without using the &first_cpu global.

---
 hw/mips/loongson3_virt.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/mips/loongson3_virt.c +5 -4
index 47d112981a2..4b19941c1dc 100644
@@ -492,9 +492,8 @@ static void mips_loongson3_virt_init(MachineState *machine)
 {
     int i;
     long bios_size;
-    MIPSCPU *cpu;
+    g_autofree MIPSCPU **cpus = NULL;
     Clock *cpuclk;
-    CPUMIPSState *env;
     DeviceState *liointc;
     DeviceState *ipi = NULL;
     char *filename;
@@ -569,13 +568,16 @@ static void mips_loongson3_virt_init(MachineState *machine)
     cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
     clock_set_hz(cpuclk, DEF_LOONGSON3_FREQ);
 
+    cpus = g_new(MIPSCPU *, machine->smp.cpus);
     for (i = 0; i < machine->smp.cpus; i++) {
+        MIPSCPU *cpu;
         int node = i / LOONGSON3_CORE_PER_NODE;
         int core = i % LOONGSON3_CORE_PER_NODE;
         int ip;
 
         /* init CPUs */
         cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk, false);
+        cpus[i] = cpu;
 
         /* Init internal devices */
         cpu_mips_irq_init_cpu(cpu);
@@ -609,7 +611,6 @@ static void mips_loongson3_virt_init(MachineState *machine)
                                pin, cpu->env.irq[ip + 2]);
         }
     }
-    env = &MIPS_CPU(first_cpu)->env;
 
     /* Allocate RAM/BIOS, 0x00000000~0x10000000 is alias of 0x80000000~0x90000000 */
     memory_region_init_rom(bios, NULL, "loongson3.bios",
@@ -640,7 +641,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
         loaderparams.kernel_filename = kernel_filename;
         loaderparams.kernel_cmdline = kernel_cmdline;
         loaderparams.initrd_filename = initrd_filename;
-        loaderparams.kernel_entry = load_kernel(env);
+        loaderparams.kernel_entry = load_kernel(&cpus[0]->env);
 
         init_boot_rom();
         init_boot_param();
Signature · 1 line
2.47.1
#4 lore raw

Re: [PATCH 02/11] hw/mips/loongson3_virt: Keep reference of vCPUs in machine_init()

Quoted 50 lines · Philippe Mathieu-Daudé

Keep references of all vCPUs created. That allows to directly access the first vCPU without using the &first_cpu global.

---
  hw/mips/loongson3_virt.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
hw/mips/loongson3_virt.c +4 -3
index 47d112981a2..4b19941c1dc 100644
@@ -492,9 +492,8 @@ static void mips_loongson3_virt_init(MachineState *machine)
  {
      int i;
      long bios_size;
-    MIPSCPU *cpu;
+    g_autofree MIPSCPU **cpus = NULL;
      Clock *cpuclk;
-    CPUMIPSState *env;
      DeviceState *liointc;
      DeviceState *ipi = NULL;
      char *filename;
@@ -569,13 +568,16 @@ static void mips_loongson3_virt_init(MachineState *machine)
      cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
      clock_set_hz(cpuclk, DEF_LOONGSON3_FREQ);
 
+    cpus = g_new(MIPSCPU *, machine->smp.cpus);
      for (i = 0; i < machine->smp.cpus; i++) {
+        MIPSCPU *cpu;
          int node = i / LOONGSON3_CORE_PER_NODE;
          int core = i % LOONGSON3_CORE_PER_NODE;
          int ip;
 
          /* init CPUs */
          cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk, false);
+        cpus[i] = cpu;
 
          /* Init internal devices */
          cpu_mips_irq_init_cpu(cpu);
@@ -609,7 +611,6 @@ static void mips_loongson3_virt_init(MachineState *machine)
                                 pin, cpu->env.irq[ip + 2]);
          }
      }
-    env = &MIPS_CPU(first_cpu)->env;
 
      /* Allocate RAM/BIOS, 0x00000000~0x10000000 is alias of 0x80000000~0x90000000 */
      memory_region_init_rom(bios, NULL, "loongson3.bios",

hw/mips/loongson3_virt.c static void mips_loongson3_virt_init(MachineState *machine) · line 644

Quoted 5 lines
hw/mips/loongson3_virt.c +1 -1
@@ -640,7 +641,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
          loaderparams.kernel_filename = kernel_filename;
          loaderparams.kernel_cmdline = kernel_cmdline;
          loaderparams.initrd_filename = initrd_filename;
-        loaderparams.kernel_entry = load_kernel(env);
+        loaderparams.kernel_entry = load_kernel(&cpus[0]->env);

We only ever use cpu[0]. We don't really need the whole array.

r~

#5 lore raw

Re: [PATCH 02/11] hw/mips/loongson3_virt: Keep reference of vCPUs in machine_init()

Quoted 50 lines · Richard Henderson
Quoted 49 lines · Philippe Mathieu-Daudé

Keep references of all vCPUs created. That allows to directly access the first vCPU without using the &first_cpu global.

---
  hw/mips/loongson3_virt.c | 9 +++++----

  1 file changed, 5 insertions(+), 4 deletions(-)
hw/mips/loongson3_virt.c +1 -2
index 47d112981a2..4b19941c1dc 100644
@@ -492,9 +492,8 @@ static void mips_loongson3_virt_init(MachineState 
 *machine)
   {
       int i;
       long bios_size;
-    MIPSCPU *cpu;
+    g_autofree MIPSCPU **cpus = NULL;
       Clock *cpuclk;
-    CPUMIPSState *env;
       DeviceState *liointc;
       DeviceState *ipi = NULL;

      char *filename;

hw/mips/loongson3_virt.c +3 -1
@@ -569,13 +568,16 @@ static void 
 mips_loongson3_virt_init(MachineState *machine)
       cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
       clock_set_hz(cpuclk, DEF_LOONGSON3_FREQ);
+    cpus = g_new(MIPSCPU *, machine->smp.cpus);
       for (i = 0; i < machine->smp.cpus; i++) {
+        MIPSCPU *cpu;
           int node = i / LOONGSON3_CORE_PER_NODE;
           int core = i % LOONGSON3_CORE_PER_NODE;
           int ip;
           /* init CPUs */
           cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk, false);
+        cpus[i] = cpu;
           /* Init internal devices */
           cpu_mips_irq_init_cpu(cpu);
@@ -609,7 +611,6 @@ static void mips_loongson3_virt_init(MachineState 
 *machine)
                                  pin, cpu->env.irq[ip + 2]);
           }
       }
-    env = &MIPS_CPU(first_cpu)->env;
       /* Allocate RAM/BIOS, 0x00000000~0x10000000 is alias of 0x80000000~0x90000000 */
       memory_region_init_rom(bios, NULL, "loongson3.bios",
@@ -640,7 +641,7 @@ static void mips_loongson3_virt_init(MachineState 
Quoted 8 lines

hw/mips/loongson3_virt.c static void mips_loongson3_virt_init(MachineState · line 645

Quoted 6 lines
hw/mips/loongson3_virt.c +1 -1
 *machine)
           loaderparams.kernel_filename = kernel_filename;
           loaderparams.kernel_cmdline = kernel_cmdline;
           loaderparams.initrd_filename = initrd_filename;
-        loaderparams.kernel_entry = load_kernel(env);
+        loaderparams.kernel_entry = load_kernel(&cpus[0]->env);

We only ever use cpu[0].  We don't really need the whole array.

Yes. What about:

-- >8 --
commit ffc8c8873c0c102457f0e660437874555b022cc2

Author: Philippe Mathieu-Daudé <[email protected]>
Date:   Sun Jan 12 21:01:24 2025 +0100

hw/mips/loongson3_virt: Invert vCPU creation order to remove &first_cpu

Create vCPUs from the last one to the first one. No need to use the &first_cpu global since we already have it referenced.

hw/mips/loongson3_virt.c +2 -4
index 47d112981a2..488eba495cd 100644
@@ -496,3 +496,2 @@ static void mips_loongson3_virt_init(MachineState *machine)
     Clock *cpuclk;
-    CPUMIPSState *env;
     DeviceState *liointc;
@@ -571,3 +570,3 @@ static void mips_loongson3_virt_init(MachineState *machine)
 
-    for (i = 0; i < machine->smp.cpus; i++) {
+    for (i = machine->smp.cpus - 1; i >= 0; --i) {
         int node = i / LOONGSON3_CORE_PER_NODE;
@@ -611,3 +610,2 @@ static void mips_loongson3_virt_init(MachineState *machine)
     }
-    env = &MIPS_CPU(first_cpu)->env;
 
@@ -642,3 +640,3 @@ static void mips_loongson3_virt_init(MachineState *machine)
         loaderparams.initrd_filename = initrd_filename;
-        loaderparams.kernel_entry = load_kernel(env);
+        loaderparams.kernel_entry = load_kernel(&cpu->env);
 

---

#6 lore raw

Re: [PATCH 02/11] hw/mips/loongson3_virt: Keep reference of vCPUs in machine_init()

Quoted 89 lines · Philippe Mathieu-Daudé
Quoted 46 lines · Richard Henderson
Quoted 45 lines · Philippe Mathieu-Daudé

Keep references of all vCPUs created. That allows to directly access the first vCPU without using the &first_cpu global.

---
  hw/mips/loongson3_virt.c | 9 +++++----

  1 file changed, 5 insertions(+), 4 deletions(-)
hw/mips/loongson3_virt.c +4 -3
index 47d112981a2..4b19941c1dc 100644
@@ -492,9 +492,8 @@ static void mips_loongson3_virt_init(MachineState *machine)
   {
       int i;
       long bios_size;
-    MIPSCPU *cpu;
+    g_autofree MIPSCPU **cpus = NULL;
       Clock *cpuclk;
-    CPUMIPSState *env;
       DeviceState *liointc;
       DeviceState *ipi = NULL;
       char *filename;
@@ -569,13 +568,16 @@ static void mips_loongson3_virt_init(MachineState *machine)
       cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
       clock_set_hz(cpuclk, DEF_LOONGSON3_FREQ);
+    cpus = g_new(MIPSCPU *, machine->smp.cpus);
       for (i = 0; i < machine->smp.cpus; i++) {
+        MIPSCPU *cpu;
           int node = i / LOONGSON3_CORE_PER_NODE;
           int core = i % LOONGSON3_CORE_PER_NODE;
           int ip;
           /* init CPUs */
           cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk, false);
+        cpus[i] = cpu;
           /* Init internal devices */
           cpu_mips_irq_init_cpu(cpu);
@@ -609,7 +611,6 @@ static void mips_loongson3_virt_init(MachineState *machine)
                                  pin, cpu->env.irq[ip + 2]);
           }
       }
-    env = &MIPS_CPU(first_cpu)->env;
       /* Allocate RAM/BIOS, 0x00000000~0x10000000 is alias of 0x80000000~0x90000000 */
       memory_region_init_rom(bios, NULL, "loongson3.bios",
Quoted 8 lines

hw/mips/loongson3_virt.c static void mips_loongson3_virt_init(MachineState *machine) · line 644

Quoted 6 lines
hw/mips/loongson3_virt.c +1 -1
@@ -640,7 +641,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
           loaderparams.kernel_filename = kernel_filename;
           loaderparams.kernel_cmdline = kernel_cmdline;
           loaderparams.initrd_filename = initrd_filename;
-        loaderparams.kernel_entry = load_kernel(env);
+        loaderparams.kernel_entry = load_kernel(&cpus[0]->env);

We only ever use cpu[0].  We don't really need the whole array.

Yes. What about:

-- >8 --
commit ffc8c8873c0c102457f0e660437874555b022cc2
Author: Philippe Mathieu-Daudé <[email protected]> Date:   Sun Jan 12 21:01:24 2025 +0100

    hw/mips/loongson3_virt: Invert vCPU creation order to remove &first_cpu

    Create vCPUs from the last one to the first one. No need to use the &first_cpu global since we already have it referenced.

hw/mips/loongson3_virt.c +1 -3
index 47d112981a2..488eba495cd 100644
@@ -496,3 +496,2 @@ static void mips_loongson3_virt_init(MachineState *machine)
      Clock *cpuclk;
-    CPUMIPSState *env;
      DeviceState *liointc;
@@ -571,3 +570,3 @@ static void mips_loongson3_virt_init(MachineState *machine)
 
-    for (i = 0; i < machine->smp.cpus; i++) {
+    for (i = machine->smp.cpus - 1; i >= 0; --i) {
          int node = i / LOONGSON3_CORE_PER_NODE;
@@ -611,3 +610,2 @@ static void mips_loongson3_virt_init(MachineState *machine)
      }
-    env = &MIPS_CPU(first_cpu)->env;
 

hw/mips/loongson3_virt.c static void mips_loongson3_virt_init(MachineState *machine)

Quoted 5 lines
hw/mips/loongson3_virt.c +1 -2
@@ -642,3 +640,3 @@ static void mips_loongson3_virt_init(MachineState *machine)
          loaderparams.initrd_filename = initrd_filename;
-        loaderparams.kernel_entry = load_kernel(env);
+        loaderparams.kernel_entry = load_kernel(&cpu->env);
 
---

Looks good, thanks.

r~

#7 lore raw

PATCH 3/11 hw/mips/loongson3_virt: Have fw_conf_init() access local loaderparams

In reply to Philippe Mathieu-Daudé

'loaderparams' is declared statically. Let fw_conf_init() access its 'cpu_freq' and 'ram_size' fields.

---
 hw/mips/loongson3_virt.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
hw/mips/loongson3_virt.c +6 -6
index 4b19941c1dc..032ff92383e 100644
@@ -280,7 +280,7 @@ static void fw_cfg_boot_set(void *opaque, const char *boot_device,
     fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
 }
 
-static void fw_conf_init(unsigned long ram_size)
+static void fw_conf_init(void)
 {
     static const uint8_t suspend[6] = {128, 0, 0, 129, 128, 128};
     FWCfgState *fw_cfg;
@@ -289,9 +289,9 @@ static void fw_conf_init(unsigned long ram_size)
     fw_cfg = fw_cfg_init_mem_wide(cfg_addr, cfg_addr + 8, 8, 0, NULL);
     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)current_machine->smp.cpus);
     fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)current_machine->smp.max_cpus);
-    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
+    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, loaderparams.ram_size);
     fw_cfg_add_i32(fw_cfg, FW_CFG_MACHINE_VERSION, 1);
-    fw_cfg_add_i64(fw_cfg, FW_CFG_CPU_FREQ, get_cpu_freq_hz());
+    fw_cfg_add_i64(fw_cfg, FW_CFG_CPU_FREQ, loaderparams.cpu_freq);
 
     fw_cfg_add_file(fw_cfg, "etc/system-states",
                     g_memdup2(suspend, sizeof(suspend)), sizeof(suspend));
@@ -635,9 +635,9 @@ static void mips_loongson3_virt_init(MachineState *machine)
      * Please use -L to set the BIOS path and -bios to set bios name.
      */
 
+    loaderparams.cpu_freq = get_cpu_freq_hz();
+    loaderparams.ram_size = ram_size;
     if (kernel_filename) {
-        loaderparams.cpu_freq = get_cpu_freq_hz();
-        loaderparams.ram_size = ram_size;
         loaderparams.kernel_filename = kernel_filename;
         loaderparams.kernel_cmdline = kernel_cmdline;
         loaderparams.initrd_filename = initrd_filename;
@@ -663,7 +663,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
             exit(1);
         }
 
-        fw_conf_init(ram_size);
+        fw_conf_init();
     }
 
     loongson3_virt_devices_init(machine, liointc);
Signature · 1 line
2.47.1
Reviewed-by on v1 3/11
#8 lore raw

Re: [PATCH 03/11] hw/mips/loongson3_virt: Have fw_conf_init() access local loaderparams

Quoted 7 lines · Philippe Mathieu-Daudé

'loaderparams' is declared statically. Let fw_conf_init() access its 'cpu_freq' and 'ram_size' fields.

---
  hw/mips/loongson3_virt.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)

r~

#9 lore raw

PATCH 4/11 hw/mips/loongson3_virt: Pass CPU argument to get_cpu_freq_hz()

In reply to Philippe Mathieu-Daudé

Pass the first vCPU as argument, allowing to remove another &first_cpu global use.

---
 hw/mips/loongson3_virt.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/mips/loongson3_virt.c +3 -3
index 032ff92383e..078ad46174f 100644
@@ -153,7 +153,7 @@ static const MemoryRegionOps loongson3_pm_ops = {
 
 #define DEF_LOONGSON3_FREQ (800 * 1000 * 1000)
 
-static uint64_t get_cpu_freq_hz(void)
+static uint64_t get_cpu_freq_hz(const MIPSCPU *cpu)
 {
 #ifdef CONFIG_KVM
     int ret;
@@ -164,7 +164,7 @@ static uint64_t get_cpu_freq_hz(void)
     };
 
     if (kvm_enabled()) {
-        ret = kvm_vcpu_ioctl(first_cpu, KVM_GET_ONE_REG, &freq_reg);
+        ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_ONE_REG, &freq_reg);
         if (ret >= 0) {
             return freq * 2;
         }
@@ -635,7 +635,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
      * Please use -L to set the BIOS path and -bios to set bios name.
      */
 
-    loaderparams.cpu_freq = get_cpu_freq_hz();
+    loaderparams.cpu_freq = get_cpu_freq_hz(cpus[0]);
     loaderparams.ram_size = ram_size;
     if (kernel_filename) {
         loaderparams.kernel_filename = kernel_filename;
Signature · 1 line
2.47.1
#11 lore raw

PATCH 5/11 hw/mips/loongson3_bootp: Include missing headers

In reply to Philippe Mathieu-Daudé

MemMapEntry is declared in "exec/hwaddr.h", cpu_to_le32() in "qemu/bswap.h". These headers are indirectly included via "cpu.h". Include them explicitly in order to avoid when removing "cpu.h":

In file included from ../../hw/mips/loongson3_bootp.c:27:
hw/mips/loongson3_bootp.h:234:14: error: unknown type name 'MemMapEntry'

    234 | extern const MemMapEntry virt_memmap[];
        |              ^
  hw/mips/loongson3_bootp.c:33:18: error: call to undeclared function 'cpu_to_le32'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     33 |     c->cputype = cpu_to_le32(Loongson_3A);
        |                  ^
---
 hw/mips/loongson3_bootp.c | 2 ++
 1 file changed, 2 insertions(+)
hw/mips/loongson3_bootp.c +2 -0
index b97b81903b7..712439c2575 100644
@@ -21,6 +21,8 @@
 #include "qemu/osdep.h"
 #include "qemu/units.h"
 #include "qemu/cutils.h"
+#include "qemu/bswap.h"
+#include "exec/hwaddr.h"
 #include "cpu.h"
 #include "hw/boards.h"
 #include "hw/mips/loongson3_bootp.h"
Signature · 1 line
2.47.1
Reviewed-by on v1 5/11
#12 lore raw

Re: [PATCH 05/11] hw/mips/loongson3_bootp: Include missing headers

Quoted 22 lines · Philippe Mathieu-Daudé

MemMapEntry is declared in "exec/hwaddr.h", cpu_to_le32() in "qemu/bswap.h". These headers are indirectly included via "cpu.h". Include them explicitly in order to avoid when removing "cpu.h":

   In file included from ../../hw/mips/loongson3_bootp.c:27:
   hw/mips/loongson3_bootp.h:234:14: error: unknown type name 'MemMapEntry'
     234 | extern const MemMapEntry virt_memmap[];
         |              ^
   hw/mips/loongson3_bootp.c:33:18: error: call to undeclared function 'cpu_to_le32'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
      33 |     c->cputype = cpu_to_le32(Loongson_3A);
         |                  ^
---
  hw/mips/loongson3_bootp.c | 2 ++
  1 file changed, 2 insertions(+)
hw/mips/loongson3_bootp.c +0 -0
index b97b81903b7..712439c2575 100644

hw/mips/loongson3_bootp.c line 28

Quoted 8 lines
hw/mips/loongson3_bootp.c +2 -0
@@ -21,6 +21,8 @@
  #include "qemu/osdep.h"
  #include "qemu/units.h"
  #include "qemu/cutils.h"
+#include "qemu/bswap.h"
+#include "exec/hwaddr.h"
  #include "cpu.h"
  #include "hw/boards.h"
  #include "hw/mips/loongson3_bootp.h"

r~

#13 lore raw

PATCH 6/11 hw/mips/loongson3: Propagate cpu_count to init_loongson_params()

In reply to Philippe Mathieu-Daudé

Propagate the %cpu_count from the machine file, allowing to remove the "hw/boards.h" dependency (which is machine specific) from loongson3_bootp.

---
 hw/mips/loongson3_bootp.h |  1 +
 hw/mips/loongson3_bootp.c | 11 ++++++-----
 hw/mips/loongson3_virt.c  |  1 +
 3 files changed, 8 insertions(+), 5 deletions(-)
hw/mips/loongson3_bootp.h +1 -0
index 9091265df7f..ee6340e42c1 100644
@@ -233,6 +233,7 @@ enum {
 
 extern const MemMapEntry virt_memmap[];
 void init_loongson_params(struct loongson_params *lp, void *p,
+                          uint32_t cpu_count,
                           uint64_t cpu_freq, uint64_t ram_size);
 void init_reset_system(struct efi_reset_system_t *reset);
 
hw/mips/loongson3_bootp.c +6 -5
index 712439c2575..91b58a71a68 100644
@@ -24,10 +24,10 @@
 #include "qemu/bswap.h"
 #include "exec/hwaddr.h"
 #include "cpu.h"
-#include "hw/boards.h"
 #include "hw/mips/loongson3_bootp.h"
 
-static void init_cpu_info(void *g_cpuinfo, uint64_t cpu_freq)
+static void init_cpu_info(void *g_cpuinfo, uint32_t cpu_count,
+                          uint64_t cpu_freq)
 {
     struct efi_cpuinfo_loongson *c = g_cpuinfo;
 
@@ -40,8 +40,8 @@ static void init_cpu_info(void *g_cpuinfo, uint64_t cpu_freq)
     }
 
     c->cpu_startup_core_id = cpu_to_le16(0);
-    c->nr_cpus = cpu_to_le32(current_machine->smp.cpus);
-    c->total_node = cpu_to_le32(DIV_ROUND_UP(current_machine->smp.cpus,
+    c->nr_cpus = cpu_to_le32(cpu_count);
+    c->total_node = cpu_to_le32(DIV_ROUND_UP(cpu_count,
                                              LOONGSON3_CORE_PER_NODE));
 }
 
@@ -112,9 +112,10 @@ static void init_special_info(void *g_special)
 }
 
 void init_loongson_params(struct loongson_params *lp, void *p,
+                          uint32_t cpu_count,
                           uint64_t cpu_freq, uint64_t ram_size)
 {
-    init_cpu_info(p, cpu_freq);
+    init_cpu_info(p, cpu_count, cpu_freq);
     lp->cpu_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
     p += ROUND_UP(sizeof(struct efi_cpuinfo_loongson), 64);
 
hw/mips/loongson3_virt.c +1 -0
index 078ad46174f..af1937455b0 100644
@@ -185,6 +185,7 @@ static void init_boot_param(void)
     init_reset_system(&(bp->reset_system));
     p += ROUND_UP(sizeof(struct boot_params), 64);
     init_loongson_params(&(bp->efi.smbios.lp), p,
+                         current_machine->smp.cpus,
                          loaderparams.cpu_freq, loaderparams.ram_size);
 
     rom_add_blob_fixed("params_rom", bp,
Signature · 1 line
2.47.1
Reviewed-by on v1 6/11
#14 lore raw

Re: [PATCH 06/11] hw/mips/loongson3: Propagate cpu_count to init_loongson_params()

Quoted 10 lines · Philippe Mathieu-Daudé

Propagate the %cpu_count from the machine file, allowing to remove the "hw/boards.h" dependency (which is machine specific) from loongson3_bootp.

---
  hw/mips/loongson3_bootp.h |  1 +
  hw/mips/loongson3_bootp.c | 11 ++++++-----
  hw/mips/loongson3_virt.c  |  1 +
  3 files changed, 8 insertions(+), 5 deletions(-)

r~

#15 lore raw

PATCH 7/11 hw/mips/loongson3_virt: Propagate cpu_count to init_boot_param()

In reply to Philippe Mathieu-Daudé

Remove one use of the 'current_machine' global.

---
 hw/mips/loongson3_virt.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/mips/loongson3_virt.c +3 -4
index af1937455b0..a240662016b 100644
@@ -173,7 +173,7 @@ static uint64_t get_cpu_freq_hz(const MIPSCPU *cpu)
     return DEF_LOONGSON3_FREQ;
 }
 
-static void init_boot_param(void)
+static void init_boot_param(unsigned cpu_count)
 {
     static void *p;
     struct boot_params *bp;
@@ -184,8 +184,7 @@ static void init_boot_param(void)
     bp->efi.smbios.vers = cpu_to_le16(1);
     init_reset_system(&(bp->reset_system));
     p += ROUND_UP(sizeof(struct boot_params), 64);
-    init_loongson_params(&(bp->efi.smbios.lp), p,
-                         current_machine->smp.cpus,
+    init_loongson_params(&(bp->efi.smbios.lp), p, cpu_count,
                          loaderparams.cpu_freq, loaderparams.ram_size);
 
     rom_add_blob_fixed("params_rom", bp,
@@ -645,7 +644,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
         loaderparams.kernel_entry = load_kernel(&cpus[0]->env);
 
         init_boot_rom();
-        init_boot_param();
+        init_boot_param(machine->smp.cpus);
     } else {
         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
                                   machine->firmware ?: LOONGSON3_BIOSNAME);
Signature · 1 line
2.47.1
Reviewed-by on v1 7/11
#16 lore raw

Re: [PATCH 07/11] hw/mips/loongson3_virt: Propagate cpu_count to init_boot_param()

Quoted 6 lines · Philippe Mathieu-Daudé

Remove one use of the 'current_machine' global.

---
  hw/mips/loongson3_virt.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)

r~

Quoted 33 lines
hw/mips/loongson3_virt.c +3 -4
index af1937455b0..a240662016b 100644
@@ -173,7 +173,7 @@ static uint64_t get_cpu_freq_hz(const MIPSCPU *cpu)
      return DEF_LOONGSON3_FREQ;
  }
 
-static void init_boot_param(void)
+static void init_boot_param(unsigned cpu_count)
  {
      static void *p;
      struct boot_params *bp;
@@ -184,8 +184,7 @@ static void init_boot_param(void)
      bp->efi.smbios.vers = cpu_to_le16(1);
      init_reset_system(&(bp->reset_system));
      p += ROUND_UP(sizeof(struct boot_params), 64);
-    init_loongson_params(&(bp->efi.smbios.lp), p,
-                         current_machine->smp.cpus,
+    init_loongson_params(&(bp->efi.smbios.lp), p, cpu_count,
                           loaderparams.cpu_freq, loaderparams.ram_size);
 
      rom_add_blob_fixed("params_rom", bp,
@@ -645,7 +644,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
          loaderparams.kernel_entry = load_kernel(&cpus[0]->env);
 
          init_boot_rom();
-        init_boot_param();
+        init_boot_param(machine->smp.cpus);
      } else {
          filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
                                    machine->firmware ?: LOONGSON3_BIOSNAME);
#17 lore raw

PATCH 8/11 hw/mips/loongson3_bootp: Propagate processor_id to init_cpu_info()

In reply to Philippe Mathieu-Daudé
---
 hw/mips/loongson3_bootp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/mips/loongson3_bootp.c +3 -3
index 91b58a71a68..1aab26df69e 100644
@@ -27,12 +27,12 @@
 #include "hw/mips/loongson3_bootp.h"
 
 static void init_cpu_info(void *g_cpuinfo, uint32_t cpu_count,
-                          uint64_t cpu_freq)
+                          uint32_t processor_id, uint64_t cpu_freq)
 {
     struct efi_cpuinfo_loongson *c = g_cpuinfo;
 
     c->cputype = cpu_to_le32(Loongson_3A);
-    c->processor_id = cpu_to_le32(MIPS_CPU(first_cpu)->env.CP0_PRid);
+    c->processor_id = cpu_to_le32(processor_id);
     if (cpu_freq > UINT_MAX) {
         c->cpu_clock_freq = cpu_to_le32(UINT_MAX);
     } else {
@@ -115,7 +115,7 @@ void init_loongson_params(struct loongson_params *lp, void *p,
                           uint32_t cpu_count,
                           uint64_t cpu_freq, uint64_t ram_size)
 {
-    init_cpu_info(p, cpu_count, cpu_freq);
+    init_cpu_info(p, MIPS_CPU(first_cpu)->env.CP0_PRid, cpu_count, cpu_freq);
     lp->cpu_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
     p += ROUND_UP(sizeof(struct efi_cpuinfo_loongson), 64);
 
Signature · 1 line
2.47.1
#19 lore raw

PATCH 9/11 hw/mips/loongson3_virt: Propagate processor_id to init_loongson_params()

In reply to Philippe Mathieu-Daudé

Remove one &first_cpu use in hw/mips/loongson3_bootp.c.

---
 hw/mips/loongson3_bootp.h | 2 +-
 hw/mips/loongson3_bootp.c | 5 ++---
 hw/mips/loongson3_virt.c  | 1 +
 3 files changed, 4 insertions(+), 4 deletions(-)
hw/mips/loongson3_bootp.h +1 -1
index ee6340e42c1..9dc325a8557 100644
@@ -233,7 +233,7 @@ enum {
 
 extern const MemMapEntry virt_memmap[];
 void init_loongson_params(struct loongson_params *lp, void *p,
-                          uint32_t cpu_count,
+                          uint32_t cpu_count, uint32_t processor_id,
                           uint64_t cpu_freq, uint64_t ram_size);
 void init_reset_system(struct efi_reset_system_t *reset);
 
hw/mips/loongson3_bootp.c +2 -3
index 1aab26df69e..67812666c5b 100644
@@ -23,7 +23,6 @@
 #include "qemu/cutils.h"
 #include "qemu/bswap.h"
 #include "exec/hwaddr.h"
-#include "cpu.h"
 #include "hw/mips/loongson3_bootp.h"
 
 static void init_cpu_info(void *g_cpuinfo, uint32_t cpu_count,
@@ -112,10 +111,10 @@ static void init_special_info(void *g_special)
 }
 
 void init_loongson_params(struct loongson_params *lp, void *p,
-                          uint32_t cpu_count,
+                          uint32_t cpu_count, uint32_t processor_id,
                           uint64_t cpu_freq, uint64_t ram_size)
 {
-    init_cpu_info(p, MIPS_CPU(first_cpu)->env.CP0_PRid, cpu_count, cpu_freq);
+    init_cpu_info(p, cpu_count, processor_id, cpu_freq);
     lp->cpu_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
     p += ROUND_UP(sizeof(struct efi_cpuinfo_loongson), 64);
 
hw/mips/loongson3_virt.c +1 -0
index a240662016b..45cd348c14e 100644
@@ -185,6 +185,7 @@ static void init_boot_param(unsigned cpu_count)
     init_reset_system(&(bp->reset_system));
     p += ROUND_UP(sizeof(struct boot_params), 64);
     init_loongson_params(&(bp->efi.smbios.lp), p, cpu_count,
+                         MIPS_CPU(first_cpu)->env.CP0_PRid,
                          loaderparams.cpu_freq, loaderparams.ram_size);
 
     rom_add_blob_fixed("params_rom", bp,
Signature · 1 line
2.47.1
Reviewed-by on v1 9/11
#20 lore raw

Re: [PATCH 09/11] hw/mips/loongson3_virt: Propagate processor_id to init_loongson_params()

Quoted 8 lines · Philippe Mathieu-Daudé

Remove one &first_cpu use in hw/mips/loongson3_bootp.c.

---
  hw/mips/loongson3_bootp.h | 2 +-
  hw/mips/loongson3_bootp.c | 5 ++---
  hw/mips/loongson3_virt.c  | 1 +
  3 files changed, 4 insertions(+), 4 deletions(-)

r~

#21 lore raw

PATCH 10/11 hw/mips/loongson3_virt: Propagate %processor_id to init_boot_param()

In reply to Philippe Mathieu-Daudé

Propagate %processor_id from mips_loongson3_virt_init() where we have a reference to the first vCPU, so use it instead of the &first_cpu global.

---
 hw/mips/loongson3_virt.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/mips/loongson3_virt.c +3 -4
index 45cd348c14e..59b1619df0c 100644
@@ -173,7 +173,7 @@ static uint64_t get_cpu_freq_hz(const MIPSCPU *cpu)
     return DEF_LOONGSON3_FREQ;
 }
 
-static void init_boot_param(unsigned cpu_count)
+static void init_boot_param(unsigned cpu_count, uint32_t processor_id)
 {
     static void *p;
     struct boot_params *bp;
@@ -184,8 +184,7 @@ static void init_boot_param(unsigned cpu_count)
     bp->efi.smbios.vers = cpu_to_le16(1);
     init_reset_system(&(bp->reset_system));
     p += ROUND_UP(sizeof(struct boot_params), 64);
-    init_loongson_params(&(bp->efi.smbios.lp), p, cpu_count,
-                         MIPS_CPU(first_cpu)->env.CP0_PRid,
+    init_loongson_params(&(bp->efi.smbios.lp), p, cpu_count, processor_id,
                          loaderparams.cpu_freq, loaderparams.ram_size);
 
     rom_add_blob_fixed("params_rom", bp,
@@ -645,7 +644,7 @@ static void mips_loongson3_virt_init(MachineState *machine)
         loaderparams.kernel_entry = load_kernel(&cpus[0]->env);
 
         init_boot_rom();
-        init_boot_param(machine->smp.cpus);
+        init_boot_param(machine->smp.cpus, cpus[0]->env.CP0_PRid);
     } else {
         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
                                   machine->firmware ?: LOONGSON3_BIOSNAME);
Signature · 1 line
2.47.1
Reviewed-by on v1 10/11
#22 lore raw

Re: [PATCH 10/11] hw/mips/loongson3_virt: Propagate %processor_id to init_boot_param()

Quoted 8 lines · Philippe Mathieu-Daudé

Propagate %processor_id from mips_loongson3_virt_init() where we have a reference to the first vCPU, so use it instead of the &first_cpu global.

---
  hw/mips/loongson3_virt.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)

r~

#23 lore raw

PATCH 11/11 hw/mips/loongson3_bootp: Move to common_ss[]

In reply to Philippe Mathieu-Daudé

loongson3_bootp.c doesn't contain any target-specific code and can be build generically, move it to common_ss[].

---
 hw/mips/meson.build | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/mips/meson.build +2 -1
index fcbee53bb32..31dbd2bf4d9 100644
@@ -1,7 +1,8 @@
 mips_ss = ss.source_set()
 mips_ss.add(files('bootloader.c', 'mips_int.c'))
 common_ss.add(when: 'CONFIG_FW_CFG_MIPS', if_true: files('fw_cfg.c'))
-mips_ss.add(when: 'CONFIG_LOONGSON3V', if_true: files('loongson3_bootp.c', 'loongson3_virt.c'))
+common_ss.add(when: 'CONFIG_LOONGSON3V', if_true: files('loongson3_bootp.c'))
+mips_ss.add(when: 'CONFIG_LOONGSON3V', if_true: files('loongson3_virt.c'))
 mips_ss.add(when: 'CONFIG_MALTA', if_true: files('malta.c'))
 mips_ss.add(when: 'CONFIG_MIPS_CPS', if_true: files('cps.c'))
 
Signature · 1 line
2.47.1
Reviewed-by on v1 11/11
#24 lore raw

Re: [PATCH 11/11] hw/mips/loongson3_bootp: Move to common_ss[]

Quoted 13 lines · Philippe Mathieu-Daudé

loongson3_bootp.c doesn't contain any target-specific code and can be build generically, move it to common_ss[].

---
  hw/mips/meson.build | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/mips/meson.build +0 -0
index fcbee53bb32..31dbd2bf4d9 100644

hw/mips/meson.build line 8

Quoted 9 lines
hw/mips/meson.build +2 -1
@@ -1,7 +1,8 @@
  mips_ss = ss.source_set()
  mips_ss.add(files('bootloader.c', 'mips_int.c'))
  common_ss.add(when: 'CONFIG_FW_CFG_MIPS', if_true: files('fw_cfg.c'))
-mips_ss.add(when: 'CONFIG_LOONGSON3V', if_true: files('loongson3_bootp.c', 'loongson3_virt.c'))
+common_ss.add(when: 'CONFIG_LOONGSON3V', if_true: files('loongson3_bootp.c'))
+mips_ss.add(when: 'CONFIG_LOONGSON3V', if_true: files('loongson3_virt.c'))
  mips_ss.add(when: 'CONFIG_MALTA', if_true: files('malta.c'))
  mips_ss.add(when: 'CONFIG_MIPS_CPS', if_true: files('cps.c'))
 

r~