[Qemu-devel] [PATCH v3 1/1] ppc: spapr: Make VCPU ID handling private to SPAPR

2017-08-08 Thread Sam Bobroff
The concept of a VCPU ID that differs from the CPU's index
(cpu->cpu_index) exists only within SPAPR machines so, move the
functions ppc_get_vcpu_id() and ppc_get_cpu_by_vcpu_id() into spapr.c
and rename them appropriately.

Signed-off-by: Sam Bobroff 
---
Changes in v3:

* Implemented spapr_find_cpu() using spapr_vcpu_id() rather than direct access
  to vcpu_id.

 hw/ppc/ppc.c   | 21 -
 hw/ppc/spapr.c | 40 +---
 hw/ppc/spapr_hcall.c   |  4 ++--
 hw/ppc/spapr_rtas.c|  4 ++--
 include/hw/ppc/spapr.h |  3 +++
 target/ppc/cpu.h   | 18 --
 target/ppc/kvm.c   |  2 +-
 7 files changed, 41 insertions(+), 51 deletions(-)

diff --git a/hw/ppc/ppc.c b/hw/ppc/ppc.c
index 4477d4ad89..f76886f4d3 100644
--- a/hw/ppc/ppc.c
+++ b/hw/ppc/ppc.c
@@ -1358,27 +1358,6 @@ void PPC_debug_write (void *opaque, uint32_t addr, 
uint32_t val)
 }
 }
 
