Include hw/boards.h a bit less
[qemu/ar7.git] / hw / core / numa.c
blobd817f06ead2af01f2e543c1ea47c6a4bd87e6591
1 /*
2 * NUMA parameter parsing routines
4 * Copyright (c) 2014 Fujitsu Ltd.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "sysemu/numa.h"
27 #include "exec/cpu-common.h"
28 #include "exec/ramlist.h"
29 #include "qemu/bitmap.h"
30 #include "qemu/error-report.h"
31 #include "qapi/error.h"
32 #include "qapi/opts-visitor.h"
33 #include "qapi/qapi-visit-machine.h"
34 #include "sysemu/qtest.h"
35 #include "qom/cpu.h"
36 #include "hw/mem/pc-dimm.h"
37 #include "migration/vmstate.h"
38 #include "hw/boards.h"
39 #include "hw/mem/memory-device.h"
40 #include "qemu/option.h"
41 #include "qemu/config-file.h"
42 #include "qemu/cutils.h"
44 QemuOptsList qemu_numa_opts = {
45 .name = "numa",
46 .implied_opt_name = "type",
47 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
48 .desc = { { 0 } } /* validated with OptsVisitor */
51 static int have_memdevs;
52 static int have_mem;
53 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
54 * For all nodes, nodeid < max_numa_nodeid
56 int nb_numa_nodes;
57 bool have_numa_distance;
58 NodeInfo numa_info[MAX_NODES];
61 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
62 Error **errp)
64 Error *err = NULL;
65 uint16_t nodenr;
66 uint16List *cpus = NULL;
67 MachineClass *mc = MACHINE_GET_CLASS(ms);
68 unsigned int max_cpus = ms->smp.max_cpus;
70 if (node->has_nodeid) {
71 nodenr = node->nodeid;
72 } else {
73 nodenr = nb_numa_nodes;
76 if (nodenr >= MAX_NODES) {
77 error_setg(errp, "Max number of NUMA nodes reached: %"
78 PRIu16 "", nodenr);
79 return;
82 if (numa_info[nodenr].present) {
83 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
84 return;
87 if (!mc->cpu_index_to_instance_props || !mc->get_default_cpu_node_id) {
88 error_setg(errp, "NUMA is not supported by this machine-type");
89 return;
91 for (cpus = node->cpus; cpus; cpus = cpus->next) {
92 CpuInstanceProperties props;
93 if (cpus->value >= max_cpus) {
94 error_setg(errp,
95 "CPU index (%" PRIu16 ")"
96 " should be smaller than maxcpus (%d)",
97 cpus->value, max_cpus);
98 return;
100 props = mc->cpu_index_to_instance_props(ms, cpus->value);
101 props.node_id = nodenr;
102 props.has_node_id = true;
103 machine_set_cpu_numa_node(ms, &props, &err);
104 if (err) {
105 error_propagate(errp, err);
106 return;
110 have_memdevs = have_memdevs ? : node->has_memdev;
111 have_mem = have_mem ? : node->has_mem;
112 if ((node->has_mem && have_memdevs) || (node->has_memdev && have_mem)) {
113 error_setg(errp, "numa configuration should use either mem= or memdev=,"
114 "mixing both is not allowed");
115 return;
118 if (node->has_mem) {
119 numa_info[nodenr].node_mem = node->mem;
120 if (!qtest_enabled()) {
121 warn_report("Parameter -numa node,mem is deprecated,"
122 " use -numa node,memdev instead");
125 if (node->has_memdev) {
126 Object *o;
127 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
128 if (!o) {
129 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
130 return;
133 object_ref(o);
134 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
135 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
137 numa_info[nodenr].present = true;
138 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
139 nb_numa_nodes++;
142 static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
144 uint16_t src = dist->src;
145 uint16_t dst = dist->dst;
146 uint8_t val = dist->val;
148 if (src >= MAX_NODES || dst >= MAX_NODES) {
149 error_setg(errp, "Parameter '%s' expects an integer between 0 and %d",
150 src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1);
151 return;
154 if (!numa_info[src].present || !numa_info[dst].present) {
155 error_setg(errp, "Source/Destination NUMA node is missing. "
156 "Please use '-numa node' option to declare it first.");
157 return;
160 if (val < NUMA_DISTANCE_MIN) {
161 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
162 "it shouldn't be less than %d.",
163 val, NUMA_DISTANCE_MIN);
164 return;
167 if (src == dst && val != NUMA_DISTANCE_MIN) {
168 error_setg(errp, "Local distance of node %d should be %d.",
169 src, NUMA_DISTANCE_MIN);
170 return;
173 numa_info[src].distance[dst] = val;
174 have_numa_distance = true;
177 void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp)
179 Error *err = NULL;
181 switch (object->type) {
182 case NUMA_OPTIONS_TYPE_NODE:
183 parse_numa_node(ms, &object->u.node, &err);
184 if (err) {
185 goto end;
187 break;
188 case NUMA_OPTIONS_TYPE_DIST:
189 parse_numa_distance(&object->u.dist, &err);
190 if (err) {
191 goto end;
193 break;
194 case NUMA_OPTIONS_TYPE_CPU:
195 if (!object->u.cpu.has_node_id) {
196 error_setg(&err, "Missing mandatory node-id property");
197 goto end;
199 if (!numa_info[object->u.cpu.node_id].present) {
200 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
201 "defined with -numa node,nodeid=ID before it's used with "
202 "-numa cpu,node-id=ID", object->u.cpu.node_id);
203 goto end;
206 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
207 &err);
208 break;
209 default:
210 abort();
213 end:
214 error_propagate(errp, err);
217 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
219 NumaOptions *object = NULL;
220 MachineState *ms = MACHINE(opaque);
221 Error *err = NULL;
222 Visitor *v = opts_visitor_new(opts);
224 visit_type_NumaOptions(v, NULL, &object, &err);
225 visit_free(v);
226 if (err) {
227 goto end;
230 /* Fix up legacy suffix-less format */
231 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
232 const char *mem_str = qemu_opt_get(opts, "mem");
233 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
236 set_numa_options(ms, object, &err);
238 end:
239 qapi_free_NumaOptions(object);
240 if (err) {
241 error_propagate(errp, err);
242 return -1;
245 return 0;
248 /* If all node pair distances are symmetric, then only distances
249 * in one direction are enough. If there is even one asymmetric
250 * pair, though, then all distances must be provided. The
251 * distance from a node to itself is always NUMA_DISTANCE_MIN,
252 * so providing it is never necessary.
254 static void validate_numa_distance(void)
256 int src, dst;
257 bool is_asymmetrical = false;
259 for (src = 0; src < nb_numa_nodes; src++) {
260 for (dst = src; dst < nb_numa_nodes; dst++) {
261 if (numa_info[src].distance[dst] == 0 &&
262 numa_info[dst].distance[src] == 0) {
263 if (src != dst) {
264 error_report("The distance between node %d and %d is "
265 "missing, at least one distance value "
266 "between each nodes should be provided.",
267 src, dst);
268 exit(EXIT_FAILURE);
272 if (numa_info[src].distance[dst] != 0 &&
273 numa_info[dst].distance[src] != 0 &&
274 numa_info[src].distance[dst] !=
275 numa_info[dst].distance[src]) {
276 is_asymmetrical = true;
281 if (is_asymmetrical) {
282 for (src = 0; src < nb_numa_nodes; src++) {
283 for (dst = 0; dst < nb_numa_nodes; dst++) {
284 if (src != dst && numa_info[src].distance[dst] == 0) {
285 error_report("At least one asymmetrical pair of "
286 "distances is given, please provide distances "
287 "for both directions of all node pairs.");
288 exit(EXIT_FAILURE);
295 static void complete_init_numa_distance(void)
297 int src, dst;
299 /* Fixup NUMA distance by symmetric policy because if it is an
300 * asymmetric distance table, it should be a complete table and
301 * there would not be any missing distance except local node, which
302 * is verified by validate_numa_distance above.
304 for (src = 0; src < nb_numa_nodes; src++) {
305 for (dst = 0; dst < nb_numa_nodes; dst++) {
306 if (numa_info[src].distance[dst] == 0) {
307 if (src == dst) {
308 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
309 } else {
310 numa_info[src].distance[dst] = numa_info[dst].distance[src];
317 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
318 int nb_nodes, ram_addr_t size)
320 int i;
321 uint64_t usedmem = 0;
323 /* Align each node according to the alignment
324 * requirements of the machine class
327 for (i = 0; i < nb_nodes - 1; i++) {
328 nodes[i].node_mem = (size / nb_nodes) &
329 ~((1 << mc->numa_mem_align_shift) - 1);
330 usedmem += nodes[i].node_mem;
332 nodes[i].node_mem = size - usedmem;
335 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
336 int nb_nodes, ram_addr_t size)
338 int i;
339 uint64_t usedmem = 0, node_mem;
340 uint64_t granularity = size / nb_nodes;
341 uint64_t propagate = 0;
343 for (i = 0; i < nb_nodes - 1; i++) {
344 node_mem = (granularity + propagate) &
345 ~((1 << mc->numa_mem_align_shift) - 1);
346 propagate = granularity + propagate - node_mem;
347 nodes[i].node_mem = node_mem;
348 usedmem += node_mem;
350 nodes[i].node_mem = size - usedmem;
353 void numa_complete_configuration(MachineState *ms)
355 int i;
356 MachineClass *mc = MACHINE_GET_CLASS(ms);
359 * If memory hotplug is enabled (slots > 0) but without '-numa'
360 * options explicitly on CLI, guestes will break.
362 * Windows: won't enable memory hotplug without SRAT table at all
364 * Linux: if QEMU is started with initial memory all below 4Gb
365 * and no SRAT table present, guest kernel will use nommu DMA ops,
366 * which breaks 32bit hw drivers when memory is hotplugged and
367 * guest tries to use it with that drivers.
369 * Enable NUMA implicitly by adding a new NUMA node automatically.
371 if (ms->ram_slots > 0 && nb_numa_nodes == 0 &&
372 mc->auto_enable_numa_with_memhp) {
373 NumaNodeOptions node = { };
374 parse_numa_node(ms, &node, &error_abort);
377 assert(max_numa_nodeid <= MAX_NODES);
379 /* No support for sparse NUMA node IDs yet: */
380 for (i = max_numa_nodeid - 1; i >= 0; i--) {
381 /* Report large node IDs first, to make mistakes easier to spot */
382 if (!numa_info[i].present) {
383 error_report("numa: Node ID missing: %d", i);
384 exit(1);
388 /* This must be always true if all nodes are present: */
389 assert(nb_numa_nodes == max_numa_nodeid);
391 if (nb_numa_nodes > 0) {
392 uint64_t numa_total;
394 if (nb_numa_nodes > MAX_NODES) {
395 nb_numa_nodes = MAX_NODES;
398 /* If no memory size is given for any node, assume the default case
399 * and distribute the available memory equally across all nodes
401 for (i = 0; i < nb_numa_nodes; i++) {
402 if (numa_info[i].node_mem != 0) {
403 break;
406 if (i == nb_numa_nodes) {
407 assert(mc->numa_auto_assign_ram);
408 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
409 if (!qtest_enabled()) {
410 warn_report("Default splitting of RAM between nodes is deprecated,"
411 " Use '-numa node,memdev' to explictly define RAM"
412 " allocation per node");
416 numa_total = 0;
417 for (i = 0; i < nb_numa_nodes; i++) {
418 numa_total += numa_info[i].node_mem;
420 if (numa_total != ram_size) {
421 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
422 " should equal RAM size (0x" RAM_ADDR_FMT ")",
423 numa_total, ram_size);
424 exit(1);
427 /* QEMU needs at least all unique node pair distances to build
428 * the whole NUMA distance table. QEMU treats the distance table
429 * as symmetric by default, i.e. distance A->B == distance B->A.
430 * Thus, QEMU is able to complete the distance table
431 * initialization even though only distance A->B is provided and
432 * distance B->A is not. QEMU knows the distance of a node to
433 * itself is always 10, so A->A distances may be omitted. When
434 * the distances of two nodes of a pair differ, i.e. distance
435 * A->B != distance B->A, then that means the distance table is
436 * asymmetric. In this case, the distances for both directions
437 * of all node pairs are required.
439 if (have_numa_distance) {
440 /* Validate enough NUMA distance information was provided. */
441 validate_numa_distance();
443 /* Validation succeeded, now fill in any missing distances. */
444 complete_init_numa_distance();
449 void parse_numa_opts(MachineState *ms)
451 qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal);
454 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
456 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
458 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
459 /* due to bug in libvirt, it doesn't pass node-id from props on
460 * device_add as expected, so we have to fix it up here */
461 if (slot->props.has_node_id) {
462 object_property_set_int(OBJECT(dev), slot->props.node_id,
463 "node-id", errp);
465 } else if (node_id != slot->props.node_id) {
466 error_setg(errp, "invalid node-id, must be %"PRId64,
467 slot->props.node_id);
471 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
472 const char *name,
473 uint64_t ram_size)
475 if (mem_path) {
476 #ifdef __linux__
477 Error *err = NULL;
478 memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, 0,
479 mem_path, &err);
480 if (err) {
481 error_report_err(err);
482 if (mem_prealloc) {
483 exit(1);
485 warn_report("falling back to regular RAM allocation");
486 error_printf("This is deprecated. Make sure that -mem-path "
487 " specified path has sufficient resources to allocate"
488 " -m specified RAM amount");
489 /* Legacy behavior: if allocation failed, fall back to
490 * regular RAM allocation.
492 mem_path = NULL;
493 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
495 #else
496 fprintf(stderr, "-mem-path not supported on this host\n");
497 exit(1);
498 #endif
499 } else {
500 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
502 vmstate_register_ram_global(mr);
505 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
506 const char *name,
507 uint64_t ram_size)
509 uint64_t addr = 0;
510 int i;
512 if (nb_numa_nodes == 0 || !have_memdevs) {
513 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
514 return;
517 memory_region_init(mr, owner, name, ram_size);
518 for (i = 0; i < nb_numa_nodes; i++) {
519 uint64_t size = numa_info[i].node_mem;
520 HostMemoryBackend *backend = numa_info[i].node_memdev;
521 if (!backend) {
522 continue;
524 MemoryRegion *seg = host_memory_backend_get_memory(backend);
526 if (memory_region_is_mapped(seg)) {
527 char *path = object_get_canonical_path_component(OBJECT(backend));
528 error_report("memory backend %s is used multiple times. Each "
529 "-numa option must use a different memdev value.",
530 path);
531 g_free(path);
532 exit(1);
535 host_memory_backend_set_mapped(backend, true);
536 memory_region_add_subregion(mr, addr, seg);
537 vmstate_register_ram_global(seg);
538 addr += size;
542 static void numa_stat_memory_devices(NumaNodeMem node_mem[])
544 MemoryDeviceInfoList *info_list = qmp_memory_device_list();
545 MemoryDeviceInfoList *info;
546 PCDIMMDeviceInfo *pcdimm_info;
547 VirtioPMEMDeviceInfo *vpi;
549 for (info = info_list; info; info = info->next) {
550 MemoryDeviceInfo *value = info->value;
552 if (value) {
553 switch (value->type) {
554 case MEMORY_DEVICE_INFO_KIND_DIMM:
555 case MEMORY_DEVICE_INFO_KIND_NVDIMM:
556 pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ?
557 value->u.dimm.data : value->u.nvdimm.data;
558 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
559 node_mem[pcdimm_info->node].node_plugged_mem +=
560 pcdimm_info->size;
561 break;
562 case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM:
563 vpi = value->u.virtio_pmem.data;
564 /* TODO: once we support numa, assign to right node */
565 node_mem[0].node_mem += vpi->size;
566 node_mem[0].node_plugged_mem += vpi->size;
567 break;
568 default:
569 g_assert_not_reached();
573 qapi_free_MemoryDeviceInfoList(info_list);
576 void query_numa_node_mem(NumaNodeMem node_mem[])
578 int i;
580 if (nb_numa_nodes <= 0) {
581 return;
584 numa_stat_memory_devices(node_mem);
585 for (i = 0; i < nb_numa_nodes; i++) {
586 node_mem[i].node_mem += numa_info[i].node_mem;
590 void ram_block_notifier_add(RAMBlockNotifier *n)
592 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
595 void ram_block_notifier_remove(RAMBlockNotifier *n)
597 QLIST_REMOVE(n, next);
600 void ram_block_notify_add(void *host, size_t size)
602 RAMBlockNotifier *notifier;
604 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
605 notifier->ram_block_added(notifier, host, size);
609 void ram_block_notify_remove(void *host, size_t size)
611 RAMBlockNotifier *notifier;
613 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
614 notifier->ram_block_removed(notifier, host, size);