Machine core patches

- Move GPIO code out of qdev.c
 - Move hotplug code out of qdev.c
 - Restrict various files to sysemu
 - Move SMP code out of machine.c
 - Add SMP parsing unit tests
 - Move dynamic sysbus device check earlier
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCAAdFiEE+qvnXhKRciHc/Wuy4+MsLN6twN4FAmGANZAACgkQ4+MsLN6t
 wN6Afw/+OnBSs8ZjSc/01nWX/lxk0lgS9np69gA6+sfJI7tuYimXUnfU6ZAv4laF
 65dPCAZ4F4nGzo10pQqnpeez+1d6YfxVniPvTO0Z75HiUR6L7E5B43f16HJ2U+zG
 qjXCP45uYH5u/jgzIQ//BeKiPE2hwr7XM6hzJb9DiUPyaLIETyjnzvARSyIx202a
 p/lP6qTsjZvGwt21rAurqoulmwrkbDh6v7znmNUw8siwkGuKlGsvhkJuENZlbRPA
 J3pGWm2d/Xvs8+fZEzRdg+LjFTt6jBdPm1IkSh4G2e2Nu3qtDrlxsg97W0D84sGy
 Fz0JlgVMF/aw+ExSImWc6Dnq26tdCixaZIQDgvBPNFop1SLD9wY7L4VKPdlly7bh
 a1YabtaYqfjS7R0h2sAX7FEBSoAzxa1Nrr2/pB2aCziutVCLsu5Yz87hk8C08oF7
 RyuSsYixZWtVMpmOaxOS9swYviM5OhFQkX6N6cew0d/0/D3ZRyAbr3XMcftqz/FW
 SO6n2ZMXcFQiHFZei39ZV72jx3lV9uAunADKAcSVVCaXfpyXVgGLSOxZ7mhOfcgd
 z63vpAqHhiTp82pzygArz1Lmr9OlhOi5TBt6gQ9dZvjCDRG3yoodVRJIEkMZLYEf
 kaFQ9WgB/apCG49tvjTFSZJvySgzN+aGiD6BriOGSnoYu5Gs3wU=
 =NzoC
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/philmd/tags/machine-20211101' into staging

Machine core patches

- Move GPIO code out of qdev.c
- Move hotplug code out of qdev.c
- Restrict various files to sysemu
- Move SMP code out of machine.c
- Add SMP parsing unit tests
- Move dynamic sysbus device check earlier

# gpg: Signature made Mon 01 Nov 2021 02:44:32 PM EDT
# gpg:                using RSA key FAABE75E12917221DCFD6BB2E3E32C2CDEADC0DE
# gpg: Good signature from "Philippe Mathieu-Daudé (F4BUG) <f4bug@amsat.org>" [full]

* remotes/philmd/tags/machine-20211101:
  machine: remove the done notifier for dynamic sysbus device type check
  qdev-monitor: Check sysbus device type before creating it
  machine: add device_type_is_dynamic_sysbus function
  tests/unit: Add an unit test for smp parsing
  hw/core/machine: Split out the smp parsing code
  hw/core: Restrict hotplug to system emulation
  hw/core: Extract hotplug-related functions to qdev-hotplug.c
  hw/core: Declare meson source set
  hw/core: Restrict sysemu specific files
  machine: Move gpio code to hw/core/gpio.c

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
staging
Richard Henderson 2021-11-02 05:53:45 -04:00
commit 8cb41fda78
13 changed files with 1145 additions and 437 deletions

View File

@ -1623,6 +1623,7 @@ F: cpu.c
F: hw/core/cpu.c
F: hw/core/machine-qmp-cmds.c
F: hw/core/machine.c
F: hw/core/machine-smp.c
F: hw/core/null-machine.c
F: hw/core/numa.c
F: hw/cpu/cluster.c
@ -1632,6 +1633,7 @@ F: include/hw/boards.h
F: include/hw/core/cpu.h
F: include/hw/cpu/cluster.h
F: include/sysemu/numa.h
F: tests/unit/test-smp-parse.c
T: git https://gitlab.com/ehabkost/qemu.git machine-next
Xtensa Machines

197
hw/core/gpio.c 100644
View File

@ -0,0 +1,197 @@
/*
* qdev GPIO helpers
*
* Copyright (c) 2009 CodeSourcery
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "qemu/osdep.h"
#include "hw/qdev-core.h"
#include "hw/irq.h"
#include "qapi/error.h"
static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
const char *name)
{
NamedGPIOList *ngl;
QLIST_FOREACH(ngl, &dev->gpios, node) {
/* NULL is a valid and matchable name. */
if (g_strcmp0(name, ngl->name) == 0) {
return ngl;
}
}
ngl = g_malloc0(sizeof(*ngl));
ngl->name = g_strdup(name);
QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
return ngl;
}
void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
qemu_irq_handler handler,
void *opaque,
const char *name, int n)
{
int i;
NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
assert(gpio_list->num_out == 0 || !name);
gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
opaque, n);
if (!name) {
name = "unnamed-gpio-in";
}
for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
gchar *propname = g_strdup_printf("%s[%u]", name, i);
object_property_add_child(OBJECT(dev), propname,
OBJECT(gpio_list->in[i]));
g_free(propname);
}
gpio_list->num_in += n;
}
void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
{
qdev_init_gpio_in_named(dev, handler, NULL, n);
}
void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
const char *name, int n)
{
int i;
NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
assert(gpio_list->num_in == 0 || !name);
if (!name) {
name = "unnamed-gpio-out";
}
memset(pins, 0, sizeof(*pins) * n);
for (i = 0; i < n; ++i) {
gchar *propname = g_strdup_printf("%s[%u]", name,
gpio_list->num_out + i);
object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
(Object **)&pins[i],
object_property_allow_set_link,
OBJ_PROP_LINK_STRONG);
g_free(propname);
}
gpio_list->num_out += n;
}
void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
{
qdev_init_gpio_out_named(dev, pins, NULL, n);
}
qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
{
NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
assert(n >= 0 && n < gpio_list->num_in);
return gpio_list->in[n];
}
qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
{
return qdev_get_gpio_in_named(dev, NULL, n);
}
void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
qemu_irq pin)
{
char *propname = g_strdup_printf("%s[%d]",
name ? name : "unnamed-gpio-out", n);
if (pin && !OBJECT(pin)->parent) {
/* We need a name for object_property_set_link to work */
object_property_add_child(container_get(qdev_get_machine(),
"/unattached"),
"non-qdev-gpio[*]", OBJECT(pin));
}
object_property_set_link(OBJECT(dev), propname, OBJECT(pin), &error_abort);
g_free(propname);
}
qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
{
g_autofree char *propname = g_strdup_printf("%s[%d]",
name ? name : "unnamed-gpio-out", n);
qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
NULL);
return ret;
}
/* disconnect a GPIO output, returning the disconnected input (if any) */
static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
const char *name, int n)
{
char *propname = g_strdup_printf("%s[%d]",
name ? name : "unnamed-gpio-out", n);
qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
NULL);
if (ret) {
object_property_set_link(OBJECT(dev), propname, NULL, NULL);
}
g_free(propname);
return ret;
}
qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
const char *name, int n)
{
qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
qdev_connect_gpio_out_named(dev, name, n, icpt);
return disconnected;
}
void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin)
{
qdev_connect_gpio_out_named(dev, NULL, n, pin);
}
void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
const char *name)
{
int i;
NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
for (i = 0; i < ngl->num_in; i++) {
const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
char *propname = g_strdup_printf("%s[%d]", nm, i);
object_property_add_alias(OBJECT(container), propname,
OBJECT(dev), propname);
g_free(propname);
}
for (i = 0; i < ngl->num_out; i++) {
const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
char *propname = g_strdup_printf("%s[%d]", nm, i);
object_property_add_alias(OBJECT(container), propname,
OBJECT(dev), propname);
g_free(propname);
}
QLIST_REMOVE(ngl, node);
QLIST_INSERT_HEAD(&container->gpios, ngl, node);
}

