blob: 7db5dde8734ea32c91c83bf50d2478e70606969b [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 }
Wanlong Gao00421092014-05-14 17:43:08 +0800181 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
Igor Mammedov7c88e652017-05-10 13:29:50 +0200182 props = mc->cpu_index_to_instance_props(ms, cpus->value);
183 props.node_id = nodenr;
184 props.has_node_id = true;
185 machine_set_cpu_numa_node(ms, &props, &error_fatal);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800186 }
187
Paolo Bonzini7febe362014-05-14 17:43:17 +0800188 if (node->has_mem && node->has_memdev) {
Gonglei01bbbcf2015-02-25 12:22:30 +0800189 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800190 return;
191 }
192
193 if (have_memdevs == -1) {
194 have_memdevs = node->has_memdev;
195 }
196 if (node->has_memdev != have_memdevs) {
197 error_setg(errp, "qemu: memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800198 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800199 return;
200 }
201
Wanlong Gao00421092014-05-14 17:43:08 +0800202 if (node->has_mem) {
203 uint64_t mem_size = node->mem;
204 const char *mem_str = qemu_opt_get(opts, "mem");
205 /* Fix up legacy suffix-less format */
206 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
207 mem_size <<= 20;
208 }
209 numa_info[nodenr].node_mem = mem_size;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800210 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800211 if (node->has_memdev) {
212 Object *o;
213 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
214 if (!o) {
215 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
216 return;
217 }
218
219 object_ref(o);
220 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
221 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
222 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300223 numa_info[nodenr].present = true;
224 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800225}
226
He Chen0f203432017-04-27 10:35:58 +0800227static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
228{
229 uint16_t src = dist->src;
230 uint16_t dst = dist->dst;
231 uint8_t val = dist->val;
232
233 if (src >= MAX_NODES || dst >= MAX_NODES) {
234 error_setg(errp,
235 "Invalid node %" PRIu16
236 ", max possible could be %" PRIu16,
237 MAX(src, dst), MAX_NODES);
238 return;
239 }
240
241 if (!numa_info[src].present || !numa_info[dst].present) {
242 error_setg(errp, "Source/Destination NUMA node is missing. "
243 "Please use '-numa node' option to declare it first.");
244 return;
245 }
246
247 if (val < NUMA_DISTANCE_MIN) {
248 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
249 "it shouldn't be less than %d.",
250 val, NUMA_DISTANCE_MIN);
251 return;
252 }
253
254 if (src == dst && val != NUMA_DISTANCE_MIN) {
255 error_setg(errp, "Local distance of node %d should be %d.",
256 src, NUMA_DISTANCE_MIN);
257 return;
258 }
259
260 numa_info[src].distance[dst] = val;
261 have_numa_distance = true;
262}
263
Markus Armbruster28d0de72015-03-13 13:35:14 +0100264static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800265{
Wanlong Gao00421092014-05-14 17:43:08 +0800266 NumaOptions *object = NULL;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200267 MachineState *ms = opaque;
Wanlong Gao00421092014-05-14 17:43:08 +0800268 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800269
Wanlong Gao00421092014-05-14 17:43:08 +0800270 {
Eric Blake09204ea2016-06-09 10:48:36 -0600271 Visitor *v = opts_visitor_new(opts);
272 visit_type_NumaOptions(v, NULL, &object, &err);
273 visit_free(v);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800274 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800275
Wanlong Gao00421092014-05-14 17:43:08 +0800276 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200277 goto end;
Wanlong Gao00421092014-05-14 17:43:08 +0800278 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800279
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600280 switch (object->type) {
Markus Armbrusterd081a492017-02-21 21:46:26 +0100281 case NUMA_OPTIONS_TYPE_NODE:
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200282 parse_numa_node(ms, &object->u.node, opts, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800283 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200284 goto end;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800285 }
286 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800287 break;
He Chen0f203432017-04-27 10:35:58 +0800288 case NUMA_OPTIONS_TYPE_DIST:
289 parse_numa_distance(&object->u.dist, &err);
290 if (err) {
291 goto end;
292 }
293 break;
Wanlong Gao00421092014-05-14 17:43:08 +0800294 default:
295 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800296 }
Wanlong Gao00421092014-05-14 17:43:08 +0800297
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200298end:
Eric Blake96a16162016-02-23 14:14:33 -0700299 qapi_free_NumaOptions(object);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200300 if (err) {
301 error_report_err(err);
302 return -1;
303 }
Wanlong Gao00421092014-05-14 17:43:08 +0800304
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200305 return 0;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800306}
307
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200308static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
309{
310 int cpu;
311 bool first = true;
312 GString *s = g_string_new(NULL);
313
314 for (cpu = find_first_bit(cpus, max_cpus);
315 cpu < max_cpus;
316 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
317 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
318 first = false;
319 }
320 return g_string_free(s, FALSE);
321}
322
323static void validate_numa_cpus(void)
324{
325 int i;
Igor Mammedovcdda2012016-11-18 12:02:54 +0100326 unsigned long *seen_cpus = bitmap_new(max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200327
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200328 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100329 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu, max_cpus)) {
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200330 bitmap_and(seen_cpus, seen_cpus,
Igor Mammedovcdda2012016-11-18 12:02:54 +0100331 numa_info[i].node_cpu, max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200332 error_report("CPU(s) present in multiple NUMA nodes: %s",
Daniel P. Berrangea8f15a22015-08-26 12:17:12 +0100333 enumerate_cpus(seen_cpus, max_cpus));
Igor Mammedovcdda2012016-11-18 12:02:54 +0100334 g_free(seen_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200335 exit(EXIT_FAILURE);
336 }
337 bitmap_or(seen_cpus, seen_cpus,
Igor Mammedovcdda2012016-11-18 12:02:54 +0100338 numa_info[i].node_cpu, max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200339 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200340
341 if (!bitmap_full(seen_cpus, max_cpus)) {
342 char *msg;
343 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
344 msg = enumerate_cpus(seen_cpus, max_cpus);
345 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
346 error_report("warning: All CPU(s) up to maxcpus should be described "
347 "in NUMA config");
348 g_free(msg);
349 }
Igor Mammedovcdda2012016-11-18 12:02:54 +0100350 g_free(seen_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200351}
352
He Chen0f203432017-04-27 10:35:58 +0800353/* If all node pair distances are symmetric, then only distances
354 * in one direction are enough. If there is even one asymmetric
355 * pair, though, then all distances must be provided. The
356 * distance from a node to itself is always NUMA_DISTANCE_MIN,
357 * so providing it is never necessary.
358 */
359static void validate_numa_distance(void)
360{
361 int src, dst;
362 bool is_asymmetrical = false;
363
364 for (src = 0; src < nb_numa_nodes; src++) {
365 for (dst = src; dst < nb_numa_nodes; dst++) {
366 if (numa_info[src].distance[dst] == 0 &&
367 numa_info[dst].distance[src] == 0) {
368 if (src != dst) {
369 error_report("The distance between node %d and %d is "
370 "missing, at least one distance value "
371 "between each nodes should be provided.",
372 src, dst);
373 exit(EXIT_FAILURE);
374 }
375 }
376
377 if (numa_info[src].distance[dst] != 0 &&
378 numa_info[dst].distance[src] != 0 &&
379 numa_info[src].distance[dst] !=
380 numa_info[dst].distance[src]) {
381 is_asymmetrical = true;
382 }
383 }
384 }
385
386 if (is_asymmetrical) {
387 for (src = 0; src < nb_numa_nodes; src++) {
388 for (dst = 0; dst < nb_numa_nodes; dst++) {
389 if (src != dst && numa_info[src].distance[dst] == 0) {
390 error_report("At least one asymmetrical pair of "
391 "distances is given, please provide distances "
392 "for both directions of all node pairs.");
393 exit(EXIT_FAILURE);
394 }
395 }
396 }
397 }
398}
399
400static void complete_init_numa_distance(void)
401{
402 int src, dst;
403
404 /* Fixup NUMA distance by symmetric policy because if it is an
405 * asymmetric distance table, it should be a complete table and
406 * there would not be any missing distance except local node, which
407 * is verified by validate_numa_distance above.
408 */
409 for (src = 0; src < nb_numa_nodes; src++) {
410 for (dst = 0; dst < nb_numa_nodes; dst++) {
411 if (numa_info[src].distance[dst] == 0) {
412 if (src == dst) {
413 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
414 } else {
415 numa_info[src].distance[dst] = numa_info[dst].distance[src];
416 }
417 }
418 }
419 }
420}
421
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200422void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
423 int nb_nodes, ram_addr_t size)
424{
425 int i;
426 uint64_t usedmem = 0;
427
428 /* Align each node according to the alignment
429 * requirements of the machine class
430 */
431
432 for (i = 0; i < nb_nodes - 1; i++) {
433 nodes[i].node_mem = (size / nb_nodes) &
434 ~((1 << mc->numa_mem_align_shift) - 1);
435 usedmem += nodes[i].node_mem;
436 }
437 nodes[i].node_mem = size - usedmem;
438}
439
440void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
441 int nb_nodes, ram_addr_t size)
442{
443 int i;
444 uint64_t usedmem = 0, node_mem;
445 uint64_t granularity = size / nb_nodes;
446 uint64_t propagate = 0;
447
448 for (i = 0; i < nb_nodes - 1; i++) {
449 node_mem = (granularity + propagate) &
450 ~((1 << mc->numa_mem_align_shift) - 1);
451 propagate = granularity + propagate - node_mem;
452 nodes[i].node_mem = node_mem;
453 usedmem += node_mem;
454 }
455 nodes[i].node_mem = size - usedmem;
456}
457
Igor Mammedovea089ee2017-05-10 13:29:45 +0200458void parse_numa_opts(MachineState *ms)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800459{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300460 int i;
Igor Mammedovea089ee2017-05-10 13:29:45 +0200461 MachineClass *mc = MACHINE_GET_CLASS(ms);
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300462
Igor Mammedovcdda2012016-11-18 12:02:54 +0100463 for (i = 0; i < MAX_NODES; i++) {
464 numa_info[i].node_cpu = bitmap_new(max_cpus);
465 }
466
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200467 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200468 exit(1);
469 }
470
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300471 assert(max_numa_nodeid <= MAX_NODES);
472
473 /* No support for sparse NUMA node IDs yet: */
474 for (i = max_numa_nodeid - 1; i >= 0; i--) {
475 /* Report large node IDs first, to make mistakes easier to spot */
476 if (!numa_info[i].present) {
477 error_report("numa: Node ID missing: %d", i);
478 exit(1);
479 }
480 }
481
482 /* This must be always true if all nodes are present: */
483 assert(nb_numa_nodes == max_numa_nodeid);
484
Wanlong Gao96d0e262014-05-14 17:43:05 +0800485 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800486 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800487
488 if (nb_numa_nodes > MAX_NODES) {
489 nb_numa_nodes = MAX_NODES;
490 }
491
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300492 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800493 * and distribute the available memory equally across all nodes
494 */
495 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800496 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800497 break;
498 }
499 }
500 if (i == nb_numa_nodes) {
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200501 assert(mc->numa_auto_assign_ram);
502 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800503 }
504
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800505 numa_total = 0;
506 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800507 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800508 }
509 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800510 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
511 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800512 numa_total, ram_size);
513 exit(1);
514 }
515
Wanlong Gao96d0e262014-05-14 17:43:05 +0800516 for (i = 0; i < nb_numa_nodes; i++) {
Bharata B Raofa9ea812015-06-29 13:50:25 +0530517 QLIST_INIT(&numa_info[i].addr);
518 }
519
Bharata B Raoabafabd2015-06-29 13:50:26 +0530520 numa_set_mem_ranges();
521
Bharata B Raofa9ea812015-06-29 13:50:25 +0530522 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100523 if (!bitmap_empty(numa_info[i].node_cpu, max_cpus)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800524 break;
525 }
526 }
Igor Mammedovea089ee2017-05-10 13:29:45 +0200527
528 /* assign CPUs to nodes using board provided default mapping */
529 if (!mc->cpu_index_to_instance_props) {
530 error_report("default CPUs to NUMA node mapping isn't supported");
531 exit(1);
532 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800533 if (i == nb_numa_nodes) {
534 for (i = 0; i < max_cpus; i++) {
Igor Mammedovea089ee2017-05-10 13:29:45 +0200535 CpuInstanceProperties props;
Igor Mammedov7c88e652017-05-10 13:29:50 +0200536 /* fetch default mapping from board and enable it */
Igor Mammedovea089ee2017-05-10 13:29:45 +0200537 props = mc->cpu_index_to_instance_props(ms, i);
Igor Mammedov7c88e652017-05-10 13:29:50 +0200538 props.has_node_id = true;
Igor Mammedov57924bc2015-03-19 17:09:21 +0000539
Igor Mammedovea089ee2017-05-10 13:29:45 +0200540 set_bit(i, numa_info[props.node_id].node_cpu);
Igor Mammedov7c88e652017-05-10 13:29:50 +0200541 machine_set_cpu_numa_node(ms, &props, &error_fatal);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800542 }
543 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200544
545 validate_numa_cpus();
He Chen0f203432017-04-27 10:35:58 +0800546
547 /* QEMU needs at least all unique node pair distances to build
548 * the whole NUMA distance table. QEMU treats the distance table
549 * as symmetric by default, i.e. distance A->B == distance B->A.
550 * Thus, QEMU is able to complete the distance table
551 * initialization even though only distance A->B is provided and
552 * distance B->A is not. QEMU knows the distance of a node to
553 * itself is always 10, so A->A distances may be omitted. When
554 * the distances of two nodes of a pair differ, i.e. distance
555 * A->B != distance B->A, then that means the distance table is
556 * asymmetric. In this case, the distances for both directions
557 * of all node pairs are required.
558 */
559 if (have_numa_distance) {
560 /* Validate enough NUMA distance information was provided. */
561 validate_numa_distance();
562
563 /* Validation succeeded, now fill in any missing distances. */
564 complete_init_numa_distance();
565 }
Bharata B Raoabafabd2015-06-29 13:50:26 +0530566 } else {
567 numa_set_mem_node_id(0, ram_size, 0);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800568 }
569}
570
Eduardo Habkostdde11112015-02-08 16:51:22 -0200571void numa_post_machine_init(void)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800572{
573 CPUState *cpu;
574 int i;
575
576 CPU_FOREACH(cpu) {
577 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100578 assert(cpu->cpu_index < max_cpus);
Wanlong Gao8c859012014-05-14 17:43:07 +0800579 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800580 cpu->numa_node = i;
581 }
582 }
583 }
584}
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800585
Paolo Bonzini7febe362014-05-14 17:43:17 +0800586static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
587 const char *name,
588 uint64_t ram_size)
589{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800590 if (mem_path) {
591#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800592 Error *err = NULL;
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800593 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800594 mem_path, &err);
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200595 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100596 error_report_err(err);
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500597 if (mem_prealloc) {
598 exit(1);
599 }
600
601 /* Legacy behavior: if allocation failed, fall back to
602 * regular RAM allocation.
603 */
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200604 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800605 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800606#else
607 fprintf(stderr, "-mem-path not supported on this host\n");
608 exit(1);
609#endif
610 } else {
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200611 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800612 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800613 vmstate_register_ram_global(mr);
614}
615
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800616void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
617 const char *name,
618 uint64_t ram_size)
619{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800620 uint64_t addr = 0;
621 int i;
622
623 if (nb_numa_nodes == 0 || !have_memdevs) {
624 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
625 return;
626 }
627
628 memory_region_init(mr, owner, name, ram_size);
629 for (i = 0; i < MAX_NODES; i++) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800630 uint64_t size = numa_info[i].node_mem;
631 HostMemoryBackend *backend = numa_info[i].node_memdev;
632 if (!backend) {
633 continue;
634 }
Markus Armbruster007b0652015-09-11 15:04:45 +0200635 MemoryRegion *seg = host_memory_backend_get_memory(backend,
636 &error_fatal);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800637
Hu Tao0462fae2014-06-30 18:28:15 +0800638 if (memory_region_is_mapped(seg)) {
639 char *path = object_get_canonical_path_component(OBJECT(backend));
640 error_report("memory backend %s is used multiple times. Each "
641 "-numa option must use a different memdev value.",
642 path);
643 exit(1);
644 }
645
Greg Kurz0b217572016-07-19 10:28:35 +0200646 host_memory_backend_set_mapped(backend, true);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800647 memory_region_add_subregion(mr, addr, seg);
648 vmstate_register_ram_global(seg);
649 addr += size;
650 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800651}
Hu Tao76b5d852014-06-16 18:05:41 +0800652
zhanghailiang5b009e42014-11-04 19:49:30 +0800653static void numa_stat_memory_devices(uint64_t node_mem[])
654{
655 MemoryDeviceInfoList *info_list = NULL;
656 MemoryDeviceInfoList **prev = &info_list;
657 MemoryDeviceInfoList *info;
658
659 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
660 for (info = info_list; info; info = info->next) {
661 MemoryDeviceInfo *value = info->value;
662
663 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600664 switch (value->type) {
zhanghailiang5b009e42014-11-04 19:49:30 +0800665 case MEMORY_DEVICE_INFO_KIND_DIMM:
Eric Blake32bafa82016-03-17 16:48:37 -0600666 node_mem[value->u.dimm.data->node] += value->u.dimm.data->size;
zhanghailiang5b009e42014-11-04 19:49:30 +0800667 break;
668 default:
669 break;
670 }
671 }
672 }
673 qapi_free_MemoryDeviceInfoList(info_list);
674}
675
676void query_numa_node_mem(uint64_t node_mem[])
677{
678 int i;
679
680 if (nb_numa_nodes <= 0) {
681 return;
682 }
683
684 numa_stat_memory_devices(node_mem);
685 for (i = 0; i < nb_numa_nodes; i++) {
686 node_mem[i] += numa_info[i].node_mem;
687 }
688}
689
Hu Tao76b5d852014-06-16 18:05:41 +0800690static int query_memdev(Object *obj, void *opaque)
691{
692 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800693 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800694
695 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800696 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800697
698 m->value = g_malloc0(sizeof(*m->value));
699
Igor Mammedove1ff3c62017-01-10 13:53:15 +0100700 m->value->id = object_property_get_str(obj, "id", NULL);
701 m->value->has_id = !!m->value->id;
702
Hu Tao76b5d852014-06-16 18:05:41 +0800703 m->value->size = object_property_get_int(obj, "size",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100704 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800705 m->value->merge = object_property_get_bool(obj, "merge",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100706 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800707 m->value->dump = object_property_get_bool(obj, "dump",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100708 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800709 m->value->prealloc = object_property_get_bool(obj,
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100710 "prealloc",
711 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800712 m->value->policy = object_property_get_enum(obj,
713 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100714 "HostMemPolicy",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100715 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800716 object_property_get_uint16List(obj, "host-nodes",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100717 &m->value->host_nodes,
718 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800719
720 m->next = *list;
721 *list = m;
722 }
723
724 return 0;
Hu Tao76b5d852014-06-16 18:05:41 +0800725}
726
727MemdevList *qmp_query_memdev(Error **errp)
728{
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100729 Object *obj = object_get_objects_root();
Chen Fanecaf54a2014-08-18 14:46:35 +0800730 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800731
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100732 object_child_foreach(obj, query_memdev, &list);
Hu Tao76b5d852014-06-16 18:05:41 +0800733 return list;
Hu Tao76b5d852014-06-16 18:05:41 +0800734}
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200735
736int numa_get_node_for_cpu(int idx)
737{
738 int i;
739
Igor Mammedovcdda2012016-11-18 12:02:54 +0100740 assert(idx < max_cpus);
741
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200742 for (i = 0; i < nb_numa_nodes; i++) {
743 if (test_bit(idx, numa_info[i].node_cpu)) {
744 break;
745 }
746 }
747 return i;
748}
Paolo Bonzini0987d732016-12-21 00:31:36 +0800749
750void ram_block_notifier_add(RAMBlockNotifier *n)
751{
752 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
753}
754
755void ram_block_notifier_remove(RAMBlockNotifier *n)
756{
757 QLIST_REMOVE(n, next);
758}
759
760void ram_block_notify_add(void *host, size_t size)
761{
762 RAMBlockNotifier *notifier;
763
764 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
765 notifier->ram_block_added(notifier, host, size);
766 }
767}
768
769void ram_block_notify_remove(void *host, size_t size)
770{
771 RAMBlockNotifier *notifier;
772
773 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
774 notifier->ram_block_removed(notifier, host, size);
775 }
776}