186ce2d28SYanan Wang /*
286ce2d28SYanan Wang * QEMU Machine core (related to -smp parsing)
386ce2d28SYanan Wang *
486ce2d28SYanan Wang * Copyright (c) 2021 Huawei Technologies Co., Ltd
586ce2d28SYanan Wang *
686ce2d28SYanan Wang * This program is free software; you can redistribute it and/or modify
786ce2d28SYanan Wang * it under the terms of the GNU General Public License as published by
886ce2d28SYanan Wang * the Free Software Foundation; either version 2 of the License,
986ce2d28SYanan Wang * or (at your option) any later version.
1086ce2d28SYanan Wang *
1186ce2d28SYanan Wang * This program is distributed in the hope that it will be useful,
1286ce2d28SYanan Wang * but WITHOUT ANY WARRANTY; without even the implied warranty of
1386ce2d28SYanan Wang * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1486ce2d28SYanan Wang * GNU General Public License for more details.
1586ce2d28SYanan Wang *
1686ce2d28SYanan Wang * You should have received a copy of the GNU General Public License
1786ce2d28SYanan Wang * along with this program; if not, see <http://www.gnu.org/licenses/>.
1886ce2d28SYanan Wang */
1986ce2d28SYanan Wang
2086ce2d28SYanan Wang #include "qemu/osdep.h"
2186ce2d28SYanan Wang #include "hw/boards.h"
2286ce2d28SYanan Wang #include "qapi/error.h"
23cc37d98bSRichard Henderson #include "qemu/error-report.h"
24cc37d98bSRichard Henderson
2586ce2d28SYanan Wang
2686ce2d28SYanan Wang /*
2786ce2d28SYanan Wang * Report information of a machine's supported CPU topology hierarchy.
2886ce2d28SYanan Wang * Topology members will be ordered from the largest to the smallest
2986ce2d28SYanan Wang * in the string.
3086ce2d28SYanan Wang */
cpu_hierarchy_to_string(MachineState * ms)3186ce2d28SYanan Wang static char *cpu_hierarchy_to_string(MachineState *ms)
3286ce2d28SYanan Wang {
3386ce2d28SYanan Wang MachineClass *mc = MACHINE_GET_CLASS(ms);
3486ce2d28SYanan Wang GString *s = g_string_new(NULL);
3586ce2d28SYanan Wang
365de1aff2SPierre Morel if (mc->smp_props.drawers_supported) {
375de1aff2SPierre Morel g_string_append_printf(s, "drawers (%u) * ", ms->smp.drawers);
385de1aff2SPierre Morel }
395de1aff2SPierre Morel
405de1aff2SPierre Morel if (mc->smp_props.books_supported) {
415de1aff2SPierre Morel g_string_append_printf(s, "books (%u) * ", ms->smp.books);
425de1aff2SPierre Morel }
435de1aff2SPierre Morel
4486ce2d28SYanan Wang g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);
4586ce2d28SYanan Wang
4686ce2d28SYanan Wang if (mc->smp_props.dies_supported) {
4786ce2d28SYanan Wang g_string_append_printf(s, " * dies (%u)", ms->smp.dies);
4886ce2d28SYanan Wang }
4986ce2d28SYanan Wang
50864c3b5cSYanan Wang if (mc->smp_props.clusters_supported) {
51864c3b5cSYanan Wang g_string_append_printf(s, " * clusters (%u)", ms->smp.clusters);
52864c3b5cSYanan Wang }
53864c3b5cSYanan Wang
548ec0a463SZhao Liu if (mc->smp_props.modules_supported) {
558ec0a463SZhao Liu g_string_append_printf(s, " * modules (%u)", ms->smp.modules);
568ec0a463SZhao Liu }
578ec0a463SZhao Liu
5886ce2d28SYanan Wang g_string_append_printf(s, " * cores (%u)", ms->smp.cores);
5986ce2d28SYanan Wang g_string_append_printf(s, " * threads (%u)", ms->smp.threads);
6086ce2d28SYanan Wang
6186ce2d28SYanan Wang return g_string_free(s, false);
6286ce2d28SYanan Wang }
6386ce2d28SYanan Wang
6486ce2d28SYanan Wang /*
653e2f1498SPhilippe Mathieu-Daudé * machine_parse_smp_config: Generic function used to parse the given
663e2f1498SPhilippe Mathieu-Daudé * SMP configuration
6786ce2d28SYanan Wang *
6886ce2d28SYanan Wang * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
6986ce2d28SYanan Wang * automatically computed based on the provided ones.
7086ce2d28SYanan Wang *
7186ce2d28SYanan Wang * In the calculation of omitted sockets/cores/threads: we prefer sockets
7286ce2d28SYanan Wang * over cores over threads before 6.2, while preferring cores over sockets
7386ce2d28SYanan Wang * over threads since 6.2.
7486ce2d28SYanan Wang *
7586ce2d28SYanan Wang * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted,
7686ce2d28SYanan Wang * maxcpus will be computed from the given parameters and cpus will be set
7786ce2d28SYanan Wang * equal to maxcpus. When only one of maxcpus and cpus is given then the
7886ce2d28SYanan Wang * omitted one will be set to its given counterpart's value. Both maxcpus and
7986ce2d28SYanan Wang * cpus may be specified, but maxcpus must be equal to or greater than cpus.
8086ce2d28SYanan Wang *
8186ce2d28SYanan Wang * For compatibility, apart from the parameters that will be computed, newly
8286ce2d28SYanan Wang * introduced topology members which are likely to be target specific should
8386ce2d28SYanan Wang * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1).
8486ce2d28SYanan Wang */
machine_parse_smp_config(MachineState * ms,const SMPConfiguration * config,Error ** errp)853e2f1498SPhilippe Mathieu-Daudé void machine_parse_smp_config(MachineState *ms,
863e2f1498SPhilippe Mathieu-Daudé const SMPConfiguration *config, Error **errp)
8786ce2d28SYanan Wang {
8886ce2d28SYanan Wang MachineClass *mc = MACHINE_GET_CLASS(ms);
8986ce2d28SYanan Wang unsigned cpus = config->has_cpus ? config->cpus : 0;
905de1aff2SPierre Morel unsigned drawers = config->has_drawers ? config->drawers : 0;
915de1aff2SPierre Morel unsigned books = config->has_books ? config->books : 0;
9286ce2d28SYanan Wang unsigned sockets = config->has_sockets ? config->sockets : 0;
9386ce2d28SYanan Wang unsigned dies = config->has_dies ? config->dies : 0;
94864c3b5cSYanan Wang unsigned clusters = config->has_clusters ? config->clusters : 0;
958ec0a463SZhao Liu unsigned modules = config->has_modules ? config->modules : 0;
9686ce2d28SYanan Wang unsigned cores = config->has_cores ? config->cores : 0;
9786ce2d28SYanan Wang unsigned threads = config->has_threads ? config->threads : 0;
9886ce2d28SYanan Wang unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
994503dcf7SZhao Liu unsigned total_cpus;
10086ce2d28SYanan Wang
10186ce2d28SYanan Wang /*
10286ce2d28SYanan Wang * Specified CPU topology parameters must be greater than zero,
10386ce2d28SYanan Wang * explicit configuration like "cpus=0" is not allowed.
10486ce2d28SYanan Wang */
10586ce2d28SYanan Wang if ((config->has_cpus && config->cpus == 0) ||
1065de1aff2SPierre Morel (config->has_drawers && config->drawers == 0) ||
1075de1aff2SPierre Morel (config->has_books && config->books == 0) ||
10886ce2d28SYanan Wang (config->has_sockets && config->sockets == 0) ||
10986ce2d28SYanan Wang (config->has_dies && config->dies == 0) ||
110864c3b5cSYanan Wang (config->has_clusters && config->clusters == 0) ||
1118ec0a463SZhao Liu (config->has_modules && config->modules == 0) ||
11286ce2d28SYanan Wang (config->has_cores && config->cores == 0) ||
11386ce2d28SYanan Wang (config->has_threads && config->threads == 0) ||
11486ce2d28SYanan Wang (config->has_maxcpus && config->maxcpus == 0)) {
11572d346f3SZhao Liu error_setg(errp, "Invalid CPU topology: "
11686ce2d28SYanan Wang "CPU topology parameters must be greater than zero");
11772d346f3SZhao Liu return;
11886ce2d28SYanan Wang }
11986ce2d28SYanan Wang
12086ce2d28SYanan Wang /*
1219d7950edSDaniel P. Berrangé * If not supported by the machine, a topology parameter must
1229d7950edSDaniel P. Berrangé * not be set to a value greater than 1.
12386ce2d28SYanan Wang */
1249d7950edSDaniel P. Berrangé if (!mc->smp_props.modules_supported &&
1259d7950edSDaniel P. Berrangé config->has_modules && config->modules > 1) {
1269d7950edSDaniel P. Berrangé error_setg(errp,
1279d7950edSDaniel P. Berrangé "modules > 1 not supported by this machine's CPU topology");
1288ec0a463SZhao Liu return;
1298ec0a463SZhao Liu }
1308ec0a463SZhao Liu modules = modules > 0 ? modules : 1;
1318ec0a463SZhao Liu
1329d7950edSDaniel P. Berrangé if (!mc->smp_props.clusters_supported &&
1339d7950edSDaniel P. Berrangé config->has_clusters && config->clusters > 1) {
1349d7950edSDaniel P. Berrangé error_setg(errp,
1359d7950edSDaniel P. Berrangé "clusters > 1 not supported by this machine's CPU topology");
13686ce2d28SYanan Wang return;
137864c3b5cSYanan Wang }
138864c3b5cSYanan Wang clusters = clusters > 0 ? clusters : 1;
13986ce2d28SYanan Wang
1409d7950edSDaniel P. Berrangé if (!mc->smp_props.dies_supported &&
1419d7950edSDaniel P. Berrangé config->has_dies && config->dies > 1) {
1429d7950edSDaniel P. Berrangé error_setg(errp,
1439d7950edSDaniel P. Berrangé "dies > 1 not supported by this machine's CPU topology");
1445de1aff2SPierre Morel return;
14554c4ea8fSZhao Liu }
14654c4ea8fSZhao Liu dies = dies > 0 ? dies : 1;
14754c4ea8fSZhao Liu
1489d7950edSDaniel P. Berrangé if (!mc->smp_props.books_supported &&
1499d7950edSDaniel P. Berrangé config->has_books && config->books > 1) {
1509d7950edSDaniel P. Berrangé error_setg(errp,
1519d7950edSDaniel P. Berrangé "books > 1 not supported by this machine's CPU topology");
15254c4ea8fSZhao Liu return;
1535de1aff2SPierre Morel }
1545de1aff2SPierre Morel books = books > 0 ? books : 1;
1555de1aff2SPierre Morel
1569d7950edSDaniel P. Berrangé if (!mc->smp_props.drawers_supported &&
1579d7950edSDaniel P. Berrangé config->has_drawers && config->drawers > 1) {
1589d7950edSDaniel P. Berrangé error_setg(errp,
1599d7950edSDaniel P. Berrangé "drawers > 1 not supported by this machine's CPU topology");
1605de1aff2SPierre Morel return;
1615de1aff2SPierre Morel }
1625de1aff2SPierre Morel drawers = drawers > 0 ? drawers : 1;
1635de1aff2SPierre Morel
16486ce2d28SYanan Wang /* compute missing values based on the provided ones */
16586ce2d28SYanan Wang if (cpus == 0 && maxcpus == 0) {
16686ce2d28SYanan Wang sockets = sockets > 0 ? sockets : 1;
16786ce2d28SYanan Wang cores = cores > 0 ? cores : 1;
16886ce2d28SYanan Wang threads = threads > 0 ? threads : 1;
16986ce2d28SYanan Wang } else {
17086ce2d28SYanan Wang maxcpus = maxcpus > 0 ? maxcpus : cpus;
17186ce2d28SYanan Wang
17286ce2d28SYanan Wang if (mc->smp_props.prefer_sockets) {
17386ce2d28SYanan Wang /* prefer sockets over cores before 6.2 */
17486ce2d28SYanan Wang if (sockets == 0) {
17586ce2d28SYanan Wang cores = cores > 0 ? cores : 1;
17686ce2d28SYanan Wang threads = threads > 0 ? threads : 1;
1775de1aff2SPierre Morel sockets = maxcpus /
1788ec0a463SZhao Liu (drawers * books * dies * clusters *
1798ec0a463SZhao Liu modules * cores * threads);
18086ce2d28SYanan Wang } else if (cores == 0) {
18186ce2d28SYanan Wang threads = threads > 0 ? threads : 1;
1825de1aff2SPierre Morel cores = maxcpus /
1838ec0a463SZhao Liu (drawers * books * sockets * dies *
1848ec0a463SZhao Liu clusters * modules * threads);
18586ce2d28SYanan Wang }
18686ce2d28SYanan Wang } else {
18786ce2d28SYanan Wang /* prefer cores over sockets since 6.2 */
18886ce2d28SYanan Wang if (cores == 0) {
18986ce2d28SYanan Wang sockets = sockets > 0 ? sockets : 1;
19086ce2d28SYanan Wang threads = threads > 0 ? threads : 1;
1915de1aff2SPierre Morel cores = maxcpus /
1928ec0a463SZhao Liu (drawers * books * sockets * dies *
1938ec0a463SZhao Liu clusters * modules * threads);
19486ce2d28SYanan Wang } else if (sockets == 0) {
19586ce2d28SYanan Wang threads = threads > 0 ? threads : 1;
1965de1aff2SPierre Morel sockets = maxcpus /
1978ec0a463SZhao Liu (drawers * books * dies * clusters *
1988ec0a463SZhao Liu modules * cores * threads);
19986ce2d28SYanan Wang }
20086ce2d28SYanan Wang }
20186ce2d28SYanan Wang
20286ce2d28SYanan Wang /* try to calculate omitted threads at last */
20386ce2d28SYanan Wang if (threads == 0) {
2045de1aff2SPierre Morel threads = maxcpus /
2058ec0a463SZhao Liu (drawers * books * sockets * dies *
2068ec0a463SZhao Liu clusters * modules * cores);
20786ce2d28SYanan Wang }
20886ce2d28SYanan Wang }
20986ce2d28SYanan Wang
2108ec0a463SZhao Liu total_cpus = drawers * books * sockets * dies *
2118ec0a463SZhao Liu clusters * modules * cores * threads;
2124503dcf7SZhao Liu maxcpus = maxcpus > 0 ? maxcpus : total_cpus;
21386ce2d28SYanan Wang cpus = cpus > 0 ? cpus : maxcpus;
21486ce2d28SYanan Wang
21586ce2d28SYanan Wang ms->smp.cpus = cpus;
2165de1aff2SPierre Morel ms->smp.drawers = drawers;
2175de1aff2SPierre Morel ms->smp.books = books;
21886ce2d28SYanan Wang ms->smp.sockets = sockets;
21986ce2d28SYanan Wang ms->smp.dies = dies;
220864c3b5cSYanan Wang ms->smp.clusters = clusters;
2218ec0a463SZhao Liu ms->smp.modules = modules;
22286ce2d28SYanan Wang ms->smp.cores = cores;
22386ce2d28SYanan Wang ms->smp.threads = threads;
22486ce2d28SYanan Wang ms->smp.max_cpus = maxcpus;
22586ce2d28SYanan Wang
22697f4effeSYicong Yang mc->smp_props.has_clusters = config->has_clusters;
22797f4effeSYicong Yang
22886ce2d28SYanan Wang /* sanity-check of the computed topology */
2294503dcf7SZhao Liu if (total_cpus != maxcpus) {
23086ce2d28SYanan Wang g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
23186ce2d28SYanan Wang error_setg(errp, "Invalid CPU topology: "
23286ce2d28SYanan Wang "product of the hierarchy must match maxcpus: "
23386ce2d28SYanan Wang "%s != maxcpus (%u)",
23486ce2d28SYanan Wang topo_msg, maxcpus);
23586ce2d28SYanan Wang return;
23686ce2d28SYanan Wang }
23786ce2d28SYanan Wang
23886ce2d28SYanan Wang if (maxcpus < cpus) {
23986ce2d28SYanan Wang g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
24086ce2d28SYanan Wang error_setg(errp, "Invalid CPU topology: "
24186ce2d28SYanan Wang "maxcpus must be equal to or greater than smp: "
24286ce2d28SYanan Wang "%s == maxcpus (%u) < smp_cpus (%u)",
24386ce2d28SYanan Wang topo_msg, maxcpus, cpus);
24486ce2d28SYanan Wang return;
24586ce2d28SYanan Wang }
24686ce2d28SYanan Wang
24786ce2d28SYanan Wang if (ms->smp.cpus < mc->min_cpus) {
24886ce2d28SYanan Wang error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
24986ce2d28SYanan Wang "supported by machine '%s' is %d",
25086ce2d28SYanan Wang ms->smp.cpus,
25186ce2d28SYanan Wang mc->name, mc->min_cpus);
25286ce2d28SYanan Wang return;
25386ce2d28SYanan Wang }
25486ce2d28SYanan Wang
25586ce2d28SYanan Wang if (ms->smp.max_cpus > mc->max_cpus) {
25686ce2d28SYanan Wang error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
25786ce2d28SYanan Wang "supported by machine '%s' is %d",
25886ce2d28SYanan Wang ms->smp.max_cpus,
25986ce2d28SYanan Wang mc->name, mc->max_cpus);
26086ce2d28SYanan Wang return;
26186ce2d28SYanan Wang }
26286ce2d28SYanan Wang }
263a1d027beSZhao Liu
machine_check_topo_support(MachineState * ms,CpuTopologyLevel topo,Error ** errp)264f35c0221SZhao Liu static bool machine_check_topo_support(MachineState *ms,
265f35c0221SZhao Liu CpuTopologyLevel topo,
266f35c0221SZhao Liu Error **errp)
267f35c0221SZhao Liu {
268f35c0221SZhao Liu MachineClass *mc = MACHINE_GET_CLASS(ms);
269f35c0221SZhao Liu
270f35c0221SZhao Liu if ((topo == CPU_TOPOLOGY_LEVEL_MODULE && !mc->smp_props.modules_supported) ||
271f35c0221SZhao Liu (topo == CPU_TOPOLOGY_LEVEL_CLUSTER && !mc->smp_props.clusters_supported) ||
272f35c0221SZhao Liu (topo == CPU_TOPOLOGY_LEVEL_DIE && !mc->smp_props.dies_supported) ||
273f35c0221SZhao Liu (topo == CPU_TOPOLOGY_LEVEL_BOOK && !mc->smp_props.books_supported) ||
274f35c0221SZhao Liu (topo == CPU_TOPOLOGY_LEVEL_DRAWER && !mc->smp_props.drawers_supported)) {
275f35c0221SZhao Liu error_setg(errp,
276f35c0221SZhao Liu "Invalid topology level: %s. "
277f35c0221SZhao Liu "The topology level is not supported by this machine",
278f35c0221SZhao Liu CpuTopologyLevel_str(topo));
279f35c0221SZhao Liu return false;
280f35c0221SZhao Liu }
281f35c0221SZhao Liu
282f35c0221SZhao Liu return true;
283f35c0221SZhao Liu }
284f35c0221SZhao Liu
machine_parse_smp_cache(MachineState * ms,const SmpCachePropertiesList * caches,Error ** errp)2854e88e7e3SZhao Liu bool machine_parse_smp_cache(MachineState *ms,
2864e88e7e3SZhao Liu const SmpCachePropertiesList *caches,
2874e88e7e3SZhao Liu Error **errp)
2884e88e7e3SZhao Liu {
289f35c0221SZhao Liu MachineClass *mc = MACHINE_GET_CLASS(ms);
2904e88e7e3SZhao Liu const SmpCachePropertiesList *node;
2914e88e7e3SZhao Liu DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
2924e88e7e3SZhao Liu
2939c264494SZhao Liu bitmap_zero(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
2944e88e7e3SZhao Liu for (node = caches; node; node = node->next) {
2954e88e7e3SZhao Liu /* Prohibit users from repeating settings. */
2964e88e7e3SZhao Liu if (test_bit(node->value->cache, caches_bitmap)) {
2974e88e7e3SZhao Liu error_setg(errp,
2984e88e7e3SZhao Liu "Invalid cache properties: %s. "
2994e88e7e3SZhao Liu "The cache properties are duplicated",
3004e88e7e3SZhao Liu CacheLevelAndType_str(node->value->cache));
3014e88e7e3SZhao Liu return false;
3024e88e7e3SZhao Liu }
3034e88e7e3SZhao Liu
3044e88e7e3SZhao Liu machine_set_cache_topo_level(ms, node->value->cache,
3054e88e7e3SZhao Liu node->value->topology);
3064e88e7e3SZhao Liu set_bit(node->value->cache, caches_bitmap);
3074e88e7e3SZhao Liu }
3084e88e7e3SZhao Liu
309f35c0221SZhao Liu for (int i = 0; i < CACHE_LEVEL_AND_TYPE__MAX; i++) {
310f35c0221SZhao Liu const SmpCacheProperties *props = &ms->smp_cache.props[i];
311f35c0221SZhao Liu
312f35c0221SZhao Liu /*
313f35c0221SZhao Liu * Reject non "default" topology level if the cache isn't
314f35c0221SZhao Liu * supported by the machine.
315f35c0221SZhao Liu */
316f35c0221SZhao Liu if (props->topology != CPU_TOPOLOGY_LEVEL_DEFAULT &&
317f35c0221SZhao Liu !mc->smp_props.cache_supported[props->cache]) {
318f35c0221SZhao Liu error_setg(errp,
319f35c0221SZhao Liu "%s cache topology not supported by this machine",
320*37ee17eeSZhao Liu CacheLevelAndType_str(props->cache));
321f35c0221SZhao Liu return false;
322f35c0221SZhao Liu }
323f35c0221SZhao Liu
324f35c0221SZhao Liu if (!machine_check_topo_support(ms, props->topology, errp)) {
325f35c0221SZhao Liu return false;
326f35c0221SZhao Liu }
327f35c0221SZhao Liu }
3284e88e7e3SZhao Liu return true;
3294e88e7e3SZhao Liu }
3304e88e7e3SZhao Liu
machine_topo_get_cores_per_socket(const MachineState * ms)331a1d027beSZhao Liu unsigned int machine_topo_get_cores_per_socket(const MachineState *ms)
332a1d027beSZhao Liu {
333dcba73b4SZhao Liu return ms->smp.cores * ms->smp.modules * ms->smp.clusters * ms->smp.dies;
334a1d027beSZhao Liu }
335a1d027beSZhao Liu
machine_topo_get_threads_per_socket(const MachineState * ms)336a1d027beSZhao Liu unsigned int machine_topo_get_threads_per_socket(const MachineState *ms)
337a1d027beSZhao Liu {
338a1d027beSZhao Liu return ms->smp.threads * machine_topo_get_cores_per_socket(ms);
339a1d027beSZhao Liu }
3404e88e7e3SZhao Liu
machine_get_cache_topo_level(const MachineState * ms,CacheLevelAndType cache)3414e88e7e3SZhao Liu CpuTopologyLevel machine_get_cache_topo_level(const MachineState *ms,
3424e88e7e3SZhao Liu CacheLevelAndType cache)
3434e88e7e3SZhao Liu {
3444e88e7e3SZhao Liu return ms->smp_cache.props[cache].topology;
3454e88e7e3SZhao Liu }
3464e88e7e3SZhao Liu
machine_set_cache_topo_level(MachineState * ms,CacheLevelAndType cache,CpuTopologyLevel level)3474e88e7e3SZhao Liu void machine_set_cache_topo_level(MachineState *ms, CacheLevelAndType cache,
3484e88e7e3SZhao Liu CpuTopologyLevel level)
3494e88e7e3SZhao Liu {
3504e88e7e3SZhao Liu ms->smp_cache.props[cache].topology = level;
3514e88e7e3SZhao Liu }
35207995a46SZhao Liu
35307995a46SZhao Liu /*
35407995a46SZhao Liu * When both cache1 and cache2 are configured with specific topology levels
35507995a46SZhao Liu * (not default level), is cache1's topology level higher than cache2?
35607995a46SZhao Liu */
smp_cache_topo_cmp(const SmpCache * smp_cache,CacheLevelAndType cache1,CacheLevelAndType cache2)35707995a46SZhao Liu static bool smp_cache_topo_cmp(const SmpCache *smp_cache,
35807995a46SZhao Liu CacheLevelAndType cache1,
35907995a46SZhao Liu CacheLevelAndType cache2)
36007995a46SZhao Liu {
36107995a46SZhao Liu /*
36207995a46SZhao Liu * Before comparing, the "default" topology level should be replaced
36307995a46SZhao Liu * with the specific level.
36407995a46SZhao Liu */
36507995a46SZhao Liu assert(smp_cache->props[cache1].topology != CPU_TOPOLOGY_LEVEL_DEFAULT);
36607995a46SZhao Liu
36707995a46SZhao Liu return smp_cache->props[cache1].topology > smp_cache->props[cache2].topology;
36807995a46SZhao Liu }
36907995a46SZhao Liu
37007995a46SZhao Liu /*
37107995a46SZhao Liu * Currently, we have no way to expose the arch-specific default cache model
37207995a46SZhao Liu * because the cache model is sometimes related to the CPU model (e.g., i386).
37307995a46SZhao Liu *
37407995a46SZhao Liu * We can only check the correctness of the cache topology after the arch loads
37507995a46SZhao Liu * the user-configured cache model from MachineState and consumes the special
37607995a46SZhao Liu * "default" level by replacing it with the specific level.
37707995a46SZhao Liu */
machine_check_smp_cache(const MachineState * ms,Error ** errp)37807995a46SZhao Liu bool machine_check_smp_cache(const MachineState *ms, Error **errp)
37907995a46SZhao Liu {
38007995a46SZhao Liu if (smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L1D,
38107995a46SZhao Liu CACHE_LEVEL_AND_TYPE_L2) ||
38207995a46SZhao Liu smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L1I,
38307995a46SZhao Liu CACHE_LEVEL_AND_TYPE_L2)) {
38407995a46SZhao Liu error_setg(errp,
38507995a46SZhao Liu "Invalid smp cache topology. "
38607995a46SZhao Liu "L2 cache topology level shouldn't be lower than L1 cache");
38707995a46SZhao Liu return false;
38807995a46SZhao Liu }
38907995a46SZhao Liu
39007995a46SZhao Liu if (smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L2,
39107995a46SZhao Liu CACHE_LEVEL_AND_TYPE_L3)) {
39207995a46SZhao Liu error_setg(errp,
39307995a46SZhao Liu "Invalid smp cache topology. "
39407995a46SZhao Liu "L3 cache topology level shouldn't be lower than L2 cache");
39507995a46SZhao Liu return false;
39607995a46SZhao Liu }
39707995a46SZhao Liu
39807995a46SZhao Liu return true;
39907995a46SZhao Liu }
400