View File

@ -0,0 +1,34 @@
/*
* Hotplug handler stubs
*
* Copyright (c) Red Hat
*
* Authors:
* Philippe Mathieu-Daudé <philmd@redhat.com>,
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "hw/qdev-core.h"
HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
{
return NULL;
}
void hotplug_handler_pre_plug(HotplugHandler *plug_handler,
DeviceState *plugged_dev,
Error **errp)
{
g_assert_not_reached();
}
void hotplug_handler_plug(HotplugHandler *plug_handler,
DeviceState *plugged_dev,
Error **errp)
{
g_assert_not_reached();
}

View File

@ -0,0 +1,181 @@
/*
* QEMU Machine core (related to -smp parsing)
*
* Copyright (c) 2021 Huawei Technologies Co., Ltd
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "qemu/osdep.h"
#include "hw/boards.h"
#include "qapi/error.h"
/*
* Report information of a machine's supported CPU topology hierarchy.
* Topology members will be ordered from the largest to the smallest
* in the string.
*/
static char *cpu_hierarchy_to_string(MachineState *ms)
{
MachineClass *mc = MACHINE_GET_CLASS(ms);
GString *s = g_string_new(NULL);
g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);
if (mc->smp_props.dies_supported) {
g_string_append_printf(s, " * dies (%u)", ms->smp.dies);
}
g_string_append_printf(s, " * cores (%u)", ms->smp.cores);
g_string_append_printf(s, " * threads (%u)", ms->smp.threads);
return g_string_free(s, false);
}
/*
* smp_parse - Generic function used to parse the given SMP configuration
*
* Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
* automatically computed based on the provided ones.
*
* In the calculation of omitted sockets/cores/threads: we prefer sockets
* over cores over threads before 6.2, while preferring cores over sockets
* over threads since 6.2.
*
* In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted,
* maxcpus will be computed from the given parameters and cpus will be set
* equal to maxcpus. When only one of maxcpus and cpus is given then the
* omitted one will be set to its given counterpart's value. Both maxcpus and
* cpus may be specified, but maxcpus must be equal to or greater than cpus.
*
* For compatibility, apart from the parameters that will be computed, newly
* introduced topology members which are likely to be target specific should
* be directly set as 1 if they are omitted (e.g. dies for PC since 4.1).
*/
void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
{
MachineClass *mc = MACHINE_GET_CLASS(ms);
unsigned cpus = config->has_cpus ? config->cpus : 0;
unsigned sockets = config->has_sockets ? config->sockets : 0;
unsigned dies = config->has_dies ? config->dies : 0;
unsigned cores = config->has_cores ? config->cores : 0;
unsigned threads = config->has_threads ? config->threads : 0;
unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
/*
* Specified CPU topology parameters must be greater than zero,
* explicit configuration like "cpus=0" is not allowed.
*/
if ((config->has_cpus && config->cpus == 0) ||
(config->has_sockets && config->sockets == 0) ||
(config->has_dies && config->dies == 0) ||
(config->has_cores && config->cores == 0) ||
(config->has_threads && config->threads == 0) ||
(config->has_maxcpus && config->maxcpus == 0)) {
warn_report("Deprecated CPU topology (considered invalid): "
"CPU topology parameters must be greater than zero");
}
/*
* If not supported by the machine, a topology parameter must be
* omitted or specified equal to 1.
*/
if (!mc->smp_props.dies_supported && dies > 1) {
error_setg(errp, "dies not supported by this machine's CPU topology");
return;
}
dies = dies > 0 ? dies : 1;
/* compute missing values based on the provided ones */
if (cpus == 0 && maxcpus == 0) {
sockets = sockets > 0 ? sockets : 1;
cores = cores > 0 ? cores : 1;
threads = threads > 0 ? threads : 1;
} else {
maxcpus = maxcpus > 0 ? maxcpus : cpus;
if (mc->smp_props.prefer_sockets) {
/* prefer sockets over cores before 6.2 */
if (sockets == 0) {
cores = cores > 0 ? cores : 1;
threads = threads > 0 ? threads : 1;
sockets = maxcpus / (dies * cores * threads);
} else if (cores == 0) {
threads = threads > 0 ? threads : 1;
cores = maxcpus / (sockets * dies * threads);
}
} else {
/* prefer cores over sockets since 6.2 */
if (cores == 0) {
sockets = sockets > 0 ? sockets : 1;
threads = threads > 0 ? threads : 1;
cores = maxcpus / (sockets * dies * threads);
} else if (sockets == 0) {
threads = threads > 0 ? threads : 1;
sockets = maxcpus / (dies * cores * threads);
}
}
/* try to calculate omitted threads at last */
if (threads == 0) {
threads = maxcpus / (sockets * dies * cores);
}
}
maxcpus = maxcpus > 0 ? maxcpus : sockets * dies * cores * threads;
cpus = cpus > 0 ? cpus : maxcpus;
ms->smp.cpus = cpus;
ms->smp.sockets = sockets;
ms->smp.dies = dies;
ms->smp.cores = cores;
ms->smp.threads = threads;
ms->smp.max_cpus = maxcpus;
/* sanity-check of the computed topology */
if (sockets * dies * cores * threads != maxcpus) {
g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
error_setg(errp, "Invalid CPU topology: "
"product of the hierarchy must match maxcpus: "
"%s != maxcpus (%u)",
topo_msg, maxcpus);
return;
}
if (maxcpus < cpus) {
g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
error_setg(errp, "Invalid CPU topology: "
"maxcpus must be equal to or greater than smp: "
"%s == maxcpus (%u) < smp_cpus (%u)",
topo_msg, maxcpus, cpus);
return;
}
if (ms->smp.cpus < mc->min_cpus) {
error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
"supported by machine '%s' is %d",
ms->smp.cpus,
mc->name, mc->min_cpus);
return;
}
if (ms->smp.max_cpus > mc->max_cpus) {
error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
"supported by machine '%s' is %d",
ms->smp.max_cpus,
mc->name, mc->max_cpus);
return;
}
}

