1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
28
29
30
31static 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73void 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
87
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
102
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
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
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
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
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
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