[PATCH v5 07/12] s390: topology: Adding books to CPU topology

Pierre Morel
2021-12-09T13:46:10Z

S390 CPU topology may have up to 5 topology containers.
The first container above the cores is level 2, the sockets.
We introduce here the books, book is the level containing sockets.

Let's add books, level3, containers to the CPU topology.

Signed-off-by: Pierre Morel <pmorel@linux.ibm.com>
---
hw/core/machine-smp.c | 28 +++++++++++++++++++++-------
hw/core/machine.c | 2 ++
hw/s390x/s390-virtio-ccw.c | 1 +
include/hw/boards.h | 4 ++++
qapi/machine.json | 7 ++++++-
softmmu/vl.c | 3 +++
6 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/hw/core/machine-smp.c b/hw/core/machine-smp.c
index 116a0cbbfa..4848f546cf 100644
--- a/hw/core/machine-smp.c
+++ b/hw/core/machine-smp.c
@@ -31,6 +31,10 @@ static char *cpu_hierarchy_to_string(MachineState *ms)
MachineClass *mc = MACHINE_GET_CLASS(ms);
GString *s = g_string_new(NULL);

+ if (mc->smp_props.books_supported) {
+ g_string_append_printf(s, " * books (%u)", ms->smp.books);
+ }
+
g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);