View File

@ -548,35 +548,30 @@ void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
{
bool allowed = false;
strList *wl;
Object *obj = OBJECT(dev);
if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
return false;
}
return device_type_is_dynamic_sysbus(mc, object_get_typename(obj));
}
bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type)
{
bool allowed = false;
strList *wl;
ObjectClass *klass = object_class_by_name(type);
for (wl = mc->allowed_dynamic_sysbus_devices;
!allowed && wl;
wl = wl->next) {
allowed |= !!object_dynamic_cast(obj, wl->value);
allowed |= !!object_class_dynamic_cast(klass, wl->value);
}
return allowed;
}
static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
{
MachineState *machine = opaque;
MachineClass *mc = MACHINE_GET_CLASS(machine);
if (!device_is_dynamic_sysbus(mc, DEVICE(sbdev))) {
error_report("Option '-device %s' cannot be handled by this machine",
object_class_get_name(object_get_class(OBJECT(sbdev))));
exit(1);
}
}
static char *machine_get_memdev(Object *obj, Error **errp)
{
MachineState *ms = MACHINE(obj);
@ -592,17 +587,6 @@ static void machine_set_memdev(Object *obj, const char *value, Error **errp)
ms->ram_memdev_id = g_strdup(value);
}
static void machine_init_notify(Notifier *notifier, void *data)
{
MachineState *machine = MACHINE(qdev_get_machine());
/*
* Loop through all dynamically created sysbus devices and check if they are
* all allowed. If a device is not allowed, error out.
*/
foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
}
HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
{
int i;
@ -749,165 +733,6 @@ void machine_set_cpu_numa_node(MachineState *machine,
}
}
/*
* Report information of a machine's supported CPU topology hierarchy.
* Topology members will be ordered from the largest to the smallest
* in the string.
*/
static char *cpu_hierarchy_to_string(MachineState *ms)
{
MachineClass *mc = MACHINE_GET_CLASS(ms);
GString *s = g_string_new(NULL);
g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);
if (mc->smp_props.dies_supported) {
g_string_append_printf(s, " * dies (%u)", ms->smp.dies);
}
g_string_append_printf(s, " * cores (%u)", ms->smp.cores);
g_string_append_printf(s, " * threads (%u)", ms->smp.threads);
return g_string_free(s, false);
}
/*
* smp_parse - Generic function used to parse the given SMP configuration
*
* Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
* automatically computed based on the provided ones.
*
* In the calculation of omitted sockets/cores/threads: we prefer sockets
* over cores over threads before 6.2, while preferring cores over sockets
* over threads since 6.2.
*
* In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted,
* maxcpus will be computed from the given parameters and cpus will be set
* equal to maxcpus. When only one of maxcpus and cpus is given then the
* omitted one will be set to its given counterpart's value. Both maxcpus and
* cpus may be specified, but maxcpus must be equal to or greater than cpus.
*
* For compatibility, apart from the parameters that will be computed, newly
* introduced topology members which are likely to be target specific should
* be directly set as 1 if they are omitted (e.g. dies for PC since 4.1).
*/
static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
{
MachineClass *mc = MACHINE_GET_CLASS(ms);
unsigned cpus = config->has_cpus ? config->cpus : 0;
unsigned sockets = config->has_sockets ? config->sockets : 0;
unsigned dies = config->has_dies ? config->dies : 0;
unsigned cores = config->has_cores ? config->cores : 0;
unsigned threads = config->has_threads ? config->threads : 0;
unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
/*
* Specified CPU topology parameters must be greater than zero,
* explicit configuration like "cpus=0" is not allowed.
*/
if ((config->has_cpus && config->cpus == 0) ||
(config->has_sockets && config->sockets == 0) ||
(config->has_dies && config->dies == 0) ||
(config->has_cores && config->cores == 0) ||
(config->has_threads && config->threads == 0) ||
(config->has_maxcpus && config->maxcpus == 0)) {
warn_report("Deprecated CPU topology (considered invalid): "
"CPU topology parameters must be greater than zero");
}
/*
* If not supported by the machine, a topology parameter must be
* omitted or specified equal to 1.
*/
if (!mc->smp_props.dies_supported && dies > 1) {
error_setg(errp, "dies not supported by this machine's CPU topology");
return;
}
dies = dies > 0 ? dies : 1;
/* compute missing values based on the provided ones */
if (cpus == 0 && maxcpus == 0) {
sockets = sockets > 0 ? sockets : 1;
cores = cores > 0 ? cores : 1;
threads = threads > 0 ? threads : 1;
} else {
maxcpus = maxcpus > 0 ? maxcpus : cpus;
if (mc->smp_props.prefer_sockets) {
/* prefer sockets over cores before 6.2 */
if (sockets == 0) {
cores = cores > 0 ? cores : 1;
threads = threads > 0 ? threads : 1;
sockets = maxcpus / (dies * cores * threads);
} else if (cores == 0) {
threads = threads > 0 ? threads : 1;
cores = maxcpus / (sockets * dies * threads);
}
} else {
/* prefer cores over sockets since 6.2 */
if (cores == 0) {
sockets = sockets > 0 ? sockets : 1;
threads = threads > 0 ? threads : 1;
cores = maxcpus / (sockets * dies * threads);
} else if (sockets == 0) {
threads = threads > 0 ? threads : 1;
sockets = maxcpus / (dies * cores * threads);
}
}
/* try to calculate omitted threads at last */
if (threads == 0) {
threads = maxcpus / (sockets * dies * cores);
}
}
maxcpus = maxcpus > 0 ? maxcpus : sockets * dies * cores * threads;
cpus = cpus > 0 ? cpus : maxcpus;
ms->smp.cpus = cpus;
ms->smp.sockets = sockets;
ms->smp.dies = dies;
ms->smp.cores = cores;
ms->smp.threads = threads;
ms->smp.max_cpus = maxcpus;
/* sanity-check of the computed topology */
if (sockets * dies * cores * threads != maxcpus) {
g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
error_setg(errp, "Invalid CPU topology: "
"product of the hierarchy must match maxcpus: "
"%s != maxcpus (%u)",
topo_msg, maxcpus);
return;
}
if (maxcpus < cpus) {
g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
error_setg(errp, "Invalid CPU topology: "
"maxcpus must be equal to or greater than smp: "
"%s == maxcpus (%u) < smp_cpus (%u)",
topo_msg, maxcpus, cpus);
return;
}
if (ms->smp.cpus < mc->min_cpus) {
error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
"supported by machine '%s' is %d",
ms->smp.cpus,
mc->name, mc->min_cpus);
return;
}
if (ms->smp.max_cpus > mc->max_cpus) {
error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
"supported by machine '%s' is %d",
ms->smp.max_cpus,
mc->name, mc->max_cpus);
return;
}
}
static void machine_get_smp(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
{
@ -1101,10 +926,6 @@ static void machine_initfn(Object *obj)
"Table (HMAT)");
}
/* Register notifier when init is done for sysbus sanity checks */
ms->sysbus_notifier.notify = machine_init_notify;
qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
/* default to mc->default_cpus */
ms->smp.cpus = mc->default_cpus;
ms->smp.max_cpus = mc->default_cpus;