-/* CPU device-tree ID helpers */
-int ppc_get_vcpu_id(PowerPCCPU *cpu)
-{
-return cpu->vcpu_id;
-}
-
-PowerPCCPU *ppc_get_cpu_by_vcpu_id(int vcpu_id)
-{
-CPUState *cs;
-
-CPU_FOREACH(cs) {
-PowerPCCPU *cpu = POWERPC_CPU(cs);
-
-if (cpu->vcpu_id == vcpu_id) {
-return cpu;
-}
-}
-
-return NULL;
-}
-
 void ppc_cpu_parse_features(const char *cpu_model)
 {
 CPUClass *cc;
diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
index d6c9b3e334..cd6eb2d4a9 100644
--- a/hw/ppc/spapr.c
+++ b/hw/ppc/spapr.c
@@ -208,7 +208,7 @@ static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, 
PowerPCCPU *cpu,
 int i, ret = 0;
 uint32_t servers_prop[smt_threads];
 uint32_t gservers_prop[smt_threads * 2];
-int index = ppc_get_vcpu_id(cpu);
+int index = spapr_vcpu_id(cpu);
 
 if (cpu->compat_pvr) {
 ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
@@ -237,7 +237,7 @@ static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, 
PowerPCCPU *cpu,
 
 static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, PowerPCCPU *cpu)
 {
-int index = ppc_get_vcpu_id(cpu);
+int index = spapr_vcpu_id(cpu);
 uint32_t associativity[] = {cpu_to_be32(0x5),
 cpu_to_be32(0x0),
 cpu_to_be32(0x0),
@@ -341,7 +341,7 @@ static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState 
*spapr)
 PowerPCCPU *cpu = POWERPC_CPU(cs);
 CPUPPCState *env = &cpu->env;
 DeviceClass *dc = DEVICE_GET_CLASS(cs);
-int index = ppc_get_vcpu_id(cpu);
+int index = spapr_vcpu_id(cpu);
 int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
 
 if ((index % smt) != 0) {
@@ -493,7 +493,7 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, 
int offset,
 PowerPCCPU *cpu = POWERPC_CPU(cs);
 CPUPPCState *env = &cpu->env;
 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
-int index = ppc_get_vcpu_id(cpu);
+int index = spapr_vcpu_id(cpu);
 uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
0x, 0x};
 uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
@@ -626,7 +626,7 @@ static void spapr_populate_cpus_dt_node(void *fdt, 
sPAPRMachineState *spapr)
  */
 CPU_FOREACH_REVERSE(cs) {
 PowerPCCPU *cpu = POWERPC_CPU(cs);
-int index = ppc_get_vcpu_id(cpu);
+int index = spapr_vcpu_id(cpu);
 DeviceClass *dc = DEVICE_GET_CLASS(cs);
 int offset;
 
@@ -2982,7 +2982,7 @@ static void *spapr_populate_hotplug_cpu_dt(CPUState *cs, 
int *fdt_offset,
 {
 PowerPCCPU *cpu = POWERPC_CPU(cs);
 DeviceClass *dc = DEVICE_GET_CLASS(cs);
-int id = ppc_get_vcpu_id(cpu);
+int id = spapr_vcpu_id(cpu);
 void *fdt;
 int offset, fdt_size;
 char *nodename;
@@ -3392,7 +3392,7 @@ static void spapr_ics_resend(XICSFabric *dev)
 
 static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
 {
-PowerPCCPU *cpu = ppc_get_cpu_by_vcpu_id(vcpu_id);
+PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
 
 return cpu ? ICP(cpu->intc) : NULL;
 }
@@ -3412,6 +3412,32 @@ static void spapr_pic_print_info(InterruptStatsProvider 
*obj,
 ics_pic_print_info(spapr->ics, mon);
 }
 
+int spapr_vcpu_id(PowerPCCPU *cpu)
+{
+CPUState *cs = CPU(cpu);
+
+if (kvm_enabled()) {
+return kvm_arch_vcpu_id(cs);
+} else {
+return cs->cpu_index;
+}
+}
+
+PowerPCCPU *spapr_find_cpu(int vcpu_id)
+{
+CPUState *cs;
+
+CPU_FOREACH(cs) {
+PowerPCCPU *cpu = POWERPC_CPU(cs);
+
+if (spapr_vcpu_id(cpu) == vcpu_id) {
+return cpu;
+}
+}
+
+return NULL;
+}
+
 static void spapr_machine_class_init(ObjectClass *oc, void *data)
 {
 MachineClass *mc = MACHINE_CLASS(oc);
diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
index 4ca233854a..7cf0993800 100644
--- a/hw/ppc/spapr_hcall.c
+++ b/hw/ppc/spapr_hcall.c
@@ -999,7 +999,7 @@ static target_ulong h_register_vpa(PowerPCCP

Re: [Qemu-devel] [PATCH v3 1/1] ppc: spapr: Make VCPU ID handling private to SPAPR

2017-08-09 Thread David Gibson
On Wed, Aug 09, 2017 at 03:38:56PM +1000, Sam Bobroff wrote:
> The concept of a VCPU ID that differs from the CPU's index
> (cpu->cpu_index) exists only within SPAPR machines so, move the
> functions ppc_get_vcpu_id() and ppc_get_cpu_by_vcpu_id() into spapr.c
> and rename them appropriately.
> 
> Signed-off-by: Sam Bobroff 

Applied to ppc-for-2.11, thanks.

> ---
> Changes in v3:
> 
> * Implemented spapr_find_cpu() using spapr_vcpu_id() rather than direct access
>   to vcpu_id.
> 
>  hw/ppc/ppc.c   | 21 -
>  hw/ppc/spapr.c | 40 +---
>  hw/ppc/spapr_hcall.c   |  4 ++--
>  hw/ppc/spapr_rtas.c|  4 ++--
>  include/hw/ppc/spapr.h |  3 +++
>  target/ppc/cpu.h   | 18 --
>  target/ppc/kvm.c   |  2 +-
>  7 files changed, 41 insertions(+), 51 deletions(-)
> 
> diff --git a/hw/ppc/ppc.c b/hw/ppc/ppc.c
> index 4477d4ad89..f76886f4d3 100644
> --- a/hw/ppc/ppc.c
> +++ b/hw/ppc/ppc.c
> @@ -1358,27 +1358,6 @@ void PPC_debug_write (void *opaque, uint32_t addr, 
> uint32_t val)
>  }
>  }
>  
> -/* CPU device-tree ID helpers */
> -int ppc_get_vcpu_id(PowerPCCPU *cpu)
> -{
> -return cpu->vcpu_id;
> -}
> -
> -PowerPCCPU *ppc_get_cpu_by_vcpu_id(int vcpu_id)
> -{
> -CPUState *cs;
> -
> -CPU_FOREACH(cs) {
> -PowerPCCPU *cpu = POWERPC_CPU(cs);
> -
> -if (cpu->vcpu_id == vcpu_id) {
> -return cpu;
> -}
> -}
> -
> -return NULL;
> -}
> -
>  void ppc_cpu_parse_features(const char *cpu_model)
>  {
>  CPUClass *cc;
> diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
> index d6c9b3e334..cd6eb2d4a9 100644
> --- a/hw/ppc/spapr.c
> +++ b/hw/ppc/spapr.c
> @@ -208,7 +208,7 @@ static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, 
> PowerPCCPU *cpu,
>  int i, ret = 0;
>  uint32_t servers_prop[smt_threads];
>  uint32_t gservers_prop[smt_threads * 2];
> -int index = ppc_get_vcpu_id(cpu);
> +int index = spapr_vcpu_id(cpu);
>  
>  if (cpu->compat_pvr) {
>  ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
> @@ -237,7 +237,7 @@ static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, 
> PowerPCCPU *cpu,
>  
>  static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, PowerPCCPU *cpu)
>  {
> -int index = ppc_get_vcpu_id(cpu);
> +int index = spapr_vcpu_id(cpu);
>  uint32_t associativity[] = {cpu_to_be32(0x5),
>  cpu_to_be32(0x0),
>  cpu_to_be32(0x0),
> @@ -341,7 +341,7 @@ static int spapr_fixup_cpu_dt(void *fdt, 
> sPAPRMachineState *spapr)
>  PowerPCCPU *cpu = POWERPC_CPU(cs);
>  CPUPPCState *env = &cpu->env;
>  DeviceClass *dc = DEVICE_GET_CLASS(cs);
> -int index = ppc_get_vcpu_id(cpu);
> +int index = spapr_vcpu_id(cpu);
>  int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
>  
>  if ((index % smt) != 0) {
> @@ -493,7 +493,7 @@ static void spapr_populate_cpu_dt(CPUState *cs, void 
> *fdt, int offset,
>  PowerPCCPU *cpu = POWERPC_CPU(cs);
>  CPUPPCState *env = &cpu->env;
>  PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
> -int index = ppc_get_vcpu_id(cpu);
> +int index = spapr_vcpu_id(cpu);
>  uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
> 0x, 0x};
>  uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
> @@ -626,7 +626,7 @@ static void spapr_populate_cpus_dt_node(void *fdt, 
> sPAPRMachineState *spapr)
>   */
>  CPU_FOREACH_REVERSE(cs) {
>  PowerPCCPU *cpu = POWERPC_CPU(cs);
> -int index = ppc_get_vcpu_id(cpu);
> +int index = spapr_vcpu_id(cpu);
>  DeviceClass *dc = DEVICE_GET_CLASS(cs);
>  int offset;
>  
> @@ -2982,7 +2982,7 @@ static void *spapr_populate_hotplug_cpu_dt(CPUState 
> *cs, int *fdt_offset,
>  {
>  PowerPCCPU *cpu = POWERPC_CPU(cs);
>  DeviceClass *dc = DEVICE_GET_CLASS(cs);
> -int id = ppc_get_vcpu_id(cpu);
> +int id = spapr_vcpu_id(cpu);
>  void *fdt;
>  int offset, fdt_size;
>  char *nodename;
> @@ -3392,7 +3392,7 @@ static void spapr_ics_resend(XICSFabric *dev)
>  
>  static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
>  {
> -PowerPCCPU *cpu = ppc_get_cpu_by_vcpu_id(vcpu_id);
> +PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
>  
>  return cpu ? ICP(cpu->intc) : NULL;
>  }
> @@ -3412,6 +3412,32 @@ static void 
> spapr_pic_print_info(InterruptStatsProvider *obj,
>  ics_pic_print_info(spapr->ics, mon);
>  }
>  
> +int spapr_vcpu_id(PowerPCCPU *cpu)
> +{
> +CPUState *cs = CPU(cpu);
> +
> +if (kvm_enabled()) {
> +return kvm_arch_vcpu_id(cs);
> +} else {
> +return cs->cpu_index;
> +}
> +}
> +
> +PowerPCCPU *spapr_find_cpu(int vcpu_id)
> +{
> +CPUState *cs;
> +
> +CPU_FOREACH(cs) {
> +PowerPCCPU *cpu = POWERPC_CPU(cs);
> +
> +if (spapr_vcpu_id(cpu) == vcpu_