blob: 84ce2af9b475dada309f72e419dcd8eff4559dd2 [file] [log] [blame]
Wanlong Gao96d0e262014-05-14 17:43:05 +08001/*
2 * NUMA parameter parsing routines
3 *
4 * Copyright (c) 2014 Fujitsu Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020026#include "sysemu/numa.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080027#include "exec/cpu-common.h"
Paolo Bonzini0987d732016-12-21 00:31:36 +080028#include "exec/ramlist.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080029#include "qemu/bitmap.h"
30#include "qom/cpu.h"
Wanlong Gao2b631ec2014-05-14 17:43:06 +080031#include "qemu/error-report.h"
32#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
Wanlong Gao00421092014-05-14 17:43:08 +080033#include "qapi-visit.h"
34#include "qapi/opts-visitor.h"
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +080035#include "hw/boards.h"
Paolo Bonzini7febe362014-05-14 17:43:17 +080036#include "sysemu/hostmem.h"
Hu Tao76b5d852014-06-16 18:05:41 +080037#include "qmp-commands.h"
zhanghailiang5b009e42014-11-04 19:49:30 +080038#include "hw/mem/pc-dimm.h"
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -020039#include "qemu/option.h"
40#include "qemu/config-file.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080041
Wanlong Gao00421092014-05-14 17:43:08 +080042QemuOptsList qemu_numa_opts = {
43 .name = "numa",
44 .implied_opt_name = "type",
45 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
46 .desc = { { 0 } } /* validated with OptsVisitor */
47};
48
Paolo Bonzini7febe362014-05-14 17:43:17 +080049static int have_memdevs = -1;
Eduardo Habkost25712ff2015-02-08 16:51:19 -020050static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
51 * For all nodes, nodeid < max_numa_nodeid
52 */
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020053int nb_numa_nodes;
He Chen0f203432017-04-27 10:35:58 +080054bool have_numa_distance;
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020055NodeInfo numa_info[MAX_NODES];
Paolo Bonzini7febe362014-05-14 17:43:17 +080056
Bharata B Raofa9ea812015-06-29 13:50:25 +053057void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
58{
Bharata B Rao672558d2015-07-09 20:57:36 +053059 struct numa_addr_range *range;
Bharata B Raofa9ea812015-06-29 13:50:25 +053060
Bharata B Raoabafabd2015-06-29 13:50:26 +053061 /*
62 * Memory-less nodes can come here with 0 size in which case,
63 * there is nothing to do.
64 */
65 if (!size) {
66 return;
67 }
68
Bharata B Rao672558d2015-07-09 20:57:36 +053069 range = g_malloc0(sizeof(*range));
Bharata B Raofa9ea812015-06-29 13:50:25 +053070 range->mem_start = addr;
71 range->mem_end = addr + size - 1;
72 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
73}
74
75void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
76{
77 struct numa_addr_range *range, *next;
78
79 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
80 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
81 QLIST_REMOVE(range, entry);
82 g_free(range);
83 return;
84 }
85 }
86}
87
Bharata B Raoabafabd2015-06-29 13:50:26 +053088static void numa_set_mem_ranges(void)
89{
90 int i;
91 ram_addr_t mem_start = 0;
92
93 /*
94 * Deduce start address of each node and use it to store
95 * the address range info in numa_info address range list
96 */
97 for (i = 0; i < nb_numa_nodes; i++) {
98 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
99 mem_start += numa_info[i].node_mem;
100 }
101}
102
Bharata B Raoe75e2a12015-06-29 13:50:27 +0530103/*
104 * Check if @addr falls under NUMA @node.
105 */
106static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
107{
108 struct numa_addr_range *range;
109
110 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
111 if (addr >= range->mem_start && addr <= range->mem_end) {
112 return true;
113 }
114 }
115 return false;
116}
117
118/*
119 * Given an address, return the index of the NUMA node to which the
120 * address belongs to.
121 */
122uint32_t numa_get_node(ram_addr_t addr, Error **errp)
123{
124 uint32_t i;
125
126 /* For non NUMA configurations, check if the addr falls under node 0 */
127 if (!nb_numa_nodes) {
128 if (numa_addr_belongs_to_node(addr, 0)) {
129 return 0;
130 }
131 }
132
133 for (i = 0; i < nb_numa_nodes; i++) {
134 if (numa_addr_belongs_to_node(addr, i)) {
135 return i;
136 }
137 }
138
139 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
140 "NUMA node", addr);
141 return -1;
142}
143
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200144static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
145 QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800146{
Wanlong Gao00421092014-05-14 17:43:08 +0800147 uint16_t nodenr;
148 uint16List *cpus = NULL;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200149 MachineClass *mc = MACHINE_GET_CLASS(ms);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800150
Wanlong Gao00421092014-05-14 17:43:08 +0800151 if (node->has_nodeid) {
152 nodenr = node->nodeid;
153 } else {
154 nodenr = nb_numa_nodes;
155 }
156
157 if (nodenr >= MAX_NODES) {
158 error_setg(errp, "Max number of NUMA nodes reached: %"
Gonglei01bbbcf2015-02-25 12:22:30 +0800159 PRIu16 "", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800160 return;
161 }
162
Eduardo Habkost1945b9d2014-06-26 18:33:19 -0300163 if (numa_info[nodenr].present) {
164 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
165 return;
166 }
167
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200168 if (!mc->cpu_index_to_instance_props) {
169 error_report("NUMA is not supported by this machine-type");
170 exit(1);
171 }
Wanlong Gao00421092014-05-14 17:43:08 +0800172 for (cpus = node->cpus; cpus; cpus = cpus->next) {
Igor Mammedov7c88e652017-05-10 13:29:50 +0200173 CpuInstanceProperties props;
Eduardo Habkost8979c942015-02-09 17:28:52 -0200174 if (cpus->value >= max_cpus) {
175 error_setg(errp,
176 "CPU index (%" PRIu16 ")"
177 " should be smaller than maxcpus (%d)",
178 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +0800179 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800180 }
Igor Mammedov7c88e652017-05-10 13:29:50 +0200181 props = mc->cpu_index_to_instance_props(ms, cpus->value);
182 props.node_id = nodenr;
183 props.has_node_id = true;
184 machine_set_cpu_numa_node(ms, &props, &error_fatal);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800185 }
186
Paolo Bonzini7febe362014-05-14 17:43:17 +0800187 if (node->has_mem && node->has_memdev) {
Gonglei01bbbcf2015-02-25 12:22:30 +0800188 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800189 return;
190 }
191
192 if (have_memdevs == -1) {
193 have_memdevs = node->has_memdev;
194 }
195 if (node->has_memdev != have_memdevs) {
196 error_setg(errp, "qemu: memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800197 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800198 return;
199 }
200
Wanlong Gao00421092014-05-14 17:43:08 +0800201 if (node->has_mem) {
202 uint64_t mem_size = node->mem;
203 const char *mem_str = qemu_opt_get(opts, "mem");
204 /* Fix up legacy suffix-less format */
205 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
206 mem_size <<= 20;
207 }
208 numa_info[nodenr].node_mem = mem_size;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800209 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800210 if (node->has_memdev) {
211 Object *o;
212 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
213 if (!o) {
214 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
215 return;
216 }
217
218 object_ref(o);
219 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
220 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
221 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300222 numa_info[nodenr].present = true;
223 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800224}
225
He Chen0f203432017-04-27 10:35:58 +0800226static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
227{
228 uint16_t src = dist->src;
229 uint16_t dst = dist->dst;
230 uint8_t val = dist->val;
231
232 if (src >= MAX_NODES || dst >= MAX_NODES) {
233 error_setg(errp,
234 "Invalid node %" PRIu16
235 ", max possible could be %" PRIu16,
236 MAX(src, dst), MAX_NODES);
237 return;
238 }
239
240 if (!numa_info[src].present || !numa_info[dst].present) {
241 error_setg(errp, "Source/Destination NUMA node is missing. "
242 "Please use '-numa node' option to declare it first.");
243 return;
244 }
245
246 if (val < NUMA_DISTANCE_MIN) {
247 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
248 "it shouldn't be less than %d.",
249 val, NUMA_DISTANCE_MIN);
250 return;
251 }
252
253 if (src == dst && val != NUMA_DISTANCE_MIN) {
254 error_setg(errp, "Local distance of node %d should be %d.",
255 src, NUMA_DISTANCE_MIN);
256 return;
257 }
258
259 numa_info[src].distance[dst] = val;
260 have_numa_distance = true;
261}
262
Markus Armbruster28d0de72015-03-13 13:35:14 +0100263static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800264{
Wanlong Gao00421092014-05-14 17:43:08 +0800265 NumaOptions *object = NULL;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200266 MachineState *ms = opaque;
Wanlong Gao00421092014-05-14 17:43:08 +0800267 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800268
Wanlong Gao00421092014-05-14 17:43:08 +0800269 {
Eric Blake09204ea2016-06-09 10:48:36 -0600270 Visitor *v = opts_visitor_new(opts);
271 visit_type_NumaOptions(v, NULL, &object, &err);
272 visit_free(v);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800273 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800274
Wanlong Gao00421092014-05-14 17:43:08 +0800275 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200276 goto end;
Wanlong Gao00421092014-05-14 17:43:08 +0800277 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800278
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600279 switch (object->type) {
Markus Armbrusterd081a492017-02-21 21:46:26 +0100280 case NUMA_OPTIONS_TYPE_NODE:
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200281 parse_numa_node(ms, &object->u.node, opts, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800282 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200283 goto end;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800284 }
285 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800286 break;
He Chen0f203432017-04-27 10:35:58 +0800287 case NUMA_OPTIONS_TYPE_DIST:
288 parse_numa_distance(&object->u.dist, &err);
289 if (err) {
290 goto end;
291 }
292 break;
Igor Mammedov419fcde2017-05-10 13:30:01 +0200293 case NUMA_OPTIONS_TYPE_CPU:
294 if (!object->u.cpu.has_node_id) {
295 error_setg(&err, "Missing mandatory node-id property");
296 goto end;
297 }
298 if (!numa_info[object->u.cpu.node_id].present) {
299 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
300 "defined with -numa node,nodeid=ID before it's used with "
301 "-numa cpu,node-id=ID", object->u.cpu.node_id);
302 goto end;
303 }
304
305 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
306 &err);
307 break;
Wanlong Gao00421092014-05-14 17:43:08 +0800308 default:
309 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800310 }
Wanlong Gao00421092014-05-14 17:43:08 +0800311
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200312end:
Eric Blake96a16162016-02-23 14:14:33 -0700313 qapi_free_NumaOptions(object);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200314 if (err) {
315 error_report_err(err);
316 return -1;
317 }
Wanlong Gao00421092014-05-14 17:43:08 +0800318
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200319 return 0;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800320}
321
He Chen0f203432017-04-27 10:35:58 +0800322/* If all node pair distances are symmetric, then only distances
323 * in one direction are enough. If there is even one asymmetric
324 * pair, though, then all distances must be provided. The
325 * distance from a node to itself is always NUMA_DISTANCE_MIN,
326 * so providing it is never necessary.
327 */
328static void validate_numa_distance(void)
329{
330 int src, dst;
331 bool is_asymmetrical = false;
332
333 for (src = 0; src < nb_numa_nodes; src++) {
334 for (dst = src; dst < nb_numa_nodes; dst++) {
335 if (numa_info[src].distance[dst] == 0 &&
336 numa_info[dst].distance[src] == 0) {
337 if (src != dst) {
338 error_report("The distance between node %d and %d is "
339 "missing, at least one distance value "
340 "between each nodes should be provided.",
341 src, dst);
342 exit(EXIT_FAILURE);
343 }
344 }
345
346 if (numa_info[src].distance[dst] != 0 &&
347 numa_info[dst].distance[src] != 0 &&
348 numa_info[src].distance[dst] !=
349 numa_info[dst].distance[src]) {
350 is_asymmetrical = true;
351 }
352 }
353 }
354
355 if (is_asymmetrical) {
356 for (src = 0; src < nb_numa_nodes; src++) {
357 for (dst = 0; dst < nb_numa_nodes; dst++) {
358 if (src != dst && numa_info[src].distance[dst] == 0) {
359 error_report("At least one asymmetrical pair of "
360 "distances is given, please provide distances "
361 "for both directions of all node pairs.");
362 exit(EXIT_FAILURE);
363 }
364 }
365 }
366 }
367}
368
369static void complete_init_numa_distance(void)
370{
371 int src, dst;
372
373 /* Fixup NUMA distance by symmetric policy because if it is an
374 * asymmetric distance table, it should be a complete table and
375 * there would not be any missing distance except local node, which
376 * is verified by validate_numa_distance above.
377 */
378 for (src = 0; src < nb_numa_nodes; src++) {
379 for (dst = 0; dst < nb_numa_nodes; dst++) {
380 if (numa_info[src].distance[dst] == 0) {
381 if (src == dst) {
382 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
383 } else {
384 numa_info[src].distance[dst] = numa_info[dst].distance[src];
385 }
386 }
387 }
388 }
389}
390
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200391void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
392 int nb_nodes, ram_addr_t size)
393{
394 int i;
395 uint64_t usedmem = 0;
396
397 /* Align each node according to the alignment
398 * requirements of the machine class
399 */
400
401 for (i = 0; i < nb_nodes - 1; i++) {
402 nodes[i].node_mem = (size / nb_nodes) &
403 ~((1 << mc->numa_mem_align_shift) - 1);
404 usedmem += nodes[i].node_mem;
405 }
406 nodes[i].node_mem = size - usedmem;
407}
408
409void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
410 int nb_nodes, ram_addr_t size)
411{
412 int i;
413 uint64_t usedmem = 0, node_mem;
414 uint64_t granularity = size / nb_nodes;
415 uint64_t propagate = 0;
416
417 for (i = 0; i < nb_nodes - 1; i++) {
418 node_mem = (granularity + propagate) &
419 ~((1 << mc->numa_mem_align_shift) - 1);
420 propagate = granularity + propagate - node_mem;
421 nodes[i].node_mem = node_mem;
422 usedmem += node_mem;
423 }
424 nodes[i].node_mem = size - usedmem;
425}
426
Igor Mammedovea089ee2017-05-10 13:29:45 +0200427void parse_numa_opts(MachineState *ms)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800428{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300429 int i;
Igor Mammedovaf9b20e2017-05-10 13:29:51 +0200430 const CPUArchIdList *possible_cpus;
Igor Mammedovea089ee2017-05-10 13:29:45 +0200431 MachineClass *mc = MACHINE_GET_CLASS(ms);
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300432
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200433 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200434 exit(1);
435 }
436
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300437 assert(max_numa_nodeid <= MAX_NODES);
438
439 /* No support for sparse NUMA node IDs yet: */
440 for (i = max_numa_nodeid - 1; i >= 0; i--) {
441 /* Report large node IDs first, to make mistakes easier to spot */
442 if (!numa_info[i].present) {
443 error_report("numa: Node ID missing: %d", i);
444 exit(1);
445 }
446 }
447
448 /* This must be always true if all nodes are present: */
449 assert(nb_numa_nodes == max_numa_nodeid);
450
Wanlong Gao96d0e262014-05-14 17:43:05 +0800451 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800452 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800453
454 if (nb_numa_nodes > MAX_NODES) {
455 nb_numa_nodes = MAX_NODES;
456 }
457
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300458 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800459 * and distribute the available memory equally across all nodes
460 */
461 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800462 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800463 break;
464 }
465 }
466 if (i == nb_numa_nodes) {
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200467 assert(mc->numa_auto_assign_ram);
468 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800469 }
470
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800471 numa_total = 0;
472 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800473 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800474 }
475 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800476 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
477 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800478 numa_total, ram_size);
479 exit(1);
480 }
481
Wanlong Gao96d0e262014-05-14 17:43:05 +0800482 for (i = 0; i < nb_numa_nodes; i++) {
Bharata B Raofa9ea812015-06-29 13:50:25 +0530483 QLIST_INIT(&numa_info[i].addr);
484 }
485
Bharata B Raoabafabd2015-06-29 13:50:26 +0530486 numa_set_mem_ranges();
487
Igor Mammedovaf9b20e2017-05-10 13:29:51 +0200488 /* assign CPUs to nodes using board provided default mapping */
489 if (!mc->cpu_index_to_instance_props || !mc->possible_cpu_arch_ids) {
490 error_report("default CPUs to NUMA node mapping isn't supported");
491 exit(1);
492 }
493
494 possible_cpus = mc->possible_cpu_arch_ids(ms);
495 for (i = 0; i < possible_cpus->len; i++) {
496 if (possible_cpus->cpus[i].props.has_node_id) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800497 break;
498 }
499 }
Igor Mammedovea089ee2017-05-10 13:29:45 +0200500
Igor Mammedovaf9b20e2017-05-10 13:29:51 +0200501 /* no CPUs are assigned to NUMA nodes */
502 if (i == possible_cpus->len) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800503 for (i = 0; i < max_cpus; i++) {
Igor Mammedovea089ee2017-05-10 13:29:45 +0200504 CpuInstanceProperties props;
Igor Mammedov7c88e652017-05-10 13:29:50 +0200505 /* fetch default mapping from board and enable it */
Igor Mammedovea089ee2017-05-10 13:29:45 +0200506 props = mc->cpu_index_to_instance_props(ms, i);
Igor Mammedov7c88e652017-05-10 13:29:50 +0200507 props.has_node_id = true;
Igor Mammedov57924bc2015-03-19 17:09:21 +0000508
Igor Mammedov7c88e652017-05-10 13:29:50 +0200509 machine_set_cpu_numa_node(ms, &props, &error_fatal);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800510 }
511 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200512
He Chen0f203432017-04-27 10:35:58 +0800513 /* QEMU needs at least all unique node pair distances to build
514 * the whole NUMA distance table. QEMU treats the distance table
515 * as symmetric by default, i.e. distance A->B == distance B->A.
516 * Thus, QEMU is able to complete the distance table
517 * initialization even though only distance A->B is provided and
518 * distance B->A is not. QEMU knows the distance of a node to
519 * itself is always 10, so A->A distances may be omitted. When
520 * the distances of two nodes of a pair differ, i.e. distance
521 * A->B != distance B->A, then that means the distance table is
522 * asymmetric. In this case, the distances for both directions
523 * of all node pairs are required.
524 */
525 if (have_numa_distance) {
526 /* Validate enough NUMA distance information was provided. */
527 validate_numa_distance();
528
529 /* Validation succeeded, now fill in any missing distances. */
530 complete_init_numa_distance();
531 }
Bharata B Raoabafabd2015-06-29 13:50:26 +0530532 } else {
533 numa_set_mem_node_id(0, ram_size, 0);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800534 }
535}
536
Paolo Bonzini7febe362014-05-14 17:43:17 +0800537static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
538 const char *name,
539 uint64_t ram_size)
540{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800541 if (mem_path) {
542#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800543 Error *err = NULL;
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800544 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800545 mem_path, &err);
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200546 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100547 error_report_err(err);
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500548 if (mem_prealloc) {
549 exit(1);
550 }
551
552 /* Legacy behavior: if allocation failed, fall back to
553 * regular RAM allocation.
554 */
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200555 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800556 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800557#else
558 fprintf(stderr, "-mem-path not supported on this host\n");
559 exit(1);
560#endif
561 } else {
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200562 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800563 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800564 vmstate_register_ram_global(mr);
565}
566
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800567void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
568 const char *name,
569 uint64_t ram_size)
570{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800571 uint64_t addr = 0;
572 int i;
573
574 if (nb_numa_nodes == 0 || !have_memdevs) {
575 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
576 return;
577 }
578
579 memory_region_init(mr, owner, name, ram_size);
580 for (i = 0; i < MAX_NODES; i++) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800581 uint64_t size = numa_info[i].node_mem;
582 HostMemoryBackend *backend = numa_info[i].node_memdev;
583 if (!backend) {
584 continue;
585 }
Markus Armbruster007b0652015-09-11 15:04:45 +0200586 MemoryRegion *seg = host_memory_backend_get_memory(backend,
587 &error_fatal);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800588
Hu Tao0462fae2014-06-30 18:28:15 +0800589 if (memory_region_is_mapped(seg)) {
590 char *path = object_get_canonical_path_component(OBJECT(backend));
591 error_report("memory backend %s is used multiple times. Each "
592 "-numa option must use a different memdev value.",
593 path);
594 exit(1);
595 }
596
Greg Kurz0b217572016-07-19 10:28:35 +0200597 host_memory_backend_set_mapped(backend, true);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800598 memory_region_add_subregion(mr, addr, seg);
599 vmstate_register_ram_global(seg);
600 addr += size;
601 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800602}
Hu Tao76b5d852014-06-16 18:05:41 +0800603
zhanghailiang5b009e42014-11-04 19:49:30 +0800604static void numa_stat_memory_devices(uint64_t node_mem[])
605{
606 MemoryDeviceInfoList *info_list = NULL;
607 MemoryDeviceInfoList **prev = &info_list;
608 MemoryDeviceInfoList *info;
609
610 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
611 for (info = info_list; info; info = info->next) {
612 MemoryDeviceInfo *value = info->value;
613
614 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600615 switch (value->type) {
zhanghailiang5b009e42014-11-04 19:49:30 +0800616 case MEMORY_DEVICE_INFO_KIND_DIMM:
Eric Blake32bafa82016-03-17 16:48:37 -0600617 node_mem[value->u.dimm.data->node] += value->u.dimm.data->size;
zhanghailiang5b009e42014-11-04 19:49:30 +0800618 break;
619 default:
620 break;
621 }
622 }
623 }
624 qapi_free_MemoryDeviceInfoList(info_list);
625}
626
627void query_numa_node_mem(uint64_t node_mem[])
628{
629 int i;
630
631 if (nb_numa_nodes <= 0) {
632 return;
633 }
634
635 numa_stat_memory_devices(node_mem);
636 for (i = 0; i < nb_numa_nodes; i++) {
637 node_mem[i] += numa_info[i].node_mem;
638 }
639}
640
Hu Tao76b5d852014-06-16 18:05:41 +0800641static int query_memdev(Object *obj, void *opaque)
642{
643 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800644 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800645
646 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800647 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800648
649 m->value = g_malloc0(sizeof(*m->value));
650
Igor Mammedove1ff3c62017-01-10 13:53:15 +0100651 m->value->id = object_property_get_str(obj, "id", NULL);
652 m->value->has_id = !!m->value->id;
653
Hu Tao76b5d852014-06-16 18:05:41 +0800654 m->value->size = object_property_get_int(obj, "size",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100655 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800656 m->value->merge = object_property_get_bool(obj, "merge",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100657 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800658 m->value->dump = object_property_get_bool(obj, "dump",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100659 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800660 m->value->prealloc = object_property_get_bool(obj,
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100661 "prealloc",
662 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800663 m->value->policy = object_property_get_enum(obj,
664 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100665 "HostMemPolicy",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100666 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800667 object_property_get_uint16List(obj, "host-nodes",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100668 &m->value->host_nodes,
669 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800670
671 m->next = *list;
672 *list = m;
673 }
674
675 return 0;
Hu Tao76b5d852014-06-16 18:05:41 +0800676}
677
678MemdevList *qmp_query_memdev(Error **errp)
679{
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100680 Object *obj = object_get_objects_root();
Chen Fanecaf54a2014-08-18 14:46:35 +0800681 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800682
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100683 object_child_foreach(obj, query_memdev, &list);
Hu Tao76b5d852014-06-16 18:05:41 +0800684 return list;
Hu Tao76b5d852014-06-16 18:05:41 +0800685}
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200686
Paolo Bonzini0987d732016-12-21 00:31:36 +0800687void ram_block_notifier_add(RAMBlockNotifier *n)
688{
689 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
690}
691
692void ram_block_notifier_remove(RAMBlockNotifier *n)
693{
694 QLIST_REMOVE(n, next);
695}
696
697void ram_block_notify_add(void *host, size_t size)
698{
699 RAMBlockNotifier *notifier;
700
701 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
702 notifier->ram_block_added(notifier, host, size);
703 }
704}
705
706void ram_block_notify_remove(void *host, size_t size)
707{
708 RAMBlockNotifier *notifier;
709
710 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
711 notifier->ram_block_removed(notifier, host, size);
712 }
713}