View File

@ -1,7 +1,6 @@
# core qdev-related obj files, also used by *-user and unit tests
hwcore_files = files(
hwcore_ss.add(files(
'bus.c',
'hotplug.c',
'qdev-properties.c',
'qdev.c',
'reset.c',
@ -11,22 +10,34 @@ hwcore_files = files(
'irq.c',
'clock.c',
'qdev-clock.c',
)
))
if have_system
hwcore_ss.add(files(
'hotplug.c',
'qdev-hotplug.c',
))
else
hwcore_ss.add(files(
'hotplug-stubs.c',
))
endif
common_ss.add(files('cpu-common.c'))
common_ss.add(when: 'CONFIG_FITLOADER', if_true: files('loader-fit.c'))
common_ss.add(when: 'CONFIG_GENERIC_LOADER', if_true: files('generic-loader.c'))
common_ss.add(when: ['CONFIG_GUEST_LOADER', fdt], if_true: files('guest-loader.c'))
common_ss.add(when: 'CONFIG_OR_IRQ', if_true: files('or-irq.c'))
common_ss.add(when: 'CONFIG_PLATFORM_BUS', if_true: files('platform-bus.c'))
common_ss.add(when: 'CONFIG_PTIMER', if_true: files('ptimer.c'))
common_ss.add(when: 'CONFIG_REGISTER', if_true: files('register.c'))
common_ss.add(when: 'CONFIG_SPLIT_IRQ', if_true: files('split-irq.c'))
common_ss.add(when: 'CONFIG_XILINX_AXI', if_true: files('stream.c'))
common_ss.add(files('machine-smp.c'))
softmmu_ss.add(when: 'CONFIG_FITLOADER', if_true: files('loader-fit.c'))
softmmu_ss.add(when: 'CONFIG_GENERIC_LOADER', if_true: files('generic-loader.c'))
softmmu_ss.add(when: ['CONFIG_GUEST_LOADER', fdt], if_true: files('guest-loader.c'))
softmmu_ss.add(when: 'CONFIG_OR_IRQ', if_true: files('or-irq.c'))
softmmu_ss.add(when: 'CONFIG_PLATFORM_BUS', if_true: files('platform-bus.c'))
softmmu_ss.add(when: 'CONFIG_PTIMER', if_true: files('ptimer.c'))
softmmu_ss.add(when: 'CONFIG_REGISTER', if_true: files('register.c'))
softmmu_ss.add(when: 'CONFIG_SPLIT_IRQ', if_true: files('split-irq.c'))
softmmu_ss.add(when: 'CONFIG_XILINX_AXI', if_true: files('stream.c'))
softmmu_ss.add(files(
'cpu-sysemu.c',
'fw-path-provider.c',
'gpio.c',
'loader.c',
'machine-hmp-cmds.c',
'machine.c',

View File

@ -0,0 +1,73 @@
/*
* QDev Hotplug handlers
*
* Copyright (c) Red Hat
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "hw/qdev-core.h"
#include "hw/boards.h"
HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
{
MachineState *machine;
MachineClass *mc;
Object *m_obj = qdev_get_machine();
if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
machine = MACHINE(m_obj);
mc = MACHINE_GET_CLASS(machine);
if (mc->get_hotplug_handler) {
return mc->get_hotplug_handler(machine, dev);
}
}
return NULL;
}
bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
{
MachineState *machine;
MachineClass *mc;
Object *m_obj = qdev_get_machine();
if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
machine = MACHINE(m_obj);
mc = MACHINE_GET_CLASS(machine);
if (mc->hotplug_allowed) {
return mc->hotplug_allowed(machine, dev, errp);
}
}
return true;
}
HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
{
if (dev->parent_bus) {
return dev->parent_bus->hotplug_handler;
}
return NULL;
}
HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
{
HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
if (hotplug_ctrl == NULL && dev->parent_bus) {
hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
}
return hotplug_ctrl;
}
/* can be used as ->unplug() callback for the simple cases */
void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
DeviceState *dev, Error **errp)
{
qdev_unrealize(dev);
}

View File

@ -33,7 +33,6 @@
#include "qapi/visitor.h"
#include "qemu/error-report.h"
#include "qemu/option.h"
#include "hw/hotplug.h"
#include "hw/irq.h"
#include "hw/qdev-properties.h"
#include "hw/boards.h"
@ -238,58 +237,6 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
dev->alias_required_for_version = required_for_version;
}
HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
{
MachineState *machine;
MachineClass *mc;
Object *m_obj = qdev_get_machine();
if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
machine = MACHINE(m_obj);
mc = MACHINE_GET_CLASS(machine);
if (mc->get_hotplug_handler) {
return mc->get_hotplug_handler(machine, dev);
}
}
return NULL;
}
bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
{
MachineState *machine;
MachineClass *mc;
Object *m_obj = qdev_get_machine();
if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
machine = MACHINE(m_obj);
mc = MACHINE_GET_CLASS(machine);
if (mc->hotplug_allowed) {
return mc->hotplug_allowed(machine, dev, errp);
}
}
return true;
}
HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
{
if (dev->parent_bus) {
return dev->parent_bus->hotplug_handler;
}
return NULL;
}
HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
{
HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
if (hotplug_ctrl == NULL && dev->parent_bus) {
hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
}
return hotplug_ctrl;
}
static int qdev_prereset(DeviceState *dev, void *opaque)
{
trace_qdev_reset_tree(dev, object_get_typename(OBJECT(dev)));
@ -371,13 +318,6 @@ static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
}
}
/* can be used as ->unplug() callback for the simple cases */
void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
DeviceState *dev, Error **errp)
{
qdev_unrealize(dev);
}
bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
{
assert(!dev->realized && !dev->parent_bus);
@ -436,180 +376,6 @@ BusState *qdev_get_parent_bus(DeviceState *dev)
return dev->parent_bus;
}
static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
const char *name)
{
NamedGPIOList *ngl;
QLIST_FOREACH(ngl, &dev->gpios, node) {
/* NULL is a valid and matchable name. */
if (g_strcmp0(name, ngl->name) == 0) {
return ngl;
}
}
ngl = g_malloc0(sizeof(*ngl));
ngl->name = g_strdup(name);
QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
return ngl;
}
void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
qemu_irq_handler handler,
void *opaque,
const char *name, int n)
{
int i;
NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
assert(gpio_list->num_out == 0 || !name);
gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
opaque, n);
if (!name) {
name = "unnamed-gpio-in";
}
for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
gchar *propname = g_strdup_printf("%s[%u]", name, i);
object_property_add_child(OBJECT(dev), propname,
OBJECT(gpio_list->in[i]));
g_free(propname);
}
gpio_list->num_in += n;
}
void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
{
qdev_init_gpio_in_named(dev, handler, NULL, n);
}
void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
const char *name, int n)
{
int i;
NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
assert(gpio_list->num_in == 0 || !name);
if (!name) {
name = "unnamed-gpio-out";
}
memset(pins, 0, sizeof(*pins) * n);
for (i = 0; i < n; ++i) {
gchar *propname = g_strdup_printf("%s[%u]", name,
gpio_list->num_out + i);
object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
(Object **)&pins[i],
object_property_allow_set_link,
OBJ_PROP_LINK_STRONG);
g_free(propname);
}
gpio_list->num_out += n;
}
void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
{
qdev_init_gpio_out_named(dev, pins, NULL, n);
}
qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
{
NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
assert(n >= 0 && n < gpio_list->num_in);
return gpio_list->in[n];
}
qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
{
return qdev_get_gpio_in_named(dev, NULL, n);
}
void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
qemu_irq pin)
{
char *propname = g_strdup_printf("%s[%d]",
name ? name : "unnamed-gpio-out", n);
if (pin && !OBJECT(pin)->parent) {
/* We need a name for object_property_set_link to work */
object_property_add_child(container_get(qdev_get_machine(),
"/unattached"),
"non-qdev-gpio[*]", OBJECT(pin));
}
object_property_set_link(OBJECT(dev), propname, OBJECT(pin), &error_abort);
g_free(propname);
}
qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
{
g_autofree char *propname = g_strdup_printf("%s[%d]",
name ? name : "unnamed-gpio-out", n);
qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
NULL);
return ret;
}
/* disconnect a GPIO output, returning the disconnected input (if any) */
static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
const char *name, int n)
{
char *propname = g_strdup_printf("%s[%d]",
name ? name : "unnamed-gpio-out", n);
qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
NULL);
if (ret) {
object_property_set_link(OBJECT(dev), propname, NULL, NULL);
}
g_free(propname);
return ret;
}
qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
const char *name, int n)
{
qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
qdev_connect_gpio_out_named(dev, name, n, icpt);
return disconnected;
}
void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
{
qdev_connect_gpio_out_named(dev, NULL, n, pin);
}
void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
const char *name)
{
int i;
NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
for (i = 0; i < ngl->num_in; i++) {
const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
char *propname = g_strdup_printf("%s[%d]", nm, i);
object_property_add_alias(OBJECT(container), propname,
OBJECT(dev), propname);
g_free(propname);
}
for (i = 0; i < ngl->num_out; i++) {
const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
char *propname = g_strdup_printf("%s[%d]", nm, i);
object_property_add_alias(OBJECT(container), propname,
OBJECT(dev), propname);
g_free(propname);
}
QLIST_REMOVE(ngl, node);
QLIST_INSERT_HEAD(&container->gpios, ngl, node);
}
BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
{
BusState *bus;

View File

@ -34,6 +34,7 @@ HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine);
void machine_set_cpu_numa_node(MachineState *machine,
const CpuInstanceProperties *props,
Error **errp);
void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp);
/**
* machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devices
@ -51,6 +52,21 @@ void machine_set_cpu_numa_node(MachineState *machine,
*/
void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
/**
* device_type_is_dynamic_sysbus: Check if type is an allowed sysbus device
* type for the machine class.
* @mc: Machine class
* @type: type to check (should be a subtype of TYPE_SYS_BUS_DEVICE)
*
* Returns: true if @type is a type in the machine's list of
* dynamically pluggable sysbus devices; otherwise false.
*
* Check if the QOM type @type is in the list of allowed sysbus device
* types (see machine_class_allowed_dynamic_sysbus_dev()).
* Note that if @type has a parent type in the list, it is allowed too.
*/
bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type);
/**
* device_is_dynamic_sysbus: test whether device is a dynamic sysbus device
* @mc: Machine class
@ -301,7 +317,6 @@ typedef struct CpuTopology {
struct MachineState {
/*< private >*/
Object parent_obj;
Notifier sysbus_notifier;
/*< public >*/

