1ac057879SMarkus Armbruster /*
2ac057879SMarkus Armbruster * NUMA parameter parsing routines
3ac057879SMarkus Armbruster *
4ac057879SMarkus Armbruster * Copyright (c) 2014 Fujitsu Ltd.
5ac057879SMarkus Armbruster *
6ac057879SMarkus Armbruster * Permission is hereby granted, free of charge, to any person obtaining a copy
7ac057879SMarkus Armbruster * of this software and associated documentation files (the "Software"), to deal
8ac057879SMarkus Armbruster * in the Software without restriction, including without limitation the rights
9ac057879SMarkus Armbruster * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10ac057879SMarkus Armbruster * copies of the Software, and to permit persons to whom the Software is
11ac057879SMarkus Armbruster * furnished to do so, subject to the following conditions:
12ac057879SMarkus Armbruster *
13ac057879SMarkus Armbruster * The above copyright notice and this permission notice shall be included in
14ac057879SMarkus Armbruster * all copies or substantial portions of the Software.
15ac057879SMarkus Armbruster *
16ac057879SMarkus Armbruster * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17ac057879SMarkus Armbruster * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18ac057879SMarkus Armbruster * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19ac057879SMarkus Armbruster * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20ac057879SMarkus Armbruster * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21ac057879SMarkus Armbruster * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22ac057879SMarkus Armbruster * THE SOFTWARE.
23ac057879SMarkus Armbruster */
24ac057879SMarkus Armbruster
25ac057879SMarkus Armbruster #include "qemu/osdep.h"
269b12dfa0SLiu Jingqi #include "qemu/units.h"
27b58c5c2dSMarkus Armbruster #include "sysemu/hostmem.h"
28ac057879SMarkus Armbruster #include "sysemu/numa.h"
29ac057879SMarkus Armbruster #include "exec/cpu-common.h"
30ac057879SMarkus Armbruster #include "exec/ramlist.h"
31ac057879SMarkus Armbruster #include "qemu/error-report.h"
32ac057879SMarkus Armbruster #include "qapi/error.h"
33ac057879SMarkus Armbruster #include "qapi/opts-visitor.h"
34ac057879SMarkus Armbruster #include "qapi/qapi-visit-machine.h"
35f8123f22SEduardo Habkost #include "sysemu/qtest.h"
362e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
37ac057879SMarkus Armbruster #include "hw/mem/pc-dimm.h"
3812e9493dSMarkus Armbruster #include "hw/boards.h"
39ac057879SMarkus Armbruster #include "hw/mem/memory-device.h"
40ac057879SMarkus Armbruster #include "qemu/option.h"
41ac057879SMarkus Armbruster #include "qemu/config-file.h"
42ac057879SMarkus Armbruster #include "qemu/cutils.h"
43ac057879SMarkus Armbruster
44ac057879SMarkus Armbruster QemuOptsList qemu_numa_opts = {
45ac057879SMarkus Armbruster .name = "numa",
46ac057879SMarkus Armbruster .implied_opt_name = "type",
47ac057879SMarkus Armbruster .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
48ac057879SMarkus Armbruster .desc = { { 0 } } /* validated with OptsVisitor */
49ac057879SMarkus Armbruster };
50ac057879SMarkus Armbruster
51b69239e0SIgor Mammedov static int have_memdevs;
numa_uses_legacy_mem(void)526b61c2c5SIgor Mammedov bool numa_uses_legacy_mem(void)
536b61c2c5SIgor Mammedov {
546b61c2c5SIgor Mammedov return !have_memdevs;
556b61c2c5SIgor Mammedov }
566b61c2c5SIgor Mammedov
57b69239e0SIgor Mammedov static int have_mem;
58ac057879SMarkus Armbruster static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
59ac057879SMarkus Armbruster * For all nodes, nodeid < max_numa_nodeid
60ac057879SMarkus Armbruster */
61ac057879SMarkus Armbruster
parse_numa_node(MachineState * ms,NumaNodeOptions * node,Error ** errp)62ac057879SMarkus Armbruster static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
63ac057879SMarkus Armbruster Error **errp)
64ac057879SMarkus Armbruster {
65ac057879SMarkus Armbruster Error *err = NULL;
66ac057879SMarkus Armbruster uint16_t nodenr;
67ac057879SMarkus Armbruster uint16List *cpus = NULL;
68ac057879SMarkus Armbruster MachineClass *mc = MACHINE_GET_CLASS(ms);
695cc8767dSLike Xu unsigned int max_cpus = ms->smp.max_cpus;
707e721e7bSTao Xu NodeInfo *numa_info = ms->numa_state->nodes;
71ac057879SMarkus Armbruster
72ac057879SMarkus Armbruster if (node->has_nodeid) {
73ac057879SMarkus Armbruster nodenr = node->nodeid;
74ac057879SMarkus Armbruster } else {
75aa570207STao Xu nodenr = ms->numa_state->num_nodes;
76ac057879SMarkus Armbruster }
77ac057879SMarkus Armbruster
78ac057879SMarkus Armbruster if (nodenr >= MAX_NODES) {
79ac057879SMarkus Armbruster error_setg(errp, "Max number of NUMA nodes reached: %"
80ac057879SMarkus Armbruster PRIu16 "", nodenr);
81ac057879SMarkus Armbruster return;
82ac057879SMarkus Armbruster }
83ac057879SMarkus Armbruster
84ac057879SMarkus Armbruster if (numa_info[nodenr].present) {
85ac057879SMarkus Armbruster error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
86ac057879SMarkus Armbruster return;
87ac057879SMarkus Armbruster }
88ac057879SMarkus Armbruster
89294aa043SMichal Privoznik /*
90294aa043SMichal Privoznik * If not set the initiator, set it to MAX_NODES. And if
91294aa043SMichal Privoznik * HMAT is enabled and this node has no cpus, QEMU will raise error.
92294aa043SMichal Privoznik */
93294aa043SMichal Privoznik numa_info[nodenr].initiator = MAX_NODES;
94294aa043SMichal Privoznik if (node->has_initiator) {
95294aa043SMichal Privoznik if (!ms->numa_state->hmat_enabled) {
96294aa043SMichal Privoznik error_setg(errp, "ACPI Heterogeneous Memory Attribute Table "
97294aa043SMichal Privoznik "(HMAT) is disabled, enable it with -machine hmat=on "
98294aa043SMichal Privoznik "before using any of hmat specific options");
99294aa043SMichal Privoznik return;
100294aa043SMichal Privoznik }
101294aa043SMichal Privoznik
102294aa043SMichal Privoznik if (node->initiator >= MAX_NODES) {
103294aa043SMichal Privoznik error_report("The initiator id %" PRIu16 " expects an integer "
104294aa043SMichal Privoznik "between 0 and %d", node->initiator,
105294aa043SMichal Privoznik MAX_NODES - 1);
106294aa043SMichal Privoznik return;
107294aa043SMichal Privoznik }
108294aa043SMichal Privoznik
109294aa043SMichal Privoznik numa_info[nodenr].initiator = node->initiator;
110294aa043SMichal Privoznik }
111294aa043SMichal Privoznik
112ac057879SMarkus Armbruster for (cpus = node->cpus; cpus; cpus = cpus->next) {
113ac057879SMarkus Armbruster CpuInstanceProperties props;
114ac057879SMarkus Armbruster if (cpus->value >= max_cpus) {
115ac057879SMarkus Armbruster error_setg(errp,
116ac057879SMarkus Armbruster "CPU index (%" PRIu16 ")"
117ac057879SMarkus Armbruster " should be smaller than maxcpus (%d)",
118ac057879SMarkus Armbruster cpus->value, max_cpus);
119ac057879SMarkus Armbruster return;
120ac057879SMarkus Armbruster }
121ac057879SMarkus Armbruster props = mc->cpu_index_to_instance_props(ms, cpus->value);
122ac057879SMarkus Armbruster props.node_id = nodenr;
123ac057879SMarkus Armbruster props.has_node_id = true;
124ac057879SMarkus Armbruster machine_set_cpu_numa_node(ms, &props, &err);
125ac057879SMarkus Armbruster if (err) {
126ac057879SMarkus Armbruster error_propagate(errp, err);
127ac057879SMarkus Armbruster return;
128ac057879SMarkus Armbruster }
129ac057879SMarkus Armbruster }
130ac057879SMarkus Armbruster
131fe8ac1faSMarkus Armbruster have_memdevs = have_memdevs || node->memdev;
132fe8ac1faSMarkus Armbruster have_mem = have_mem || node->has_mem;
133fe8ac1faSMarkus Armbruster if ((node->has_mem && have_memdevs) || (node->memdev && have_mem)) {
134b69239e0SIgor Mammedov error_setg(errp, "numa configuration should use either mem= or memdev=,"
135b69239e0SIgor Mammedov "mixing both is not allowed");
136ac057879SMarkus Armbruster return;
137ac057879SMarkus Armbruster }
138ac057879SMarkus Armbruster
139ac057879SMarkus Armbruster if (node->has_mem) {
14032a354dcSIgor Mammedov if (!mc->numa_mem_supported) {
14132a354dcSIgor Mammedov error_setg(errp, "Parameter -numa node,mem is not supported by this"
14232a354dcSIgor Mammedov " machine type");
14332a354dcSIgor Mammedov error_append_hint(errp, "Use -numa node,memdev instead\n");
14432a354dcSIgor Mammedov return;
14532a354dcSIgor Mammedov }
14632a354dcSIgor Mammedov
147ac057879SMarkus Armbruster numa_info[nodenr].node_mem = node->mem;
148f8123f22SEduardo Habkost if (!qtest_enabled()) {
149cdf80365SIgor Mammedov warn_report("Parameter -numa node,mem is deprecated,"
150cdf80365SIgor Mammedov " use -numa node,memdev instead");
151ac057879SMarkus Armbruster }
152f8123f22SEduardo Habkost }
153fe8ac1faSMarkus Armbruster if (node->memdev) {
154ac057879SMarkus Armbruster Object *o;
155ac057879SMarkus Armbruster o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
156ac057879SMarkus Armbruster if (!o) {
157ac057879SMarkus Armbruster error_setg(errp, "memdev=%s is ambiguous", node->memdev);
158ac057879SMarkus Armbruster return;
159ac057879SMarkus Armbruster }
160ac057879SMarkus Armbruster
161ac057879SMarkus Armbruster object_ref(o);
162ac057879SMarkus Armbruster numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
163ac057879SMarkus Armbruster numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
164ac057879SMarkus Armbruster }
165244b3f44STao Xu
166ac057879SMarkus Armbruster numa_info[nodenr].present = true;
167ac057879SMarkus Armbruster max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
168aa570207STao Xu ms->numa_state->num_nodes++;
169ac057879SMarkus Armbruster }
170ac057879SMarkus Armbruster
171aa570207STao Xu static
parse_numa_distance(MachineState * ms,NumaDistOptions * dist,Error ** errp)172aa570207STao Xu void parse_numa_distance(MachineState *ms, NumaDistOptions *dist, Error **errp)
173ac057879SMarkus Armbruster {
174ac057879SMarkus Armbruster uint16_t src = dist->src;
175ac057879SMarkus Armbruster uint16_t dst = dist->dst;
176ac057879SMarkus Armbruster uint8_t val = dist->val;
1777e721e7bSTao Xu NodeInfo *numa_info = ms->numa_state->nodes;
178ac057879SMarkus Armbruster
179ac057879SMarkus Armbruster if (src >= MAX_NODES || dst >= MAX_NODES) {
180ac057879SMarkus Armbruster error_setg(errp, "Parameter '%s' expects an integer between 0 and %d",
181ac057879SMarkus Armbruster src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1);
182ac057879SMarkus Armbruster return;
183ac057879SMarkus Armbruster }
184ac057879SMarkus Armbruster
185ac057879SMarkus Armbruster if (!numa_info[src].present || !numa_info[dst].present) {
186ac057879SMarkus Armbruster error_setg(errp, "Source/Destination NUMA node is missing. "
187ac057879SMarkus Armbruster "Please use '-numa node' option to declare it first.");
188ac057879SMarkus Armbruster return;
189ac057879SMarkus Armbruster }
190ac057879SMarkus Armbruster
191ac057879SMarkus Armbruster if (val < NUMA_DISTANCE_MIN) {
192ac057879SMarkus Armbruster error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
193ac057879SMarkus Armbruster "it shouldn't be less than %d.",
194ac057879SMarkus Armbruster val, NUMA_DISTANCE_MIN);
195ac057879SMarkus Armbruster return;
196ac057879SMarkus Armbruster }
197ac057879SMarkus Armbruster
198ac057879SMarkus Armbruster if (src == dst && val != NUMA_DISTANCE_MIN) {
199ac057879SMarkus Armbruster error_setg(errp, "Local distance of node %d should be %d.",
200ac057879SMarkus Armbruster src, NUMA_DISTANCE_MIN);
201ac057879SMarkus Armbruster return;
202ac057879SMarkus Armbruster }
203ac057879SMarkus Armbruster
204ac057879SMarkus Armbruster numa_info[src].distance[dst] = val;
205118154b7STao Xu ms->numa_state->have_numa_distance = true;
206ac057879SMarkus Armbruster }
207ac057879SMarkus Armbruster
parse_numa_hmat_lb(NumaState * numa_state,NumaHmatLBOptions * node,Error ** errp)2089b12dfa0SLiu Jingqi void parse_numa_hmat_lb(NumaState *numa_state, NumaHmatLBOptions *node,
2099b12dfa0SLiu Jingqi Error **errp)
2109b12dfa0SLiu Jingqi {
2119b12dfa0SLiu Jingqi int i, first_bit, last_bit;
2129b12dfa0SLiu Jingqi uint64_t max_entry, temp_base, bitmap_copy;
2139b12dfa0SLiu Jingqi NodeInfo *numa_info = numa_state->nodes;
2149b12dfa0SLiu Jingqi HMAT_LB_Info *hmat_lb =
2159b12dfa0SLiu Jingqi numa_state->hmat_lb[node->hierarchy][node->data_type];
2169b12dfa0SLiu Jingqi HMAT_LB_Data lb_data = {};
2179b12dfa0SLiu Jingqi HMAT_LB_Data *lb_temp;
2189b12dfa0SLiu Jingqi
2199b12dfa0SLiu Jingqi /* Error checking */
2209b12dfa0SLiu Jingqi if (node->initiator > numa_state->num_nodes) {
2219b12dfa0SLiu Jingqi error_setg(errp, "Invalid initiator=%d, it should be less than %d",
2229b12dfa0SLiu Jingqi node->initiator, numa_state->num_nodes);
2239b12dfa0SLiu Jingqi return;
2249b12dfa0SLiu Jingqi }
2259b12dfa0SLiu Jingqi if (node->target > numa_state->num_nodes) {
2269b12dfa0SLiu Jingqi error_setg(errp, "Invalid target=%d, it should be less than %d",
2279b12dfa0SLiu Jingqi node->target, numa_state->num_nodes);
2289b12dfa0SLiu Jingqi return;
2299b12dfa0SLiu Jingqi }
2300a5b5acdSAnkit Agrawal if (!numa_info[node->initiator].has_cpu &&
2310a5b5acdSAnkit Agrawal !numa_info[node->initiator].has_gi) {
2329b12dfa0SLiu Jingqi error_setg(errp, "Invalid initiator=%d, it isn't an "
2339b12dfa0SLiu Jingqi "initiator proximity domain", node->initiator);
2349b12dfa0SLiu Jingqi return;
2359b12dfa0SLiu Jingqi }
2369b12dfa0SLiu Jingqi if (!numa_info[node->target].present) {
2379b12dfa0SLiu Jingqi error_setg(errp, "The target=%d should point to an existing node",
2389b12dfa0SLiu Jingqi node->target);
2399b12dfa0SLiu Jingqi return;
2409b12dfa0SLiu Jingqi }
2419b12dfa0SLiu Jingqi
2429b12dfa0SLiu Jingqi if (!hmat_lb) {
2439b12dfa0SLiu Jingqi hmat_lb = g_malloc0(sizeof(*hmat_lb));
2449b12dfa0SLiu Jingqi numa_state->hmat_lb[node->hierarchy][node->data_type] = hmat_lb;
2459b12dfa0SLiu Jingqi hmat_lb->list = g_array_new(false, true, sizeof(HMAT_LB_Data));
2469b12dfa0SLiu Jingqi }
2479b12dfa0SLiu Jingqi hmat_lb->hierarchy = node->hierarchy;
2489b12dfa0SLiu Jingqi hmat_lb->data_type = node->data_type;
2499b12dfa0SLiu Jingqi lb_data.initiator = node->initiator;
2509b12dfa0SLiu Jingqi lb_data.target = node->target;
2519b12dfa0SLiu Jingqi
2520aa4bd6aSMarkus Armbruster if (node->data_type <= HMAT_LB_DATA_TYPE_WRITE_LATENCY) {
2539b12dfa0SLiu Jingqi /* Input latency data */
2549b12dfa0SLiu Jingqi
2559b12dfa0SLiu Jingqi if (!node->has_latency) {
2569b12dfa0SLiu Jingqi error_setg(errp, "Missing 'latency' option");
2579b12dfa0SLiu Jingqi return;
2589b12dfa0SLiu Jingqi }
2599b12dfa0SLiu Jingqi if (node->has_bandwidth) {
2609b12dfa0SLiu Jingqi error_setg(errp, "Invalid option 'bandwidth' since "
2619b12dfa0SLiu Jingqi "the data type is latency");
2629b12dfa0SLiu Jingqi return;
2639b12dfa0SLiu Jingqi }
2649b12dfa0SLiu Jingqi
2659b12dfa0SLiu Jingqi /* Detect duplicate configuration */
2669b12dfa0SLiu Jingqi for (i = 0; i < hmat_lb->list->len; i++) {
2679b12dfa0SLiu Jingqi lb_temp = &g_array_index(hmat_lb->list, HMAT_LB_Data, i);
2689b12dfa0SLiu Jingqi
2699b12dfa0SLiu Jingqi if (node->initiator == lb_temp->initiator &&
2709b12dfa0SLiu Jingqi node->target == lb_temp->target) {
2719b12dfa0SLiu Jingqi error_setg(errp, "Duplicate configuration of the latency for "
2729b12dfa0SLiu Jingqi "initiator=%d and target=%d", node->initiator,
2739b12dfa0SLiu Jingqi node->target);
2749b12dfa0SLiu Jingqi return;
2759b12dfa0SLiu Jingqi }
2769b12dfa0SLiu Jingqi }
2779b12dfa0SLiu Jingqi
2789b12dfa0SLiu Jingqi hmat_lb->base = hmat_lb->base ? hmat_lb->base : UINT64_MAX;
2799b12dfa0SLiu Jingqi
2809b12dfa0SLiu Jingqi if (node->latency) {
2819b12dfa0SLiu Jingqi /* Calculate the temporary base and compressed latency */
2829b12dfa0SLiu Jingqi max_entry = node->latency;
2839b12dfa0SLiu Jingqi temp_base = 1;
2849b12dfa0SLiu Jingqi while (QEMU_IS_ALIGNED(max_entry, 10)) {
2859b12dfa0SLiu Jingqi max_entry /= 10;
2869b12dfa0SLiu Jingqi temp_base *= 10;
2879b12dfa0SLiu Jingqi }
2889b12dfa0SLiu Jingqi
2899b12dfa0SLiu Jingqi /* Calculate the max compressed latency */
2909b12dfa0SLiu Jingqi temp_base = MIN(hmat_lb->base, temp_base);
2919b12dfa0SLiu Jingqi max_entry = node->latency / hmat_lb->base;
2929b12dfa0SLiu Jingqi max_entry = MAX(hmat_lb->range_bitmap, max_entry);
2939b12dfa0SLiu Jingqi
2949b12dfa0SLiu Jingqi /*
2959b12dfa0SLiu Jingqi * For latency hmat_lb->range_bitmap record the max compressed
2969b12dfa0SLiu Jingqi * latency which should be less than 0xFFFF (UINT16_MAX)
2979b12dfa0SLiu Jingqi */
2989b12dfa0SLiu Jingqi if (max_entry >= UINT16_MAX) {
2999b12dfa0SLiu Jingqi error_setg(errp, "Latency %" PRIu64 " between initiator=%d and "
3009b12dfa0SLiu Jingqi "target=%d should not differ from previously entered "
3019b12dfa0SLiu Jingqi "min or max values on more than %d", node->latency,
3029b12dfa0SLiu Jingqi node->initiator, node->target, UINT16_MAX - 1);
3039b12dfa0SLiu Jingqi return;
3049b12dfa0SLiu Jingqi } else {
3059b12dfa0SLiu Jingqi hmat_lb->base = temp_base;
3069b12dfa0SLiu Jingqi hmat_lb->range_bitmap = max_entry;
3079b12dfa0SLiu Jingqi }
3089b12dfa0SLiu Jingqi
3099b12dfa0SLiu Jingqi /*
3109b12dfa0SLiu Jingqi * Set lb_info_provided bit 0 as 1,
3119b12dfa0SLiu Jingqi * latency information is provided
3129b12dfa0SLiu Jingqi */
3139b12dfa0SLiu Jingqi numa_info[node->target].lb_info_provided |= BIT(0);
3149b12dfa0SLiu Jingqi }
3159b12dfa0SLiu Jingqi lb_data.data = node->latency;
3160aa4bd6aSMarkus Armbruster } else if (node->data_type >= HMAT_LB_DATA_TYPE_ACCESS_BANDWIDTH) {
3179b12dfa0SLiu Jingqi /* Input bandwidth data */
3189b12dfa0SLiu Jingqi if (!node->has_bandwidth) {
3199b12dfa0SLiu Jingqi error_setg(errp, "Missing 'bandwidth' option");
3209b12dfa0SLiu Jingqi return;
3219b12dfa0SLiu Jingqi }
3229b12dfa0SLiu Jingqi if (node->has_latency) {
3239b12dfa0SLiu Jingqi error_setg(errp, "Invalid option 'latency' since "
3249b12dfa0SLiu Jingqi "the data type is bandwidth");
3259b12dfa0SLiu Jingqi return;
3269b12dfa0SLiu Jingqi }
3279b12dfa0SLiu Jingqi if (!QEMU_IS_ALIGNED(node->bandwidth, MiB)) {
3289b12dfa0SLiu Jingqi error_setg(errp, "Bandwidth %" PRIu64 " between initiator=%d and "
3299b12dfa0SLiu Jingqi "target=%d should be 1MB aligned", node->bandwidth,
3309b12dfa0SLiu Jingqi node->initiator, node->target);
3319b12dfa0SLiu Jingqi return;
3329b12dfa0SLiu Jingqi }
3339b12dfa0SLiu Jingqi
3349b12dfa0SLiu Jingqi /* Detect duplicate configuration */
3359b12dfa0SLiu Jingqi for (i = 0; i < hmat_lb->list->len; i++) {
3369b12dfa0SLiu Jingqi lb_temp = &g_array_index(hmat_lb->list, HMAT_LB_Data, i);
3379b12dfa0SLiu Jingqi
3389b12dfa0SLiu Jingqi if (node->initiator == lb_temp->initiator &&
3399b12dfa0SLiu Jingqi node->target == lb_temp->target) {
3409b12dfa0SLiu Jingqi error_setg(errp, "Duplicate configuration of the bandwidth for "
3419b12dfa0SLiu Jingqi "initiator=%d and target=%d", node->initiator,
3429b12dfa0SLiu Jingqi node->target);
3439b12dfa0SLiu Jingqi return;
3449b12dfa0SLiu Jingqi }
3459b12dfa0SLiu Jingqi }
3469b12dfa0SLiu Jingqi
3479b12dfa0SLiu Jingqi hmat_lb->base = hmat_lb->base ? hmat_lb->base : 1;
3489b12dfa0SLiu Jingqi
3499b12dfa0SLiu Jingqi if (node->bandwidth) {
3509b12dfa0SLiu Jingqi /* Keep bitmap unchanged when bandwidth out of range */
3519b12dfa0SLiu Jingqi bitmap_copy = hmat_lb->range_bitmap;
3529b12dfa0SLiu Jingqi bitmap_copy |= node->bandwidth;
3539b12dfa0SLiu Jingqi first_bit = ctz64(bitmap_copy);
3549b12dfa0SLiu Jingqi temp_base = UINT64_C(1) << first_bit;
3559b12dfa0SLiu Jingqi max_entry = node->bandwidth / temp_base;
3569b12dfa0SLiu Jingqi last_bit = 64 - clz64(bitmap_copy);
3579b12dfa0SLiu Jingqi
3589b12dfa0SLiu Jingqi /*
3599b12dfa0SLiu Jingqi * For bandwidth, first_bit record the base unit of bandwidth bits,
3609b12dfa0SLiu Jingqi * last_bit record the last bit of the max bandwidth. The max
3619b12dfa0SLiu Jingqi * compressed bandwidth should be less than 0xFFFF (UINT16_MAX)
3629b12dfa0SLiu Jingqi */
3639b12dfa0SLiu Jingqi if ((last_bit - first_bit) > UINT16_BITS ||
3649b12dfa0SLiu Jingqi max_entry >= UINT16_MAX) {
3659b12dfa0SLiu Jingqi error_setg(errp, "Bandwidth %" PRIu64 " between initiator=%d "
3669b12dfa0SLiu Jingqi "and target=%d should not differ from previously "
3679b12dfa0SLiu Jingqi "entered values on more than %d", node->bandwidth,
3689b12dfa0SLiu Jingqi node->initiator, node->target, UINT16_MAX - 1);
3699b12dfa0SLiu Jingqi return;
3709b12dfa0SLiu Jingqi } else {
3719b12dfa0SLiu Jingqi hmat_lb->base = temp_base;
3729b12dfa0SLiu Jingqi hmat_lb->range_bitmap = bitmap_copy;
3739b12dfa0SLiu Jingqi }
3749b12dfa0SLiu Jingqi
3759b12dfa0SLiu Jingqi /*
3769b12dfa0SLiu Jingqi * Set lb_info_provided bit 1 as 1,
3779b12dfa0SLiu Jingqi * bandwidth information is provided
3789b12dfa0SLiu Jingqi */
3799b12dfa0SLiu Jingqi numa_info[node->target].lb_info_provided |= BIT(1);
3809b12dfa0SLiu Jingqi }
3819b12dfa0SLiu Jingqi lb_data.data = node->bandwidth;
3829b12dfa0SLiu Jingqi } else {
383*848ca151SPierrick Bouvier g_assert_not_reached();
3849b12dfa0SLiu Jingqi }
3859b12dfa0SLiu Jingqi
3869b12dfa0SLiu Jingqi g_array_append_val(hmat_lb->list, lb_data);
3879b12dfa0SLiu Jingqi }
3889b12dfa0SLiu Jingqi
parse_numa_hmat_cache(MachineState * ms,NumaHmatCacheOptions * node,Error ** errp)389c412a48dSLiu Jingqi void parse_numa_hmat_cache(MachineState *ms, NumaHmatCacheOptions *node,
390c412a48dSLiu Jingqi Error **errp)
391c412a48dSLiu Jingqi {
392c412a48dSLiu Jingqi int nb_numa_nodes = ms->numa_state->num_nodes;
393c412a48dSLiu Jingqi NodeInfo *numa_info = ms->numa_state->nodes;
394c412a48dSLiu Jingqi NumaHmatCacheOptions *hmat_cache = NULL;
395c412a48dSLiu Jingqi
396c412a48dSLiu Jingqi if (node->node_id >= nb_numa_nodes) {
397c412a48dSLiu Jingqi error_setg(errp, "Invalid node-id=%" PRIu32 ", it should be less "
398c412a48dSLiu Jingqi "than %d", node->node_id, nb_numa_nodes);
399c412a48dSLiu Jingqi return;
400c412a48dSLiu Jingqi }
401c412a48dSLiu Jingqi
402c412a48dSLiu Jingqi if (numa_info[node->node_id].lb_info_provided != (BIT(0) | BIT(1))) {
403c412a48dSLiu Jingqi error_setg(errp, "The latency and bandwidth information of "
404c412a48dSLiu Jingqi "node-id=%" PRIu32 " should be provided before memory side "
405c412a48dSLiu Jingqi "cache attributes", node->node_id);
406c412a48dSLiu Jingqi return;
407c412a48dSLiu Jingqi }
408c412a48dSLiu Jingqi
409c412a48dSLiu Jingqi if (node->level < 1 || node->level >= HMAT_LB_LEVELS) {
410c412a48dSLiu Jingqi error_setg(errp, "Invalid level=%" PRIu8 ", it should be larger than 0 "
411c412a48dSLiu Jingqi "and less than or equal to %d", node->level,
412c412a48dSLiu Jingqi HMAT_LB_LEVELS - 1);
413c412a48dSLiu Jingqi return;
414c412a48dSLiu Jingqi }
415c412a48dSLiu Jingqi
416c412a48dSLiu Jingqi assert(node->associativity < HMAT_CACHE_ASSOCIATIVITY__MAX);
417c412a48dSLiu Jingqi assert(node->policy < HMAT_CACHE_WRITE_POLICY__MAX);
418c412a48dSLiu Jingqi if (ms->numa_state->hmat_cache[node->node_id][node->level]) {
419c412a48dSLiu Jingqi error_setg(errp, "Duplicate configuration of the side cache for "
420c412a48dSLiu Jingqi "node-id=%" PRIu32 " and level=%" PRIu8,
421c412a48dSLiu Jingqi node->node_id, node->level);
422c412a48dSLiu Jingqi return;
423c412a48dSLiu Jingqi }
424c412a48dSLiu Jingqi
425c412a48dSLiu Jingqi if ((node->level > 1) &&
4261b5e843aSIgor Mammedov ms->numa_state->hmat_cache[node->node_id][node->level - 1] == NULL) {
4271b5e843aSIgor Mammedov error_setg(errp, "Cache level=%u shall be defined first",
4281b5e843aSIgor Mammedov node->level - 1);
4291b5e843aSIgor Mammedov return;
4301b5e843aSIgor Mammedov }
4311b5e843aSIgor Mammedov
4321b5e843aSIgor Mammedov if ((node->level > 1) &&
433fadb055bSIgor Mammedov (node->size <=
434c412a48dSLiu Jingqi ms->numa_state->hmat_cache[node->node_id][node->level - 1]->size)) {
435c412a48dSLiu Jingqi error_setg(errp, "Invalid size=%" PRIu64 ", the size of level=%" PRIu8
436fadb055bSIgor Mammedov " should be larger than the size(%" PRIu64 ") of "
437c412a48dSLiu Jingqi "level=%u", node->size, node->level,
438c412a48dSLiu Jingqi ms->numa_state->hmat_cache[node->node_id]
439c412a48dSLiu Jingqi [node->level - 1]->size,
440c412a48dSLiu Jingqi node->level - 1);
441c412a48dSLiu Jingqi return;
442c412a48dSLiu Jingqi }
443c412a48dSLiu Jingqi
444c412a48dSLiu Jingqi if ((node->level < HMAT_LB_LEVELS - 1) &&
445c412a48dSLiu Jingqi ms->numa_state->hmat_cache[node->node_id][node->level + 1] &&
446fadb055bSIgor Mammedov (node->size >=
447c412a48dSLiu Jingqi ms->numa_state->hmat_cache[node->node_id][node->level + 1]->size)) {
448c412a48dSLiu Jingqi error_setg(errp, "Invalid size=%" PRIu64 ", the size of level=%" PRIu8
449fadb055bSIgor Mammedov " should be less than the size(%" PRIu64 ") of "
450c412a48dSLiu Jingqi "level=%u", node->size, node->level,
451c412a48dSLiu Jingqi ms->numa_state->hmat_cache[node->node_id]
452c412a48dSLiu Jingqi [node->level + 1]->size,
453c412a48dSLiu Jingqi node->level + 1);
454c412a48dSLiu Jingqi return;
455c412a48dSLiu Jingqi }
456c412a48dSLiu Jingqi
457c412a48dSLiu Jingqi hmat_cache = g_malloc0(sizeof(*hmat_cache));
458c412a48dSLiu Jingqi memcpy(hmat_cache, node, sizeof(*hmat_cache));
459c412a48dSLiu Jingqi ms->numa_state->hmat_cache[node->node_id][node->level] = hmat_cache;
460c412a48dSLiu Jingqi }
461c412a48dSLiu Jingqi
set_numa_options(MachineState * ms,NumaOptions * object,Error ** errp)462ac057879SMarkus Armbruster void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp)
463ac057879SMarkus Armbruster {
4645275db59SIgor Mammedov if (!ms->numa_state) {
465aa570207STao Xu error_setg(errp, "NUMA is not supported by this machine-type");
466992861fbSMarkus Armbruster return;
467aa570207STao Xu }
468ac057879SMarkus Armbruster
469ac057879SMarkus Armbruster switch (object->type) {
470ac057879SMarkus Armbruster case NUMA_OPTIONS_TYPE_NODE:
471992861fbSMarkus Armbruster parse_numa_node(ms, &object->u.node, errp);
472ac057879SMarkus Armbruster break;
473ac057879SMarkus Armbruster case NUMA_OPTIONS_TYPE_DIST:
474992861fbSMarkus Armbruster parse_numa_distance(ms, &object->u.dist, errp);
475ac057879SMarkus Armbruster break;
476ac057879SMarkus Armbruster case NUMA_OPTIONS_TYPE_CPU:
477ac057879SMarkus Armbruster if (!object->u.cpu.has_node_id) {
478992861fbSMarkus Armbruster error_setg(errp, "Missing mandatory node-id property");
479992861fbSMarkus Armbruster return;
480ac057879SMarkus Armbruster }
4817e721e7bSTao Xu if (!ms->numa_state->nodes[object->u.cpu.node_id].present) {
482992861fbSMarkus Armbruster error_setg(errp, "Invalid node-id=%" PRId64 ", NUMA node must be "
483ac057879SMarkus Armbruster "defined with -numa node,nodeid=ID before it's used with "
484ac057879SMarkus Armbruster "-numa cpu,node-id=ID", object->u.cpu.node_id);
485992861fbSMarkus Armbruster return;
486ac057879SMarkus Armbruster }
487ac057879SMarkus Armbruster
488992861fbSMarkus Armbruster machine_set_cpu_numa_node(ms,
489992861fbSMarkus Armbruster qapi_NumaCpuOptions_base(&object->u.cpu),
490992861fbSMarkus Armbruster errp);
491ac057879SMarkus Armbruster break;
4929b12dfa0SLiu Jingqi case NUMA_OPTIONS_TYPE_HMAT_LB:
4939b12dfa0SLiu Jingqi if (!ms->numa_state->hmat_enabled) {
4949b12dfa0SLiu Jingqi error_setg(errp, "ACPI Heterogeneous Memory Attribute Table "
4959b12dfa0SLiu Jingqi "(HMAT) is disabled, enable it with -machine hmat=on "
4969b12dfa0SLiu Jingqi "before using any of hmat specific options");
4979b12dfa0SLiu Jingqi return;
4989b12dfa0SLiu Jingqi }
4999b12dfa0SLiu Jingqi
500992861fbSMarkus Armbruster parse_numa_hmat_lb(ms->numa_state, &object->u.hmat_lb, errp);
5019b12dfa0SLiu Jingqi break;
502c412a48dSLiu Jingqi case NUMA_OPTIONS_TYPE_HMAT_CACHE:
503c412a48dSLiu Jingqi if (!ms->numa_state->hmat_enabled) {
504c412a48dSLiu Jingqi error_setg(errp, "ACPI Heterogeneous Memory Attribute Table "
505c412a48dSLiu Jingqi "(HMAT) is disabled, enable it with -machine hmat=on "
506c412a48dSLiu Jingqi "before using any of hmat specific options");
507c412a48dSLiu Jingqi return;
508c412a48dSLiu Jingqi }
509c412a48dSLiu Jingqi
510992861fbSMarkus Armbruster parse_numa_hmat_cache(ms, &object->u.hmat_cache, errp);
511c412a48dSLiu Jingqi break;
512ac057879SMarkus Armbruster default:
513ac057879SMarkus Armbruster abort();
514ac057879SMarkus Armbruster }
515ac057879SMarkus Armbruster }
516ac057879SMarkus Armbruster
parse_numa(void * opaque,QemuOpts * opts,Error ** errp)517ac057879SMarkus Armbruster static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
518ac057879SMarkus Armbruster {
519ac057879SMarkus Armbruster NumaOptions *object = NULL;
520ac057879SMarkus Armbruster MachineState *ms = MACHINE(opaque);
521ac057879SMarkus Armbruster Error *err = NULL;
522ac057879SMarkus Armbruster Visitor *v = opts_visitor_new(opts);
523ac057879SMarkus Armbruster
524b11a093cSMarkus Armbruster visit_type_NumaOptions(v, NULL, &object, errp);
525ac057879SMarkus Armbruster visit_free(v);
526b11a093cSMarkus Armbruster if (!object) {
527b11a093cSMarkus Armbruster return -1;
528ac057879SMarkus Armbruster }
529ac057879SMarkus Armbruster
530ac057879SMarkus Armbruster /* Fix up legacy suffix-less format */
531ac057879SMarkus Armbruster if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
532ac057879SMarkus Armbruster const char *mem_str = qemu_opt_get(opts, "mem");
533a73049b2SEric Blake int ret = qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
534a73049b2SEric Blake
535a73049b2SEric Blake if (ret < 0) {
536a73049b2SEric Blake error_setg_errno(&err, -ret, "could not parse memory size '%s'",
537a73049b2SEric Blake mem_str);
538a73049b2SEric Blake }
539ac057879SMarkus Armbruster }
540ac057879SMarkus Armbruster
541a73049b2SEric Blake if (!err) {
542ac057879SMarkus Armbruster set_numa_options(ms, object, &err);
543a73049b2SEric Blake }
544ac057879SMarkus Armbruster
545ac057879SMarkus Armbruster qapi_free_NumaOptions(object);
546ac057879SMarkus Armbruster if (err) {
547ac057879SMarkus Armbruster error_propagate(errp, err);
548ac057879SMarkus Armbruster return -1;
549ac057879SMarkus Armbruster }
550ac057879SMarkus Armbruster
551ac057879SMarkus Armbruster return 0;
552ac057879SMarkus Armbruster }
553ac057879SMarkus Armbruster
554ac057879SMarkus Armbruster /* If all node pair distances are symmetric, then only distances
555ac057879SMarkus Armbruster * in one direction are enough. If there is even one asymmetric
556ac057879SMarkus Armbruster * pair, though, then all distances must be provided. The
557ac057879SMarkus Armbruster * distance from a node to itself is always NUMA_DISTANCE_MIN,
558ac057879SMarkus Armbruster * so providing it is never necessary.
559ac057879SMarkus Armbruster */
validate_numa_distance(MachineState * ms)560aa570207STao Xu static void validate_numa_distance(MachineState *ms)
561ac057879SMarkus Armbruster {
562ac057879SMarkus Armbruster int src, dst;
563ac057879SMarkus Armbruster bool is_asymmetrical = false;
564aa570207STao Xu int nb_numa_nodes = ms->numa_state->num_nodes;
5657e721e7bSTao Xu NodeInfo *numa_info = ms->numa_state->nodes;
566ac057879SMarkus Armbruster
567ac057879SMarkus Armbruster for (src = 0; src < nb_numa_nodes; src++) {
568ac057879SMarkus Armbruster for (dst = src; dst < nb_numa_nodes; dst++) {
569ac057879SMarkus Armbruster if (numa_info[src].distance[dst] == 0 &&
570ac057879SMarkus Armbruster numa_info[dst].distance[src] == 0) {
571ac057879SMarkus Armbruster if (src != dst) {
572ac057879SMarkus Armbruster error_report("The distance between node %d and %d is "
573ac057879SMarkus Armbruster "missing, at least one distance value "
574ac057879SMarkus Armbruster "between each nodes should be provided.",
575ac057879SMarkus Armbruster src, dst);
576ac057879SMarkus Armbruster exit(EXIT_FAILURE);
577ac057879SMarkus Armbruster }
578ac057879SMarkus Armbruster }
579ac057879SMarkus Armbruster
580ac057879SMarkus Armbruster if (numa_info[src].distance[dst] != 0 &&
581ac057879SMarkus Armbruster numa_info[dst].distance[src] != 0 &&
582ac057879SMarkus Armbruster numa_info[src].distance[dst] !=
583ac057879SMarkus Armbruster numa_info[dst].distance[src]) {
584ac057879SMarkus Armbruster is_asymmetrical = true;
585ac057879SMarkus Armbruster }
586ac057879SMarkus Armbruster }
587ac057879SMarkus Armbruster }
588ac057879SMarkus Armbruster
589ac057879SMarkus Armbruster if (is_asymmetrical) {
590ac057879SMarkus Armbruster for (src = 0; src < nb_numa_nodes; src++) {
591ac057879SMarkus Armbruster for (dst = 0; dst < nb_numa_nodes; dst++) {
592ac057879SMarkus Armbruster if (src != dst && numa_info[src].distance[dst] == 0) {
593ac057879SMarkus Armbruster error_report("At least one asymmetrical pair of "
594ac057879SMarkus Armbruster "distances is given, please provide distances "
595ac057879SMarkus Armbruster "for both directions of all node pairs.");
596ac057879SMarkus Armbruster exit(EXIT_FAILURE);
597ac057879SMarkus Armbruster }
598ac057879SMarkus Armbruster }
599ac057879SMarkus Armbruster }
600ac057879SMarkus Armbruster }
601ac057879SMarkus Armbruster }
602ac057879SMarkus Armbruster
complete_init_numa_distance(MachineState * ms)603aa570207STao Xu static void complete_init_numa_distance(MachineState *ms)
604ac057879SMarkus Armbruster {
605ac057879SMarkus Armbruster int src, dst;
6067e721e7bSTao Xu NodeInfo *numa_info = ms->numa_state->nodes;
607ac057879SMarkus Armbruster
608ac057879SMarkus Armbruster /* Fixup NUMA distance by symmetric policy because if it is an
609ac057879SMarkus Armbruster * asymmetric distance table, it should be a complete table and
610ac057879SMarkus Armbruster * there would not be any missing distance except local node, which
611ac057879SMarkus Armbruster * is verified by validate_numa_distance above.
612ac057879SMarkus Armbruster */
613aa570207STao Xu for (src = 0; src < ms->numa_state->num_nodes; src++) {
614aa570207STao Xu for (dst = 0; dst < ms->numa_state->num_nodes; dst++) {
615ac057879SMarkus Armbruster if (numa_info[src].distance[dst] == 0) {
616ac057879SMarkus Armbruster if (src == dst) {
617ac057879SMarkus Armbruster numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
618ac057879SMarkus Armbruster } else {
619ac057879SMarkus Armbruster numa_info[src].distance[dst] = numa_info[dst].distance[src];
620ac057879SMarkus Armbruster }
621ac057879SMarkus Armbruster }
622ac057879SMarkus Armbruster }
623ac057879SMarkus Armbruster }
624ac057879SMarkus Armbruster }
625ac057879SMarkus Armbruster
numa_init_memdev_container(MachineState * ms,MemoryRegion * ram)6266b61c2c5SIgor Mammedov static void numa_init_memdev_container(MachineState *ms, MemoryRegion *ram)
6276b61c2c5SIgor Mammedov {
6286b61c2c5SIgor Mammedov int i;
6296b61c2c5SIgor Mammedov uint64_t addr = 0;
6306b61c2c5SIgor Mammedov
6316b61c2c5SIgor Mammedov for (i = 0; i < ms->numa_state->num_nodes; i++) {
6326b61c2c5SIgor Mammedov uint64_t size = ms->numa_state->nodes[i].node_mem;
6336b61c2c5SIgor Mammedov HostMemoryBackend *backend = ms->numa_state->nodes[i].node_memdev;
6346b61c2c5SIgor Mammedov if (!backend) {
6356b61c2c5SIgor Mammedov continue;
6366b61c2c5SIgor Mammedov }
6376b61c2c5SIgor Mammedov MemoryRegion *seg = machine_consume_memdev(ms, backend);
6386b61c2c5SIgor Mammedov memory_region_add_subregion(ram, addr, seg);
6396b61c2c5SIgor Mammedov addr += size;
6406b61c2c5SIgor Mammedov }
6416b61c2c5SIgor Mammedov }
6426b61c2c5SIgor Mammedov
numa_complete_configuration(MachineState * ms)643ac057879SMarkus Armbruster void numa_complete_configuration(MachineState *ms)
644ac057879SMarkus Armbruster {
645ac057879SMarkus Armbruster int i;
646ac057879SMarkus Armbruster MachineClass *mc = MACHINE_GET_CLASS(ms);
6477e721e7bSTao Xu NodeInfo *numa_info = ms->numa_state->nodes;
648ac057879SMarkus Armbruster
649ac057879SMarkus Armbruster /*
650195784a0SDavid Hildenbrand * If memory hotplug is enabled (slot > 0) or memory devices are enabled
651b326b6eaSPaolo Bonzini * (ms->maxram_size > ms->ram_size) but without '-numa' options explicitly on
652195784a0SDavid Hildenbrand * CLI, guests will break.
653ac057879SMarkus Armbruster *
654ac057879SMarkus Armbruster * Windows: won't enable memory hotplug without SRAT table at all
655ac057879SMarkus Armbruster *
656ac057879SMarkus Armbruster * Linux: if QEMU is started with initial memory all below 4Gb
657ac057879SMarkus Armbruster * and no SRAT table present, guest kernel will use nommu DMA ops,
658ac057879SMarkus Armbruster * which breaks 32bit hw drivers when memory is hotplugged and
659ac057879SMarkus Armbruster * guest tries to use it with that drivers.
660ac057879SMarkus Armbruster *
661ac057879SMarkus Armbruster * Enable NUMA implicitly by adding a new NUMA node automatically.
6620533ef5fSTao Xu *
6630533ef5fSTao Xu * Or if MachineClass::auto_enable_numa is true and no NUMA nodes,
6640533ef5fSTao Xu * assume there is just one node with whole RAM.
665ac057879SMarkus Armbruster */
6660533ef5fSTao Xu if (ms->numa_state->num_nodes == 0 &&
667195784a0SDavid Hildenbrand ((ms->ram_slots && mc->auto_enable_numa_with_memhp) ||
668195784a0SDavid Hildenbrand (ms->maxram_size > ms->ram_size && mc->auto_enable_numa_with_memdev) ||
6690533ef5fSTao Xu mc->auto_enable_numa)) {
670ac057879SMarkus Armbruster NumaNodeOptions node = { };
671ac057879SMarkus Armbruster parse_numa_node(ms, &node, &error_abort);
672b326b6eaSPaolo Bonzini numa_info[0].node_mem = ms->ram_size;
673ac057879SMarkus Armbruster }
674ac057879SMarkus Armbruster
675ac057879SMarkus Armbruster assert(max_numa_nodeid <= MAX_NODES);
676ac057879SMarkus Armbruster
677ac057879SMarkus Armbruster /* No support for sparse NUMA node IDs yet: */
678ac057879SMarkus Armbruster for (i = max_numa_nodeid - 1; i >= 0; i--) {
679ac057879SMarkus Armbruster /* Report large node IDs first, to make mistakes easier to spot */
680ac057879SMarkus Armbruster if (!numa_info[i].present) {
681ac057879SMarkus Armbruster error_report("numa: Node ID missing: %d", i);
682ac057879SMarkus Armbruster exit(1);
683ac057879SMarkus Armbruster }
684ac057879SMarkus Armbruster }
685ac057879SMarkus Armbruster
686ac057879SMarkus Armbruster /* This must be always true if all nodes are present: */
687aa570207STao Xu assert(ms->numa_state->num_nodes == max_numa_nodeid);
688ac057879SMarkus Armbruster
689aa570207STao Xu if (ms->numa_state->num_nodes > 0) {
690ac057879SMarkus Armbruster uint64_t numa_total;
691ac057879SMarkus Armbruster
692ac057879SMarkus Armbruster numa_total = 0;
693aa570207STao Xu for (i = 0; i < ms->numa_state->num_nodes; i++) {
694ac057879SMarkus Armbruster numa_total += numa_info[i].node_mem;
695ac057879SMarkus Armbruster }
696b326b6eaSPaolo Bonzini if (numa_total != ms->ram_size) {
697ac057879SMarkus Armbruster error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
698ac057879SMarkus Armbruster " should equal RAM size (0x" RAM_ADDR_FMT ")",
699b326b6eaSPaolo Bonzini numa_total, ms->ram_size);
700ac057879SMarkus Armbruster exit(1);
701ac057879SMarkus Armbruster }
702ac057879SMarkus Armbruster
7036b61c2c5SIgor Mammedov if (!numa_uses_legacy_mem() && mc->default_ram_id) {
70426f88d84SPaolo Bonzini if (ms->memdev) {
705ea81f98bSIgor Mammedov error_report("'-machine memory-backend' and '-numa memdev'"
706ea81f98bSIgor Mammedov " properties are mutually exclusive");
707ea81f98bSIgor Mammedov exit(1);
708ea81f98bSIgor Mammedov }
7096b61c2c5SIgor Mammedov ms->ram = g_new(MemoryRegion, 1);
7106b61c2c5SIgor Mammedov memory_region_init(ms->ram, OBJECT(ms), mc->default_ram_id,
711b326b6eaSPaolo Bonzini ms->ram_size);
7126b61c2c5SIgor Mammedov numa_init_memdev_container(ms, ms->ram);
7136b61c2c5SIgor Mammedov }
714ac057879SMarkus Armbruster /* QEMU needs at least all unique node pair distances to build
715ac057879SMarkus Armbruster * the whole NUMA distance table. QEMU treats the distance table
716ac057879SMarkus Armbruster * as symmetric by default, i.e. distance A->B == distance B->A.
717ac057879SMarkus Armbruster * Thus, QEMU is able to complete the distance table
718ac057879SMarkus Armbruster * initialization even though only distance A->B is provided and
719ac057879SMarkus Armbruster * distance B->A is not. QEMU knows the distance of a node to
720ac057879SMarkus Armbruster * itself is always 10, so A->A distances may be omitted. When
721ac057879SMarkus Armbruster * the distances of two nodes of a pair differ, i.e. distance
722ac057879SMarkus Armbruster * A->B != distance B->A, then that means the distance table is
723ac057879SMarkus Armbruster * asymmetric. In this case, the distances for both directions
724ac057879SMarkus Armbruster * of all node pairs are required.
725ac057879SMarkus Armbruster */
726118154b7STao Xu if (ms->numa_state->have_numa_distance) {
727ac057879SMarkus Armbruster /* Validate enough NUMA distance information was provided. */
728aa570207STao Xu validate_numa_distance(ms);
729ac057879SMarkus Armbruster
730ac057879SMarkus Armbruster /* Validation succeeded, now fill in any missing distances. */
731aa570207STao Xu complete_init_numa_distance(ms);
732ac057879SMarkus Armbruster }
733ac057879SMarkus Armbruster }
734ac057879SMarkus Armbruster }
735ac057879SMarkus Armbruster
parse_numa_opts(MachineState * ms)736ac057879SMarkus Armbruster void parse_numa_opts(MachineState *ms)
737ac057879SMarkus Armbruster {
738ac057879SMarkus Armbruster qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal);
739ac057879SMarkus Armbruster }
740ac057879SMarkus Armbruster
numa_cpu_pre_plug(const CPUArchId * slot,DeviceState * dev,Error ** errp)741ac057879SMarkus Armbruster void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
742ac057879SMarkus Armbruster {
743ac057879SMarkus Armbruster int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
744ac057879SMarkus Armbruster
745ac057879SMarkus Armbruster if (node_id == CPU_UNSET_NUMA_NODE_ID) {
746ac057879SMarkus Armbruster /* due to bug in libvirt, it doesn't pass node-id from props on
747ac057879SMarkus Armbruster * device_add as expected, so we have to fix it up here */
748ac057879SMarkus Armbruster if (slot->props.has_node_id) {
7495325cc34SMarkus Armbruster object_property_set_int(OBJECT(dev), "node-id",
7505325cc34SMarkus Armbruster slot->props.node_id, errp);
751ac057879SMarkus Armbruster }
752ac057879SMarkus Armbruster } else if (node_id != slot->props.node_id) {
753ac057879SMarkus Armbruster error_setg(errp, "invalid node-id, must be %"PRId64,
754ac057879SMarkus Armbruster slot->props.node_id);
755ac057879SMarkus Armbruster }
756ac057879SMarkus Armbruster }
757ac057879SMarkus Armbruster
numa_stat_memory_devices(NumaNodeMem node_mem[])758ac057879SMarkus Armbruster static void numa_stat_memory_devices(NumaNodeMem node_mem[])
759ac057879SMarkus Armbruster {
760ac057879SMarkus Armbruster MemoryDeviceInfoList *info_list = qmp_memory_device_list();
761ac057879SMarkus Armbruster MemoryDeviceInfoList *info;
762ac057879SMarkus Armbruster PCDIMMDeviceInfo *pcdimm_info;
763c35d17caSPeter Maydell VirtioPMEMDeviceInfo *vpi;
76416647a82SDavid Hildenbrand VirtioMEMDeviceInfo *vmi;
765bd989ed4SPaolo Bonzini SgxEPCDeviceInfo *se;
766ac057879SMarkus Armbruster
767ac057879SMarkus Armbruster for (info = info_list; info; info = info->next) {
768ac057879SMarkus Armbruster MemoryDeviceInfo *value = info->value;
769ac057879SMarkus Armbruster
770ac057879SMarkus Armbruster if (value) {
771ac057879SMarkus Armbruster switch (value->type) {
772ac057879SMarkus Armbruster case MEMORY_DEVICE_INFO_KIND_DIMM:
773ac057879SMarkus Armbruster case MEMORY_DEVICE_INFO_KIND_NVDIMM:
774c35d17caSPeter Maydell pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ?
775c35d17caSPeter Maydell value->u.dimm.data : value->u.nvdimm.data;
776ac057879SMarkus Armbruster node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
777ac057879SMarkus Armbruster node_mem[pcdimm_info->node].node_plugged_mem +=
778ac057879SMarkus Armbruster pcdimm_info->size;
779c35d17caSPeter Maydell break;
780c35d17caSPeter Maydell case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM:
781c35d17caSPeter Maydell vpi = value->u.virtio_pmem.data;
782c35d17caSPeter Maydell /* TODO: once we support numa, assign to right node */
783c35d17caSPeter Maydell node_mem[0].node_mem += vpi->size;
784c35d17caSPeter Maydell node_mem[0].node_plugged_mem += vpi->size;
785c35d17caSPeter Maydell break;
78616647a82SDavid Hildenbrand case MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM:
78716647a82SDavid Hildenbrand vmi = value->u.virtio_mem.data;
78816647a82SDavid Hildenbrand node_mem[vmi->node].node_mem += vmi->size;
78916647a82SDavid Hildenbrand node_mem[vmi->node].node_plugged_mem += vmi->size;
79016647a82SDavid Hildenbrand break;
791bd989ed4SPaolo Bonzini case MEMORY_DEVICE_INFO_KIND_SGX_EPC:
792bd989ed4SPaolo Bonzini se = value->u.sgx_epc.data;
79311058123SYang Zhong node_mem[se->node].node_mem += se->size;
79411058123SYang Zhong node_mem[se->node].node_plugged_mem = 0;
795bd989ed4SPaolo Bonzini break;
796c35d17caSPeter Maydell default:
797c35d17caSPeter Maydell g_assert_not_reached();
798ac057879SMarkus Armbruster }
799ac057879SMarkus Armbruster }
800ac057879SMarkus Armbruster }
801ac057879SMarkus Armbruster qapi_free_MemoryDeviceInfoList(info_list);
802ac057879SMarkus Armbruster }
803ac057879SMarkus Armbruster
query_numa_node_mem(NumaNodeMem node_mem[],MachineState * ms)804aa570207STao Xu void query_numa_node_mem(NumaNodeMem node_mem[], MachineState *ms)
805ac057879SMarkus Armbruster {
806ac057879SMarkus Armbruster int i;
807ac057879SMarkus Armbruster
808aa570207STao Xu if (ms->numa_state == NULL || ms->numa_state->num_nodes <= 0) {
809ac057879SMarkus Armbruster return;
810ac057879SMarkus Armbruster }
811ac057879SMarkus Armbruster
812ac057879SMarkus Armbruster numa_stat_memory_devices(node_mem);
813aa570207STao Xu for (i = 0; i < ms->numa_state->num_nodes; i++) {
8147e721e7bSTao Xu node_mem[i].node_mem += ms->numa_state->nodes[i].node_mem;
815ac057879SMarkus Armbruster }
816ac057879SMarkus Armbruster }
817ac057879SMarkus Armbruster
ram_block_notify_add_single(RAMBlock * rb,void * opaque)818082851a3SDavid Hildenbrand static int ram_block_notify_add_single(RAMBlock *rb, void *opaque)
819082851a3SDavid Hildenbrand {
820082851a3SDavid Hildenbrand const ram_addr_t max_size = qemu_ram_get_max_length(rb);
8218f44304cSDavid Hildenbrand const ram_addr_t size = qemu_ram_get_used_length(rb);
822082851a3SDavid Hildenbrand void *host = qemu_ram_get_host_addr(rb);
823082851a3SDavid Hildenbrand RAMBlockNotifier *notifier = opaque;
824082851a3SDavid Hildenbrand
825082851a3SDavid Hildenbrand if (host) {
8268f44304cSDavid Hildenbrand notifier->ram_block_added(notifier, host, size, max_size);
827082851a3SDavid Hildenbrand }
828082851a3SDavid Hildenbrand return 0;
829082851a3SDavid Hildenbrand }
830082851a3SDavid Hildenbrand
ram_block_notify_remove_single(RAMBlock * rb,void * opaque)8311f0fea38SStefan Hajnoczi static int ram_block_notify_remove_single(RAMBlock *rb, void *opaque)
8321f0fea38SStefan Hajnoczi {
8331f0fea38SStefan Hajnoczi const ram_addr_t max_size = qemu_ram_get_max_length(rb);
8341f0fea38SStefan Hajnoczi const ram_addr_t size = qemu_ram_get_used_length(rb);
8351f0fea38SStefan Hajnoczi void *host = qemu_ram_get_host_addr(rb);
8361f0fea38SStefan Hajnoczi RAMBlockNotifier *notifier = opaque;
8371f0fea38SStefan Hajnoczi
8381f0fea38SStefan Hajnoczi if (host) {
8391f0fea38SStefan Hajnoczi notifier->ram_block_removed(notifier, host, size, max_size);
8401f0fea38SStefan Hajnoczi }
8411f0fea38SStefan Hajnoczi return 0;
8421f0fea38SStefan Hajnoczi }
8431f0fea38SStefan Hajnoczi
ram_block_notifier_add(RAMBlockNotifier * n)844ac057879SMarkus Armbruster void ram_block_notifier_add(RAMBlockNotifier *n)
845ac057879SMarkus Armbruster {
846ac057879SMarkus Armbruster QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
847e15c7d1eSDavid Hildenbrand
848082851a3SDavid Hildenbrand /* Notify about all existing ram blocks. */
849e15c7d1eSDavid Hildenbrand if (n->ram_block_added) {
850082851a3SDavid Hildenbrand qemu_ram_foreach_block(ram_block_notify_add_single, n);
851ac057879SMarkus Armbruster }
852e15c7d1eSDavid Hildenbrand }
853ac057879SMarkus Armbruster
ram_block_notifier_remove(RAMBlockNotifier * n)854ac057879SMarkus Armbruster void ram_block_notifier_remove(RAMBlockNotifier *n)
855ac057879SMarkus Armbruster {
856ac057879SMarkus Armbruster QLIST_REMOVE(n, next);
8571f0fea38SStefan Hajnoczi
8581f0fea38SStefan Hajnoczi if (n->ram_block_removed) {
8591f0fea38SStefan Hajnoczi qemu_ram_foreach_block(ram_block_notify_remove_single, n);
8601f0fea38SStefan Hajnoczi }
861ac057879SMarkus Armbruster }
862ac057879SMarkus Armbruster
ram_block_notify_add(void * host,size_t size,size_t max_size)8638f44304cSDavid Hildenbrand void ram_block_notify_add(void *host, size_t size, size_t max_size)
864ac057879SMarkus Armbruster {
865ac057879SMarkus Armbruster RAMBlockNotifier *notifier;
8664fdd0a1aSStefan Hajnoczi RAMBlockNotifier *next;
867ac057879SMarkus Armbruster
8684fdd0a1aSStefan Hajnoczi QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) {
869e15c7d1eSDavid Hildenbrand if (notifier->ram_block_added) {
8708f44304cSDavid Hildenbrand notifier->ram_block_added(notifier, host, size, max_size);
871ac057879SMarkus Armbruster }
872ac057879SMarkus Armbruster }
873e15c7d1eSDavid Hildenbrand }
874ac057879SMarkus Armbruster
ram_block_notify_remove(void * host,size_t size,size_t max_size)8758f44304cSDavid Hildenbrand void ram_block_notify_remove(void *host, size_t size, size_t max_size)
876ac057879SMarkus Armbruster {
877ac057879SMarkus Armbruster RAMBlockNotifier *notifier;
8784fdd0a1aSStefan Hajnoczi RAMBlockNotifier *next;
879ac057879SMarkus Armbruster
8804fdd0a1aSStefan Hajnoczi QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) {
881e15c7d1eSDavid Hildenbrand if (notifier->ram_block_removed) {
8828f44304cSDavid Hildenbrand notifier->ram_block_removed(notifier, host, size, max_size);
8838f44304cSDavid Hildenbrand }
8848f44304cSDavid Hildenbrand }
885e15c7d1eSDavid Hildenbrand }
8868f44304cSDavid Hildenbrand
ram_block_notify_resize(void * host,size_t old_size,size_t new_size)8878f44304cSDavid Hildenbrand void ram_block_notify_resize(void *host, size_t old_size, size_t new_size)
8888f44304cSDavid Hildenbrand {
8898f44304cSDavid Hildenbrand RAMBlockNotifier *notifier;
8904fdd0a1aSStefan Hajnoczi RAMBlockNotifier *next;
8918f44304cSDavid Hildenbrand
8924fdd0a1aSStefan Hajnoczi QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) {
8938f44304cSDavid Hildenbrand if (notifier->ram_block_resized) {
8948f44304cSDavid Hildenbrand notifier->ram_block_resized(notifier, host, old_size, new_size);
8958f44304cSDavid Hildenbrand }
896ac057879SMarkus Armbruster }
897ac057879SMarkus Armbruster }
898