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 error_setg(errp, "Invalid CPU topology: " 109 "CPU topology parameters must be greater than zero"); 110 return; 111 } 112 113 /* 114 * If not supported by the machine, a topology parameter must be 115 * omitted or specified equal to 1. 116 */ 117 if (!mc->smp_props.dies_supported && dies > 1) { 118 error_setg(errp, "dies not supported by this machine's CPU topology"); 119 return; 120 } 121 if (!mc->smp_props.clusters_supported && clusters > 1) { 122 error_setg(errp, "clusters not supported by this machine's CPU topology"); 123 return; 124 } 125 126 dies = dies > 0 ? dies : 1; 127 clusters = clusters > 0 ? clusters : 1; 128 129 if (!mc->smp_props.books_supported && books > 1) { 130 error_setg(errp, "books not supported by this machine's CPU topology"); 131 return; 132 } 133 books = books > 0 ? books : 1; 134 135 if (!mc->smp_props.drawers_supported && drawers > 1) { 136 error_setg(errp, 137 "drawers not supported by this machine's CPU topology"); 138 return; 139 } 140 drawers = drawers > 0 ? drawers : 1; 141 142 /* compute missing values based on the provided ones */ 143 if (cpus == 0 && maxcpus == 0) { 144 sockets = sockets > 0 ? sockets : 1; 145 cores = cores > 0 ? cores : 1; 146 threads = threads > 0 ? threads : 1; 147 } else { 148 maxcpus = maxcpus > 0 ? maxcpus : cpus; 149 150 if (mc->smp_props.prefer_sockets) { 151 /* prefer sockets over cores before 6.2 */ 152 if (sockets == 0) { 153 cores = cores > 0 ? cores : 1; 154 threads = threads > 0 ? threads : 1; 155 sockets = maxcpus / 156 (drawers * books * dies * clusters * cores * threads); 157 } else if (cores == 0) { 158 threads = threads > 0 ? threads : 1; 159 cores = maxcpus / 160 (drawers * books * sockets * dies * clusters * threads); 161 } 162 } else { 163 /* prefer cores over sockets since 6.2 */ 164 if (cores == 0) { 165 sockets = sockets > 0 ? sockets : 1; 166 threads = threads > 0 ? threads : 1; 167 cores = maxcpus / 168 (drawers * books * sockets * dies * clusters * threads); 169 } else if (sockets == 0) { 170 threads = threads > 0 ? threads : 1; 171 sockets = maxcpus / 172 (drawers * books * dies * clusters * cores * threads); 173 } 174 } 175 176 /* try to calculate omitted threads at last */ 177 if (threads == 0) { 178 threads = maxcpus / 179 (drawers * books * sockets * dies * clusters * cores); 180 } 181 } 182 183 maxcpus = maxcpus > 0 ? maxcpus : drawers * books * sockets * dies * 184 clusters * cores * threads; 185 cpus = cpus > 0 ? cpus : maxcpus; 186 187 ms->smp.cpus = cpus; 188 ms->smp.drawers = drawers; 189 ms->smp.books = books; 190 ms->smp.sockets = sockets; 191 ms->smp.dies = dies; 192 ms->smp.clusters = clusters; 193 ms->smp.cores = cores; 194 ms->smp.threads = threads; 195 ms->smp.max_cpus = maxcpus; 196 197 mc->smp_props.has_clusters = config->has_clusters; 198 199 /* sanity-check of the computed topology */ 200 if (drawers * books * sockets * dies * clusters * cores * threads != 201 maxcpus) { 202 g_autofree char *topo_msg = cpu_hierarchy_to_string(ms); 203 error_setg(errp, "Invalid CPU topology: " 204 "product of the hierarchy must match maxcpus: " 205 "%s != maxcpus (%u)", 206 topo_msg, maxcpus); 207 return; 208 } 209 210 if (maxcpus < cpus) { 211 g_autofree char *topo_msg = cpu_hierarchy_to_string(ms); 212 error_setg(errp, "Invalid CPU topology: " 213 "maxcpus must be equal to or greater than smp: " 214 "%s == maxcpus (%u) < smp_cpus (%u)", 215 topo_msg, maxcpus, cpus); 216 return; 217 } 218 219 if (ms->smp.cpus < mc->min_cpus) { 220 error_setg(errp, "Invalid SMP CPUs %d. The min CPUs " 221 "supported by machine '%s' is %d", 222 ms->smp.cpus, 223 mc->name, mc->min_cpus); 224 return; 225 } 226 227 if (ms->smp.max_cpus > mc->max_cpus) { 228 error_setg(errp, "Invalid SMP CPUs %d. The max CPUs " 229 "supported by machine '%s' is %d", 230 ms->smp.max_cpus, 231 mc->name, mc->max_cpus); 232 return; 233 } 234 } 235 236 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms) 237 { 238 return ms->smp.cores * ms->smp.clusters * ms->smp.dies; 239 } 240 241 unsigned int machine_topo_get_threads_per_socket(const MachineState *ms) 242 { 243 return ms->smp.threads * machine_topo_get_cores_per_socket(ms); 244 } 245