View File

@ -2365,6 +2365,7 @@ bsd_user_ss = ss.source_set()
chardev_ss = ss.source_set()
common_ss = ss.source_set()
crypto_ss = ss.source_set()
hwcore_ss = ss.source_set()
io_ss = ss.source_set()
linux_user_ss = ss.source_set()
qmp_ss = ss.source_set()
@ -2806,7 +2807,8 @@ libchardev = static_library('chardev', chardev_ss.sources() + genh,
chardev = declare_dependency(link_whole: libchardev)
libhwcore = static_library('hwcore', sources: hwcore_files + genh,
hwcore_ss = hwcore_ss.apply(config_host, strict: false)
libhwcore = static_library('hwcore', sources: hwcore_ss.sources() + genh,
name_suffix: 'fa',
build_by_default: false)
hwcore = declare_dependency(link_whole: libhwcore)

View File

@ -42,6 +42,7 @@
#include "qemu/cutils.h"
#include "hw/qdev-properties.h"
#include "hw/clock.h"
#include "hw/boards.h"
/*
* Aliases were a bad idea from the start. Let's keep them
@ -254,6 +255,16 @@ static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
return NULL;
}
if (object_class_dynamic_cast(oc, TYPE_SYS_BUS_DEVICE)) {
/* sysbus devices need to be allowed by the machine */
MachineClass *mc = MACHINE_CLASS(object_get_class(qdev_get_machine()));
if (!device_type_is_dynamic_sysbus(mc, *driver)) {
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
"a dynamic sysbus device type for the machine");
return NULL;
}
}
return dc;
}

View File

@ -46,6 +46,7 @@ tests = {
'test-uuid': [],
'ptimer-test': ['ptimer-test-stubs.c', meson.project_source_root() / 'hw/core/ptimer.c'],
'test-qapi-util': [],
'test-smp-parse': [qom, meson.project_source_root() / 'hw/core/machine-smp.c'],
}
if have_system or have_tools

