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
24
25
26
27
28
29static 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71void 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
85
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
100
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
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
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
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
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
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}
196
197unsigned int machine_topo_get_cores_per_socket(const MachineState *ms)
198{
199 return ms->smp.cores * ms->smp.clusters * ms->smp.dies;
200}
201
202unsigned int machine_topo_get_threads_per_socket(const MachineState *ms)
203{
204 return ms->smp.threads * machine_topo_get_cores_per_socket(ms);
205}
206