]> git.proxmox.com Git - mirror_qemu.git/blame - numa.c
Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20171115' into staging
[mirror_qemu.git] / numa.c
CommitLineData
96d0e26c
WG
1/*
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
d38ea87a 25#include "qemu/osdep.h"
e35704ba 26#include "sysemu/numa.h"
96d0e26c 27#include "exec/cpu-common.h"
0987d735 28#include "exec/ramlist.h"
96d0e26c
WG
29#include "qemu/bitmap.h"
30#include "qom/cpu.h"
2b631ec2
WG
31#include "qemu/error-report.h"
32#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
0042109a
WG
33#include "qapi-visit.h"
34#include "qapi/opts-visitor.h"
dfabb8b9 35#include "hw/boards.h"
7febe36f 36#include "sysemu/hostmem.h"
76b5d850 37#include "qmp-commands.h"
5b009e40 38#include "hw/mem/pc-dimm.h"
7dcd1d70
EH
39#include "qemu/option.h"
40#include "qemu/config-file.h"
cc001888 41#include "qemu/cutils.h"
0042109a
WG
42
43QemuOptsList qemu_numa_opts = {
44 .name = "numa",
45 .implied_opt_name = "type",
46 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
47 .desc = { { 0 } } /* validated with OptsVisitor */
48};
49
7febe36f 50static int have_memdevs = -1;
25712ffe
EH
51static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
52 * For all nodes, nodeid < max_numa_nodeid
53 */
de1a7c84 54int nb_numa_nodes;
0f203430 55bool have_numa_distance;
de1a7c84 56NodeInfo numa_info[MAX_NODES];
7febe36f 57
fa9ea81d
BR
58void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
59{
672558d2 60 struct numa_addr_range *range;
fa9ea81d 61
abafabd8
BR
62 /*
63 * Memory-less nodes can come here with 0 size in which case,
64 * there is nothing to do.
65 */
66 if (!size) {
67 return;
68 }
69
672558d2 70 range = g_malloc0(sizeof(*range));
fa9ea81d
BR
71 range->mem_start = addr;
72 range->mem_end = addr + size - 1;
73 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
74}
75
76void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
77{
78 struct numa_addr_range *range, *next;
79
80 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
81 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
82 QLIST_REMOVE(range, entry);
83 g_free(range);
84 return;
85 }
86 }
87}
88
abafabd8
BR
89static void numa_set_mem_ranges(void)
90{
91 int i;
92 ram_addr_t mem_start = 0;
93
94 /*
95 * Deduce start address of each node and use it to store
96 * the address range info in numa_info address range list
97 */
98 for (i = 0; i < nb_numa_nodes; i++) {
99 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
100 mem_start += numa_info[i].node_mem;
101 }
102}
103
e75e2a14
BR
104/*
105 * Check if @addr falls under NUMA @node.
106 */
107static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
108{
109 struct numa_addr_range *range;
110
111 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
112 if (addr >= range->mem_start && addr <= range->mem_end) {
113 return true;
114 }
115 }
116 return false;
117}
118
119/*
120 * Given an address, return the index of the NUMA node to which the
121 * address belongs to.
122 */
123uint32_t numa_get_node(ram_addr_t addr, Error **errp)
124{
125 uint32_t i;
126
127 /* For non NUMA configurations, check if the addr falls under node 0 */
128 if (!nb_numa_nodes) {
129 if (numa_addr_belongs_to_node(addr, 0)) {
130 return 0;
131 }
132 }
133
134 for (i = 0; i < nb_numa_nodes; i++) {
135 if (numa_addr_belongs_to_node(addr, i)) {
136 return i;
137 }
138 }
139
140 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
141 "NUMA node", addr);
142 return -1;
143}
144
64c2a8f6 145static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
cc001888 146 Error **errp)
96d0e26c 147{
0042109a
WG
148 uint16_t nodenr;
149 uint16List *cpus = NULL;
64c2a8f6 150 MachineClass *mc = MACHINE_GET_CLASS(ms);
96d0e26c 151
0042109a
WG
152 if (node->has_nodeid) {
153 nodenr = node->nodeid;
96d0e26c 154 } else {
0042109a 155 nodenr = nb_numa_nodes;
96d0e26c
WG
156 }
157
0042109a
WG
158 if (nodenr >= MAX_NODES) {
159 error_setg(errp, "Max number of NUMA nodes reached: %"
01bbbcf4 160 PRIu16 "", nodenr);
0042109a 161 return;
96d0e26c
WG
162 }
163
1945b9d8
EH
164 if (numa_info[nodenr].present) {
165 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
166 return;
167 }
168
64c2a8f6
IM
169 if (!mc->cpu_index_to_instance_props) {
170 error_report("NUMA is not supported by this machine-type");
171 exit(1);
172 }
0042109a 173 for (cpus = node->cpus; cpus; cpus = cpus->next) {
7c88e65d 174 CpuInstanceProperties props;
8979c945
EH
175 if (cpus->value >= max_cpus) {
176 error_setg(errp,
177 "CPU index (%" PRIu16 ")"
178 " should be smaller than maxcpus (%d)",
179 cpus->value, max_cpus);
0042109a
WG
180 return;
181 }
7c88e65d
IM
182 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);
96d0e26c
WG
186 }
187
7febe36f 188 if (node->has_mem && node->has_memdev) {
d0e31a10 189 error_setg(errp, "cannot specify both mem= and memdev=");
7febe36f
PB
190 return;
191 }
192
193 if (have_memdevs == -1) {
194 have_memdevs = node->has_memdev;
195 }
196 if (node->has_memdev != have_memdevs) {
d0e31a10 197 error_setg(errp, "memdev option must be specified for either "
01bbbcf4 198 "all or no nodes");
7febe36f
PB
199 return;
200 }
201
0042109a 202 if (node->has_mem) {
cc001888 203 numa_info[nodenr].node_mem = node->mem;
0042109a 204 }
7febe36f
PB
205 if (node->has_memdev) {
206 Object *o;
207 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
208 if (!o) {
209 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
210 return;
211 }
212
213 object_ref(o);
61d7c144 214 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
7febe36f
PB
215 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
216 }
1af878e0
EH
217 numa_info[nodenr].present = true;
218 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
96d0e26c
WG
219}
220
0f203430
HC
221static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
222{
223 uint16_t src = dist->src;
224 uint16_t dst = dist->dst;
225 uint8_t val = dist->val;
226
227 if (src >= MAX_NODES || dst >= MAX_NODES) {
228 error_setg(errp,
f892291e 229 "Invalid node %d, max possible could be %d",
0f203430
HC
230 MAX(src, dst), MAX_NODES);
231 return;
232 }
233
234 if (!numa_info[src].present || !numa_info[dst].present) {
235 error_setg(errp, "Source/Destination NUMA node is missing. "
236 "Please use '-numa node' option to declare it first.");
237 return;
238 }
239
240 if (val < NUMA_DISTANCE_MIN) {
241 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
242 "it shouldn't be less than %d.",
243 val, NUMA_DISTANCE_MIN);
244 return;
245 }
246
247 if (src == dst && val != NUMA_DISTANCE_MIN) {
248 error_setg(errp, "Local distance of node %d should be %d.",
249 src, NUMA_DISTANCE_MIN);
250 return;
251 }
252
253 numa_info[src].distance[dst] = val;
254 have_numa_distance = true;
255}
256
28d0de7a 257static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
96d0e26c 258{
0042109a 259 NumaOptions *object = NULL;
64c2a8f6 260 MachineState *ms = opaque;
0042109a 261 Error *err = NULL;
96d0e26c 262
0042109a 263 {
09204eac
EB
264 Visitor *v = opts_visitor_new(opts);
265 visit_type_NumaOptions(v, NULL, &object, &err);
266 visit_free(v);
96d0e26c 267 }
96d0e26c 268
0042109a 269 if (err) {
157e94e8 270 goto end;
0042109a 271 }
96d0e26c 272
cc001888
IM
273 /* Fix up legacy suffix-less format */
274 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
275 const char *mem_str = qemu_opt_get(opts, "mem");
276 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
277 }
278
1fd5d4fe 279 switch (object->type) {
d081a49a 280 case NUMA_OPTIONS_TYPE_NODE:
cc001888 281 parse_numa_node(ms, &object->u.node, &err);
0042109a 282 if (err) {
157e94e8 283 goto end;
96d0e26c 284 }
0042109a
WG
285 nb_numa_nodes++;
286 break;
0f203430
HC
287 case NUMA_OPTIONS_TYPE_DIST:
288 parse_numa_distance(&object->u.dist, &err);
289 if (err) {
290 goto end;
291 }
292 break;
419fcdec
IM
293 case NUMA_OPTIONS_TYPE_CPU:
294 if (!object->u.cpu.has_node_id) {
295 error_setg(&err, "Missing mandatory node-id property");
296 goto end;
297 }
298 if (!numa_info[object->u.cpu.node_id].present) {
299 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
300 "defined with -numa node,nodeid=ID before it's used with "
301 "-numa cpu,node-id=ID", object->u.cpu.node_id);
302 goto end;
303 }
304
305 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
306 &err);
307 break;
0042109a
WG
308 default:
309 abort();
310 }
96d0e26c 311
157e94e8 312end:
96a1616c 313 qapi_free_NumaOptions(object);
157e94e8
MAL
314 if (err) {
315 error_report_err(err);
316 return -1;
317 }
0042109a 318
157e94e8 319 return 0;
96d0e26c
WG
320}
321
0f203430
HC
322/* If all node pair distances are symmetric, then only distances
323 * in one direction are enough. If there is even one asymmetric
324 * pair, though, then all distances must be provided. The
325 * distance from a node to itself is always NUMA_DISTANCE_MIN,
326 * so providing it is never necessary.
327 */
328static void validate_numa_distance(void)
3ef71975 329{
0f203430
HC
330 int src, dst;
331 bool is_asymmetrical = false;
332
333 for (src = 0; src < nb_numa_nodes; src++) {
334 for (dst = src; dst < nb_numa_nodes; dst++) {
335 if (numa_info[src].distance[dst] == 0 &&
336 numa_info[dst].distance[src] == 0) {
337 if (src != dst) {
338 error_report("The distance between node %d and %d is "
339 "missing, at least one distance value "
340 "between each nodes should be provided.",
341 src, dst);
342 exit(EXIT_FAILURE);
343 }
344 }
3ef71975 345
0f203430
HC
346 if (numa_info[src].distance[dst] != 0 &&
347 numa_info[dst].distance[src] != 0 &&
348 numa_info[src].distance[dst] !=
349 numa_info[dst].distance[src]) {
350 is_asymmetrical = true;
351 }
352 }
353 }
354
355 if (is_asymmetrical) {
356 for (src = 0; src < nb_numa_nodes; src++) {
357 for (dst = 0; dst < nb_numa_nodes; dst++) {
358 if (src != dst && numa_info[src].distance[dst] == 0) {
359 error_report("At least one asymmetrical pair of "
360 "distances is given, please provide distances "
361 "for both directions of all node pairs.");
362 exit(EXIT_FAILURE);
363 }
364 }
365 }
3ef71975 366 }
3ef71975
EH
367}
368
0f203430 369static void complete_init_numa_distance(void)
3ef71975 370{
0f203430 371 int src, dst;
3ef71975 372
0f203430
HC
373 /* Fixup NUMA distance by symmetric policy because if it is an
374 * asymmetric distance table, it should be a complete table and
375 * there would not be any missing distance except local node, which
376 * is verified by validate_numa_distance above.
377 */
378 for (src = 0; src < nb_numa_nodes; src++) {
379 for (dst = 0; dst < nb_numa_nodes; dst++) {
380 if (numa_info[src].distance[dst] == 0) {
381 if (src == dst) {
382 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
383 } else {
384 numa_info[src].distance[dst] = numa_info[dst].distance[src];
385 }
386 }
3ef71975 387 }
3ef71975 388 }
0f203430 389}
549fc54b 390
3bfe5716
LV
391void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
392 int nb_nodes, ram_addr_t size)
393{
394 int i;
395 uint64_t usedmem = 0;
396
397 /* Align each node according to the alignment
398 * requirements of the machine class
399 */
400
401 for (i = 0; i < nb_nodes - 1; i++) {
402 nodes[i].node_mem = (size / nb_nodes) &
403 ~((1 << mc->numa_mem_align_shift) - 1);
404 usedmem += nodes[i].node_mem;
549fc54b 405 }
3bfe5716 406 nodes[i].node_mem = size - usedmem;
3ef71975
EH
407}
408
3bfe5716
LV
409void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
410 int nb_nodes, ram_addr_t size)
96d0e26c 411{
12d6e464 412 int i;
3bfe5716
LV
413 uint64_t usedmem = 0, node_mem;
414 uint64_t granularity = size / nb_nodes;
415 uint64_t propagate = 0;
416
417 for (i = 0; i < nb_nodes - 1; i++) {
418 node_mem = (granularity + propagate) &
419 ~((1 << mc->numa_mem_align_shift) - 1);
420 propagate = granularity + propagate - node_mem;
421 nodes[i].node_mem = node_mem;
422 usedmem += node_mem;
423 }
424 nodes[i].node_mem = size - usedmem;
425}
12d6e464 426
ea089eeb 427void parse_numa_opts(MachineState *ms)
96d0e26c 428{
12d6e464 429 int i;
ea089eeb 430 MachineClass *mc = MACHINE_GET_CLASS(ms);
cdda2018 431
64c2a8f6 432 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
7dcd1d70
EH
433 exit(1);
434 }
435
12d6e464
EH
436 assert(max_numa_nodeid <= MAX_NODES);
437
438 /* No support for sparse NUMA node IDs yet: */
439 for (i = max_numa_nodeid - 1; i >= 0; i--) {
440 /* Report large node IDs first, to make mistakes easier to spot */
441 if (!numa_info[i].present) {
442 error_report("numa: Node ID missing: %d", i);
443 exit(1);
444 }
445 }
446
447 /* This must be always true if all nodes are present: */
448 assert(nb_numa_nodes == max_numa_nodeid);
449
96d0e26c 450 if (nb_numa_nodes > 0) {
2b631ec2 451 uint64_t numa_total;
96d0e26c
WG
452
453 if (nb_numa_nodes > MAX_NODES) {
454 nb_numa_nodes = MAX_NODES;
455 }
456
9851d0fe 457 /* If no memory size is given for any node, assume the default case
96d0e26c
WG
458 * and distribute the available memory equally across all nodes
459 */
460 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 461 if (numa_info[i].node_mem != 0) {
96d0e26c
WG
462 break;
463 }
464 }
465 if (i == nb_numa_nodes) {
3bfe5716
LV
466 assert(mc->numa_auto_assign_ram);
467 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
96d0e26c
WG
468 }
469
2b631ec2
WG
470 numa_total = 0;
471 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 472 numa_total += numa_info[i].node_mem;
2b631ec2
WG
473 }
474 if (numa_total != ram_size) {
c68233ae
HT
475 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
476 " should equal RAM size (0x" RAM_ADDR_FMT ")",
2b631ec2
WG
477 numa_total, ram_size);
478 exit(1);
479 }
480
fa9ea81d
BR
481 for (i = 0; i < nb_numa_nodes; i++) {
482 QLIST_INIT(&numa_info[i].addr);
483 }
484
abafabd8
BR
485 numa_set_mem_ranges();
486
0f203430
HC
487 /* QEMU needs at least all unique node pair distances to build
488 * the whole NUMA distance table. QEMU treats the distance table
489 * as symmetric by default, i.e. distance A->B == distance B->A.
490 * Thus, QEMU is able to complete the distance table
491 * initialization even though only distance A->B is provided and
492 * distance B->A is not. QEMU knows the distance of a node to
493 * itself is always 10, so A->A distances may be omitted. When
494 * the distances of two nodes of a pair differ, i.e. distance
495 * A->B != distance B->A, then that means the distance table is
496 * asymmetric. In this case, the distances for both directions
497 * of all node pairs are required.
498 */
499 if (have_numa_distance) {
500 /* Validate enough NUMA distance information was provided. */
501 validate_numa_distance();
96d0e26c 502
0f203430
HC
503 /* Validation succeeded, now fill in any missing distances. */
504 complete_init_numa_distance();
96d0e26c 505 }
abafabd8
BR
506 } else {
507 numa_set_mem_node_id(0, ram_size, 0);
96d0e26c
WG
508 }
509}
dfabb8b9 510
a0ceb640
IM
511void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
512{
a0ceb640
IM
513 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
514
a0ceb640
IM
515 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
516 /* due to bug in libvirt, it doesn't pass node-id from props on
517 * device_add as expected, so we have to fix it up here */
d41f3e75
IM
518 if (slot->props.has_node_id) {
519 object_property_set_int(OBJECT(dev), slot->props.node_id,
520 "node-id", errp);
521 }
522 } else if (node_id != slot->props.node_id) {
a0ceb640
IM
523 error_setg(errp, "node-id=%d must match numa node specified "
524 "with -numa option", node_id);
525 }
526}
527
7febe36f
PB
528static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
529 const char *name,
530 uint64_t ram_size)
531{
0b183fc8
PB
532 if (mem_path) {
533#ifdef __linux__
7f56e740 534 Error *err = NULL;
dbcb8981 535 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
7f56e740 536 mem_path, &err);
c3ba3095 537 if (err) {
29b762f5 538 error_report_err(err);
fae947b0
LC
539 if (mem_prealloc) {
540 exit(1);
541 }
542
543 /* Legacy behavior: if allocation failed, fall back to
544 * regular RAM allocation.
545 */
1cfe48c1 546 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
7f56e740 547 }
0b183fc8
PB
548#else
549 fprintf(stderr, "-mem-path not supported on this host\n");
550 exit(1);
551#endif
552 } else {
1cfe48c1 553 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
0b183fc8 554 }
7febe36f
PB
555 vmstate_register_ram_global(mr);
556}
557
dfabb8b9
PB
558void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
559 const char *name,
560 uint64_t ram_size)
561{
7febe36f
PB
562 uint64_t addr = 0;
563 int i;
564
565 if (nb_numa_nodes == 0 || !have_memdevs) {
566 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
567 return;
568 }
569
570 memory_region_init(mr, owner, name, ram_size);
f51878ba 571 for (i = 0; i < nb_numa_nodes; i++) {
7febe36f
PB
572 uint64_t size = numa_info[i].node_mem;
573 HostMemoryBackend *backend = numa_info[i].node_memdev;
574 if (!backend) {
575 continue;
576 }
007b0657
MA
577 MemoryRegion *seg = host_memory_backend_get_memory(backend,
578 &error_fatal);
7febe36f 579
0462faee
HT
580 if (memory_region_is_mapped(seg)) {
581 char *path = object_get_canonical_path_component(OBJECT(backend));
582 error_report("memory backend %s is used multiple times. Each "
583 "-numa option must use a different memdev value.",
584 path);
585 exit(1);
586 }
587
0b217571 588 host_memory_backend_set_mapped(backend, true);
7febe36f
PB
589 memory_region_add_subregion(mr, addr, seg);
590 vmstate_register_ram_global(seg);
591 addr += size;
592 }
dfabb8b9 593}
76b5d850 594
31959e82 595static void numa_stat_memory_devices(NumaNodeMem node_mem[])
5b009e40
HZ
596{
597 MemoryDeviceInfoList *info_list = NULL;
598 MemoryDeviceInfoList **prev = &info_list;
599 MemoryDeviceInfoList *info;
31959e82 600 PCDIMMDeviceInfo *pcdimm_info;
5b009e40
HZ
601
602 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
603 for (info = info_list; info; info = info->next) {
604 MemoryDeviceInfo *value = info->value;
605
606 if (value) {
1fd5d4fe 607 switch (value->type) {
31959e82
VG
608 case MEMORY_DEVICE_INFO_KIND_DIMM: {
609 pcdimm_info = value->u.dimm.data;
610 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
611 if (pcdimm_info->hotpluggable && pcdimm_info->hotplugged) {
612 node_mem[pcdimm_info->node].node_plugged_mem +=
613 pcdimm_info->size;
614 }
5b009e40 615 break;
31959e82
VG
616 }
617
5b009e40
HZ
618 default:
619 break;
620 }
621 }
622 }
623 qapi_free_MemoryDeviceInfoList(info_list);
624}
625
31959e82 626void query_numa_node_mem(NumaNodeMem node_mem[])
5b009e40
HZ
627{
628 int i;
629
630 if (nb_numa_nodes <= 0) {
631 return;
632 }
633
634 numa_stat_memory_devices(node_mem);
635 for (i = 0; i < nb_numa_nodes; i++) {
31959e82 636 node_mem[i].node_mem += numa_info[i].node_mem;
5b009e40
HZ
637 }
638}
639
76b5d850
HT
640static int query_memdev(Object *obj, void *opaque)
641{
642 MemdevList **list = opaque;
b0e90181 643 MemdevList *m = NULL;
76b5d850
HT
644
645 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
b0e90181 646 m = g_malloc0(sizeof(*m));
76b5d850
HT
647
648 m->value = g_malloc0(sizeof(*m->value));
649
e1ff3c67
IM
650 m->value->id = object_property_get_str(obj, "id", NULL);
651 m->value->has_id = !!m->value->id;
652
61d7c144
MAL
653 m->value->size = object_property_get_uint(obj, "size",
654 &error_abort);
76b5d850 655 m->value->merge = object_property_get_bool(obj, "merge",
2f6f826e 656 &error_abort);
76b5d850 657 m->value->dump = object_property_get_bool(obj, "dump",
2f6f826e 658 &error_abort);
76b5d850 659 m->value->prealloc = object_property_get_bool(obj,
2f6f826e
MA
660 "prealloc",
661 &error_abort);
76b5d850
HT
662 m->value->policy = object_property_get_enum(obj,
663 "policy",
a3590dac 664 "HostMemPolicy",
2f6f826e 665 &error_abort);
76b5d850 666 object_property_get_uint16List(obj, "host-nodes",
2f6f826e
MA
667 &m->value->host_nodes,
668 &error_abort);
76b5d850
HT
669
670 m->next = *list;
671 *list = m;
672 }
673
674 return 0;
76b5d850
HT
675}
676
677MemdevList *qmp_query_memdev(Error **errp)
678{
2f6f826e 679 Object *obj = object_get_objects_root();
ecaf54a0 680 MemdevList *list = NULL;
76b5d850 681
2f6f826e 682 object_child_foreach(obj, query_memdev, &list);
76b5d850 683 return list;
76b5d850 684}
6bea1ddf 685
0987d735
PB
686void ram_block_notifier_add(RAMBlockNotifier *n)
687{
688 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
689}
690
691void ram_block_notifier_remove(RAMBlockNotifier *n)
692{
693 QLIST_REMOVE(n, next);
694}
695
696void ram_block_notify_add(void *host, size_t size)
697{
698 RAMBlockNotifier *notifier;
699
700 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
701 notifier->ram_block_added(notifier, host, size);
702 }
703}
704
705void ram_block_notify_remove(void *host, size_t size)
706{
707 RAMBlockNotifier *notifier;
708
709 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
710 notifier->ram_block_removed(notifier, host, size);
711 }
712}