View File

@ -0,0 +1,594 @@
/*
* SMP parsing unit-tests
*
* Copyright (c) 2021 Huawei Technologies Co., Ltd
*
* Authors:
* Yanan Wang <wangyanan55@huawei.com>
*
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qom/object.h"
#include "qemu/module.h"
#include "qapi/error.h"
#include "hw/boards.h"
#define T true
#define F false
#define MIN_CPUS 1 /* set the min CPUs supported by the machine as 1 */
#define MAX_CPUS 512 /* set the max CPUs supported by the machine as 512 */
/*
* Used to define the generic 3-level CPU topology hierarchy
* -sockets/cores/threads
*/
#define SMP_CONFIG_GENERIC(ha, a, hb, b, hc, c, hd, d, he, e) \
{ \
.has_cpus = ha, .cpus = a, \
.has_sockets = hb, .sockets = b, \
.has_cores = hc, .cores = c, \
.has_threads = hd, .threads = d, \
.has_maxcpus = he, .maxcpus = e, \
}
#define CPU_TOPOLOGY_GENERIC(a, b, c, d, e) \
{ \
.cpus = a, \
.sockets = b, \
.cores = c, \
.threads = d, \
.max_cpus = e, \
}
/*
* Currently a 4-level topology hierarchy is supported on PC machines
* -sockets/dies/cores/threads
*/
#define SMP_CONFIG_WITH_DIES(ha, a, hb, b, hc, c, hd, d, he, e, hf, f) \
{ \
.has_cpus = ha, .cpus = a, \
.has_sockets = hb, .sockets = b, \
.has_dies = hc, .dies = c, \
.has_cores = hd, .cores = d, \
.has_threads = he, .threads = e, \
.has_maxcpus = hf, .maxcpus = f, \
}
/**
* @config - the given SMP configuration
* @expect_prefer_sockets - the expected parsing result for the
* valid configuration, when sockets are preferred over cores
* @expect_prefer_cores - the expected parsing result for the
* valid configuration, when cores are preferred over sockets
* @expect_error - the expected error report when the given
* configuration is invalid
*/
typedef struct SMPTestData {
SMPConfiguration config;
CpuTopology expect_prefer_sockets;
CpuTopology expect_prefer_cores;
const char *expect_error;
} SMPTestData;
/* Type info of the tested machine */
static const TypeInfo smp_machine_info = {
.name = TYPE_MACHINE,
.parent = TYPE_OBJECT,
.class_size = sizeof(MachineClass),
.instance_size = sizeof(MachineState),
};
/*
* List all the possible valid sub-collections of the generic 5
* topology parameters (i.e. cpus/maxcpus/sockets/cores/threads),
* then test the automatic calculation algorithm of the missing
* values in the parser.
*/
static struct SMPTestData data_generic_valid[] = {
{
/* config: no configuration provided
* expect: cpus=1,sockets=1,cores=1,threads=1,maxcpus=1 */
.config = SMP_CONFIG_GENERIC(F, 0, F, 0, F, 0, F, 0, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(1, 1, 1, 1, 1),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(1, 1, 1, 1, 1),
}, {
/* config: -smp 8
* prefer_sockets: cpus=8,sockets=8,cores=1,threads=1,maxcpus=8
* prefer_cores: cpus=8,sockets=1,cores=8,threads=1,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, F, 0, F, 0, F, 0, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 8, 1, 1, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 1, 8, 1, 8),
}, {
/* config: -smp sockets=2
* expect: cpus=2,sockets=2,cores=1,threads=1,maxcpus=2 */
.config = SMP_CONFIG_GENERIC(F, 0, T, 2, F, 0, F, 0, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(2, 2, 1, 1, 2),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(2, 2, 1, 1, 2),
}, {
/* config: -smp cores=4
* expect: cpus=4,sockets=1,cores=4,threads=1,maxcpus=4 */
.config = SMP_CONFIG_GENERIC(F, 0, F, 0, T, 4, F, 0, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(4, 1, 4, 1, 4),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(4, 1, 4, 1, 4),
}, {
/* config: -smp threads=2
* expect: cpus=2,sockets=1,cores=1,threads=2,maxcpus=2 */
.config = SMP_CONFIG_GENERIC(F, 0, F, 0, F, 0, T, 2, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(2, 1, 1, 2, 2),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(2, 1, 1, 2, 2),
}, {
/* config: -smp maxcpus=16
* prefer_sockets: cpus=16,sockets=16,cores=1,threads=1,maxcpus=16
* prefer_cores: cpus=16,sockets=1,cores=16,threads=1,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, F, 0, F, 0, F, 0, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 16, 1, 1, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 1, 16, 1, 16),
}, {
/* config: -smp 8,sockets=2
* expect: cpus=8,sockets=2,cores=4,threads=1,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, F, 0, F, 0, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
}, {
/* config: -smp 8,cores=4
* expect: cpus=8,sockets=2,cores=4,threads=1,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, F, 0, T, 4, F, 0, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
}, {
/* config: -smp 8,threads=2
* prefer_sockets: cpus=8,sockets=4,cores=1,threads=2,maxcpus=8
* prefer_cores: cpus=8,sockets=1,cores=4,threads=2,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, F, 0, F, 0, T, 2, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 4, 1, 2, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8),
}, {
/* config: -smp 8,maxcpus=16
* prefer_sockets: cpus=8,sockets=16,cores=1,threads=1,maxcpus=16
* prefer_cores: cpus=8,sockets=1,cores=16,threads=1,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 8, F, 0, F, 0, F, 0, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 16, 1, 1, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 1, 16, 1, 16),
}, {
/* config: -smp sockets=2,cores=4
* expect: cpus=8,sockets=2,cores=4,threads=1,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(F, 0, T, 2, T, 4, F, 0, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
}, {
/* config: -smp sockets=2,threads=2
* expect: cpus=4,sockets=2,cores=1,threads=2,maxcpus=4 */
.config = SMP_CONFIG_GENERIC(F, 0, T, 2, F, 0, T, 2, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(4, 2, 1, 2, 4),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(4, 2, 1, 2, 4),
}, {
/* config: -smp sockets=2,maxcpus=16
* expect: cpus=16,sockets=2,cores=8,threads=1,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, T, 2, F, 0, F, 0, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 2, 8, 1, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 2, 8, 1, 16),
}, {
/* config: -smp cores=4,threads=2
* expect: cpus=8,sockets=1,cores=4,threads=2,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(F, 0, F, 0, T, 4, T, 2, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8),
}, {
/* config: -smp cores=4,maxcpus=16
* expect: cpus=16,sockets=4,cores=4,threads=1,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, F, 0, T, 4, F, 0, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 4, 4, 1, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 4, 4, 1, 16),
}, {
/* config: -smp threads=2,maxcpus=16
* prefer_sockets: cpus=16,sockets=8,cores=1,threads=2,maxcpus=16
* prefer_cores: cpus=16,sockets=1,cores=8,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, F, 0, F, 0, T, 2, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 8, 1, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 1, 8, 2, 16),
}, {
/* config: -smp 8,sockets=2,cores=4
* expect: cpus=8,sockets=2,cores=4,threads=1,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, F, 0, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
}, {
/* config: -smp 8,sockets=2,threads=2
* expect: cpus=8,sockets=2,cores=2,threads=2,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, F, 0, T, 2, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 2, 2, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 2, 2, 8),
}, {
/* config: -smp 8,sockets=2,maxcpus=16
* expect: cpus=8,sockets=2,cores=8,threads=1,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, F, 0, F, 0, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 8, 1, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 8, 1, 16),
}, {
/* config: -smp 8,cores=4,threads=2
* expect: cpus=8,sockets=1,cores=4,threads=2,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, F, 0, T, 4, T, 2, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8),
}, {
/* config: -smp 8,cores=4,maxcpus=16
* expect: cpus=8,sockets=4,cores=4,threads=1,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 8, F, 0, T, 4, F, 0, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 4, 4, 1, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 4, 4, 1, 16),
}, {
/* config: -smp 8,threads=2,maxcpus=16
* prefer_sockets: cpus=8,sockets=8,cores=1,threads=2,maxcpus=16
* prefer_cores: cpus=8,sockets=1,cores=8,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 8, F, 0, F, 0, T, 2, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 8, 1, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 1, 8, 2, 16),
}, {
/* config: -smp sockets=2,cores=4,threads=2
* expect: cpus=16,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, T, 2, T, 4, T, 2, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
}, {
/* config: -smp sockets=2,cores=4,maxcpus=16
* expect: cpus=16,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, T, 2, T, 4, F, 0, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
}, {
/* config: -smp sockets=2,threads=2,maxcpus=16
* expect: cpus=16,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, T, 2, F, 0, T, 2, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
}, {
/* config: -smp cores=4,threads=2,maxcpus=16
* expect: cpus=16,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, F, 0, T, 4, T, 2, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
}, {
/* config: -smp 8,sockets=2,cores=4,threads=1
* expect: cpus=8,sockets=2,cores=4,threads=1,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, T, 1, F, 0),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8),
}, {
/* config: -smp 8,sockets=2,cores=4,maxcpus=16
* expect: cpus=8,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, F, 0, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16),
}, {
/* config: -smp 8,sockets=2,threads=2,maxcpus=16
* expect: cpus=8,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, F, 0, T, 2, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16),
}, {
/* config: -smp 8,cores=4,threads=2,maxcpus=16
* expect: cpus=8,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 8, F, 0, T, 4, T, 2, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16),
}, {
/* config: -smp sockets=2,cores=4,threads=2,maxcpus=16
* expect: cpus=16,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(F, 0, T, 2, T, 4, T, 2, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16),
}, {
/* config: -smp 8,sockets=2,cores=4,threads=2,maxcpus=16
* expect: cpus=8,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, T, 2, T, 16),
.expect_prefer_sockets = CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16),
.expect_prefer_cores = CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16),
},
};
static struct SMPTestData data_generic_invalid[] = {
{
/* config: -smp 2,dies=2 */
.config = SMP_CONFIG_WITH_DIES(T, 2, F, 0, T, 2, F, 0, F, 0, F, 0),
.expect_error = "dies not supported by this machine's CPU topology",
}, {
/* config: -smp 8,sockets=2,cores=4,threads=2,maxcpus=8 */
.config = SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, T, 2, T, 8),
.expect_error = "Invalid CPU topology: "
"product of the hierarchy must match maxcpus: "
"sockets (2) * cores (4) * threads (2) "
"!= maxcpus (8)",
}, {
/* config: -smp 18,sockets=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_GENERIC(T, 18, T, 2, T, 4, T, 2, T, 16),
.expect_error = "Invalid CPU topology: "
"maxcpus must be equal to or greater than smp: "
"sockets (2) * cores (4) * threads (2) "
"== maxcpus (16) < smp_cpus (18)",
}, {
/* config: -smp 1
* should tweak the supported min CPUs to 2 for testing */
.config = SMP_CONFIG_GENERIC(T, 1, F, 0, F, 0, F, 0, F, 0),
.expect_error = "Invalid SMP CPUs 1. The min CPUs supported "
"by machine '(null)' is 2",
}, {
/* config: -smp 512
* should tweak the supported max CPUs to 511 for testing */
.config = SMP_CONFIG_GENERIC(T, 512, F, 0, F, 0, F, 0, F, 0),
.expect_error = "Invalid SMP CPUs 512. The max CPUs supported "
"by machine '(null)' is 511",
},
};
static struct SMPTestData data_with_dies_invalid[] = {
{
/* config: -smp 16,sockets=2,dies=2,cores=4,threads=2,maxcpus=16 */
.config = SMP_CONFIG_WITH_DIES(T, 16, T, 2, T, 2, T, 4, T, 2, T, 16),
.expect_error = "Invalid CPU topology: "
"product of the hierarchy must match maxcpus: "
"sockets (2) * dies (2) * cores (4) * threads (2) "
"!= maxcpus (16)",
}, {
/* config: -smp 34,sockets=2,dies=2,cores=4,threads=2,maxcpus=32 */
.config = SMP_CONFIG_WITH_DIES(T, 34, T, 2, T, 2, T, 4, T, 2, T, 32),
.expect_error = "Invalid CPU topology: "
"maxcpus must be equal to or greater than smp: "
"sockets (2) * dies (2) * cores (4) * threads (2) "
"== maxcpus (32) < smp_cpus (34)",
},
};
static char *smp_config_to_string(SMPConfiguration *config)
{
return g_strdup_printf(
"(SMPConfiguration) {\n"
" .has_cpus = %5s, cpus = %" PRId64 ",\n"
" .has_sockets = %5s, sockets = %" PRId64 ",\n"
" .has_dies = %5s, dies = %" PRId64 ",\n"
" .has_cores = %5s, cores = %" PRId64 ",\n"
" .has_threads = %5s, threads = %" PRId64 ",\n"
" .has_maxcpus = %5s, maxcpus = %" PRId64 ",\n"
"}",
config->has_cpus ? "true" : "false", config->cpus,
config->has_sockets ? "true" : "false", config->sockets,
config->has_dies ? "true" : "false", config->dies,
config->has_cores ? "true" : "false", config->cores,
config->has_threads ? "true" : "false", config->threads,
config->has_maxcpus ? "true" : "false", config->maxcpus);
}
static char *cpu_topology_to_string(CpuTopology *topo)
{
return g_strdup_printf(
"(CpuTopology) {\n"
" .cpus = %u,\n"
" .sockets = %u,\n"
" .dies = %u,\n"
" .cores = %u,\n"
" .threads = %u,\n"
" .max_cpus = %u,\n"
"}",
topo->cpus, topo->sockets, topo->dies,
topo->cores, topo->threads, topo->max_cpus);
}
static void check_parse(MachineState *ms, SMPConfiguration *config,
CpuTopology *expect_topo, const char *expect_err,
bool is_valid)
{
g_autofree char *config_str = smp_config_to_string(config);
g_autofree char *expect_topo_str = cpu_topology_to_string(expect_topo);
g_autofree char *output_topo_str = NULL;
Error *err = NULL;
/* call the generic parser smp_parse() */
smp_parse(ms, config, &err);
output_topo_str = cpu_topology_to_string(&ms->smp);
/* when the configuration is supposed to be valid */
if (is_valid) {
if ((err == NULL) &&
(ms->smp.cpus == expect_topo->cpus) &&
(ms->smp.sockets == expect_topo->sockets) &&
(ms->smp.dies == expect_topo->dies) &&
(ms->smp.cores == expect_topo->cores) &&
(ms->smp.threads == expect_topo->threads) &&
(ms->smp.max_cpus == expect_topo->max_cpus)) {
return;
}
if (err != NULL) {
g_printerr("Test smp_parse failed!\n"
"Input configuration: %s\n"
"Should be valid: yes\n"
"Expected topology: %s\n\n"
"Result is valid: no\n"
"Output error report: %s\n",
config_str, expect_topo_str, error_get_pretty(err));
goto end;
}
g_printerr("Test smp_parse failed!\n"
"Input configuration: %s\n"
"Should be valid: yes\n"
"Expected topology: %s\n\n"
"Result is valid: yes\n"
"Output topology: %s\n",
config_str, expect_topo_str, output_topo_str);
goto end;
}
/* when the configuration is supposed to be invalid */
if (err != NULL) {
if (expect_err == NULL ||
g_str_equal(expect_err, error_get_pretty(err))) {
error_free(err);
return;
}
g_printerr("Test smp_parse failed!\n"
"Input configuration: %s\n"
"Should be valid: no\n"
"Expected error report: %s\n\n"
"Result is valid: no\n"
"Output error report: %s\n",
config_str, expect_err, error_get_pretty(err));
goto end;
}
g_printerr("Test smp_parse failed!\n"
"Input configuration: %s\n"
"Should be valid: no\n"
"Expected error report: %s\n\n"
"Result is valid: yes\n"
"Output topology: %s\n",
config_str, expect_err, output_topo_str);
end:
if (err != NULL) {
error_free(err);
}
abort();
}
static void smp_parse_test(MachineState *ms, SMPTestData *data, bool is_valid)
{
MachineClass *mc = MACHINE_GET_CLASS(ms);
mc->smp_props.prefer_sockets = true;
check_parse(ms, &data->config, &data->expect_prefer_sockets,
data->expect_error, is_valid);
mc->smp_props.prefer_sockets = false;
check_parse(ms, &data->config, &data->expect_prefer_cores,
data->expect_error, is_valid);
}
/* The parsed results of the unsupported parameters should be 1 */
static void unsupported_params_init(MachineClass *mc, SMPTestData *data)
{
if (!mc->smp_props.dies_supported) {
data->expect_prefer_sockets.dies = 1;
data->expect_prefer_cores.dies = 1;
}
}
/* Reset the related machine properties before each sub-test */
static void smp_machine_class_init(MachineClass *mc)
{
mc->min_cpus = MIN_CPUS;
mc->max_cpus = MAX_CPUS;
mc->smp_props.prefer_sockets = true;
mc->smp_props.dies_supported = false;
}
static void test_generic(void)
{
Object *obj = object_new(TYPE_MACHINE);
MachineState *ms = MACHINE(obj);
MachineClass *mc = MACHINE_GET_CLASS(obj);
SMPTestData *data = &(SMPTestData){{ }};
int i;
smp_machine_class_init(mc);
for (i = 0; i < ARRAY_SIZE(data_generic_valid); i++) {
*data = data_generic_valid[i];
unsupported_params_init(mc, data);
smp_parse_test(ms, data, true);
/* Unsupported parameters can be provided with their values as 1 */
data->config.has_dies = true;
data->config.dies = 1;
smp_parse_test(ms, data, true);
}
/* Reset the supported min CPUs and max CPUs */
mc->min_cpus = 2;
mc->max_cpus = 511;
for (i = 0; i < ARRAY_SIZE(data_generic_invalid); i++) {
*data = data_generic_invalid[i];
unsupported_params_init(mc, data);
smp_parse_test(ms, data, false);
}
object_unref(obj);
}
static void test_with_dies(void)
{
Object *obj = object_new(TYPE_MACHINE);
MachineState *ms = MACHINE(obj);
MachineClass *mc = MACHINE_GET_CLASS(obj);
SMPTestData *data = &(SMPTestData){{ }};
unsigned int num_dies = 2;
int i;
smp_machine_class_init(mc);
mc->smp_props.dies_supported = true;
for (i = 0; i < ARRAY_SIZE(data_generic_valid); i++) {
*data = data_generic_valid[i];
unsupported_params_init(mc, data);
/* when dies parameter is omitted, it will be set as 1 */
data->expect_prefer_sockets.dies = 1;
data->expect_prefer_cores.dies = 1;
smp_parse_test(ms, data, true);
/* when dies parameter is specified */
data->config.has_dies = true;
data->config.dies = num_dies;
if (data->config.has_cpus) {
data->config.cpus *= num_dies;
}
if (data->config.has_maxcpus) {
data->config.maxcpus *= num_dies;
}
data->expect_prefer_sockets.dies = num_dies;
data->expect_prefer_sockets.cpus *= num_dies;
data->expect_prefer_sockets.max_cpus *= num_dies;
data->expect_prefer_cores.dies = num_dies;
data->expect_prefer_cores.cpus *= num_dies;
data->expect_prefer_cores.max_cpus *= num_dies;
smp_parse_test(ms, data, true);
}
for (i = 0; i < ARRAY_SIZE(data_with_dies_invalid); i++) {
*data = data_with_dies_invalid[i];
unsupported_params_init(mc, data);
smp_parse_test(ms, data, false);
}
object_unref(obj);
}
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
module_call_init(MODULE_INIT_QOM);
type_register_static(&smp_machine_info);
g_test_add_func("/test-smp-parse/generic", test_generic);
g_test_add_func("/test-smp-parse/with_dies", test_with_dies);
g_test_run();
return 0;
}