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