diff mbox

[RFC,v2,12/23] spapr: CPU hotplug support

Message ID 1427117764-23008-13-git-send-email-bharata@linux.vnet.ibm.com
State New
Headers show

Commit Message

Bharata B Rao March 23, 2015, 1:35 p.m. UTC
Support CPU hotplug via device-add command. Set up device tree
entries for the hotplugged CPU core and use the exising EPOW event
infrastructure to send CPU hotplug notification to the guest.

Signed-off-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
---
 hw/ppc/spapr.c        | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr_events.c |  8 +++---
 hw/ppc/spapr_rtas.c   | 11 ++++++++
 3 files changed, 91 insertions(+), 3 deletions(-)

Comments

David Gibson March 25, 2015, 3:03 a.m. UTC | #1
On Mon, Mar 23, 2015 at 07:05:53PM +0530, Bharata B Rao wrote:
> Support CPU hotplug via device-add command. Set up device tree
> entries for the hotplugged CPU core and use the exising EPOW event
> infrastructure to send CPU hotplug notification to the guest.
> 
> Signed-off-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
> ---
>  hw/ppc/spapr.c        | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  hw/ppc/spapr_events.c |  8 +++---
>  hw/ppc/spapr_rtas.c   | 11 ++++++++
>  3 files changed, 91 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
> index f52d38f..b48994b 100644
> --- a/hw/ppc/spapr.c
> +++ b/hw/ppc/spapr.c
> @@ -33,6 +33,7 @@
>  #include "sysemu/block-backend.h"
>  #include "sysemu/cpus.h"
>  #include "sysemu/kvm.h"
> +#include "sysemu/device_tree.h"
>  #include "kvm_ppc.h"
>  #include "mmu-hash64.h"
>  #include "qom/cpu.h"
> @@ -660,6 +661,10 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset)
>      QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL);
>      unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0;
>      uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1;
> +    sPAPRDRConnector *drc =
> +        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
> +    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
> +    int drc_index = drck->get_index(drc);
>  
>      _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
>      _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
> @@ -728,6 +733,8 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset)
>  
>      _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
>                              cs->cpu_index / cpus_per_socket)));
> +    _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
> +

What effect will this have when running with DR disabled?

