s390x/flic: factor out injection of floating interrupts
[qemu/ar7.git] / numa.c
bloba9528aaa7d1a783f218843428d9cc9f6ea40811e
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 "qom/cpu.h"
31 #include "qemu/error-report.h"
32 #include "qapi-visit.h"
33 #include "qapi/opts-visitor.h"
34 #include "hw/boards.h"
35 #include "sysemu/hostmem.h"
36 #include "qmp-commands.h"
37 #include "hw/mem/pc-dimm.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qemu/cutils.h"
42 QemuOptsList 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 */
49 static int have_memdevs = -1;
50 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
51 * For all nodes, nodeid < max_numa_nodeid
53 int nb_numa_nodes;
54 bool have_numa_distance;
55 NodeInfo numa_info[MAX_NODES];
58 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
59 Error **errp)
61 uint16_t nodenr;
62 uint16List *cpus = NULL;
63 MachineClass *mc = MACHINE_GET_CLASS(ms);
65 if (node->has_nodeid) {
66 nodenr = node->nodeid;
67 } else {
68 nodenr = nb_numa_nodes;
71 if (nodenr >= MAX_NODES) {
72 error_setg(errp, "Max number of NUMA nodes reached: %"
73 PRIu16 "", nodenr);
74 return;
77 if (numa_info[nodenr].present) {
78 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
79 return;
82 if (!mc->cpu_index_to_instance_props) {
83 error_report("NUMA is not supported by this machine-type");
84 exit(1);
86 for (cpus = node->cpus; cpus; cpus = cpus->next) {
87 CpuInstanceProperties props;
88 if (cpus->value >= max_cpus) {
89 error_setg(errp,
90 "CPU index (%" PRIu16 ")"
91 " should be smaller than maxcpus (%d)",
92 cpus->value, max_cpus);
93 return;
95 props = mc->cpu_index_to_instance_props(ms, cpus->value);
96 props.node_id = nodenr;
97 props.has_node_id = true;
98 machine_set_cpu_numa_node(ms, &props, &error_fatal);
101 if (node->has_mem && node->has_memdev) {
102 error_setg(errp, "cannot specify both mem= and memdev=");
103 return;
106 if (have_memdevs == -1) {
107 have_memdevs = node->has_memdev;
109 if (node->has_memdev != have_memdevs) {
110 error_setg(errp, "memdev option must be specified for either "
111 "all or no nodes");
112 return;
115 if (node->has_mem) {
116 numa_info[nodenr].node_mem = node->mem;
118 if (node->has_memdev) {
119 Object *o;
120 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
121 if (!o) {
122 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
123 return;
126 object_ref(o);
127 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
128 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
130 numa_info[nodenr].present = true;
131 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
132 nb_numa_nodes++;
135 static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
137 uint16_t src = dist->src;
138 uint16_t dst = dist->dst;
139 uint8_t val = dist->val;
141 if (src >= MAX_NODES || dst >= MAX_NODES) {
142 error_setg(errp,
143 "Invalid node %d, max possible could be %d",
144 MAX(src, dst), MAX_NODES);
145 return;
148 if (!numa_info[src].present || !numa_info[dst].present) {
149 error_setg(errp, "Source/Destination NUMA node is missing. "
150 "Please use '-numa node' option to declare it first.");
151 return;
154 if (val < NUMA_DISTANCE_MIN) {
155 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
156 "it shouldn't be less than %d.",
157 val, NUMA_DISTANCE_MIN);
158 return;
161 if (src == dst && val != NUMA_DISTANCE_MIN) {
162 error_setg(errp, "Local distance of node %d should be %d.",
163 src, NUMA_DISTANCE_MIN);
164 return;
167 numa_info[src].distance[dst] = val;
168 have_numa_distance = true;
171 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
173 NumaOptions *object = NULL;
174 MachineState *ms = opaque;
175 Error *err = NULL;
178 Visitor *v = opts_visitor_new(opts);
179 visit_type_NumaOptions(v, NULL, &object, &err);
180 visit_free(v);
183 if (err) {
184 goto end;
187 /* Fix up legacy suffix-less format */
188 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
189 const char *mem_str = qemu_opt_get(opts, "mem");
190 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
193 switch (object->type) {
194 case NUMA_OPTIONS_TYPE_NODE:
195 parse_numa_node(ms, &object->u.node, &err);
196 if (err) {
197 goto end;
199 break;
200 case NUMA_OPTIONS_TYPE_DIST:
201 parse_numa_distance(&object->u.dist, &err);
202 if (err) {
203 goto end;
205 break;
206 case NUMA_OPTIONS_TYPE_CPU:
207 if (!object->u.cpu.has_node_id) {
208 error_setg(&err, "Missing mandatory node-id property");
209 goto end;
211 if (!numa_info[object->u.cpu.node_id].present) {
212 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
213 "defined with -numa node,nodeid=ID before it's used with "
214 "-numa cpu,node-id=ID", object->u.cpu.node_id);
215 goto end;
218 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
219 &err);
220 break;
221 default:
222 abort();
225 end:
226 qapi_free_NumaOptions(object);
227 if (err) {
228 error_report_err(err);
229 return -1;
232 return 0;
235 /* If all node pair distances are symmetric, then only distances
236 * in one direction are enough. If there is even one asymmetric
237 * pair, though, then all distances must be provided. The
238 * distance from a node to itself is always NUMA_DISTANCE_MIN,
239 * so providing it is never necessary.
241 static void validate_numa_distance(void)
243 int src, dst;
244 bool is_asymmetrical = false;
246 for (src = 0; src < nb_numa_nodes; src++) {
247 for (dst = src; dst < nb_numa_nodes; dst++) {
248 if (numa_info[src].distance[dst] == 0 &&
249 numa_info[dst].distance[src] == 0) {
250 if (src != dst) {
251 error_report("The distance between node %d and %d is "
252 "missing, at least one distance value "
253 "between each nodes should be provided.",
254 src, dst);
255 exit(EXIT_FAILURE);
259 if (numa_info[src].distance[dst] != 0 &&
260 numa_info[dst].distance[src] != 0 &&
261 numa_info[src].distance[dst] !=
262 numa_info[dst].distance[src]) {
263 is_asymmetrical = true;
268 if (is_asymmetrical) {
269 for (src = 0; src < nb_numa_nodes; src++) {
270 for (dst = 0; dst < nb_numa_nodes; dst++) {
271 if (src != dst && numa_info[src].distance[dst] == 0) {
272 error_report("At least one asymmetrical pair of "
273 "distances is given, please provide distances "
274 "for both directions of all node pairs.");
275 exit(EXIT_FAILURE);
282 static void complete_init_numa_distance(void)
284 int src, dst;
286 /* Fixup NUMA distance by symmetric policy because if it is an
287 * asymmetric distance table, it should be a complete table and
288 * there would not be any missing distance except local node, which
289 * is verified by validate_numa_distance above.
291 for (src = 0; src < nb_numa_nodes; src++) {
292 for (dst = 0; dst < nb_numa_nodes; dst++) {
293 if (numa_info[src].distance[dst] == 0) {
294 if (src == dst) {
295 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
296 } else {
297 numa_info[src].distance[dst] = numa_info[dst].distance[src];
304 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
305 int nb_nodes, ram_addr_t size)
307 int i;
308 uint64_t usedmem = 0;
310 /* Align each node according to the alignment
311 * requirements of the machine class
314 for (i = 0; i < nb_nodes - 1; i++) {
315 nodes[i].node_mem = (size / nb_nodes) &
316 ~((1 << mc->numa_mem_align_shift) - 1);
317 usedmem += nodes[i].node_mem;
319 nodes[i].node_mem = size - usedmem;
322 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
323 int nb_nodes, ram_addr_t size)
325 int i;
326 uint64_t usedmem = 0, node_mem;
327 uint64_t granularity = size / nb_nodes;
328 uint64_t propagate = 0;
330 for (i = 0; i < nb_nodes - 1; i++) {
331 node_mem = (granularity + propagate) &
332 ~((1 << mc->numa_mem_align_shift) - 1);
333 propagate = granularity + propagate - node_mem;
334 nodes[i].node_mem = node_mem;
335 usedmem += node_mem;
337 nodes[i].node_mem = size - usedmem;
340 void parse_numa_opts(MachineState *ms)
342 int i;
343 MachineClass *mc = MACHINE_GET_CLASS(ms);
345 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
346 exit(1);
350 * If memory hotplug is enabled (slots > 0) but without '-numa'
351 * options explicitly on CLI, guestes will break.
353 * Windows: won't enable memory hotplug without SRAT table at all
355 * Linux: if QEMU is started with initial memory all below 4Gb
356 * and no SRAT table present, guest kernel will use nommu DMA ops,
357 * which breaks 32bit hw drivers when memory is hotplugged and
358 * guest tries to use it with that drivers.
360 * Enable NUMA implicitly by adding a new NUMA node automatically.
362 if (ms->ram_slots > 0 && nb_numa_nodes == 0 &&
363 mc->auto_enable_numa_with_memhp) {
364 NumaNodeOptions node = { };
365 parse_numa_node(ms, &node, NULL);
368 assert(max_numa_nodeid <= MAX_NODES);
370 /* No support for sparse NUMA node IDs yet: */
371 for (i = max_numa_nodeid - 1; i >= 0; i--) {
372 /* Report large node IDs first, to make mistakes easier to spot */
373 if (!numa_info[i].present) {
374 error_report("numa: Node ID missing: %d", i);
375 exit(1);
379 /* This must be always true if all nodes are present: */
380 assert(nb_numa_nodes == max_numa_nodeid);
382 if (nb_numa_nodes > 0) {
383 uint64_t numa_total;
385 if (nb_numa_nodes > MAX_NODES) {
386 nb_numa_nodes = MAX_NODES;
389 /* If no memory size is given for any node, assume the default case
390 * and distribute the available memory equally across all nodes
392 for (i = 0; i < nb_numa_nodes; i++) {
393 if (numa_info[i].node_mem != 0) {
394 break;
397 if (i == nb_numa_nodes) {
398 assert(mc->numa_auto_assign_ram);
399 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
402 numa_total = 0;
403 for (i = 0; i < nb_numa_nodes; i++) {
404 numa_total += numa_info[i].node_mem;
406 if (numa_total != ram_size) {
407 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
408 " should equal RAM size (0x" RAM_ADDR_FMT ")",
409 numa_total, ram_size);
410 exit(1);
413 /* QEMU needs at least all unique node pair distances to build
414 * the whole NUMA distance table. QEMU treats the distance table
415 * as symmetric by default, i.e. distance A->B == distance B->A.
416 * Thus, QEMU is able to complete the distance table
417 * initialization even though only distance A->B is provided and
418 * distance B->A is not. QEMU knows the distance of a node to
419 * itself is always 10, so A->A distances may be omitted. When
420 * the distances of two nodes of a pair differ, i.e. distance
421 * A->B != distance B->A, then that means the distance table is
422 * asymmetric. In this case, the distances for both directions
423 * of all node pairs are required.
425 if (have_numa_distance) {
426 /* Validate enough NUMA distance information was provided. */
427 validate_numa_distance();
429 /* Validation succeeded, now fill in any missing distances. */
430 complete_init_numa_distance();
435 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
437 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
439 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
440 /* due to bug in libvirt, it doesn't pass node-id from props on
441 * device_add as expected, so we have to fix it up here */
442 if (slot->props.has_node_id) {
443 object_property_set_int(OBJECT(dev), slot->props.node_id,
444 "node-id", errp);
446 } else if (node_id != slot->props.node_id) {
447 error_setg(errp, "node-id=%d must match numa node specified "
448 "with -numa option", node_id);
452 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
453 const char *name,
454 uint64_t ram_size)
456 if (mem_path) {
457 #ifdef __linux__
458 Error *err = NULL;
459 memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, false,
460 mem_path, &err);
461 if (err) {
462 error_report_err(err);
463 if (mem_prealloc) {
464 exit(1);
466 error_report("falling back to regular RAM allocation.");
468 /* Legacy behavior: if allocation failed, fall back to
469 * regular RAM allocation.
471 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
473 #else
474 fprintf(stderr, "-mem-path not supported on this host\n");
475 exit(1);
476 #endif
477 } else {
478 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
480 vmstate_register_ram_global(mr);
483 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
484 const char *name,
485 uint64_t ram_size)
487 uint64_t addr = 0;
488 int i;
490 if (nb_numa_nodes == 0 || !have_memdevs) {
491 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
492 return;
495 memory_region_init(mr, owner, name, ram_size);
496 for (i = 0; i < nb_numa_nodes; i++) {
497 uint64_t size = numa_info[i].node_mem;
498 HostMemoryBackend *backend = numa_info[i].node_memdev;
499 if (!backend) {
500 continue;
502 MemoryRegion *seg = host_memory_backend_get_memory(backend,
503 &error_fatal);
505 if (memory_region_is_mapped(seg)) {
506 char *path = object_get_canonical_path_component(OBJECT(backend));
507 error_report("memory backend %s is used multiple times. Each "
508 "-numa option must use a different memdev value.",
509 path);
510 exit(1);
513 host_memory_backend_set_mapped(backend, true);
514 memory_region_add_subregion(mr, addr, seg);
515 vmstate_register_ram_global(seg);
516 addr += size;
520 static void numa_stat_memory_devices(NumaNodeMem node_mem[])
522 MemoryDeviceInfoList *info_list = NULL;
523 MemoryDeviceInfoList **prev = &info_list;
524 MemoryDeviceInfoList *info;
525 PCDIMMDeviceInfo *pcdimm_info;
527 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
528 for (info = info_list; info; info = info->next) {
529 MemoryDeviceInfo *value = info->value;
531 if (value) {
532 switch (value->type) {
533 case MEMORY_DEVICE_INFO_KIND_DIMM: {
534 pcdimm_info = value->u.dimm.data;
535 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
536 if (pcdimm_info->hotpluggable && pcdimm_info->hotplugged) {
537 node_mem[pcdimm_info->node].node_plugged_mem +=
538 pcdimm_info->size;
540 break;
543 default:
544 break;
548 qapi_free_MemoryDeviceInfoList(info_list);
551 void query_numa_node_mem(NumaNodeMem node_mem[])
553 int i;
555 if (nb_numa_nodes <= 0) {
556 return;
559 numa_stat_memory_devices(node_mem);
560 for (i = 0; i < nb_numa_nodes; i++) {
561 node_mem[i].node_mem += numa_info[i].node_mem;
565 static int query_memdev(Object *obj, void *opaque)
567 MemdevList **list = opaque;
568 MemdevList *m = NULL;
570 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
571 m = g_malloc0(sizeof(*m));
573 m->value = g_malloc0(sizeof(*m->value));
575 m->value->id = object_property_get_str(obj, "id", NULL);
576 m->value->has_id = !!m->value->id;
578 m->value->size = object_property_get_uint(obj, "size",
579 &error_abort);
580 m->value->merge = object_property_get_bool(obj, "merge",
581 &error_abort);
582 m->value->dump = object_property_get_bool(obj, "dump",
583 &error_abort);
584 m->value->prealloc = object_property_get_bool(obj,
585 "prealloc",
586 &error_abort);
587 m->value->policy = object_property_get_enum(obj,
588 "policy",
589 "HostMemPolicy",
590 &error_abort);
591 object_property_get_uint16List(obj, "host-nodes",
592 &m->value->host_nodes,
593 &error_abort);
595 m->next = *list;
596 *list = m;
599 return 0;
602 MemdevList *qmp_query_memdev(Error **errp)
604 Object *obj = object_get_objects_root();
605 MemdevList *list = NULL;
607 object_child_foreach(obj, query_memdev, &list);
608 return list;
611 void ram_block_notifier_add(RAMBlockNotifier *n)
613 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
616 void ram_block_notifier_remove(RAMBlockNotifier *n)
618 QLIST_REMOVE(n, next);
621 void ram_block_notify_add(void *host, size_t size)
623 RAMBlockNotifier *notifier;
625 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
626 notifier->ram_block_added(notifier, host, size);
630 void ram_block_notify_remove(void *host, size_t size)
632 RAMBlockNotifier *notifier;
634 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
635 notifier->ram_block_removed(notifier, host, size);