xref: /openbmc/qemu/hw/core/machine-smp.c (revision 6ee45fac)
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 #include "qemu/error-report.h"
24 
25 
26 /*
27  * Report information of a machine's supported CPU topology hierarchy.
28  * Topology members will be ordered from the largest to the smallest
29  * in the string.
30  */
31 static char *cpu_hierarchy_to_string(MachineState *ms)
32 {
33     MachineClass *mc = MACHINE_GET_CLASS(ms);
34     GString *s = g_string_new(NULL);
35 
36     if (mc->smp_props.drawers_supported) {
37         g_string_append_printf(s, "drawers (%u) * ", ms->smp.drawers);
38     }
39 
40     if (mc->smp_props.books_supported) {
41         g_string_append_printf(s, "books (%u) * ", ms->smp.books);
42     }
43 
44     g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);
45 
46     if (mc->smp_props.dies_supported) {
47         g_string_append_printf(s, " * dies (%u)", ms->smp.dies);
48     }
49 
50     if (mc->smp_props.clusters_supported) {
51         g_string_append_printf(s, " * clusters (%u)", ms->smp.clusters);
52     }
53 
54     g_string_append_printf(s, " * cores (%u)", ms->smp.cores);
55     g_string_append_printf(s, " * threads (%u)", ms->smp.threads);
56 
57     return g_string_free(s, false);
58 }
59 
60 /*
61  * machine_parse_smp_config: Generic function used to parse the given
62  *                           SMP configuration
63  *
64  * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
65  * automatically computed based on the provided ones.
66  *
67  * In the calculation of omitted sockets/cores/threads: we prefer sockets
68  * over cores over threads before 6.2, while preferring cores over sockets
69  * over threads since 6.2.
70  *
71  * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted,
72  * maxcpus will be computed from the given parameters and cpus will be set
73  * equal to maxcpus. When only one of maxcpus and cpus is given then the
74  * omitted one will be set to its given counterpart's value. Both maxcpus and
75  * cpus may be specified, but maxcpus must be equal to or greater than cpus.
76  *
77  * For compatibility, apart from the parameters that will be computed, newly
78  * introduced topology members which are likely to be target specific should
79  * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1).
80  */
81 void machine_parse_smp_config(MachineState *ms,
82                               const SMPConfiguration *config, Error **errp)
83 {
84     MachineClass *mc = MACHINE_GET_CLASS(ms);
85     unsigned cpus    = config->has_cpus ? config->cpus : 0;
86     unsigned drawers = config->has_drawers ? config->drawers : 0;
87     unsigned books   = config->has_books ? config->books : 0;
88     unsigned sockets = config->has_sockets ? config->sockets : 0;
89     unsigned dies    = config->has_dies ? config->dies : 0;
90     unsigned clusters = config->has_clusters ? config->clusters : 0;
91     unsigned cores   = config->has_cores ? config->cores : 0;
92     unsigned threads = config->has_threads ? config->threads : 0;
93     unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
94 
95     /*
96      * Specified CPU topology parameters must be greater than zero,
97      * explicit configuration like "cpus=0" is not allowed.
98      */
99     if ((config->has_cpus && config->cpus == 0) ||
100         (config->has_drawers && config->drawers == 0) ||
101         (config->has_books && config->books == 0) ||
102         (config->has_sockets && config->sockets == 0) ||
103         (config->has_dies && config->dies == 0) ||
104         (config->has_clusters && config->clusters == 0) ||
105         (config->has_cores && config->cores == 0) ||
106         (config->has_threads && config->threads == 0) ||
107         (config->has_maxcpus && config->maxcpus == 0)) {
108         warn_report("Deprecated CPU topology (considered invalid): "
109                     "CPU topology parameters must be greater than zero");
110     }
111 
112     /*
113      * If not supported by the machine, a topology parameter must be
114      * omitted or specified equal to 1.
115      */
116     if (!mc->smp_props.dies_supported && dies > 1) {
117         error_setg(errp, "dies not supported by this machine's CPU topology");
118         return;
119     }
120     if (!mc->smp_props.clusters_supported && clusters > 1) {
121         error_setg(errp, "clusters not supported by this machine's CPU topology");
122         return;
123     }
124 
125     dies = dies > 0 ? dies : 1;
126     clusters = clusters > 0 ? clusters : 1;
127 
128     if (!mc->smp_props.books_supported && books > 1) {
129         error_setg(errp, "books not supported by this machine's CPU topology");
130         return;
131     }
132     books = books > 0 ? books : 1;
133 
134     if (!mc->smp_props.drawers_supported && drawers > 1) {
135         error_setg(errp,
136                    "drawers not supported by this machine's CPU topology");
137         return;
138     }
139     drawers = drawers > 0 ? drawers : 1;
140 
141     /* compute missing values based on the provided ones */
142     if (cpus == 0 && maxcpus == 0) {
143         sockets = sockets > 0 ? sockets : 1;
144         cores = cores > 0 ? cores : 1;
145         threads = threads > 0 ? threads : 1;
146     } else {
147         maxcpus = maxcpus > 0 ? maxcpus : cpus;
148 
149         if (mc->smp_props.prefer_sockets) {
150             /* prefer sockets over cores before 6.2 */
151             if (sockets == 0) {
152                 cores = cores > 0 ? cores : 1;
153                 threads = threads > 0 ? threads : 1;
154                 sockets = maxcpus /
155                           (drawers * books * dies * clusters * cores * threads);
156             } else if (cores == 0) {
157                 threads = threads > 0 ? threads : 1;
158                 cores = maxcpus /
159                         (drawers * books * sockets * dies * clusters * threads);
160             }
161         } else {
162             /* prefer cores over sockets since 6.2 */
163             if (cores == 0) {
164                 sockets = sockets > 0 ? sockets : 1;
165                 threads = threads > 0 ? threads : 1;
166                 cores = maxcpus /
167                         (drawers * books * sockets * dies * clusters * threads);
168             } else if (sockets == 0) {
169                 threads = threads > 0 ? threads : 1;
170                 sockets = maxcpus /
171                           (drawers * books * dies * clusters * cores * threads);
172             }
173         }
174 
175         /* try to calculate omitted threads at last */
176         if (threads == 0) {
177             threads = maxcpus /
178                       (drawers * books * sockets * dies * clusters * cores);
179         }
180     }
181 
182     maxcpus = maxcpus > 0 ? maxcpus : drawers * books * sockets * dies *
183                                       clusters * cores * threads;
184     cpus = cpus > 0 ? cpus : maxcpus;
185 
186     ms->smp.cpus = cpus;
187     ms->smp.drawers = drawers;
188     ms->smp.books = books;
189     ms->smp.sockets = sockets;
190     ms->smp.dies = dies;
191     ms->smp.clusters = clusters;
192     ms->smp.cores = cores;
193     ms->smp.threads = threads;
194     ms->smp.max_cpus = maxcpus;
195 
196     mc->smp_props.has_clusters = config->has_clusters;
197 
198     /* sanity-check of the computed topology */
199     if (drawers * books * sockets * dies * clusters * cores * threads !=
200         maxcpus) {
201         g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
202         error_setg(errp, "Invalid CPU topology: "
203                    "product of the hierarchy must match maxcpus: "
204                    "%s != maxcpus (%u)",
205                    topo_msg, maxcpus);
206         return;
207     }
208 
209     if (maxcpus < cpus) {
210         g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
211         error_setg(errp, "Invalid CPU topology: "
212                    "maxcpus must be equal to or greater than smp: "
213                    "%s == maxcpus (%u) < smp_cpus (%u)",
214                    topo_msg, maxcpus, cpus);
215         return;
216     }
217 
218     if (ms->smp.cpus < mc->min_cpus) {
219         error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
220                    "supported by machine '%s' is %d",
221                    ms->smp.cpus,
222                    mc->name, mc->min_cpus);
223         return;
224     }
225 
226     if (ms->smp.max_cpus > mc->max_cpus) {
227         error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
228                    "supported by machine '%s' is %d",
229                    ms->smp.max_cpus,
230                    mc->name, mc->max_cpus);
231         return;
232     }
233 }
234 
235 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms)
236 {
237     return ms->smp.cores * ms->smp.clusters * ms->smp.dies;
238 }
239 
240 unsigned int machine_topo_get_threads_per_socket(const MachineState *ms)
241 {
242     return ms->smp.threads * machine_topo_get_cores_per_socket(ms);
243 }
244