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 if (mc->smp_props.modules_supported) { 55 g_string_append_printf(s, " * modules (%u)", ms->smp.modules); 56 } 57 58 g_string_append_printf(s, " * cores (%u)", ms->smp.cores); 59 g_string_append_printf(s, " * threads (%u)", ms->smp.threads); 60 61 return g_string_free(s, false); 62 } 63 64 /* 65 * machine_parse_smp_config: Generic function used to parse the given 66 * SMP configuration 67 * 68 * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be 69 * automatically computed based on the provided ones. 70 * 71 * In the calculation of omitted sockets/cores/threads: we prefer sockets 72 * over cores over threads before 6.2, while preferring cores over sockets 73 * over threads since 6.2. 74 * 75 * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted, 76 * maxcpus will be computed from the given parameters and cpus will be set 77 * equal to maxcpus. When only one of maxcpus and cpus is given then the 78 * omitted one will be set to its given counterpart's value. Both maxcpus and 79 * cpus may be specified, but maxcpus must be equal to or greater than cpus. 80 * 81 * For compatibility, apart from the parameters that will be computed, newly 82 * introduced topology members which are likely to be target specific should 83 * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1). 84 */ 85 void machine_parse_smp_config(MachineState *ms, 86 const SMPConfiguration *config, Error **errp) 87 { 88 MachineClass *mc = MACHINE_GET_CLASS(ms); 89 unsigned cpus = config->has_cpus ? config->cpus : 0; 90 unsigned drawers = config->has_drawers ? config->drawers : 0; 91 unsigned books = config->has_books ? config->books : 0; 92 unsigned sockets = config->has_sockets ? config->sockets : 0; 93 unsigned dies = config->has_dies ? config->dies : 0; 94 unsigned clusters = config->has_clusters ? config->clusters : 0; 95 unsigned modules = config->has_modules ? config->modules : 0; 96 unsigned cores = config->has_cores ? config->cores : 0; 97 unsigned threads = config->has_threads ? config->threads : 0; 98 unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0; 99 unsigned total_cpus; 100 101 /* 102 * Specified CPU topology parameters must be greater than zero, 103 * explicit configuration like "cpus=0" is not allowed. 104 */ 105 if ((config->has_cpus && config->cpus == 0) || 106 (config->has_drawers && config->drawers == 0) || 107 (config->has_books && config->books == 0) || 108 (config->has_sockets && config->sockets == 0) || 109 (config->has_dies && config->dies == 0) || 110 (config->has_clusters && config->clusters == 0) || 111 (config->has_modules && config->modules == 0) || 112 (config->has_cores && config->cores == 0) || 113 (config->has_threads && config->threads == 0) || 114 (config->has_maxcpus && config->maxcpus == 0)) { 115 error_setg(errp, "Invalid CPU topology: " 116 "CPU topology parameters must be greater than zero"); 117 return; 118 } 119 120 /* 121 * If not supported by the machine, a topology parameter must 122 * not be set to a value greater than 1. 123 */ 124 if (!mc->smp_props.modules_supported && 125 config->has_modules && config->modules > 1) { 126 error_setg(errp, 127 "modules > 1 not supported by this machine's CPU topology"); 128 return; 129 } 130 modules = modules > 0 ? modules : 1; 131 132 if (!mc->smp_props.clusters_supported && 133 config->has_clusters && config->clusters > 1) { 134 error_setg(errp, 135 "clusters > 1 not supported by this machine's CPU topology"); 136 return; 137 } 138 clusters = clusters > 0 ? clusters : 1; 139 140 if (!mc->smp_props.dies_supported && 141 config->has_dies && config->dies > 1) { 142 error_setg(errp, 143 "dies > 1 not supported by this machine's CPU topology"); 144 return; 145 } 146 dies = dies > 0 ? dies : 1; 147 148 if (!mc->smp_props.books_supported && 149 config->has_books && config->books > 1) { 150 error_setg(errp, 151 "books > 1 not supported by this machine's CPU topology"); 152 return; 153 } 154 books = books > 0 ? books : 1; 155 156 if (!mc->smp_props.drawers_supported && 157 config->has_drawers && config->drawers > 1) { 158 error_setg(errp, 159 "drawers > 1 not supported by this machine's CPU topology"); 160 return; 161 } 162 drawers = drawers > 0 ? drawers : 1; 163 164 /* compute missing values based on the provided ones */ 165 if (cpus == 0 && maxcpus == 0) { 166 sockets = sockets > 0 ? sockets : 1; 167 cores = cores > 0 ? cores : 1; 168 threads = threads > 0 ? threads : 1; 169 } else { 170 maxcpus = maxcpus > 0 ? maxcpus : cpus; 171 172 if (mc->smp_props.prefer_sockets) { 173 /* prefer sockets over cores before 6.2 */ 174 if (sockets == 0) { 175 cores = cores > 0 ? cores : 1; 176 threads = threads > 0 ? threads : 1; 177 sockets = maxcpus / 178 (drawers * books * dies * clusters * 179 modules * cores * threads); 180 } else if (cores == 0) { 181 threads = threads > 0 ? threads : 1; 182 cores = maxcpus / 183 (drawers * books * sockets * dies * 184 clusters * modules * threads); 185 } 186 } else { 187 /* prefer cores over sockets since 6.2 */ 188 if (cores == 0) { 189 sockets = sockets > 0 ? sockets : 1; 190 threads = threads > 0 ? threads : 1; 191 cores = maxcpus / 192 (drawers * books * sockets * dies * 193 clusters * modules * threads); 194 } else if (sockets == 0) { 195 threads = threads > 0 ? threads : 1; 196 sockets = maxcpus / 197 (drawers * books * dies * clusters * 198 modules * cores * threads); 199 } 200 } 201 202 /* try to calculate omitted threads at last */ 203 if (threads == 0) { 204 threads = maxcpus / 205 (drawers * books * sockets * dies * 206 clusters * modules * cores); 207 } 208 } 209 210 total_cpus = drawers * books * sockets * dies * 211 clusters * modules * cores * threads; 212 maxcpus = maxcpus > 0 ? maxcpus : total_cpus; 213 cpus = cpus > 0 ? cpus : maxcpus; 214 215 ms->smp.cpus = cpus; 216 ms->smp.drawers = drawers; 217 ms->smp.books = books; 218 ms->smp.sockets = sockets; 219 ms->smp.dies = dies; 220 ms->smp.clusters = clusters; 221 ms->smp.modules = modules; 222 ms->smp.cores = cores; 223 ms->smp.threads = threads; 224 ms->smp.max_cpus = maxcpus; 225 226 mc->smp_props.has_clusters = config->has_clusters; 227 228 /* sanity-check of the computed topology */ 229 if (total_cpus != maxcpus) { 230 g_autofree char *topo_msg = cpu_hierarchy_to_string(ms); 231 error_setg(errp, "Invalid CPU topology: " 232 "product of the hierarchy must match maxcpus: " 233 "%s != maxcpus (%u)", 234 topo_msg, maxcpus); 235 return; 236 } 237 238 if (maxcpus < cpus) { 239 g_autofree char *topo_msg = cpu_hierarchy_to_string(ms); 240 error_setg(errp, "Invalid CPU topology: " 241 "maxcpus must be equal to or greater than smp: " 242 "%s == maxcpus (%u) < smp_cpus (%u)", 243 topo_msg, maxcpus, cpus); 244 return; 245 } 246 247 if (ms->smp.cpus < mc->min_cpus) { 248 error_setg(errp, "Invalid SMP CPUs %d. The min CPUs " 249 "supported by machine '%s' is %d", 250 ms->smp.cpus, 251 mc->name, mc->min_cpus); 252 return; 253 } 254 255 if (ms->smp.max_cpus > mc->max_cpus) { 256 error_setg(errp, "Invalid SMP CPUs %d. The max CPUs " 257 "supported by machine '%s' is %d", 258 ms->smp.max_cpus, 259 mc->name, mc->max_cpus); 260 return; 261 } 262 } 263 264 static bool machine_check_topo_support(MachineState *ms, 265 CpuTopologyLevel topo, 266 Error **errp) 267 { 268 MachineClass *mc = MACHINE_GET_CLASS(ms); 269 270 if ((topo == CPU_TOPOLOGY_LEVEL_MODULE && !mc->smp_props.modules_supported) || 271 (topo == CPU_TOPOLOGY_LEVEL_CLUSTER && !mc->smp_props.clusters_supported) || 272 (topo == CPU_TOPOLOGY_LEVEL_DIE && !mc->smp_props.dies_supported) || 273 (topo == CPU_TOPOLOGY_LEVEL_BOOK && !mc->smp_props.books_supported) || 274 (topo == CPU_TOPOLOGY_LEVEL_DRAWER && !mc->smp_props.drawers_supported)) { 275 error_setg(errp, 276 "Invalid topology level: %s. " 277 "The topology level is not supported by this machine", 278 CpuTopologyLevel_str(topo)); 279 return false; 280 } 281 282 return true; 283 } 284 285 bool machine_parse_smp_cache(MachineState *ms, 286 const SmpCachePropertiesList *caches, 287 Error **errp) 288 { 289 MachineClass *mc = MACHINE_GET_CLASS(ms); 290 const SmpCachePropertiesList *node; 291 DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX); 292 293 bitmap_zero(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX); 294 for (node = caches; node; node = node->next) { 295 /* Prohibit users from repeating settings. */ 296 if (test_bit(node->value->cache, caches_bitmap)) { 297 error_setg(errp, 298 "Invalid cache properties: %s. " 299 "The cache properties are duplicated", 300 CacheLevelAndType_str(node->value->cache)); 301 return false; 302 } 303 304 machine_set_cache_topo_level(ms, node->value->cache, 305 node->value->topology); 306 set_bit(node->value->cache, caches_bitmap); 307 } 308 309 for (int i = 0; i < CACHE_LEVEL_AND_TYPE__MAX; i++) { 310 const SmpCacheProperties *props = &ms->smp_cache.props[i]; 311 312 /* 313 * Reject non "default" topology level if the cache isn't 314 * supported by the machine. 315 */ 316 if (props->topology != CPU_TOPOLOGY_LEVEL_DEFAULT && 317 !mc->smp_props.cache_supported[props->cache]) { 318 error_setg(errp, 319 "%s cache topology not supported by this machine", 320 CacheLevelAndType_str(props->cache)); 321 return false; 322 } 323 324 if (!machine_check_topo_support(ms, props->topology, errp)) { 325 return false; 326 } 327 } 328 return true; 329 } 330 331 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms) 332 { 333 return ms->smp.cores * ms->smp.modules * ms->smp.clusters * ms->smp.dies; 334 } 335 336 unsigned int machine_topo_get_threads_per_socket(const MachineState *ms) 337 { 338 return ms->smp.threads * machine_topo_get_cores_per_socket(ms); 339 } 340 341 CpuTopologyLevel machine_get_cache_topo_level(const MachineState *ms, 342 CacheLevelAndType cache) 343 { 344 return ms->smp_cache.props[cache].topology; 345 } 346 347 void machine_set_cache_topo_level(MachineState *ms, CacheLevelAndType cache, 348 CpuTopologyLevel level) 349 { 350 ms->smp_cache.props[cache].topology = level; 351 } 352 353 /* 354 * When both cache1 and cache2 are configured with specific topology levels 355 * (not default level), is cache1's topology level higher than cache2? 356 */ 357 static bool smp_cache_topo_cmp(const SmpCache *smp_cache, 358 CacheLevelAndType cache1, 359 CacheLevelAndType cache2) 360 { 361 /* 362 * Before comparing, the "default" topology level should be replaced 363 * with the specific level. 364 */ 365 assert(smp_cache->props[cache1].topology != CPU_TOPOLOGY_LEVEL_DEFAULT); 366 367 return smp_cache->props[cache1].topology > smp_cache->props[cache2].topology; 368 } 369 370 /* 371 * Currently, we have no way to expose the arch-specific default cache model 372 * because the cache model is sometimes related to the CPU model (e.g., i386). 373 * 374 * We can only check the correctness of the cache topology after the arch loads 375 * the user-configured cache model from MachineState and consumes the special 376 * "default" level by replacing it with the specific level. 377 */ 378 bool machine_check_smp_cache(const MachineState *ms, Error **errp) 379 { 380 if (smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L1D, 381 CACHE_LEVEL_AND_TYPE_L2) || 382 smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L1I, 383 CACHE_LEVEL_AND_TYPE_L2)) { 384 error_setg(errp, 385 "Invalid smp cache topology. " 386 "L2 cache topology level shouldn't be lower than L1 cache"); 387 return false; 388 } 389 390 if (smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L2, 391 CACHE_LEVEL_AND_TYPE_L3)) { 392 error_setg(errp, 393 "Invalid smp cache topology. " 394 "L3 cache topology level shouldn't be lower than L2 cache"); 395 return false; 396 } 397 398 return true; 399 } 400