spapr: Support setting of compat CPU type for CPU cores
RFC PATCH 1/1 spapr: Support setting of compat CPU type for CPU cores
Compat CPU type is typically specified on -cpu cmdline option like:
-cpu host,compat=power7 or -cpu POWER8E,compat=power7 etc. With the introduction of sPAPR CPU core devices, we need to support the same for core devices too.
Support the specification of CPU compat type on device_add command for sPAPRCPUCore devices like:
(qemu) device_add POWER8E-spapr-cpu-core,id=core3,compat=power7,core-id=24
---
Changes in v1:
- In the routine that extracts "compat=" from -cpu cmdline, made the parsing generic as suggested by Thomas Huth so that it works in the presence of any other additional features.
- Addressed review comments by David with major one being setting of compat property directly instead of going via ->parse_features().
TODO:
- Reconcile with Igor's work that make cpu features as global properties.
- Find a way to export the compat infomation via query-hotpluggable-cpus.
v0: https://lists.gnu.org/archive/html/qemu-devel/2016-06/msg05279.html
hw/ppc/spapr.c | 8 +++++ hw/ppc/spapr_cpu_core.c | 78 +++++++++++++++++++++++++++++++++++++++++ include/hw/ppc/spapr_cpu_core.h | 2 ++ 3 files changed, 88 insertions(+)
hw/ppc/spapr.c +8 -0
@@ -1807,6 +1807,7 @@ static void ppc_spapr_init(MachineState *machine)if (i < spapr_cores) {char *type =spapr_get_cpu_core_type (machine->cpu_model);Object *core; +char *compat;if (!object_class_by_name (type)) {error_report ("Unable to find sPAPR CPU Core definition" );
@@ -1818,6 +1819,13 @@ static void ppc_spapr_init(MachineState *machine) &error_fatal);object_property_set_int (core, core_dt_id,CPU_CORE_PROP_CORE_ID , &error_fatal); + compat =spapr_get_cpu_compat_type (machine->cpu_model); +if (compat) { +object_property_set_str (core, compat,"compat" , + &error_fatal); +g_free (compat); + } +object_property_set_bool (core, true,"realized" , &error_fatal); } }
hw/ppc/spapr_cpu_core.c +78 -0
134 lines not on this page · raw message
include/hw/ppc/spapr_cpu_core.h +2 -0
@@ -24,11 +24,13 @@ typedef struct sPAPRCPUCore {/*< public >*/ void *threads;ObjectClass *cpu_class; +char *cpu_compat; } sPAPRCPUCore;void spapr_core_pre_plug (HotplugHandler *hotplug_dev,DeviceState *dev,Error **errp);char *spapr_get_cpu_core_type (const char *model); +char *spapr_get_cpu_compat_type (const char *model);void spapr_core_plug (HotplugHandler *hotplug_dev,DeviceState *dev,Error **errp); void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
Signature · 1 line
2.1.0
Quoted 62 lines · Bharata B Rao
Compat CPU type is typically specified on -cpu cmdline option like:
-cpu host,compat=power7 or -cpu POWER8E,compat=power7 etc. With the introduction of sPAPR CPU core devices, we need to support the same for core devices too.Support the specification of CPU compat type on
device_addcommand for sPAPRCPUCore devices like:
(qemu)device_addPOWER8E-spapr-cpu-core,id=core3,compat=power7,core-id=24Signed-off-by Bharata B Rao <[email protected]>---
Changes in v1:
- In the routine that extracts "compat=" from -cpu cmdline, made the parsing generic as suggested by Thomas Huth so that it works in the presence of any other additional features.
- Addressed review comments by David with major one being setting of compat property directly instead of going via ->parse_features().TODO:
- Reconcile with Igor's work that make cpu features as global properties.
- Find a way to export the compat infomation via query-hotpluggable-cpus.v0: https://lists.gnu.org/archive/html/qemu-devel/2016-06/msg05279.html
hw/ppc/spapr.c | 8 +++++ hw/ppc/spapr_cpu_core.c | 78 +++++++++++++++++++++++++++++++++++++++++ include/hw/ppc/spapr_cpu_core.h | 2 ++ 3 files changed, 88 insertions(+)hw/ppc/spapr.c +8
-0@@ -1807,6 +1807,7 @@ static void ppc_spapr_init(MachineState *machine)if (i < spapr_cores) {char *type =spapr_get_cpu_core_type (machine->cpu_model);Object *core; +char *compat;if (!object_class_by_name (type)) {error_report ("Unable to find sPAPR CPU Core definition" );@@ -1818,6 +1819,13 @@ static void ppc_spapr_init(MachineState *machine) &error_fatal);object_property_set_int (core, core_dt_id,CPU_CORE_PROP_CORE_ID , &error_fatal); + compat =spapr_get_cpu_compat_type (machine->cpu_model); +if (compat) { +object_property_set_str (core, compat,"compat" , + &error_fatal); +g_free (compat); + } +object_property_set_bool (core, true,"realized" , &error_fatal); } }hw/ppc/spapr_cpu_core.c +0
-0@@ -96,6 +96,31 @@ char *spapr_get_cpu_core_type(const char *model)return core_type; }
hw/ppc/spapr_cpu_core.c char *spapr_get_cpu_core_type(const char *model) · line 107
Quoted 10 lines
hw/ppc/spapr_cpu_core.c +9
-0+/* +* Returns the CPU compat type specified in -cpu @model. +*/ +char *spapr_get_cpu_compat_type (const char *model) +{ +char *model_str =g_strdup (model); +char *featurestr, *compat =NULL ; + + featurestr = model_str ?strtok (model_str,"," ) :NULL ;
AFAICT strtok() is not thread-safe.
hw/ppc/spapr_cpu_core.c char *spapr_get_cpu_core_type(const char *model) · line 111
Quoted 4 lines
hw/ppc/spapr_cpu_core.c +4
-0+while (featurestr) { +char *val; +if (!strncmp (featurestr,"compat=" ,7 )) { + val =strchr (featurestr,'=' );
You don't technically need the strchr(), since from the strncmp above, you know the answer will be featurestr + 6.
hw/ppc/spapr_cpu_core.c char *spapr_get_cpu_core_type(const char *model) · line 122
Quoted 11 lines
hw/ppc/spapr_cpu_core.c +11
-0+ val++; + compat =g_strdup (val); + goto out; + } + featurestr =strtok (NULL ,"," ); + } + +out: +g_free (model_str); +return compat; +}
Couldn't we use one of the existing opts parsing functions in qemu for the above, anyway?
Quoted 20 lines
hw/ppc/spapr_cpu_core.c +7
-0static void spapr_core_release (DeviceState *dev,void *opaque) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev));@@ -223,12 +248,31 @@ void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,CPUCore *cc =CPU_CORE (dev);char *base_core_type =spapr_get_cpu_core_type (machine->cpu_model);const char *type =object_get_typename (OBJECT (dev)); +char *base_compat_type =NULL ; +char *compat =NULL ; +bool compat_set;if (strcmp (base_core_type, type)) {error_setg (&local_err,"CPU core type should be %s" , base_core_type); goto out; } + base_compat_type =spapr_get_cpu_compat_type (machine->cpu_model); + compat =object_property_get_str (OBJECT (dev),"compat" ,NULL ); + compat_set = compat && *compat; +
hw/ppc/spapr_cpu_core.c void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 273
Quoted 10 lines
hw/ppc/spapr_cpu_core.c +10
-0+if (base_compat_type) { +if ((compat_set &&strcmp (base_compat_type, compat)) || + !compat_set) { +error_setg (&local_err,"CPU compat type should be %s" , + base_compat_type); + goto out; + } + }else if (compat_set) { +error_setg (&local_err,"CPU compat type shouldn't be set" ); + goto out;
I don't think we want this else clause, because it will forbid the use of -global core_type,compat=whatever, which Igor says is the preferred approach for the future (IIUC, using -global is equivalent to setting the property explicitly on every instance).
The if clause should be ok, since it implements a fallback to using -cpu, which we do want.
Quoted 100 lines
hw/ppc/spapr_cpu_core.c +36
-0+ } +if (!smc->dr_cpu_enabled && dev->hotplugged) {error_setg (&local_err,"CPU hotplug not supported for this machine" ); goto out;@@ -256,6 +300,8 @@ void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, } out: +g_free (compat); +g_free (base_compat_type);g_free (base_core_type);error_propagate (errp, local_err); }@@ -288,6 +334,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)Error *local_err =NULL ;Object *obj;int i; +char *compat =object_property_get_str (OBJECT (sc),"compat" ,NULL ); +bool compat_set = compat && *compat; sc->threads =g_malloc0 (size * cc->nr_threads);for (i =0 ; i < cc->nr_threads; i++) {@@ -300,11 +348,18 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)if (local_err) { goto err; } +if (compat_set) { +object_property_set_str (obj, compat,"compat" , &local_err); +if (local_err) { + goto err; + } + } }object_child_foreach (OBJECT (dev), spapr_cpu_core_realize_child, &local_err);if (local_err) { goto err; }else { +g_free (compat);return ; }@@ -315,9 +370,31 @@ err: i--; }g_free (sc->threads); +g_free (compat);error_propagate (errp, local_err); } +static char *spapr_cpu_core_prop_get_compat (Object *obj,Error **errp) +{ +sPAPRCPUCore *core =SPAPR_CPU_CORE (obj); + +return g_strdup (core->cpu_compat); +} + +static void spapr_cpu_core_prop_set_compat (Object *obj,const char *val, +Error **errp) +{ +sPAPRCPUCore *core =SPAPR_CPU_CORE (obj); + + core->cpu_compat =g_strdup (val); +} + +static void spapr_cpu_core_instance_init (Object *obj) +{ +object_property_add_str (obj,"compat" , spapr_cpu_core_prop_get_compat, + spapr_cpu_core_prop_set_compat,NULL ); +} +static void spapr_cpu_core_class_init (ObjectClass *oc,void *data) {DeviceClass *dc =DEVICE_CLASS (oc);@@ -388,6 +465,7 @@ static const TypeInfo spapr_cpu_core_type_info = { .parent = TYPE_CPU_CORE, .abstract = true, .instance_size = sizeof(sPAPRCPUCore), + .instance_init = spapr_cpu_core_instance_init, .class_init = spapr_cpu_core_class_init, };include/hw/ppc/spapr_cpu_core.h +2
-0@@ -24,11 +24,13 @@ typedef struct sPAPRCPUCore {/*< public >*/ void *threads;ObjectClass *cpu_class; +char *cpu_compat; } sPAPRCPUCore;void spapr_core_pre_plug (HotplugHandler *hotplug_dev,DeviceState *dev,Error **errp);char *spapr_get_cpu_core_type (const char *model); +char *spapr_get_cpu_compat_type (const char *model);void spapr_core_plug (HotplugHandler *hotplug_dev,DeviceState *dev,Error **errp); void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 64 lines · David Gibson
Quoted 63 lines · Bharata B Rao
Compat CPU type is typically specified on -cpu cmdline option like:
-cpu host,compat=power7 or -cpu POWER8E,compat=power7 etc. With the introduction of sPAPR CPU core devices, we need to support the same for core devices too.Support the specification of CPU compat type on
device_addcommand for sPAPRCPUCore devices like:
(qemu)device_addPOWER8E-spapr-cpu-core,id=core3,compat=power7,core-id=24Signed-off-by Bharata B Rao <[email protected]>---
Changes in v1:
- In the routine that extracts "compat=" from -cpu cmdline, made the parsing generic as suggested by Thomas Huth so that it works in the presence of any other additional features.
- Addressed review comments by David with major one being setting of compat property directly instead of going via ->parse_features().TODO:
- Reconcile with Igor's work that make cpu features as global properties.
- Find a way to export the compat infomation via query-hotpluggable-cpus.v0: https://lists.gnu.org/archive/html/qemu-devel/2016-06/msg05279.html
hw/ppc/spapr.c | 8 +++++ hw/ppc/spapr_cpu_core.c | 78 +++++++++++++++++++++++++++++++++++++++++ include/hw/ppc/spapr_cpu_core.h | 2 ++ 3 files changed, 88 insertions(+)hw/ppc/spapr.c +8
-0@@ -1807,6 +1807,7 @@ static void ppc_spapr_init(MachineState *machine)if (i < spapr_cores) {char *type =spapr_get_cpu_core_type (machine->cpu_model);Object *core; +char *compat;if (!object_class_by_name (type)) {error_report ("Unable to find sPAPR CPU Core definition" );@@ -1818,6 +1819,13 @@ static void ppc_spapr_init(MachineState *machine) &error_fatal);object_property_set_int (core, core_dt_id,CPU_CORE_PROP_CORE_ID , &error_fatal); + compat =spapr_get_cpu_compat_type (machine->cpu_model); +if (compat) { +object_property_set_str (core, compat,"compat" , + &error_fatal); +g_free (compat); + } +object_property_set_bool (core, true,"realized" , &error_fatal); } }hw/ppc/spapr_cpu_core.c +0
-0@@ -96,6 +96,31 @@ char *spapr_get_cpu_core_type(const char *model)return core_type; }
Quoted 11 lines
hw/ppc/spapr_cpu_core.c char *spapr_get_cpu_core_type(const char *model) · line 107
Quoted 9 lines
hw/ppc/spapr_cpu_core.c +9
-0+/* +* Returns the CPU compat type specified in -cpu @model. +*/ +char *spapr_get_cpu_compat_type (const char *model) +{ +char *model_str =g_strdup (model); +char *featurestr, *compat =NULL ; + + featurestr = model_str ?strtok (model_str,"," ) :NULL ;AFAICT
strtok()is not thread-safe.
Followed cpu_common_parse_features().
Quoted 8 lines
hw/ppc/spapr_cpu_core.c char *spapr_get_cpu_core_type(const char *model) · line 111
Quoted 5 lines
hw/ppc/spapr_cpu_core.c +4
-0+while (featurestr) { +char *val; +if (!strncmp (featurestr,"compat=" ,7 )) { + val =strchr (featurestr,'=' );You don't technically need the
strchr(), since from the strncmp above, you know the answer will be featurestr + 6.
Ok fixed.
Quoted 15 lines
hw/ppc/spapr_cpu_core.c char *spapr_get_cpu_core_type(const char *model) · line 122
Quoted 12 lines
hw/ppc/spapr_cpu_core.c +11
-0+ val++; + compat =g_strdup (val); + goto out; + } + featurestr =strtok (NULL ,"," ); + } + +out: +g_free (model_str); +return compat; +}Couldn't we use one of the existing opts parsing functions in qemu for the above, anyway?
Using qemu_get_opt() is one option but that would need some work of setting the opts infrastructure for parsing -cpu option, but I assume with Igor's global CPU features work, that would be unwelcome.
Quoted 37 lines
Quoted 21 lines
hw/ppc/spapr_cpu_core.c +7
-0static void spapr_core_release (DeviceState *dev,void *opaque) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev));@@ -223,12 +248,31 @@ void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,CPUCore *cc =CPU_CORE (dev);char *base_core_type =spapr_get_cpu_core_type (machine->cpu_model);const char *type =object_get_typename (OBJECT (dev)); +char *base_compat_type =NULL ; +char *compat =NULL ; +bool compat_set;if (strcmp (base_core_type, type)) {error_setg (&local_err,"CPU core type should be %s" , base_core_type); goto out; } + base_compat_type =spapr_get_cpu_compat_type (machine->cpu_model); + compat =object_property_get_str (OBJECT (dev),"compat" ,NULL ); + compat_set = compat && *compat; +hw/ppc/spapr_cpu_core.c void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 273
Quoted 10 lines
hw/ppc/spapr_cpu_core.c +10
-0+if (base_compat_type) { +if ((compat_set &&strcmp (base_compat_type, compat)) || + !compat_set) { +error_setg (&local_err,"CPU compat type should be %s" , + base_compat_type); + goto out; + } + }else if (compat_set) { +error_setg (&local_err,"CPU compat type shouldn't be set" ); + goto out;I don't think we want this else clause, because it will forbid the use of -global core_type,compat=whatever, which Igor says is the preferred approach for the future (IIUC, using -global is equivalent to setting the property explicitly on every instance).
Quoted 2 lines
The if clause should be ok, since it implements a fallback to using -cpu, which we do want.
In my new version, I am not touching ->pre_plug() at all since there is nothing to validate. All we do is to apply the compat property to all CPU threads if it is specified.
Regards,
Bharata.