Merge tag 'edgar/xilinx-next-2022-09-21.for-upstream' of https://github.com/edgarigl...
[qemu.git] / hw / core / machine-smp.c
1 /*
2 * QEMU Machine core (related to -smp parsing)
3 *
4 * Copyright (c) 2021 Huawei Technologies Co., Ltd
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "hw/boards.h"
22 #include "qapi/error.h"
23
24 /*
25 * Report information of a machine's supported CPU topology hierarchy.
26 * Topology members will be ordered from the largest to the smallest
27 * in the string.
28 */
29 static char *cpu_hierarchy_to_string(MachineState *ms)
30 {
31 MachineClass *mc = MACHINE_GET_CLASS(ms);
32 GString *s = g_string_new(NULL);
33
34 g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);
35
36 if (mc->smp_props.dies_supported) {
37 g_string_append_printf(s, " * dies (%u)", ms->smp.dies);
38 }
39
40 if (mc->smp_props.clusters_supported) {
41 g_string_append_printf(s, " * clusters (%u)", ms->smp.clusters);
42 }
43
44 g_string_append_printf(s, " * cores (%u)", ms->smp.cores);
45 g_string_append_printf(s, " * threads (%u)", ms->smp.threads);
46
47 return g_string_free(s, false);
48 }
49
50 /*
51 * machine_parse_smp_config: Generic function used to parse the given
52 * SMP configuration
53 *
54 * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
55 * automatically computed based on the provided ones.
56 *
57 * In the calculation of omitted sockets/cores/threads: we prefer sockets
58 * over cores over threads before 6.2, while preferring cores over sockets
59 * over threads since 6.2.
60 *
61 * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted,
62 * maxcpus will be computed from the given parameters and cpus will be set
63 * equal to maxcpus. When only one of maxcpus and cpus is given then the
64 * omitted one will be set to its given counterpart's value. Both maxcpus and
65 * cpus may be specified, but maxcpus must be equal to or greater than cpus.
66 *
67 * For compatibility, apart from the parameters that will be computed, newly
68 * introduced topology members which are likely to be target specific should
69 * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1).
70 */
71 void machine_parse_smp_config(MachineState *ms,
72 const SMPConfiguration *config, Error **errp)
73 {
74 MachineClass *mc = MACHINE_GET_CLASS(ms);
75 unsigned cpus = config->has_cpus ? config->cpus : 0;
76 unsigned sockets = config->has_sockets ? config->sockets : 0;
77 unsigned dies = config->has_dies ? config->dies : 0;
78 unsigned clusters = config->has_clusters ? config->clusters : 0;
79 unsigned cores = config->has_cores ? config->cores : 0;
80 unsigned threads = config->has_threads ? config->threads : 0;
81 unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
82
83 /*
84 * Specified CPU topology parameters must be greater than zero,
85 * explicit configuration like "cpus=0" is not allowed.
86 */
87 if ((config->has_cpus && config->cpus == 0) ||
88 (config->has_sockets && config->sockets == 0) ||
89 (config->has_dies && config->dies == 0) ||
90 (config->has_clusters && config->clusters == 0) ||
91 (config->has_cores && config->cores == 0) ||
92 (config->has_threads && config->threads == 0) ||
93 (config->has_maxcpus && config->maxcpus == 0)) {
94 warn_report("Deprecated CPU topology (considered invalid): "
95 "CPU topology parameters must be greater than zero");
96 }
97
98 /*
99 * If not supported by the machine, a topology parameter must be
100 * omitted or specified equal to 1.
101 */
102 if (!mc->smp_props.dies_supported && dies > 1) {
103 error_setg(errp, "dies not supported by this machine's CPU topology");
104 return;
105 }
106 if (!mc->smp_props.clusters_supported && clusters > 1) {
107 error_setg(errp, "clusters not supported by this machine's CPU topology");
108 return;
109 }
110
111 dies = dies > 0 ? dies : 1;
112 clusters = clusters > 0 ? clusters : 1;
113
114 /* compute missing values based on the provided ones */
115 if (cpus == 0 && maxcpus == 0) {
116 sockets = sockets > 0 ? sockets : 1;
117 cores = cores > 0 ? cores : 1;
118 threads = threads > 0 ? threads : 1;
119 } else {
120 maxcpus = maxcpus > 0 ? maxcpus : cpus;
121
122 if (mc->smp_props.prefer_sockets) {
123 /* prefer sockets over cores before 6.2 */
124 if (sockets == 0) {
125 cores = cores > 0 ? cores : 1;
126 threads = threads > 0 ? threads : 1;
127 sockets = maxcpus / (dies * clusters * cores * threads);
128 } else if (cores == 0) {
129 threads = threads > 0 ? threads : 1;
130 cores = maxcpus / (sockets * dies * clusters * threads);
131 }
132 } else {
133 /* prefer cores over sockets since 6.2 */
134 if (cores == 0) {
135 sockets = sockets > 0 ? sockets : 1;
136 threads = threads > 0 ? threads : 1;
137 cores = maxcpus / (sockets * dies * clusters * threads);
138 } else if (sockets == 0) {
139 threads = threads > 0 ? threads : 1;
140 sockets = maxcpus / (dies * clusters * cores * threads);
141 }
142 }
143
144 /* try to calculate omitted threads at last */
145 if (threads == 0) {
146 threads = maxcpus / (sockets * dies * clusters * cores);
147 }
148 }
149
150 maxcpus = maxcpus > 0 ? maxcpus : sockets * dies * clusters * cores * threads;
151 cpus = cpus > 0 ? cpus : maxcpus;
152
153 ms->smp.cpus = cpus;
154 ms->smp.sockets = sockets;
155 ms->smp.dies = dies;
156 ms->smp.clusters = clusters;
157 ms->smp.cores = cores;
158 ms->smp.threads = threads;
159 ms->smp.max_cpus = maxcpus;
160
161 /* sanity-check of the computed topology */
162 if (sockets * dies * clusters * cores * threads != maxcpus) {
163 g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
164 error_setg(errp, "Invalid CPU topology: "
165 "product of the hierarchy must match maxcpus: "
166 "%s != maxcpus (%u)",
167 topo_msg, maxcpus);
168 return;
169 }
170
171 if (maxcpus < cpus) {
172 g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
173 error_setg(errp, "Invalid CPU topology: "
174 "maxcpus must be equal to or greater than smp: "
175 "%s == maxcpus (%u) < smp_cpus (%u)",
176 topo_msg, maxcpus, cpus);
177 return;
178 }
179
180 if (ms->smp.cpus < mc->min_cpus) {
181 error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
182 "supported by machine '%s' is %d",
183 ms->smp.cpus,
184 mc->name, mc->min_cpus);
185 return;
186 }
187
188 if (ms->smp.max_cpus > mc->max_cpus) {
189 error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
190 "supported by machine '%s' is %d",
191 ms->smp.max_cpus,
192 mc->name, mc->max_cpus);
193 return;
194 }
195 }