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