xref: /openbmc/qemu/hw/core/numa.c (revision 118154b7674b85827f272332efe86fc58cb76249)
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"
26b58c5c2dSMarkus Armbruster #include "sysemu/hostmem.h"
27ac057879SMarkus Armbruster #include "sysemu/numa.h"
2846517dd4SMarkus Armbruster #include "sysemu/sysemu.h"
29ac057879SMarkus Armbruster #include "exec/cpu-common.h"
30ac057879SMarkus Armbruster #include "exec/ramlist.h"
31ac057879SMarkus Armbruster #include "qemu/bitmap.h"
32ac057879SMarkus Armbruster #include "qemu/error-report.h"
33ac057879SMarkus Armbruster #include "qapi/error.h"
34ac057879SMarkus Armbruster #include "qapi/opts-visitor.h"
35ac057879SMarkus Armbruster #include "qapi/qapi-visit-machine.h"
36f8123f22SEduardo Habkost #include "sysemu/qtest.h"
372e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
38ac057879SMarkus Armbruster #include "hw/mem/pc-dimm.h"
39d6454270SMarkus Armbruster #include "migration/vmstate.h"
4012e9493dSMarkus Armbruster #include "hw/boards.h"
41ac057879SMarkus Armbruster #include "hw/mem/memory-device.h"
42ac057879SMarkus Armbruster #include "qemu/option.h"
43ac057879SMarkus Armbruster #include "qemu/config-file.h"
44ac057879SMarkus Armbruster #include "qemu/cutils.h"
45ac057879SMarkus Armbruster 
46ac057879SMarkus Armbruster QemuOptsList qemu_numa_opts = {
47ac057879SMarkus Armbruster     .name = "numa",
48ac057879SMarkus Armbruster     .implied_opt_name = "type",
49ac057879SMarkus Armbruster     .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
50ac057879SMarkus Armbruster     .desc = { { 0 } } /* validated with OptsVisitor */
51ac057879SMarkus Armbruster };
52ac057879SMarkus Armbruster 
53b69239e0SIgor Mammedov static int have_memdevs;
54b69239e0SIgor Mammedov static int have_mem;
55ac057879SMarkus Armbruster static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
56ac057879SMarkus Armbruster                              * For all nodes, nodeid < max_numa_nodeid
57ac057879SMarkus Armbruster                              */
58ac057879SMarkus Armbruster NodeInfo numa_info[MAX_NODES];
59ac057879SMarkus Armbruster 
60ac057879SMarkus Armbruster 
61ac057879SMarkus Armbruster static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
62ac057879SMarkus Armbruster                             Error **errp)
63ac057879SMarkus Armbruster {
64ac057879SMarkus Armbruster     Error *err = NULL;
65ac057879SMarkus Armbruster     uint16_t nodenr;
66ac057879SMarkus Armbruster     uint16List *cpus = NULL;
67ac057879SMarkus Armbruster     MachineClass *mc = MACHINE_GET_CLASS(ms);
685cc8767dSLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
69ac057879SMarkus Armbruster 
70ac057879SMarkus Armbruster     if (node->has_nodeid) {
71ac057879SMarkus Armbruster         nodenr = node->nodeid;
72ac057879SMarkus Armbruster     } else {
73aa570207STao Xu         nodenr = ms->numa_state->num_nodes;
74ac057879SMarkus Armbruster     }
75ac057879SMarkus Armbruster 
76ac057879SMarkus Armbruster     if (nodenr >= MAX_NODES) {
77ac057879SMarkus Armbruster         error_setg(errp, "Max number of NUMA nodes reached: %"
78ac057879SMarkus Armbruster                    PRIu16 "", nodenr);
79ac057879SMarkus Armbruster         return;
80ac057879SMarkus Armbruster     }
81ac057879SMarkus Armbruster 
82ac057879SMarkus Armbruster     if (numa_info[nodenr].present) {
83ac057879SMarkus Armbruster         error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
84ac057879SMarkus Armbruster         return;
85ac057879SMarkus Armbruster     }
86ac057879SMarkus Armbruster 
87ac057879SMarkus Armbruster     if (!mc->cpu_index_to_instance_props || !mc->get_default_cpu_node_id) {
88ac057879SMarkus Armbruster         error_setg(errp, "NUMA is not supported by this machine-type");
89ac057879SMarkus Armbruster         return;
90ac057879SMarkus Armbruster     }
91ac057879SMarkus Armbruster     for (cpus = node->cpus; cpus; cpus = cpus->next) {
92ac057879SMarkus Armbruster         CpuInstanceProperties props;
93ac057879SMarkus Armbruster         if (cpus->value >= max_cpus) {
94ac057879SMarkus Armbruster             error_setg(errp,
95ac057879SMarkus Armbruster                        "CPU index (%" PRIu16 ")"
96ac057879SMarkus Armbruster                        " should be smaller than maxcpus (%d)",
97ac057879SMarkus Armbruster                        cpus->value, max_cpus);
98ac057879SMarkus Armbruster             return;
99ac057879SMarkus Armbruster         }
100ac057879SMarkus Armbruster         props = mc->cpu_index_to_instance_props(ms, cpus->value);
101ac057879SMarkus Armbruster         props.node_id = nodenr;
102ac057879SMarkus Armbruster         props.has_node_id = true;
103ac057879SMarkus Armbruster         machine_set_cpu_numa_node(ms, &props, &err);
104ac057879SMarkus Armbruster         if (err) {
105ac057879SMarkus Armbruster             error_propagate(errp, err);
106ac057879SMarkus Armbruster             return;
107ac057879SMarkus Armbruster         }
108ac057879SMarkus Armbruster     }
109ac057879SMarkus Armbruster 
110b69239e0SIgor Mammedov     have_memdevs = have_memdevs ? : node->has_memdev;
111b69239e0SIgor Mammedov     have_mem = have_mem ? : node->has_mem;
112b69239e0SIgor Mammedov     if ((node->has_mem && have_memdevs) || (node->has_memdev && have_mem)) {
113b69239e0SIgor Mammedov         error_setg(errp, "numa configuration should use either mem= or memdev=,"
114b69239e0SIgor Mammedov                    "mixing both is not allowed");
115ac057879SMarkus Armbruster         return;
116ac057879SMarkus Armbruster     }
117ac057879SMarkus Armbruster 
118ac057879SMarkus Armbruster     if (node->has_mem) {
119ac057879SMarkus Armbruster         numa_info[nodenr].node_mem = node->mem;
120f8123f22SEduardo Habkost         if (!qtest_enabled()) {
121cdf80365SIgor Mammedov             warn_report("Parameter -numa node,mem is deprecated,"
122cdf80365SIgor Mammedov                         " use -numa node,memdev instead");
123ac057879SMarkus Armbruster         }
124f8123f22SEduardo Habkost     }
125ac057879SMarkus Armbruster     if (node->has_memdev) {
126ac057879SMarkus Armbruster         Object *o;
127ac057879SMarkus Armbruster         o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
128ac057879SMarkus Armbruster         if (!o) {
129ac057879SMarkus Armbruster             error_setg(errp, "memdev=%s is ambiguous", node->memdev);
130ac057879SMarkus Armbruster             return;
131ac057879SMarkus Armbruster         }
132ac057879SMarkus Armbruster 
133ac057879SMarkus Armbruster         object_ref(o);
134ac057879SMarkus Armbruster         numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
135ac057879SMarkus Armbruster         numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
136ac057879SMarkus Armbruster     }
137ac057879SMarkus Armbruster     numa_info[nodenr].present = true;
138ac057879SMarkus Armbruster     max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
139aa570207STao Xu     ms->numa_state->num_nodes++;
140ac057879SMarkus Armbruster }
141ac057879SMarkus Armbruster 
142aa570207STao Xu static
143aa570207STao Xu void parse_numa_distance(MachineState *ms, NumaDistOptions *dist, Error **errp)
144ac057879SMarkus Armbruster {
145ac057879SMarkus Armbruster     uint16_t src = dist->src;
146ac057879SMarkus Armbruster     uint16_t dst = dist->dst;
147ac057879SMarkus Armbruster     uint8_t val = dist->val;
148ac057879SMarkus Armbruster 
149ac057879SMarkus Armbruster     if (src >= MAX_NODES || dst >= MAX_NODES) {
150ac057879SMarkus Armbruster         error_setg(errp, "Parameter '%s' expects an integer between 0 and %d",
151ac057879SMarkus Armbruster                    src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1);
152ac057879SMarkus Armbruster         return;
153ac057879SMarkus Armbruster     }
154ac057879SMarkus Armbruster 
155ac057879SMarkus Armbruster     if (!numa_info[src].present || !numa_info[dst].present) {
156ac057879SMarkus Armbruster         error_setg(errp, "Source/Destination NUMA node is missing. "
157ac057879SMarkus Armbruster                    "Please use '-numa node' option to declare it first.");
158ac057879SMarkus Armbruster         return;
159ac057879SMarkus Armbruster     }
160ac057879SMarkus Armbruster 
161ac057879SMarkus Armbruster     if (val < NUMA_DISTANCE_MIN) {
162ac057879SMarkus Armbruster         error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
163ac057879SMarkus Armbruster                    "it shouldn't be less than %d.",
164ac057879SMarkus Armbruster                    val, NUMA_DISTANCE_MIN);
165ac057879SMarkus Armbruster         return;
166ac057879SMarkus Armbruster     }
167ac057879SMarkus Armbruster 
168ac057879SMarkus Armbruster     if (src == dst && val != NUMA_DISTANCE_MIN) {
169ac057879SMarkus Armbruster         error_setg(errp, "Local distance of node %d should be %d.",
170ac057879SMarkus Armbruster                    src, NUMA_DISTANCE_MIN);
171ac057879SMarkus Armbruster         return;
172ac057879SMarkus Armbruster     }
173ac057879SMarkus Armbruster 
174ac057879SMarkus Armbruster     numa_info[src].distance[dst] = val;
175*118154b7STao Xu     ms->numa_state->have_numa_distance = true;
176ac057879SMarkus Armbruster }
177ac057879SMarkus Armbruster 
178ac057879SMarkus Armbruster void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp)
179ac057879SMarkus Armbruster {
180ac057879SMarkus Armbruster     Error *err = NULL;
181aa570207STao Xu     MachineClass *mc = MACHINE_GET_CLASS(ms);
182aa570207STao Xu 
183aa570207STao Xu     if (!mc->numa_mem_supported) {
184aa570207STao Xu         error_setg(errp, "NUMA is not supported by this machine-type");
185aa570207STao Xu         goto end;
186aa570207STao Xu     }
187ac057879SMarkus Armbruster 
188ac057879SMarkus Armbruster     switch (object->type) {
189ac057879SMarkus Armbruster     case NUMA_OPTIONS_TYPE_NODE:
190ac057879SMarkus Armbruster         parse_numa_node(ms, &object->u.node, &err);
191ac057879SMarkus Armbruster         if (err) {
192ac057879SMarkus Armbruster             goto end;
193ac057879SMarkus Armbruster         }
194ac057879SMarkus Armbruster         break;
195ac057879SMarkus Armbruster     case NUMA_OPTIONS_TYPE_DIST:
196aa570207STao Xu         parse_numa_distance(ms, &object->u.dist, &err);
197ac057879SMarkus Armbruster         if (err) {
198ac057879SMarkus Armbruster             goto end;
199ac057879SMarkus Armbruster         }
200ac057879SMarkus Armbruster         break;
201ac057879SMarkus Armbruster     case NUMA_OPTIONS_TYPE_CPU:
202ac057879SMarkus Armbruster         if (!object->u.cpu.has_node_id) {
203ac057879SMarkus Armbruster             error_setg(&err, "Missing mandatory node-id property");
204ac057879SMarkus Armbruster             goto end;
205ac057879SMarkus Armbruster         }
206ac057879SMarkus Armbruster         if (!numa_info[object->u.cpu.node_id].present) {
207ac057879SMarkus Armbruster             error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
208ac057879SMarkus Armbruster                 "defined with -numa node,nodeid=ID before it's used with "
209ac057879SMarkus Armbruster                 "-numa cpu,node-id=ID", object->u.cpu.node_id);
210ac057879SMarkus Armbruster             goto end;
211ac057879SMarkus Armbruster         }
212ac057879SMarkus Armbruster 
213ac057879SMarkus Armbruster         machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
214ac057879SMarkus Armbruster                                   &err);
215ac057879SMarkus Armbruster         break;
216ac057879SMarkus Armbruster     default:
217ac057879SMarkus Armbruster         abort();
218ac057879SMarkus Armbruster     }
219ac057879SMarkus Armbruster 
220ac057879SMarkus Armbruster end:
221ac057879SMarkus Armbruster     error_propagate(errp, err);
222ac057879SMarkus Armbruster }
223ac057879SMarkus Armbruster 
224ac057879SMarkus Armbruster static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
225ac057879SMarkus Armbruster {
226ac057879SMarkus Armbruster     NumaOptions *object = NULL;
227ac057879SMarkus Armbruster     MachineState *ms = MACHINE(opaque);
228ac057879SMarkus Armbruster     Error *err = NULL;
229ac057879SMarkus Armbruster     Visitor *v = opts_visitor_new(opts);
230ac057879SMarkus Armbruster 
231ac057879SMarkus Armbruster     visit_type_NumaOptions(v, NULL, &object, &err);
232ac057879SMarkus Armbruster     visit_free(v);
233ac057879SMarkus Armbruster     if (err) {
234ac057879SMarkus Armbruster         goto end;
235ac057879SMarkus Armbruster     }
236ac057879SMarkus Armbruster 
237ac057879SMarkus Armbruster     /* Fix up legacy suffix-less format */
238ac057879SMarkus Armbruster     if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
239ac057879SMarkus Armbruster         const char *mem_str = qemu_opt_get(opts, "mem");
240ac057879SMarkus Armbruster         qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
241ac057879SMarkus Armbruster     }
242ac057879SMarkus Armbruster 
243ac057879SMarkus Armbruster     set_numa_options(ms, object, &err);
244ac057879SMarkus Armbruster 
245ac057879SMarkus Armbruster end:
246ac057879SMarkus Armbruster     qapi_free_NumaOptions(object);
247ac057879SMarkus Armbruster     if (err) {
248ac057879SMarkus Armbruster         error_propagate(errp, err);
249ac057879SMarkus Armbruster         return -1;
250ac057879SMarkus Armbruster     }
251ac057879SMarkus Armbruster 
252ac057879SMarkus Armbruster     return 0;
253ac057879SMarkus Armbruster }
254ac057879SMarkus Armbruster 
255ac057879SMarkus Armbruster /* If all node pair distances are symmetric, then only distances
256ac057879SMarkus Armbruster  * in one direction are enough. If there is even one asymmetric
257ac057879SMarkus Armbruster  * pair, though, then all distances must be provided. The
258ac057879SMarkus Armbruster  * distance from a node to itself is always NUMA_DISTANCE_MIN,
259ac057879SMarkus Armbruster  * so providing it is never necessary.
260ac057879SMarkus Armbruster  */
261aa570207STao Xu static void validate_numa_distance(MachineState *ms)
262ac057879SMarkus Armbruster {
263ac057879SMarkus Armbruster     int src, dst;
264ac057879SMarkus Armbruster     bool is_asymmetrical = false;
265aa570207STao Xu     int nb_numa_nodes = ms->numa_state->num_nodes;
266ac057879SMarkus Armbruster 
267ac057879SMarkus Armbruster     for (src = 0; src < nb_numa_nodes; src++) {
268ac057879SMarkus Armbruster         for (dst = src; dst < nb_numa_nodes; dst++) {
269ac057879SMarkus Armbruster             if (numa_info[src].distance[dst] == 0 &&
270ac057879SMarkus Armbruster                 numa_info[dst].distance[src] == 0) {
271ac057879SMarkus Armbruster                 if (src != dst) {
272ac057879SMarkus Armbruster                     error_report("The distance between node %d and %d is "
273ac057879SMarkus Armbruster                                  "missing, at least one distance value "
274ac057879SMarkus Armbruster                                  "between each nodes should be provided.",
275ac057879SMarkus Armbruster                                  src, dst);
276ac057879SMarkus Armbruster                     exit(EXIT_FAILURE);
277ac057879SMarkus Armbruster                 }
278ac057879SMarkus Armbruster             }
279ac057879SMarkus Armbruster 
280ac057879SMarkus Armbruster             if (numa_info[src].distance[dst] != 0 &&
281ac057879SMarkus Armbruster                 numa_info[dst].distance[src] != 0 &&
282ac057879SMarkus Armbruster                 numa_info[src].distance[dst] !=
283ac057879SMarkus Armbruster                 numa_info[dst].distance[src]) {
284ac057879SMarkus Armbruster                 is_asymmetrical = true;
285ac057879SMarkus Armbruster             }
286ac057879SMarkus Armbruster         }
287ac057879SMarkus Armbruster     }
288ac057879SMarkus Armbruster 
289ac057879SMarkus Armbruster     if (is_asymmetrical) {
290ac057879SMarkus Armbruster         for (src = 0; src < nb_numa_nodes; src++) {
291ac057879SMarkus Armbruster             for (dst = 0; dst < nb_numa_nodes; dst++) {
292ac057879SMarkus Armbruster                 if (src != dst && numa_info[src].distance[dst] == 0) {
293ac057879SMarkus Armbruster                     error_report("At least one asymmetrical pair of "
294ac057879SMarkus Armbruster                             "distances is given, please provide distances "
295ac057879SMarkus Armbruster                             "for both directions of all node pairs.");
296ac057879SMarkus Armbruster                     exit(EXIT_FAILURE);
297ac057879SMarkus Armbruster                 }
298ac057879SMarkus Armbruster             }
299ac057879SMarkus Armbruster         }
300ac057879SMarkus Armbruster     }
301ac057879SMarkus Armbruster }
302ac057879SMarkus Armbruster 
303aa570207STao Xu static void complete_init_numa_distance(MachineState *ms)
304ac057879SMarkus Armbruster {
305ac057879SMarkus Armbruster     int src, dst;
306ac057879SMarkus Armbruster 
307ac057879SMarkus Armbruster     /* Fixup NUMA distance by symmetric policy because if it is an
308ac057879SMarkus Armbruster      * asymmetric distance table, it should be a complete table and
309ac057879SMarkus Armbruster      * there would not be any missing distance except local node, which
310ac057879SMarkus Armbruster      * is verified by validate_numa_distance above.
311ac057879SMarkus Armbruster      */
312aa570207STao Xu     for (src = 0; src < ms->numa_state->num_nodes; src++) {
313aa570207STao Xu         for (dst = 0; dst < ms->numa_state->num_nodes; dst++) {
314ac057879SMarkus Armbruster             if (numa_info[src].distance[dst] == 0) {
315ac057879SMarkus Armbruster                 if (src == dst) {
316ac057879SMarkus Armbruster                     numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
317ac057879SMarkus Armbruster                 } else {
318ac057879SMarkus Armbruster                     numa_info[src].distance[dst] = numa_info[dst].distance[src];
319ac057879SMarkus Armbruster                 }
320ac057879SMarkus Armbruster             }
321ac057879SMarkus Armbruster         }
322ac057879SMarkus Armbruster     }
323ac057879SMarkus Armbruster }
324ac057879SMarkus Armbruster 
325ac057879SMarkus Armbruster void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
326ac057879SMarkus Armbruster                                  int nb_nodes, ram_addr_t size)
327ac057879SMarkus Armbruster {
328ac057879SMarkus Armbruster     int i;
329ac057879SMarkus Armbruster     uint64_t usedmem = 0;
330ac057879SMarkus Armbruster 
331ac057879SMarkus Armbruster     /* Align each node according to the alignment
332ac057879SMarkus Armbruster      * requirements of the machine class
333ac057879SMarkus Armbruster      */
334ac057879SMarkus Armbruster 
335ac057879SMarkus Armbruster     for (i = 0; i < nb_nodes - 1; i++) {
336ac057879SMarkus Armbruster         nodes[i].node_mem = (size / nb_nodes) &
337ac057879SMarkus Armbruster                             ~((1 << mc->numa_mem_align_shift) - 1);
338ac057879SMarkus Armbruster         usedmem += nodes[i].node_mem;
339ac057879SMarkus Armbruster     }
340ac057879SMarkus Armbruster     nodes[i].node_mem = size - usedmem;
341ac057879SMarkus Armbruster }
342ac057879SMarkus Armbruster 
343ac057879SMarkus Armbruster void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
344ac057879SMarkus Armbruster                                   int nb_nodes, ram_addr_t size)
345ac057879SMarkus Armbruster {
346ac057879SMarkus Armbruster     int i;
347ac057879SMarkus Armbruster     uint64_t usedmem = 0, node_mem;
348ac057879SMarkus Armbruster     uint64_t granularity = size / nb_nodes;
349ac057879SMarkus Armbruster     uint64_t propagate = 0;
350ac057879SMarkus Armbruster 
351ac057879SMarkus Armbruster     for (i = 0; i < nb_nodes - 1; i++) {
352ac057879SMarkus Armbruster         node_mem = (granularity + propagate) &
353ac057879SMarkus Armbruster                    ~((1 << mc->numa_mem_align_shift) - 1);
354ac057879SMarkus Armbruster         propagate = granularity + propagate - node_mem;
355ac057879SMarkus Armbruster         nodes[i].node_mem = node_mem;
356ac057879SMarkus Armbruster         usedmem += node_mem;
357ac057879SMarkus Armbruster     }
358ac057879SMarkus Armbruster     nodes[i].node_mem = size - usedmem;
359ac057879SMarkus Armbruster }
360ac057879SMarkus Armbruster 
361ac057879SMarkus Armbruster void numa_complete_configuration(MachineState *ms)
362ac057879SMarkus Armbruster {
363ac057879SMarkus Armbruster     int i;
364ac057879SMarkus Armbruster     MachineClass *mc = MACHINE_GET_CLASS(ms);
365ac057879SMarkus Armbruster 
366ac057879SMarkus Armbruster     /*
367ac057879SMarkus Armbruster      * If memory hotplug is enabled (slots > 0) but without '-numa'
368ac057879SMarkus Armbruster      * options explicitly on CLI, guestes will break.
369ac057879SMarkus Armbruster      *
370ac057879SMarkus Armbruster      *   Windows: won't enable memory hotplug without SRAT table at all
371ac057879SMarkus Armbruster      *
372ac057879SMarkus Armbruster      *   Linux: if QEMU is started with initial memory all below 4Gb
373ac057879SMarkus Armbruster      *   and no SRAT table present, guest kernel will use nommu DMA ops,
374ac057879SMarkus Armbruster      *   which breaks 32bit hw drivers when memory is hotplugged and
375ac057879SMarkus Armbruster      *   guest tries to use it with that drivers.
376ac057879SMarkus Armbruster      *
377ac057879SMarkus Armbruster      * Enable NUMA implicitly by adding a new NUMA node automatically.
378ac057879SMarkus Armbruster      */
379aa570207STao Xu     if (ms->ram_slots > 0 && ms->numa_state->num_nodes == 0 &&
380ac057879SMarkus Armbruster         mc->auto_enable_numa_with_memhp) {
381ac057879SMarkus Armbruster             NumaNodeOptions node = { };
382ac057879SMarkus Armbruster             parse_numa_node(ms, &node, &error_abort);
383ac057879SMarkus Armbruster     }
384ac057879SMarkus Armbruster 
385ac057879SMarkus Armbruster     assert(max_numa_nodeid <= MAX_NODES);
386ac057879SMarkus Armbruster 
387ac057879SMarkus Armbruster     /* No support for sparse NUMA node IDs yet: */
388ac057879SMarkus Armbruster     for (i = max_numa_nodeid - 1; i >= 0; i--) {
389ac057879SMarkus Armbruster         /* Report large node IDs first, to make mistakes easier to spot */
390ac057879SMarkus Armbruster         if (!numa_info[i].present) {
391ac057879SMarkus Armbruster             error_report("numa: Node ID missing: %d", i);
392ac057879SMarkus Armbruster             exit(1);
393ac057879SMarkus Armbruster         }
394ac057879SMarkus Armbruster     }
395ac057879SMarkus Armbruster 
396ac057879SMarkus Armbruster     /* This must be always true if all nodes are present: */
397aa570207STao Xu     assert(ms->numa_state->num_nodes == max_numa_nodeid);
398ac057879SMarkus Armbruster 
399aa570207STao Xu     if (ms->numa_state->num_nodes > 0) {
400ac057879SMarkus Armbruster         uint64_t numa_total;
401ac057879SMarkus Armbruster 
402aa570207STao Xu         if (ms->numa_state->num_nodes > MAX_NODES) {
403aa570207STao Xu             ms->numa_state->num_nodes = MAX_NODES;
404ac057879SMarkus Armbruster         }
405ac057879SMarkus Armbruster 
406ac057879SMarkus Armbruster         /* If no memory size is given for any node, assume the default case
407ac057879SMarkus Armbruster          * and distribute the available memory equally across all nodes
408ac057879SMarkus Armbruster          */
409aa570207STao Xu         for (i = 0; i < ms->numa_state->num_nodes; i++) {
410ac057879SMarkus Armbruster             if (numa_info[i].node_mem != 0) {
411ac057879SMarkus Armbruster                 break;
412ac057879SMarkus Armbruster             }
413ac057879SMarkus Armbruster         }
414aa570207STao Xu         if (i == ms->numa_state->num_nodes) {
415ac057879SMarkus Armbruster             assert(mc->numa_auto_assign_ram);
416aa570207STao Xu             mc->numa_auto_assign_ram(mc, numa_info,
417aa570207STao Xu                                      ms->numa_state->num_nodes, ram_size);
418f8123f22SEduardo Habkost             if (!qtest_enabled()) {
4194bb4a273SIgor Mammedov                 warn_report("Default splitting of RAM between nodes is deprecated,"
4204bb4a273SIgor Mammedov                             " Use '-numa node,memdev' to explictly define RAM"
4214bb4a273SIgor Mammedov                             " allocation per node");
422ac057879SMarkus Armbruster             }
423f8123f22SEduardo Habkost         }
424ac057879SMarkus Armbruster 
425ac057879SMarkus Armbruster         numa_total = 0;
426aa570207STao Xu         for (i = 0; i < ms->numa_state->num_nodes; i++) {
427ac057879SMarkus Armbruster             numa_total += numa_info[i].node_mem;
428ac057879SMarkus Armbruster         }
429ac057879SMarkus Armbruster         if (numa_total != ram_size) {
430ac057879SMarkus Armbruster             error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
431ac057879SMarkus Armbruster                          " should equal RAM size (0x" RAM_ADDR_FMT ")",
432ac057879SMarkus Armbruster                          numa_total, ram_size);
433ac057879SMarkus Armbruster             exit(1);
434ac057879SMarkus Armbruster         }
435ac057879SMarkus Armbruster 
436ac057879SMarkus Armbruster         /* QEMU needs at least all unique node pair distances to build
437ac057879SMarkus Armbruster          * the whole NUMA distance table. QEMU treats the distance table
438ac057879SMarkus Armbruster          * as symmetric by default, i.e. distance A->B == distance B->A.
439ac057879SMarkus Armbruster          * Thus, QEMU is able to complete the distance table
440ac057879SMarkus Armbruster          * initialization even though only distance A->B is provided and
441ac057879SMarkus Armbruster          * distance B->A is not. QEMU knows the distance of a node to
442ac057879SMarkus Armbruster          * itself is always 10, so A->A distances may be omitted. When
443ac057879SMarkus Armbruster          * the distances of two nodes of a pair differ, i.e. distance
444ac057879SMarkus Armbruster          * A->B != distance B->A, then that means the distance table is
445ac057879SMarkus Armbruster          * asymmetric. In this case, the distances for both directions
446ac057879SMarkus Armbruster          * of all node pairs are required.
447ac057879SMarkus Armbruster          */
448*118154b7STao Xu         if (ms->numa_state->have_numa_distance) {
449ac057879SMarkus Armbruster             /* Validate enough NUMA distance information was provided. */
450aa570207STao Xu             validate_numa_distance(ms);
451ac057879SMarkus Armbruster 
452ac057879SMarkus Armbruster             /* Validation succeeded, now fill in any missing distances. */
453aa570207STao Xu             complete_init_numa_distance(ms);
454ac057879SMarkus Armbruster         }
455ac057879SMarkus Armbruster     }
456ac057879SMarkus Armbruster }
457ac057879SMarkus Armbruster 
458ac057879SMarkus Armbruster void parse_numa_opts(MachineState *ms)
459ac057879SMarkus Armbruster {
460ac057879SMarkus Armbruster     qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal);
461ac057879SMarkus Armbruster }
462ac057879SMarkus Armbruster 
463ac057879SMarkus Armbruster void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
464ac057879SMarkus Armbruster {
465ac057879SMarkus Armbruster     int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
466ac057879SMarkus Armbruster 
467ac057879SMarkus Armbruster     if (node_id == CPU_UNSET_NUMA_NODE_ID) {
468ac057879SMarkus Armbruster         /* due to bug in libvirt, it doesn't pass node-id from props on
469ac057879SMarkus Armbruster          * device_add as expected, so we have to fix it up here */
470ac057879SMarkus Armbruster         if (slot->props.has_node_id) {
471ac057879SMarkus Armbruster             object_property_set_int(OBJECT(dev), slot->props.node_id,
472ac057879SMarkus Armbruster                                     "node-id", errp);
473ac057879SMarkus Armbruster         }
474ac057879SMarkus Armbruster     } else if (node_id != slot->props.node_id) {
475ac057879SMarkus Armbruster         error_setg(errp, "invalid node-id, must be %"PRId64,
476ac057879SMarkus Armbruster                    slot->props.node_id);
477ac057879SMarkus Armbruster     }
478ac057879SMarkus Armbruster }
479ac057879SMarkus Armbruster 
480ac057879SMarkus Armbruster static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
481ac057879SMarkus Armbruster                                            const char *name,
482ac057879SMarkus Armbruster                                            uint64_t ram_size)
483ac057879SMarkus Armbruster {
484ac057879SMarkus Armbruster     if (mem_path) {
485ac057879SMarkus Armbruster #ifdef __linux__
486ac057879SMarkus Armbruster         Error *err = NULL;
487ac057879SMarkus Armbruster         memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, 0,
488ac057879SMarkus Armbruster                                          mem_path, &err);
489ac057879SMarkus Armbruster         if (err) {
490ac057879SMarkus Armbruster             error_report_err(err);
491ac057879SMarkus Armbruster             if (mem_prealloc) {
492ac057879SMarkus Armbruster                 exit(1);
493ac057879SMarkus Armbruster             }
494cb79224bSIgor Mammedov             warn_report("falling back to regular RAM allocation");
495cb79224bSIgor Mammedov             error_printf("This is deprecated. Make sure that -mem-path "
496cb79224bSIgor Mammedov                          " specified path has sufficient resources to allocate"
497cb79224bSIgor Mammedov                          " -m specified RAM amount");
498ac057879SMarkus Armbruster             /* Legacy behavior: if allocation failed, fall back to
499ac057879SMarkus Armbruster              * regular RAM allocation.
500ac057879SMarkus Armbruster              */
501ac057879SMarkus Armbruster             mem_path = NULL;
502ac057879SMarkus Armbruster             memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
503ac057879SMarkus Armbruster         }
504ac057879SMarkus Armbruster #else
505ac057879SMarkus Armbruster         fprintf(stderr, "-mem-path not supported on this host\n");
506ac057879SMarkus Armbruster         exit(1);
507ac057879SMarkus Armbruster #endif
508ac057879SMarkus Armbruster     } else {
509ac057879SMarkus Armbruster         memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
510ac057879SMarkus Armbruster     }
511ac057879SMarkus Armbruster     vmstate_register_ram_global(mr);
512ac057879SMarkus Armbruster }
513ac057879SMarkus Armbruster 
514ac057879SMarkus Armbruster void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
515ac057879SMarkus Armbruster                                           const char *name,
516ac057879SMarkus Armbruster                                           uint64_t ram_size)
517ac057879SMarkus Armbruster {
518ac057879SMarkus Armbruster     uint64_t addr = 0;
519ac057879SMarkus Armbruster     int i;
520aa570207STao Xu     MachineState *ms = MACHINE(qdev_get_machine());
521ac057879SMarkus Armbruster 
522aa570207STao Xu     if (ms->numa_state == NULL ||
523aa570207STao Xu         ms->numa_state->num_nodes == 0 || !have_memdevs) {
524ac057879SMarkus Armbruster         allocate_system_memory_nonnuma(mr, owner, name, ram_size);
525ac057879SMarkus Armbruster         return;
526ac057879SMarkus Armbruster     }
527ac057879SMarkus Armbruster 
528ac057879SMarkus Armbruster     memory_region_init(mr, owner, name, ram_size);
529aa570207STao Xu     for (i = 0; i < ms->numa_state->num_nodes; i++) {
530ac057879SMarkus Armbruster         uint64_t size = numa_info[i].node_mem;
531ac057879SMarkus Armbruster         HostMemoryBackend *backend = numa_info[i].node_memdev;
532ac057879SMarkus Armbruster         if (!backend) {
533ac057879SMarkus Armbruster             continue;
534ac057879SMarkus Armbruster         }
535ac057879SMarkus Armbruster         MemoryRegion *seg = host_memory_backend_get_memory(backend);
536ac057879SMarkus Armbruster 
537ac057879SMarkus Armbruster         if (memory_region_is_mapped(seg)) {
538ac057879SMarkus Armbruster             char *path = object_get_canonical_path_component(OBJECT(backend));
539ac057879SMarkus Armbruster             error_report("memory backend %s is used multiple times. Each "
540ac057879SMarkus Armbruster                          "-numa option must use a different memdev value.",
541ac057879SMarkus Armbruster                          path);
542ac057879SMarkus Armbruster             g_free(path);
543ac057879SMarkus Armbruster             exit(1);
544ac057879SMarkus Armbruster         }
545ac057879SMarkus Armbruster 
546ac057879SMarkus Armbruster         host_memory_backend_set_mapped(backend, true);
547ac057879SMarkus Armbruster         memory_region_add_subregion(mr, addr, seg);
548ac057879SMarkus Armbruster         vmstate_register_ram_global(seg);
549ac057879SMarkus Armbruster         addr += size;
550ac057879SMarkus Armbruster     }
551ac057879SMarkus Armbruster }
552ac057879SMarkus Armbruster 
553ac057879SMarkus Armbruster static void numa_stat_memory_devices(NumaNodeMem node_mem[])
554ac057879SMarkus Armbruster {
555ac057879SMarkus Armbruster     MemoryDeviceInfoList *info_list = qmp_memory_device_list();
556ac057879SMarkus Armbruster     MemoryDeviceInfoList *info;
557ac057879SMarkus Armbruster     PCDIMMDeviceInfo     *pcdimm_info;
558c35d17caSPeter Maydell     VirtioPMEMDeviceInfo *vpi;
559ac057879SMarkus Armbruster 
560ac057879SMarkus Armbruster     for (info = info_list; info; info = info->next) {
561ac057879SMarkus Armbruster         MemoryDeviceInfo *value = info->value;
562ac057879SMarkus Armbruster 
563ac057879SMarkus Armbruster         if (value) {
564ac057879SMarkus Armbruster             switch (value->type) {
565ac057879SMarkus Armbruster             case MEMORY_DEVICE_INFO_KIND_DIMM:
566ac057879SMarkus Armbruster             case MEMORY_DEVICE_INFO_KIND_NVDIMM:
567c35d17caSPeter Maydell                 pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ?
568c35d17caSPeter Maydell                               value->u.dimm.data : value->u.nvdimm.data;
569ac057879SMarkus Armbruster                 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
570ac057879SMarkus Armbruster                 node_mem[pcdimm_info->node].node_plugged_mem +=
571ac057879SMarkus Armbruster                     pcdimm_info->size;
572c35d17caSPeter Maydell                 break;
573c35d17caSPeter Maydell             case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM:
574c35d17caSPeter Maydell                 vpi = value->u.virtio_pmem.data;
575c35d17caSPeter Maydell                 /* TODO: once we support numa, assign to right node */
576c35d17caSPeter Maydell                 node_mem[0].node_mem += vpi->size;
577c35d17caSPeter Maydell                 node_mem[0].node_plugged_mem += vpi->size;
578c35d17caSPeter Maydell                 break;
579c35d17caSPeter Maydell             default:
580c35d17caSPeter Maydell                 g_assert_not_reached();
581ac057879SMarkus Armbruster             }
582ac057879SMarkus Armbruster         }
583ac057879SMarkus Armbruster     }
584ac057879SMarkus Armbruster     qapi_free_MemoryDeviceInfoList(info_list);
585ac057879SMarkus Armbruster }
586ac057879SMarkus Armbruster 
587aa570207STao Xu void query_numa_node_mem(NumaNodeMem node_mem[], MachineState *ms)
588ac057879SMarkus Armbruster {
589ac057879SMarkus Armbruster     int i;
590ac057879SMarkus Armbruster 
591aa570207STao Xu     if (ms->numa_state == NULL || ms->numa_state->num_nodes <= 0) {
592ac057879SMarkus Armbruster         return;
593ac057879SMarkus Armbruster     }
594ac057879SMarkus Armbruster 
595ac057879SMarkus Armbruster     numa_stat_memory_devices(node_mem);
596aa570207STao Xu     for (i = 0; i < ms->numa_state->num_nodes; i++) {
597ac057879SMarkus Armbruster         node_mem[i].node_mem += numa_info[i].node_mem;
598ac057879SMarkus Armbruster     }
599ac057879SMarkus Armbruster }
600ac057879SMarkus Armbruster 
601ac057879SMarkus Armbruster void ram_block_notifier_add(RAMBlockNotifier *n)
602ac057879SMarkus Armbruster {
603ac057879SMarkus Armbruster     QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
604ac057879SMarkus Armbruster }
605ac057879SMarkus Armbruster 
606ac057879SMarkus Armbruster void ram_block_notifier_remove(RAMBlockNotifier *n)
607ac057879SMarkus Armbruster {
608ac057879SMarkus Armbruster     QLIST_REMOVE(n, next);
609ac057879SMarkus Armbruster }
610ac057879SMarkus Armbruster 
611ac057879SMarkus Armbruster void ram_block_notify_add(void *host, size_t size)
612ac057879SMarkus Armbruster {
613ac057879SMarkus Armbruster     RAMBlockNotifier *notifier;
614ac057879SMarkus Armbruster 
615ac057879SMarkus Armbruster     QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
616ac057879SMarkus Armbruster         notifier->ram_block_added(notifier, host, size);
617ac057879SMarkus Armbruster     }
618ac057879SMarkus Armbruster }
619ac057879SMarkus Armbruster 
620ac057879SMarkus Armbruster void ram_block_notify_remove(void *host, size_t size)
621ac057879SMarkus Armbruster {
622ac057879SMarkus Armbruster     RAMBlockNotifier *notifier;
623ac057879SMarkus Armbruster 
624ac057879SMarkus Armbruster     QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
625ac057879SMarkus Armbruster         notifier->ram_block_removed(notifier, host, size);
626ac057879SMarkus Armbruster     }
627ac057879SMarkus Armbruster }
628