blob: 718248161c586d88195921c9675fc175cd034423 [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) {
Eduardo Habkost8979c942015-02-09 17:28:52 -0200173 if (cpus->value >= max_cpus) {
174 error_setg(errp,
175 "CPU index (%" PRIu16 ")"
176 " should be smaller than maxcpus (%d)",
177 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +0800178 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800179 }
Wanlong Gao00421092014-05-14 17:43:08 +0800180 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800181 }
182
Paolo Bonzini7febe362014-05-14 17:43:17 +0800183 if (node->has_mem && node->has_memdev) {
Gonglei01bbbcf2015-02-25 12:22:30 +0800184 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800185 return;
186 }
187
188 if (have_memdevs == -1) {
189 have_memdevs = node->has_memdev;
190 }
191 if (node->has_memdev != have_memdevs) {
192 error_setg(errp, "qemu: memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800193 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800194 return;
195 }
196
Wanlong Gao00421092014-05-14 17:43:08 +0800197 if (node->has_mem) {
198 uint64_t mem_size = node->mem;
199 const char *mem_str = qemu_opt_get(opts, "mem");
200 /* Fix up legacy suffix-less format */
201 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
202 mem_size <<= 20;
203 }
204 numa_info[nodenr].node_mem = mem_size;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800205 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800206 if (node->has_memdev) {
207 Object *o;
208 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
209 if (!o) {
210 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
211 return;
212 }
213
214 object_ref(o);
215 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
216 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
217 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300218 numa_info[nodenr].present = true;
219 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800220}
221
He Chen0f203432017-04-27 10:35:58 +0800222static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
223{
224 uint16_t src = dist->src;
225 uint16_t dst = dist->dst;
226 uint8_t val = dist->val;
227
228 if (src >= MAX_NODES || dst >= MAX_NODES) {
229 error_setg(errp,
230 "Invalid node %" PRIu16
231 ", max possible could be %" PRIu16,
232 MAX(src, dst), MAX_NODES);
233 return;
234 }
235
236 if (!numa_info[src].present || !numa_info[dst].present) {
237 error_setg(errp, "Source/Destination NUMA node is missing. "
238 "Please use '-numa node' option to declare it first.");
239 return;
240 }
241
242 if (val < NUMA_DISTANCE_MIN) {
243 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
244 "it shouldn't be less than %d.",
245 val, NUMA_DISTANCE_MIN);
246 return;
247 }
248
249 if (src == dst && val != NUMA_DISTANCE_MIN) {
250 error_setg(errp, "Local distance of node %d should be %d.",
251 src, NUMA_DISTANCE_MIN);
252 return;
253 }
254
255 numa_info[src].distance[dst] = val;
256 have_numa_distance = true;
257}
258
Markus Armbruster28d0de72015-03-13 13:35:14 +0100259static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800260{
Wanlong Gao00421092014-05-14 17:43:08 +0800261 NumaOptions *object = NULL;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200262 MachineState *ms = opaque;
Wanlong Gao00421092014-05-14 17:43:08 +0800263 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800264
Wanlong Gao00421092014-05-14 17:43:08 +0800265 {
Eric Blake09204ea2016-06-09 10:48:36 -0600266 Visitor *v = opts_visitor_new(opts);
267 visit_type_NumaOptions(v, NULL, &object, &err);
268 visit_free(v);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800269 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800270
Wanlong Gao00421092014-05-14 17:43:08 +0800271 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200272 goto end;
Wanlong Gao00421092014-05-14 17:43:08 +0800273 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800274
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600275 switch (object->type) {
Markus Armbrusterd081a492017-02-21 21:46:26 +0100276 case NUMA_OPTIONS_TYPE_NODE:
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200277 parse_numa_node(ms, &object->u.node, opts, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800278 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200279 goto end;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800280 }
281 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800282 break;
He Chen0f203432017-04-27 10:35:58 +0800283 case NUMA_OPTIONS_TYPE_DIST:
284 parse_numa_distance(&object->u.dist, &err);
285 if (err) {
286 goto end;
287 }
288 break;
Wanlong Gao00421092014-05-14 17:43:08 +0800289 default:
290 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800291 }
Wanlong Gao00421092014-05-14 17:43:08 +0800292
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200293end:
Eric Blake96a16162016-02-23 14:14:33 -0700294 qapi_free_NumaOptions(object);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200295 if (err) {
296 error_report_err(err);
297 return -1;
298 }
Wanlong Gao00421092014-05-14 17:43:08 +0800299
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200300 return 0;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800301}
302
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200303static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
304{
305 int cpu;
306 bool first = true;
307 GString *s = g_string_new(NULL);
308
309 for (cpu = find_first_bit(cpus, max_cpus);
310 cpu < max_cpus;
311 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
312 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
313 first = false;
314 }
315 return g_string_free(s, FALSE);
316}
317
318static void validate_numa_cpus(void)
319{
320 int i;
Igor Mammedovcdda2012016-11-18 12:02:54 +0100321 unsigned long *seen_cpus = bitmap_new(max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200322
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200323 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100324 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu, max_cpus)) {
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200325 bitmap_and(seen_cpus, seen_cpus,
Igor Mammedovcdda2012016-11-18 12:02:54 +0100326 numa_info[i].node_cpu, max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200327 error_report("CPU(s) present in multiple NUMA nodes: %s",
Daniel P. Berrangea8f15a22015-08-26 12:17:12 +0100328 enumerate_cpus(seen_cpus, max_cpus));
Igor Mammedovcdda2012016-11-18 12:02:54 +0100329 g_free(seen_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200330 exit(EXIT_FAILURE);
331 }
332 bitmap_or(seen_cpus, seen_cpus,
Igor Mammedovcdda2012016-11-18 12:02:54 +0100333 numa_info[i].node_cpu, max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200334 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200335
336 if (!bitmap_full(seen_cpus, max_cpus)) {
337 char *msg;
338 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
339 msg = enumerate_cpus(seen_cpus, max_cpus);
340 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
341 error_report("warning: All CPU(s) up to maxcpus should be described "
342 "in NUMA config");
343 g_free(msg);
344 }
Igor Mammedovcdda2012016-11-18 12:02:54 +0100345 g_free(seen_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200346}
347
He Chen0f203432017-04-27 10:35:58 +0800348/* If all node pair distances are symmetric, then only distances
349 * in one direction are enough. If there is even one asymmetric
350 * pair, though, then all distances must be provided. The
351 * distance from a node to itself is always NUMA_DISTANCE_MIN,
352 * so providing it is never necessary.
353 */
354static void validate_numa_distance(void)
355{
356 int src, dst;
357 bool is_asymmetrical = false;
358
359 for (src = 0; src < nb_numa_nodes; src++) {
360 for (dst = src; dst < nb_numa_nodes; dst++) {
361 if (numa_info[src].distance[dst] == 0 &&
362 numa_info[dst].distance[src] == 0) {
363 if (src != dst) {
364 error_report("The distance between node %d and %d is "
365 "missing, at least one distance value "
366 "between each nodes should be provided.",
367 src, dst);
368 exit(EXIT_FAILURE);
369 }
370 }
371
372 if (numa_info[src].distance[dst] != 0 &&
373 numa_info[dst].distance[src] != 0 &&
374 numa_info[src].distance[dst] !=
375 numa_info[dst].distance[src]) {
376 is_asymmetrical = true;
377 }
378 }
379 }
380
381 if (is_asymmetrical) {
382 for (src = 0; src < nb_numa_nodes; src++) {
383 for (dst = 0; dst < nb_numa_nodes; dst++) {
384 if (src != dst && numa_info[src].distance[dst] == 0) {
385 error_report("At least one asymmetrical pair of "
386 "distances is given, please provide distances "
387 "for both directions of all node pairs.");
388 exit(EXIT_FAILURE);
389 }
390 }
391 }
392 }
393}
394
395static void complete_init_numa_distance(void)
396{
397 int src, dst;
398
399 /* Fixup NUMA distance by symmetric policy because if it is an
400 * asymmetric distance table, it should be a complete table and
401 * there would not be any missing distance except local node, which
402 * is verified by validate_numa_distance above.
403 */
404 for (src = 0; src < nb_numa_nodes; src++) {
405 for (dst = 0; dst < nb_numa_nodes; dst++) {
406 if (numa_info[src].distance[dst] == 0) {
407 if (src == dst) {
408 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
409 } else {
410 numa_info[src].distance[dst] = numa_info[dst].distance[src];
411 }
412 }
413 }
414 }
415}
416
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200417void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
418 int nb_nodes, ram_addr_t size)
419{
420 int i;
421 uint64_t usedmem = 0;
422
423 /* Align each node according to the alignment
424 * requirements of the machine class
425 */
426
427 for (i = 0; i < nb_nodes - 1; i++) {
428 nodes[i].node_mem = (size / nb_nodes) &
429 ~((1 << mc->numa_mem_align_shift) - 1);
430 usedmem += nodes[i].node_mem;
431 }
432 nodes[i].node_mem = size - usedmem;
433}
434
435void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
436 int nb_nodes, ram_addr_t size)
437{
438 int i;
439 uint64_t usedmem = 0, node_mem;
440 uint64_t granularity = size / nb_nodes;
441 uint64_t propagate = 0;
442
443 for (i = 0; i < nb_nodes - 1; i++) {
444 node_mem = (granularity + propagate) &
445 ~((1 << mc->numa_mem_align_shift) - 1);
446 propagate = granularity + propagate - node_mem;
447 nodes[i].node_mem = node_mem;
448 usedmem += node_mem;
449 }
450 nodes[i].node_mem = size - usedmem;
451}
452
Igor Mammedovea089ee2017-05-10 13:29:45 +0200453void parse_numa_opts(MachineState *ms)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800454{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300455 int i;
Igor Mammedovea089ee2017-05-10 13:29:45 +0200456 MachineClass *mc = MACHINE_GET_CLASS(ms);
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300457
Igor Mammedovcdda2012016-11-18 12:02:54 +0100458 for (i = 0; i < MAX_NODES; i++) {
459 numa_info[i].node_cpu = bitmap_new(max_cpus);
460 }
461
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200462 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200463 exit(1);
464 }
465
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300466 assert(max_numa_nodeid <= MAX_NODES);
467
468 /* No support for sparse NUMA node IDs yet: */
469 for (i = max_numa_nodeid - 1; i >= 0; i--) {
470 /* Report large node IDs first, to make mistakes easier to spot */
471 if (!numa_info[i].present) {
472 error_report("numa: Node ID missing: %d", i);
473 exit(1);
474 }
475 }
476
477 /* This must be always true if all nodes are present: */
478 assert(nb_numa_nodes == max_numa_nodeid);
479
Wanlong Gao96d0e262014-05-14 17:43:05 +0800480 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800481 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800482
483 if (nb_numa_nodes > MAX_NODES) {
484 nb_numa_nodes = MAX_NODES;
485 }
486
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300487 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800488 * and distribute the available memory equally across all nodes
489 */
490 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800491 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800492 break;
493 }
494 }
495 if (i == nb_numa_nodes) {
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200496 assert(mc->numa_auto_assign_ram);
497 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800498 }
499
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800500 numa_total = 0;
501 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800502 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800503 }
504 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800505 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
506 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800507 numa_total, ram_size);
508 exit(1);
509 }
510
Wanlong Gao96d0e262014-05-14 17:43:05 +0800511 for (i = 0; i < nb_numa_nodes; i++) {
Bharata B Raofa9ea812015-06-29 13:50:25 +0530512 QLIST_INIT(&numa_info[i].addr);
513 }
514
Bharata B Raoabafabd2015-06-29 13:50:26 +0530515 numa_set_mem_ranges();
516
Bharata B Raofa9ea812015-06-29 13:50:25 +0530517 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100518 if (!bitmap_empty(numa_info[i].node_cpu, max_cpus)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800519 break;
520 }
521 }
Igor Mammedovea089ee2017-05-10 13:29:45 +0200522
523 /* assign CPUs to nodes using board provided default mapping */
524 if (!mc->cpu_index_to_instance_props) {
525 error_report("default CPUs to NUMA node mapping isn't supported");
526 exit(1);
527 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800528 if (i == nb_numa_nodes) {
529 for (i = 0; i < max_cpus; i++) {
Igor Mammedovea089ee2017-05-10 13:29:45 +0200530 CpuInstanceProperties props;
531 props = mc->cpu_index_to_instance_props(ms, i);
Igor Mammedov57924bc2015-03-19 17:09:21 +0000532
Igor Mammedovea089ee2017-05-10 13:29:45 +0200533 set_bit(i, numa_info[props.node_id].node_cpu);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800534 }
535 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200536
537 validate_numa_cpus();
He Chen0f203432017-04-27 10:35:58 +0800538
539 /* QEMU needs at least all unique node pair distances to build
540 * the whole NUMA distance table. QEMU treats the distance table
541 * as symmetric by default, i.e. distance A->B == distance B->A.
542 * Thus, QEMU is able to complete the distance table
543 * initialization even though only distance A->B is provided and
544 * distance B->A is not. QEMU knows the distance of a node to
545 * itself is always 10, so A->A distances may be omitted. When
546 * the distances of two nodes of a pair differ, i.e. distance
547 * A->B != distance B->A, then that means the distance table is
548 * asymmetric. In this case, the distances for both directions
549 * of all node pairs are required.
550 */
551 if (have_numa_distance) {
552 /* Validate enough NUMA distance information was provided. */
553 validate_numa_distance();
554
555 /* Validation succeeded, now fill in any missing distances. */
556 complete_init_numa_distance();
557 }
Bharata B Raoabafabd2015-06-29 13:50:26 +0530558 } else {
559 numa_set_mem_node_id(0, ram_size, 0);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800560 }
561}
562
Eduardo Habkostdde11112015-02-08 16:51:22 -0200563void numa_post_machine_init(void)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800564{
565 CPUState *cpu;
566 int i;
567
568 CPU_FOREACH(cpu) {
569 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100570 assert(cpu->cpu_index < max_cpus);
Wanlong Gao8c859012014-05-14 17:43:07 +0800571 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800572 cpu->numa_node = i;
573 }
574 }
575 }
576}
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800577
Paolo Bonzini7febe362014-05-14 17:43:17 +0800578static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
579 const char *name,
580 uint64_t ram_size)
581{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800582 if (mem_path) {
583#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800584 Error *err = NULL;
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800585 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800586 mem_path, &err);
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200587 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100588 error_report_err(err);
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500589 if (mem_prealloc) {
590 exit(1);
591 }
592
593 /* Legacy behavior: if allocation failed, fall back to
594 * regular RAM allocation.
595 */
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200596 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800597 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800598#else
599 fprintf(stderr, "-mem-path not supported on this host\n");
600 exit(1);
601#endif
602 } else {
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200603 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800604 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800605 vmstate_register_ram_global(mr);
606}
607
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800608void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
609 const char *name,
610 uint64_t ram_size)
611{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800612 uint64_t addr = 0;
613 int i;
614
615 if (nb_numa_nodes == 0 || !have_memdevs) {
616 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
617 return;
618 }
619
620 memory_region_init(mr, owner, name, ram_size);
621 for (i = 0; i < MAX_NODES; i++) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800622 uint64_t size = numa_info[i].node_mem;
623 HostMemoryBackend *backend = numa_info[i].node_memdev;
624 if (!backend) {
625 continue;
626 }
Markus Armbruster007b0652015-09-11 15:04:45 +0200627 MemoryRegion *seg = host_memory_backend_get_memory(backend,
628 &error_fatal);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800629
Hu Tao0462fae2014-06-30 18:28:15 +0800630 if (memory_region_is_mapped(seg)) {
631 char *path = object_get_canonical_path_component(OBJECT(backend));
632 error_report("memory backend %s is used multiple times. Each "
633 "-numa option must use a different memdev value.",
634 path);
635 exit(1);
636 }
637
Greg Kurz0b217572016-07-19 10:28:35 +0200638 host_memory_backend_set_mapped(backend, true);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800639 memory_region_add_subregion(mr, addr, seg);
640 vmstate_register_ram_global(seg);
641 addr += size;
642 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800643}
Hu Tao76b5d852014-06-16 18:05:41 +0800644
zhanghailiang5b009e42014-11-04 19:49:30 +0800645static void numa_stat_memory_devices(uint64_t node_mem[])
646{
647 MemoryDeviceInfoList *info_list = NULL;
648 MemoryDeviceInfoList **prev = &info_list;
649 MemoryDeviceInfoList *info;
650
651 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
652 for (info = info_list; info; info = info->next) {
653 MemoryDeviceInfo *value = info->value;
654
655 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600656 switch (value->type) {
zhanghailiang5b009e42014-11-04 19:49:30 +0800657 case MEMORY_DEVICE_INFO_KIND_DIMM:
Eric Blake32bafa82016-03-17 16:48:37 -0600658 node_mem[value->u.dimm.data->node] += value->u.dimm.data->size;
zhanghailiang5b009e42014-11-04 19:49:30 +0800659 break;
660 default:
661 break;
662 }
663 }
664 }
665 qapi_free_MemoryDeviceInfoList(info_list);
666}
667
668void query_numa_node_mem(uint64_t node_mem[])
669{
670 int i;
671
672 if (nb_numa_nodes <= 0) {
673 return;
674 }
675
676 numa_stat_memory_devices(node_mem);
677 for (i = 0; i < nb_numa_nodes; i++) {
678 node_mem[i] += numa_info[i].node_mem;
679 }
680}
681
Hu Tao76b5d852014-06-16 18:05:41 +0800682static int query_memdev(Object *obj, void *opaque)
683{
684 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800685 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800686
687 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800688 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800689
690 m->value = g_malloc0(sizeof(*m->value));
691
Igor Mammedove1ff3c62017-01-10 13:53:15 +0100692 m->value->id = object_property_get_str(obj, "id", NULL);
693 m->value->has_id = !!m->value->id;
694
Hu Tao76b5d852014-06-16 18:05:41 +0800695 m->value->size = object_property_get_int(obj, "size",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100696 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800697 m->value->merge = object_property_get_bool(obj, "merge",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100698 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800699 m->value->dump = object_property_get_bool(obj, "dump",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100700 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800701 m->value->prealloc = object_property_get_bool(obj,
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100702 "prealloc",
703 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800704 m->value->policy = object_property_get_enum(obj,
705 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100706 "HostMemPolicy",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100707 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800708 object_property_get_uint16List(obj, "host-nodes",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100709 &m->value->host_nodes,
710 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800711
712 m->next = *list;
713 *list = m;
714 }
715
716 return 0;
Hu Tao76b5d852014-06-16 18:05:41 +0800717}
718
719MemdevList *qmp_query_memdev(Error **errp)
720{
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100721 Object *obj = object_get_objects_root();
Chen Fanecaf54a2014-08-18 14:46:35 +0800722 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800723
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100724 object_child_foreach(obj, query_memdev, &list);
Hu Tao76b5d852014-06-16 18:05:41 +0800725 return list;
Hu Tao76b5d852014-06-16 18:05:41 +0800726}
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200727
728int numa_get_node_for_cpu(int idx)
729{
730 int i;
731
Igor Mammedovcdda2012016-11-18 12:02:54 +0100732 assert(idx < max_cpus);
733
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200734 for (i = 0; i < nb_numa_nodes; i++) {
735 if (test_bit(idx, numa_info[i].node_cpu)) {
736 break;
737 }
738 }
739 return i;
740}
Paolo Bonzini0987d732016-12-21 00:31:36 +0800741
742void ram_block_notifier_add(RAMBlockNotifier *n)
743{
744 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
745}
746
747void ram_block_notifier_remove(RAMBlockNotifier *n)
748{
749 QLIST_REMOVE(n, next);
750}
751
752void ram_block_notify_add(void *host, size_t size)
753{
754 RAMBlockNotifier *notifier;
755
756 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
757 notifier->ram_block_added(notifier, host, size);
758 }
759}
760
761void ram_block_notify_remove(void *host, size_t size)
762{
763 RAMBlockNotifier *notifier;
764
765 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
766 notifier->ram_block_removed(notifier, host, size);
767 }
768}