>      _FDT(spapr_fixup_cpu_numa_smt_dt(fdt, offset, cs, spapr));
>  }
> @@ -1840,6 +1847,70 @@ static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
>      }
>  }
>  
> +static void spapr_cpu_hotplug_add(DeviceState *dev, CPUState *cs, Error **errp)
> +{
> +    PowerPCCPU *cpu = POWERPC_CPU(cs);
> +    DeviceClass *dc = DEVICE_GET_CLASS(cs);
> +    int id = ppc_get_vcpu_dt_id(cpu);
> +    sPAPRDRConnector *drc =
> +        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, id);
> +    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
> +    void *fdt;
> +    int offset, i, fdt_size;
> +    char *nodename;
> +
> +    fdt = create_device_tree(&fdt_size);
> +    nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
> +    offset = fdt_add_subnode(fdt, 0, nodename);
> +
> +    /* Set NUMA node for the added CPU core */
> +    for (i = 0; i < nb_numa_nodes; i++) {
> +        if (test_bit(cs->cpu_index, numa_info[i].node_cpu)) {
> +            cs->numa_node = i;
> +            break;
> +        }
> +    }
> +
> +    spapr_populate_cpu_dt(cs, fdt, offset);
> +    g_free(nodename);
> +
> +    drck->attach(drc, dev, fdt, offset, !dev->hotplugged, errp);
> +    if (*errp) {
> +        g_free(fdt);
> +    }
> +}
> +
> +static void spapr_cpu_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
> +                            Error **errp)
> +{
> +    CPUState *cs = CPU(dev);
> +    PowerPCCPU *cpu = POWERPC_CPU(cs);
> +    int id = ppc_get_vcpu_dt_id(cpu);
> +    sPAPRDRConnector *drc =
> +        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, id);
> +    int smt = kvmppc_smt_threads();
> +    Error *local_err = NULL;
> +
> +    /*
> +     * SMT threads return from here, only main thread (core) will
> +     * continue and signal hotplug event to the guest.
> +     */
> +    if ((id % smt) != 0) {
> +        return;
> +    }
> +
> +    g_assert(drc);
> +
> +    spapr_cpu_hotplug_add(dev, cs, &local_err);
> +    if (local_err) {
> +        error_propagate(errp, local_err);
> +        return;
> +    }
> +    spapr_hotplug_req_add_event(drc);
> +
> +    return;
> +}
> +
>  static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
>                                        DeviceState *dev, Error **errp)
>  {
> @@ -1848,6 +1919,10 @@ static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
>          PowerPCCPU *cpu = POWERPC_CPU(cs);
>  
>          spapr_cpu_init(cpu);
> +        spapr_cpu_reset(cpu);
> +        if (dev->hotplugged && spapr->dr_cpu_enabled) {
> +            spapr_cpu_plug(hotplug_dev, dev, errp);
> +        }
>      }
>  }
>  
> diff --git a/hw/ppc/spapr_events.c b/hw/ppc/spapr_events.c
> index be82815..4ae818a 100644
> --- a/hw/ppc/spapr_events.c
> +++ b/hw/ppc/spapr_events.c
> @@ -421,14 +421,16 @@ static void spapr_hotplug_req_event(sPAPRDRConnector *drc, uint8_t hp_action)
>      hp->hdr.section_length = cpu_to_be16(sizeof(*hp));
>      hp->hdr.section_version = 1; /* includes extended modifier */
>      hp->hotplug_action = hp_action;
> -
> +    hp->drc.index = cpu_to_be32(drck->get_index(drc));
> +    hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
>  
>      switch (drc_type) {
>      case SPAPR_DR_CONNECTOR_TYPE_PCI:
> -        hp->drc.index = cpu_to_be32(drck->get_index(drc));
> -        hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
>          hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_PCI;
>          break;
> +    case SPAPR_DR_CONNECTOR_TYPE_CPU:
> +        hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_CPU;
> +        break;
>      default:
>          /* we shouldn't be signaling hotplug events for resources
>           * that don't support them
> diff --git a/hw/ppc/spapr_rtas.c b/hw/ppc/spapr_rtas.c
> index 57ec97a..48aeb86 100644
> --- a/hw/ppc/spapr_rtas.c
> +++ b/hw/ppc/spapr_rtas.c
> @@ -121,6 +121,16 @@ static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
>      rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
>  }
>  
> +static void spapr_cpu_set_endianness(PowerPCCPU *cpu)
> +{
> +    PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
> +    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu);

In some ways it might be nicer to store the global endian mode in
sPAPRMachineState, updating it on H_SET_MODE, rather than copying the
first cpu, although I guess that works.

> +    if (!(*pcc->interrupts_big_endian)(fcpu)) {
> +        cpu->env.spr[SPR_LPCR] |= LPCR_ILE;
> +    }
> +}
> +
>  static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPREnvironment *spapr,
>                             uint32_t token, uint32_t nargs,
>                             target_ulong args,
> @@ -157,6 +167,7 @@ static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPREnvironment *spapr,
>          env->nip = start;
>          env->gpr[3] = r3;
>          cs->halted = 0;
> +        spapr_cpu_set_endianness(cpu);
>  
>          qemu_cpu_kick(cs);
>
Bharata B Rao March 25, 2015, 8:36 a.m. UTC | #2
On Wed, Mar 25, 2015 at 02:03:45PM +1100, David Gibson wrote:
> On Mon, Mar 23, 2015 at 07:05:53PM +0530, Bharata B Rao wrote:
> > Support CPU hotplug via device-add command. Set up device tree
> > entries for the hotplugged CPU core and use the exising EPOW event
> > infrastructure to send CPU hotplug notification to the guest.
> > 
> > Signed-off-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
> > ---
> >  hw/ppc/spapr.c        | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++
> >  hw/ppc/spapr_events.c |  8 +++---
> >  hw/ppc/spapr_rtas.c   | 11 ++++++++
> >  3 files changed, 91 insertions(+), 3 deletions(-)
> > 
> > diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
> > index f52d38f..b48994b 100644
> > --- a/hw/ppc/spapr.c
> > +++ b/hw/ppc/spapr.c
> > @@ -33,6 +33,7 @@
> >  #include "sysemu/block-backend.h"
> >  #include "sysemu/cpus.h"
> >  #include "sysemu/kvm.h"
> > +#include "sysemu/device_tree.h"
> >  #include "kvm_ppc.h"
> >  #include "mmu-hash64.h"
> >  #include "qom/cpu.h"
> > @@ -660,6 +661,10 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset)
> >      QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL);
> >      unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0;
> >      uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1;
> > +    sPAPRDRConnector *drc =
> > +        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
> > +    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
> > +    int drc_index = drck->get_index(drc);
> >  
> >      _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
> >      _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
> > @@ -728,6 +733,8 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset)
> >  
> >      _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
> >                              cs->cpu_index / cpus_per_socket)));
> > +    _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
> > +
> 
> What effect will this have when running with DR disabled?

I realize now that I shouldn't probably populate this at all in that case.
This routine is now common for both bootpath and hotplug path. Will take
care of this in next post.

> 
> >      _FDT(spapr_fixup_cpu_numa_smt_dt(fdt, offset, cs, spapr));
> >  }
> > @@ -1840,6 +1847,70 @@ static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
> >      }
> >  }
> >  
> > +static void spapr_cpu_hotplug_add(DeviceState *dev, CPUState *cs, Error **errp)
> > +{
> > +    PowerPCCPU *cpu = POWERPC_CPU(cs);
> > +    DeviceClass *dc = DEVICE_GET_CLASS(cs);
> > +    int id = ppc_get_vcpu_dt_id(cpu);
> > +    sPAPRDRConnector *drc =
> > +        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, id);
> > +    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
> > +    void *fdt;
> > +    int offset, i, fdt_size;
> > +    char *nodename;
> > +
> > +    fdt = create_device_tree(&fdt_size);
> > +    nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
> > +    offset = fdt_add_subnode(fdt, 0, nodename);
> > +
> > +    /* Set NUMA node for the added CPU core */
> > +    for (i = 0; i < nb_numa_nodes; i++) {
> > +        if (test_bit(cs->cpu_index, numa_info[i].node_cpu)) {
> > +            cs->numa_node = i;
> > +            break;
> > +        }
> > +    }
> > +
> > +    spapr_populate_cpu_dt(cs, fdt, offset);
> > +    g_free(nodename);
> > +
> > +    drck->attach(drc, dev, fdt, offset, !dev->hotplugged, errp);
> > +    if (*errp) {
> > +        g_free(fdt);
> > +    }
> > +}
> > +
> > +static void spapr_cpu_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
> > +                            Error **errp)
> > +{
> > +    CPUState *cs = CPU(dev);
> > +    PowerPCCPU *cpu = POWERPC_CPU(cs);
> > +    int id = ppc_get_vcpu_dt_id(cpu);
> > +    sPAPRDRConnector *drc =
> > +        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, id);
> > +    int smt = kvmppc_smt_threads();
> > +    Error *local_err = NULL;
> > +
> > +    /*
> > +     * SMT threads return from here, only main thread (core) will
> > +     * continue and signal hotplug event to the guest.
> > +     */
> > +    if ((id % smt) != 0) {
> > +        return;
> > +    }
> > +
> > +    g_assert(drc);
> > +
> > +    spapr_cpu_hotplug_add(dev, cs, &local_err);
> > +    if (local_err) {
> > +        error_propagate(errp, local_err);
> > +        return;
> > +    }
> > +    spapr_hotplug_req_add_event(drc);
> > +
> > +    return;
> > +}
> > +
> >  static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
> >                                        DeviceState *dev, Error **errp)
> >  {
> > @@ -1848,6 +1919,10 @@ static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
> >          PowerPCCPU *cpu = POWERPC_CPU(cs);
> >  
> >          spapr_cpu_init(cpu);
> > +        spapr_cpu_reset(cpu);
> > +        if (dev->hotplugged && spapr->dr_cpu_enabled) {
> > +            spapr_cpu_plug(hotplug_dev, dev, errp);
> > +        }
> >      }
> >  }
> >  
> > diff --git a/hw/ppc/spapr_events.c b/hw/ppc/spapr_events.c
> > index be82815..4ae818a 100644
> > --- a/hw/ppc/spapr_events.c
> > +++ b/hw/ppc/spapr_events.c
> > @@ -421,14 +421,16 @@ static void spapr_hotplug_req_event(sPAPRDRConnector *drc, uint8_t hp_action)
> >      hp->hdr.section_length = cpu_to_be16(sizeof(*hp));
> >      hp->hdr.section_version = 1; /* includes extended modifier */
> >      hp->hotplug_action = hp_action;
> > -
> > +    hp->drc.index = cpu_to_be32(drck->get_index(drc));
> > +    hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
> >  
> >      switch (drc_type) {
> >      case SPAPR_DR_CONNECTOR_TYPE_PCI:
> > -        hp->drc.index = cpu_to_be32(drck->get_index(drc));
> > -        hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
> >          hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_PCI;
> >          break;
> > +    case SPAPR_DR_CONNECTOR_TYPE_CPU:
> > +        hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_CPU;
> > +        break;
> >      default:
> >          /* we shouldn't be signaling hotplug events for resources
> >           * that don't support them
> > diff --git a/hw/ppc/spapr_rtas.c b/hw/ppc/spapr_rtas.c
> > index 57ec97a..48aeb86 100644
> > --- a/hw/ppc/spapr_rtas.c
> > +++ b/hw/ppc/spapr_rtas.c
> > @@ -121,6 +121,16 @@ static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
> >      rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
> >  }
> >  
> > +static void spapr_cpu_set_endianness(PowerPCCPU *cpu)
> > +{
> > +    PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
> > +    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu);
> 
> In some ways it might be nicer to store the global endian mode in
> sPAPRMachineState, updating it on H_SET_MODE, rather than copying the
> first cpu, although I guess that works.

I can give it a stab at that if you feel strongly about it.

Regards,
Bharata.
David Gibson March 26, 2015, 1:42 a.m. UTC | #3
On Wed, Mar 25, 2015 at 02:06:29PM +0530, Bharata B Rao wrote:
> On Wed, Mar 25, 2015 at 02:03:45PM +1100, David Gibson wrote:
> > On Mon, Mar 23, 2015 at 07:05:53PM +0530, Bharata B Rao wrote:
> > > Support CPU hotplug via device-add command. Set up device tree
> > > entries for the hotplugged CPU core and use the exising EPOW event
> > > infrastructure to send CPU hotplug notification to the guest.
> > > 
> > > Signed-off-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
> > > ---
> > >  hw/ppc/spapr.c        | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++
> > >  hw/ppc/spapr_events.c |  8 +++---
> > >  hw/ppc/spapr_rtas.c   | 11 ++++++++
> > >  3 files changed, 91 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
> > > index f52d38f..b48994b 100644
> > > --- a/hw/ppc/spapr.c
> > > +++ b/hw/ppc/spapr.c
> > > @@ -33,6 +33,7 @@
> > >  #include "sysemu/block-backend.h"
> > >  #include "sysemu/cpus.h"
> > >  #include "sysemu/kvm.h"
> > > +#include "sysemu/device_tree.h"
> > >  #include "kvm_ppc.h"
> > >  #include "mmu-hash64.h"
> > >  #include "qom/cpu.h"
> > > @@ -660,6 +661,10 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset)
> > >      QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL);
> > >      unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0;
> > >      uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1;
> > > +    sPAPRDRConnector *drc =
> > > +        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
> > > +    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
> > > +    int drc_index = drck->get_index(drc);
> > >  
> > >      _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
> > >      _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
> > > @@ -728,6 +733,8 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset)
> > >  
> > >      _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
> > >                              cs->cpu_index / cpus_per_socket)));
> > > +    _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
> > > +
> > 
> > What effect will this have when running with DR disabled?
> 
> I realize now that I shouldn't probably populate this at all in that case.
> This routine is now common for both bootpath and hotplug path. Will take
> care of this in next post.

Ok.

[snip]
> > > +static void spapr_cpu_set_endianness(PowerPCCPU *cpu)
> > > +{
> > > +    PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
> > > +    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu);
> > 
> > In some ways it might be nicer to store the global endian mode in
> > sPAPRMachineState, updating it on H_SET_MODE, rather than copying the
> > first cpu, although I guess that works.
> 
> I can give it a stab at that if you feel strongly about it.

Not that strongly.  It's a small cleanup we can consider later.
diff mbox

Patch

diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
index f52d38f..b48994b 100644
--- a/hw/ppc/spapr.c
+++ b/hw/ppc/spapr.c
@@ -33,6 +33,7 @@ 
 #include "sysemu/block-backend.h"
 #include "sysemu/cpus.h"
 #include "sysemu/kvm.h"
+#include "sysemu/device_tree.h"
 #include "kvm_ppc.h"
 #include "mmu-hash64.h"
 #include "qom/cpu.h"
@@ -660,6 +661,10 @@  static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset)
     QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL);
     unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0;
     uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1;
+    sPAPRDRConnector *drc =
+        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
+    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
+    int drc_index = drck->get_index(drc);
 
     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
@@ -728,6 +733,8 @@  static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset)
 
     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
                             cs->cpu_index / cpus_per_socket)));
+    _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
+
 
     _FDT(spapr_fixup_cpu_numa_smt_dt(fdt, offset, cs, spapr));
 }
@@ -1840,6 +1847,70 @@  static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
     }
 }
 
+static void spapr_cpu_hotplug_add(DeviceState *dev, CPUState *cs, Error **errp)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(cs);
+    DeviceClass *dc = DEVICE_GET_CLASS(cs);
+    int id = ppc_get_vcpu_dt_id(cpu);
+    sPAPRDRConnector *drc =
+        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, id);
+    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
+    void *fdt;
+    int offset, i, fdt_size;
+    char *nodename;
+
+    fdt = create_device_tree(&fdt_size);
+    nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
+    offset = fdt_add_subnode(fdt, 0, nodename);
+
+    /* Set NUMA node for the added CPU core */
+    for (i = 0; i < nb_numa_nodes; i++) {
+        if (test_bit(cs->cpu_index, numa_info[i].node_cpu)) {
+            cs->numa_node = i;
+            break;
+        }
+    }
+
+    spapr_populate_cpu_dt(cs, fdt, offset);
+    g_free(nodename);
+
+    drck->attach(drc, dev, fdt, offset, !dev->hotplugged, errp);
+    if (*errp) {
+        g_free(fdt);
+    }
+}
+
+static void spapr_cpu_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
+                            Error **errp)
+{
+    CPUState *cs = CPU(dev);
+    PowerPCCPU *cpu = POWERPC_CPU(cs);
+    int id = ppc_get_vcpu_dt_id(cpu);
+    sPAPRDRConnector *drc =
+        spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, id);
+    int smt = kvmppc_smt_threads();
+    Error *local_err = NULL;
+
+    /*
+     * SMT threads return from here, only main thread (core) will
+     * continue and signal hotplug event to the guest.
+     */
+    if ((id % smt) != 0) {
+        return;
+    }
+
+    g_assert(drc);
+
+    spapr_cpu_hotplug_add(dev, cs, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+    spapr_hotplug_req_add_event(drc);
+
+    return;
+}
+
 static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
                                       DeviceState *dev, Error **errp)
 {
@@ -1848,6 +1919,10 @@  static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
         PowerPCCPU *cpu = POWERPC_CPU(cs);
 
         spapr_cpu_init(cpu);
+        spapr_cpu_reset(cpu);
+        if (dev->hotplugged && spapr->dr_cpu_enabled) {
+            spapr_cpu_plug(hotplug_dev, dev, errp);
+        }
     }
 }
 
