2
0

machine-smp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #include "qemu/osdep.h"
  20. #include "hw/boards.h"
  21. #include "qapi/error.h"
  22. #include "qemu/error-report.h"
  23. /*
  24. * Report information of a machine's supported CPU topology hierarchy.
  25. * Topology members will be ordered from the largest to the smallest
  26. * in the string.
  27. */
  28. static char *cpu_hierarchy_to_string(MachineState *ms)
  29. {
  30. MachineClass *mc = MACHINE_GET_CLASS(ms);
  31. GString *s = g_string_new(NULL);
  32. if (mc->smp_props.drawers_supported) {
  33. g_string_append_printf(s, "drawers (%u) * ", ms->smp.drawers);
  34. }
  35. if (mc->smp_props.books_supported) {
  36. g_string_append_printf(s, "books (%u) * ", ms->smp.books);
  37. }
  38. g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);
  39. if (mc->smp_props.dies_supported) {
  40. g_string_append_printf(s, " * dies (%u)", ms->smp.dies);
  41. }
  42. if (mc->smp_props.clusters_supported) {
  43. g_string_append_printf(s, " * clusters (%u)", ms->smp.clusters);
  44. }
  45. if (mc->smp_props.modules_supported) {
  46. g_string_append_printf(s, " * modules (%u)", ms->smp.modules);
  47. }
  48. g_string_append_printf(s, " * cores (%u)", ms->smp.cores);
  49. g_string_append_printf(s, " * threads (%u)", ms->smp.threads);
  50. return g_string_free(s, false);
  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 drawers = config->has_drawers ? config->drawers : 0;
  79. unsigned books = config->has_books ? config->books : 0;
  80. unsigned sockets = config->has_sockets ? config->sockets : 0;
  81. unsigned dies = config->has_dies ? config->dies : 0;
  82. unsigned clusters = config->has_clusters ? config->clusters : 0;
  83. unsigned modules = config->has_modules ? config->modules : 0;
  84. unsigned cores = config->has_cores ? config->cores : 0;
  85. unsigned threads = config->has_threads ? config->threads : 0;
  86. unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
  87. unsigned total_cpus;
  88. /*
  89. * Specified CPU topology parameters must be greater than zero,
  90. * explicit configuration like "cpus=0" is not allowed.
  91. */
  92. if ((config->has_cpus && config->cpus == 0) ||
  93. (config->has_drawers && config->drawers == 0) ||
  94. (config->has_books && config->books == 0) ||
  95. (config->has_sockets && config->sockets == 0) ||
  96. (config->has_dies && config->dies == 0) ||
  97. (config->has_clusters && config->clusters == 0) ||
  98. (config->has_modules && config->modules == 0) ||
  99. (config->has_cores && config->cores == 0) ||
  100. (config->has_threads && config->threads == 0) ||
  101. (config->has_maxcpus && config->maxcpus == 0)) {
  102. error_setg(errp, "Invalid CPU topology: "
  103. "CPU topology parameters must be greater than zero");
  104. return;
  105. }
  106. /*
  107. * If not supported by the machine, a topology parameter must
  108. * not be set to a value greater than 1.
  109. */
  110. if (!mc->smp_props.modules_supported &&
  111. config->has_modules && config->modules > 1) {
  112. error_setg(errp,
  113. "modules > 1 not supported by this machine's CPU topology");
  114. return;
  115. }
  116. modules = modules > 0 ? modules : 1;
  117. if (!mc->smp_props.clusters_supported &&
  118. config->has_clusters && config->clusters > 1) {
  119. error_setg(errp,
  120. "clusters > 1 not supported by this machine's CPU topology");
  121. return;
  122. }
  123. clusters = clusters > 0 ? clusters : 1;
  124. if (!mc->smp_props.dies_supported &&
  125. config->has_dies && config->dies > 1) {
  126. error_setg(errp,
  127. "dies > 1 not supported by this machine's CPU topology");
  128. return;
  129. }
  130. dies = dies > 0 ? dies : 1;
  131. if (!mc->smp_props.books_supported &&
  132. config->has_books && config->books > 1) {
  133. error_setg(errp,
  134. "books > 1 not supported by this machine's CPU topology");
  135. return;
  136. }
  137. books = books > 0 ? books : 1;
  138. if (!mc->smp_props.drawers_supported &&
  139. config->has_drawers && config->drawers > 1) {
  140. error_setg(errp,
  141. "drawers > 1 not supported by this machine's CPU topology");
  142. return;
  143. }
  144. drawers = drawers > 0 ? drawers : 1;
  145. /* compute missing values based on the provided ones */
  146. if (cpus == 0 && maxcpus == 0) {
  147. sockets = sockets > 0 ? sockets : 1;
  148. cores = cores > 0 ? cores : 1;
  149. threads = threads > 0 ? threads : 1;
  150. } else {
  151. maxcpus = maxcpus > 0 ? maxcpus : cpus;
  152. if (mc->smp_props.prefer_sockets) {
  153. /* prefer sockets over cores before 6.2 */
  154. if (sockets == 0) {
  155. cores = cores > 0 ? cores : 1;
  156. threads = threads > 0 ? threads : 1;
  157. sockets = maxcpus /
  158. (drawers * books * dies * clusters *
  159. modules * cores * threads);
  160. } else if (cores == 0) {
  161. threads = threads > 0 ? threads : 1;
  162. cores = maxcpus /
  163. (drawers * books * sockets * dies *
  164. clusters * modules * threads);
  165. }
  166. } else {
  167. /* prefer cores over sockets since 6.2 */
  168. if (cores == 0) {
  169. sockets = sockets > 0 ? sockets : 1;
  170. threads = threads > 0 ? threads : 1;
  171. cores = maxcpus /
  172. (drawers * books * sockets * dies *
  173. clusters * modules * threads);
  174. } else if (sockets == 0) {
  175. threads = threads > 0 ? threads : 1;
  176. sockets = maxcpus /
  177. (drawers * books * dies * clusters *
  178. modules * cores * threads);
  179. }
  180. }
  181. /* try to calculate omitted threads at last */
  182. if (threads == 0) {
  183. threads = maxcpus /
  184. (drawers * books * sockets * dies *
  185. clusters * modules * cores);
  186. }
  187. }
  188. total_cpus = drawers * books * sockets * dies *
  189. clusters * modules * cores * threads;
  190. maxcpus = maxcpus > 0 ? maxcpus : total_cpus;
  191. cpus = cpus > 0 ? cpus : maxcpus;
  192. ms->smp.cpus = cpus;
  193. ms->smp.drawers = drawers;
  194. ms->smp.books = books;
  195. ms->smp.sockets = sockets;
  196. ms->smp.dies = dies;
  197. ms->smp.clusters = clusters;
  198. ms->smp.modules = modules;
  199. ms->smp.cores = cores;
  200. ms->smp.threads = threads;
  201. ms->smp.max_cpus = maxcpus;
  202. mc->smp_props.has_clusters = config->has_clusters;
  203. /* sanity-check of the computed topology */
  204. if (total_cpus != maxcpus) {
  205. g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
  206. error_setg(errp, "Invalid CPU topology: "
  207. "product of the hierarchy must match maxcpus: "
  208. "%s != maxcpus (%u)",
  209. topo_msg, maxcpus);
  210. return;
  211. }
  212. if (maxcpus < cpus) {
  213. g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
  214. error_setg(errp, "Invalid CPU topology: "
  215. "maxcpus must be equal to or greater than smp: "
  216. "%s == maxcpus (%u) < smp_cpus (%u)",
  217. topo_msg, maxcpus, cpus);
  218. return;
  219. }
  220. if (ms->smp.cpus < mc->min_cpus) {
  221. error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
  222. "supported by machine '%s' is %d",
  223. ms->smp.cpus,
  224. mc->name, mc->min_cpus);
  225. return;
  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. static bool machine_check_topo_support(MachineState *ms,
  236. CpuTopologyLevel topo,
  237. Error **errp)
  238. {
  239. MachineClass *mc = MACHINE_GET_CLASS(ms);
  240. if ((topo == CPU_TOPOLOGY_LEVEL_MODULE && !mc->smp_props.modules_supported) ||
  241. (topo == CPU_TOPOLOGY_LEVEL_CLUSTER && !mc->smp_props.clusters_supported) ||
  242. (topo == CPU_TOPOLOGY_LEVEL_DIE && !mc->smp_props.dies_supported) ||
  243. (topo == CPU_TOPOLOGY_LEVEL_BOOK && !mc->smp_props.books_supported) ||
  244. (topo == CPU_TOPOLOGY_LEVEL_DRAWER && !mc->smp_props.drawers_supported)) {
  245. error_setg(errp,
  246. "Invalid topology level: %s. "
  247. "The topology level is not supported by this machine",
  248. CpuTopologyLevel_str(topo));
  249. return false;
  250. }
  251. return true;
  252. }
  253. bool machine_parse_smp_cache(MachineState *ms,
  254. const SmpCachePropertiesList *caches,
  255. Error **errp)
  256. {
  257. MachineClass *mc = MACHINE_GET_CLASS(ms);
  258. const SmpCachePropertiesList *node;
  259. DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
  260. bitmap_zero(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
  261. for (node = caches; node; node = node->next) {
  262. /* Prohibit users from repeating settings. */
  263. if (test_bit(node->value->cache, caches_bitmap)) {
  264. error_setg(errp,
  265. "Invalid cache properties: %s. "
  266. "The cache properties are duplicated",
  267. CacheLevelAndType_str(node->value->cache));
  268. return false;
  269. }
  270. machine_set_cache_topo_level(ms, node->value->cache,
  271. node->value->topology);
  272. set_bit(node->value->cache, caches_bitmap);
  273. }
  274. for (int i = 0; i < CACHE_LEVEL_AND_TYPE__MAX; i++) {
  275. const SmpCacheProperties *props = &ms->smp_cache.props[i];
  276. /*
  277. * Reject non "default" topology level if the cache isn't
  278. * supported by the machine.
  279. */
  280. if (props->topology != CPU_TOPOLOGY_LEVEL_DEFAULT &&
  281. !mc->smp_props.cache_supported[props->cache]) {
  282. error_setg(errp,
  283. "%s cache topology not supported by this machine",
  284. CacheLevelAndType_str(props->cache));
  285. return false;
  286. }
  287. if (props->topology == CPU_TOPOLOGY_LEVEL_THREAD) {
  288. error_setg(errp,
  289. "%s level cache not supported by this machine",
  290. CpuTopologyLevel_str(props->topology));
  291. return false;
  292. }
  293. if (!machine_check_topo_support(ms, props->topology, errp)) {
  294. return false;
  295. }
  296. }
  297. mc->smp_props.has_caches = true;
  298. return true;
  299. }
  300. unsigned int machine_topo_get_cores_per_socket(const MachineState *ms)
  301. {
  302. return ms->smp.cores * ms->smp.modules * ms->smp.clusters * ms->smp.dies;
  303. }
  304. unsigned int machine_topo_get_threads_per_socket(const MachineState *ms)
  305. {
  306. return ms->smp.threads * machine_topo_get_cores_per_socket(ms);
  307. }
  308. CpuTopologyLevel machine_get_cache_topo_level(const MachineState *ms,
  309. CacheLevelAndType cache)
  310. {
  311. return ms->smp_cache.props[cache].topology;
  312. }
  313. void machine_set_cache_topo_level(MachineState *ms, CacheLevelAndType cache,
  314. CpuTopologyLevel level)
  315. {
  316. ms->smp_cache.props[cache].topology = level;
  317. }
  318. /*
  319. * When both cache1 and cache2 are configured with specific topology levels
  320. * (not default level), is cache1's topology level higher than cache2?
  321. */
  322. static bool smp_cache_topo_cmp(const SmpCache *smp_cache,
  323. CacheLevelAndType cache1,
  324. CacheLevelAndType cache2)
  325. {
  326. /*
  327. * Before comparing, the "default" topology level should be replaced
  328. * with the specific level.
  329. */
  330. assert(smp_cache->props[cache1].topology != CPU_TOPOLOGY_LEVEL_DEFAULT);
  331. return smp_cache->props[cache1].topology > smp_cache->props[cache2].topology;
  332. }
  333. /*
  334. * Currently, we have no way to expose the arch-specific default cache model
  335. * because the cache model is sometimes related to the CPU model (e.g., i386).
  336. *
  337. * We can only check the correctness of the cache topology after the arch loads
  338. * the user-configured cache model from MachineState and consumes the special
  339. * "default" level by replacing it with the specific level.
  340. */
  341. bool machine_check_smp_cache(const MachineState *ms, Error **errp)
  342. {
  343. if (smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L1D,
  344. CACHE_LEVEL_AND_TYPE_L2) ||
  345. smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L1I,
  346. CACHE_LEVEL_AND_TYPE_L2)) {
  347. error_setg(errp,
  348. "Invalid smp cache topology. "
  349. "L2 cache topology level shouldn't be lower than L1 cache");
  350. return false;
  351. }
  352. if (smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L2,
  353. CACHE_LEVEL_AND_TYPE_L3)) {
  354. error_setg(errp,
  355. "Invalid smp cache topology. "
  356. "L3 cache topology level shouldn't be lower than L2 cache");
  357. return false;
  358. }
  359. return true;
  360. }