if (mc->smp_props.dies_supported) {
@@ -67,6 +71,7 @@ void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
{
MachineClass *mc = MACHINE_GET_CLASS(ms);
unsigned cpus = config->has_cpus ? config->cpus : 0;
+ unsigned books = config->has_books ? config->books : 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;
@@ -78,6 +83,7 @@ void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
* explicit configuration like "cpus=0" is not allowed.
*/
if ((config->has_cpus && config->cpus == 0) ||
+ (config->has_books && config->books == 0) ||
(config->has_sockets && config->sockets == 0) ||
(config->has_dies && config->dies == 0) ||
(config->has_cores && config->cores == 0) ||
@@ -98,6 +104,13 @@ void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)

dies = dies > 0 ? dies : 1;

+ if (!mc->smp_props.books_supported && books > 1) {
+ error_setg(errp, "books not supported by this machine's CPU topology");
+ return;
+ }
+
+ books = books > 0 ? books : 1;
+
/* compute missing values based on the provided ones */
if (cpus == 0 && maxcpus == 0) {
sockets = sockets > 0 ? sockets : 1;
@@ -111,33 +124,34 @@ void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
if (sockets == 0) {
cores = cores > 0 ? cores : 1;
threads = threads > 0 ? threads : 1;
- sockets = maxcpus / (dies * cores * threads);
+ sockets = maxcpus / (books * dies * cores * threads);
} else if (cores == 0) {
threads = threads > 0 ? threads : 1;
- cores = maxcpus / (sockets * dies * threads);
+ cores = maxcpus / (books * 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);
+ cores = maxcpus / (books * sockets * dies * threads);
} else if (sockets == 0) {
threads = threads > 0 ? threads : 1;
- sockets = maxcpus / (dies * cores * threads);
+ sockets = maxcpus / (books * dies * cores * threads);
}
}

/* try to calculate omitted threads at last */
if (threads == 0) {
- threads = maxcpus / (sockets * dies * cores);
+ threads = maxcpus / (books * sockets * dies * cores);
}
}

- maxcpus = maxcpus > 0 ? maxcpus : sockets * dies * cores * threads;
+ maxcpus = maxcpus > 0 ? maxcpus : books * sockets * dies * cores * threads;
cpus = cpus > 0 ? cpus : maxcpus;

ms->smp.cpus = cpus;
+ ms->smp.books = books;
ms->smp.sockets = sockets;
ms->smp.dies = dies;
ms->smp.cores = cores;
@@ -145,7 +159,7 @@ void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
ms->smp.max_cpus = maxcpus;

/* sanity-check of the computed topology */
- if (sockets * dies * cores * threads != maxcpus) {
+ if (books * 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: "
diff --git a/hw/core/machine.c b/hw/core/machine.c
index 53a99abc56..d98c9105f7 100644
--- a/hw/core/machine.c
+++ b/hw/core/machine.c
@@ -740,6 +740,7 @@ static void machine_get_smp(Object *obj, Visitor *v, const char *name,
MachineState *ms = MACHINE(obj);
SMPConfiguration *config = &(SMPConfiguration){
.has_cpus = true, .cpus = ms->smp.cpus,
+ .has_books = true, .books = ms->smp.books,
.has_sockets = true, .sockets = ms->smp.sockets,
.has_dies = true, .dies = ms->smp.dies,
.has_cores = true, .cores = ms->smp.cores,
@@ -930,6 +931,7 @@ static void machine_initfn(Object *obj)
/* default to mc->default_cpus */
ms->smp.cpus = mc->default_cpus;
ms->smp.max_cpus = mc->default_cpus;
+ ms->smp.books = 1;
ms->smp.sockets = 1;
ms->smp.dies = 1;
ms->smp.cores = 1;
diff --git a/hw/s390x/s390-virtio-ccw.c b/hw/s390x/s390-virtio-ccw.c
index 6218352e68..b3c405b4d0 100644
--- a/hw/s390x/s390-virtio-ccw.c
+++ b/hw/s390x/s390-virtio-ccw.c
@@ -666,6 +666,7 @@ static void ccw_machine_class_init(ObjectClass *oc, void *data)
hc->unplug_request = s390_machine_device_unplug_request;
nc->nmi_monitor_handler = s390_nmi;
mc->default_ram_id = "s390.ram";
+ mc->smp_props.books_supported = true;
}

static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
diff --git a/include/hw/boards.h b/include/hw/boards.h
index 9c1c190104..68c58b4b4a 100644
--- a/include/hw/boards.h
+++ b/include/hw/boards.h
@@ -128,10 +128,12 @@ typedef struct {
* SMPCompatProps:
* @prefer_sockets - whether sockets are preferred over cores in smp parsing
* @dies_supported - whether dies are supported by the machine
+ * @books_supported - whether books are supported by the machine
*/
typedef struct {
bool prefer_sockets;
bool dies_supported;
+ bool books_supported;
} SMPCompatProps;

/**
@@ -296,6 +298,7 @@ typedef struct DeviceMemoryState {
/**
* CpuTopology:
* @cpus: the number of present logical processors on the machine
+ * @books: the number of books on the machine
* @sockets: the number of sockets on the machine
* @dies: the number of dies in one socket
* @cores: the number of cores in one die
@@ -304,6 +307,7 @@ typedef struct DeviceMemoryState {
*/
typedef struct CpuTopology {
unsigned int cpus;
+ unsigned int books;
unsigned int sockets;
unsigned int dies;
unsigned int cores;
diff --git a/qapi/machine.json b/qapi/machine.json
index 067e3f5378..5761c02070 100644
--- a/qapi/machine.json
+++ b/qapi/machine.json
@@ -866,12 +866,13 @@
# a CPU is being hotplugged.
#
# @node-id: NUMA node ID the CPU belongs to
+# @book-id: book number within node/board the CPU belongs to
# @socket-id: socket number within node/board the CPU belongs to
# @die-id: die number within node/board the CPU belongs to (Since 4.1)
# @core-id: core number within die the CPU belongs to
# @thread-id: thread number within core the CPU belongs to
#
-# Note: currently there are 5 properties that could be present
+# Note: currently there are 6 properties that could be present
# but management should be prepared to pass through other
# properties with device_add command to allow for future
# interface extension. This also requires the filed names to be kept in
@@ -881,6 +882,7 @@
##
{ 'struct': 'CpuInstanceProperties',
'data': { '*node-id': 'int',
+ '*book-id': 'int',
'*socket-id': 'int',
'*die-id': 'int',
'*core-id': 'int',
@@ -1392,6 +1394,8 @@
#
# @cpus: number of virtual CPUs in the virtual machine
#
+# @books: number of books in the CPU topology
+#
# @sockets: number of sockets in the CPU topology
#
# @dies: number of dies per socket in the CPU topology
@@ -1406,6 +1410,7 @@
##
{ 'struct': 'SMPConfiguration', 'data': {
'*cpus': 'int',
+ '*books': 'int',
'*sockets': 'int',
'*dies': 'int',
'*cores': 'int',
diff --git a/softmmu/vl.c b/softmmu/vl.c
index 620a1f1367..764403d0b3 100644
--- a/softmmu/vl.c
+++ b/softmmu/vl.c
@@ -720,6 +720,9 @@ static QemuOptsList qemu_smp_opts = {
{
.name = "cpus",
.type = QEMU_OPT_NUMBER,
+ }, {
+ .name = "books",
+ .type = QEMU_OPT_NUMBER,
}, {
.name = "sockets",
.type = QEMU_OPT_NUMBER,
--
2.27.0