diff --git a/hw/ppc/spapr_events.c b/hw/ppc/spapr_events.c
index be82815..4ae818a 100644
--- a/hw/ppc/spapr_events.c
+++ b/hw/ppc/spapr_events.c
@@ -421,14 +421,16 @@  static void spapr_hotplug_req_event(sPAPRDRConnector *drc, uint8_t hp_action)
     hp->hdr.section_length = cpu_to_be16(sizeof(*hp));
     hp->hdr.section_version = 1; /* includes extended modifier */
     hp->hotplug_action = hp_action;
-
+    hp->drc.index = cpu_to_be32(drck->get_index(drc));
+    hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
 
     switch (drc_type) {
     case SPAPR_DR_CONNECTOR_TYPE_PCI:
-        hp->drc.index = cpu_to_be32(drck->get_index(drc));
-        hp->hotplug_identifier = RTAS_LOG_V6_HP_ID_DRC_INDEX;
         hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_PCI;
         break;
+    case SPAPR_DR_CONNECTOR_TYPE_CPU:
+        hp->hotplug_type = RTAS_LOG_V6_HP_TYPE_CPU;
+        break;
     default:
         /* we shouldn't be signaling hotplug events for resources
          * that don't support them
diff --git a/hw/ppc/spapr_rtas.c b/hw/ppc/spapr_rtas.c
index 57ec97a..48aeb86 100644
--- a/hw/ppc/spapr_rtas.c
+++ b/hw/ppc/spapr_rtas.c
@@ -121,6 +121,16 @@  static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 }
 
+static void spapr_cpu_set_endianness(PowerPCCPU *cpu)
+{
+    PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu);
+
+    if (!(*pcc->interrupts_big_endian)(fcpu)) {
+        cpu->env.spr[SPR_LPCR] |= LPCR_ILE;
+    }
+}
+
 static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPREnvironment *spapr,
                            uint32_t token, uint32_t nargs,
                            target_ulong args,
@@ -157,6 +167,7 @@  static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPREnvironment *spapr,
         env->nip = start;
         env->gpr[3] = r3;
         cs->halted = 0;
+        spapr_cpu_set_endianness(cpu);
 
         qemu_cpu_kick(cs);