blob: 572712ccf990ca7ec08e5d396e792f1c3c548179 [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"
28#include "qemu/bitmap.h"
29#include "qom/cpu.h"
Wanlong Gao2b631ec2014-05-14 17:43:06 +080030#include "qemu/error-report.h"
31#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
Wanlong Gao00421092014-05-14 17:43:08 +080032#include "qapi-visit.h"
33#include "qapi/opts-visitor.h"
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +080034#include "hw/boards.h"
Paolo Bonzini7febe362014-05-14 17:43:17 +080035#include "sysemu/hostmem.h"
Hu Tao76b5d852014-06-16 18:05:41 +080036#include "qmp-commands.h"
zhanghailiang5b009e42014-11-04 19:49:30 +080037#include "hw/mem/pc-dimm.h"
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -020038#include "qemu/option.h"
39#include "qemu/config-file.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080040
Wanlong Gao00421092014-05-14 17:43:08 +080041QemuOptsList qemu_numa_opts = {
42 .name = "numa",
43 .implied_opt_name = "type",
44 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
45 .desc = { { 0 } } /* validated with OptsVisitor */
46};
47
Paolo Bonzini7febe362014-05-14 17:43:17 +080048static int have_memdevs = -1;
Eduardo Habkost25712ff2015-02-08 16:51:19 -020049static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
50 * For all nodes, nodeid < max_numa_nodeid
51 */
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020052int nb_numa_nodes;
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020053NodeInfo numa_info[MAX_NODES];
Paolo Bonzini7febe362014-05-14 17:43:17 +080054
Bharata B Raofa9ea812015-06-29 13:50:25 +053055void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
56{
Bharata B Rao672558d2015-07-09 20:57:36 +053057 struct numa_addr_range *range;
Bharata B Raofa9ea812015-06-29 13:50:25 +053058
Bharata B Raoabafabd2015-06-29 13:50:26 +053059 /*
60 * Memory-less nodes can come here with 0 size in which case,
61 * there is nothing to do.
62 */
63 if (!size) {
64 return;
65 }
66
Bharata B Rao672558d2015-07-09 20:57:36 +053067 range = g_malloc0(sizeof(*range));
Bharata B Raofa9ea812015-06-29 13:50:25 +053068 range->mem_start = addr;
69 range->mem_end = addr + size - 1;
70 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
71}
72
73void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
74{
75 struct numa_addr_range *range, *next;
76
77 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
78 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
79 QLIST_REMOVE(range, entry);
80 g_free(range);
81 return;
82 }
83 }
84}
85
Bharata B Raoabafabd2015-06-29 13:50:26 +053086static void numa_set_mem_ranges(void)
87{
88 int i;
89 ram_addr_t mem_start = 0;
90
91 /*
92 * Deduce start address of each node and use it to store
93 * the address range info in numa_info address range list
94 */
95 for (i = 0; i < nb_numa_nodes; i++) {
96 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
97 mem_start += numa_info[i].node_mem;
98 }
99}
100
Bharata B Raoe75e2a12015-06-29 13:50:27 +0530101/*
102 * Check if @addr falls under NUMA @node.
103 */
104static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
105{
106 struct numa_addr_range *range;
107
108 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
109 if (addr >= range->mem_start && addr <= range->mem_end) {
110 return true;
111 }
112 }
113 return false;
114}
115
116/*
117 * Given an address, return the index of the NUMA node to which the
118 * address belongs to.
119 */
120uint32_t numa_get_node(ram_addr_t addr, Error **errp)
121{
122 uint32_t i;
123
124 /* For non NUMA configurations, check if the addr falls under node 0 */
125 if (!nb_numa_nodes) {
126 if (numa_addr_belongs_to_node(addr, 0)) {
127 return 0;
128 }
129 }
130
131 for (i = 0; i < nb_numa_nodes; i++) {
132 if (numa_addr_belongs_to_node(addr, i)) {
133 return i;
134 }
135 }
136
137 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
138 "NUMA node", addr);
139 return -1;
140}
141
Wanlong Gao00421092014-05-14 17:43:08 +0800142static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800143{
Wanlong Gao00421092014-05-14 17:43:08 +0800144 uint16_t nodenr;
145 uint16List *cpus = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800146
Wanlong Gao00421092014-05-14 17:43:08 +0800147 if (node->has_nodeid) {
148 nodenr = node->nodeid;
149 } else {
150 nodenr = nb_numa_nodes;
151 }
152
153 if (nodenr >= MAX_NODES) {
154 error_setg(errp, "Max number of NUMA nodes reached: %"
Gonglei01bbbcf2015-02-25 12:22:30 +0800155 PRIu16 "", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800156 return;
157 }
158
Eduardo Habkost1945b9d2014-06-26 18:33:19 -0300159 if (numa_info[nodenr].present) {
160 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
161 return;
162 }
163
Wanlong Gao00421092014-05-14 17:43:08 +0800164 for (cpus = node->cpus; cpus; cpus = cpus->next) {
Eduardo Habkost8979c942015-02-09 17:28:52 -0200165 if (cpus->value >= max_cpus) {
166 error_setg(errp,
167 "CPU index (%" PRIu16 ")"
168 " should be smaller than maxcpus (%d)",
169 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +0800170 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800171 }
Wanlong Gao00421092014-05-14 17:43:08 +0800172 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800173 }
174
Paolo Bonzini7febe362014-05-14 17:43:17 +0800175 if (node->has_mem && node->has_memdev) {
Gonglei01bbbcf2015-02-25 12:22:30 +0800176 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800177 return;
178 }
179
180 if (have_memdevs == -1) {
181 have_memdevs = node->has_memdev;
182 }
183 if (node->has_memdev != have_memdevs) {
184 error_setg(errp, "qemu: memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800185 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800186 return;
187 }
188
Wanlong Gao00421092014-05-14 17:43:08 +0800189 if (node->has_mem) {
190 uint64_t mem_size = node->mem;
191 const char *mem_str = qemu_opt_get(opts, "mem");
192 /* Fix up legacy suffix-less format */
193 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
194 mem_size <<= 20;
195 }
196 numa_info[nodenr].node_mem = mem_size;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800197 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800198 if (node->has_memdev) {
199 Object *o;
200 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
201 if (!o) {
202 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
203 return;
204 }
205
206 object_ref(o);
207 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
208 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
209 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300210 numa_info[nodenr].present = true;
211 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800212}
213
Markus Armbruster28d0de72015-03-13 13:35:14 +0100214static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800215{
Wanlong Gao00421092014-05-14 17:43:08 +0800216 NumaOptions *object = NULL;
217 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800218
Wanlong Gao00421092014-05-14 17:43:08 +0800219 {
220 OptsVisitor *ov = opts_visitor_new(opts);
Eric Blake51e72bc2016-01-29 06:48:54 -0700221 visit_type_NumaOptions(opts_get_visitor(ov), NULL, &object, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800222 opts_visitor_cleanup(ov);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800223 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800224
Wanlong Gao00421092014-05-14 17:43:08 +0800225 if (err) {
226 goto error;
227 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800228
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600229 switch (object->type) {
Wanlong Gao00421092014-05-14 17:43:08 +0800230 case NUMA_OPTIONS_KIND_NODE:
Eric Blake32bafa82016-03-17 16:48:37 -0600231 numa_node_parse(object->u.node.data, opts, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800232 if (err) {
233 goto error;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800234 }
235 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800236 break;
237 default:
238 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800239 }
Wanlong Gao00421092014-05-14 17:43:08 +0800240
241 return 0;
242
243error:
Markus Armbruster29b762f2015-02-10 15:06:23 +0100244 error_report_err(err);
Eric Blake96a16162016-02-23 14:14:33 -0700245 qapi_free_NumaOptions(object);
Wanlong Gao00421092014-05-14 17:43:08 +0800246
247 return -1;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800248}
249
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200250static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
251{
252 int cpu;
253 bool first = true;
254 GString *s = g_string_new(NULL);
255
256 for (cpu = find_first_bit(cpus, max_cpus);
257 cpu < max_cpus;
258 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
259 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
260 first = false;
261 }
262 return g_string_free(s, FALSE);
263}
264
265static void validate_numa_cpus(void)
266{
267 int i;
268 DECLARE_BITMAP(seen_cpus, MAX_CPUMASK_BITS);
269
270 bitmap_zero(seen_cpus, MAX_CPUMASK_BITS);
271 for (i = 0; i < nb_numa_nodes; i++) {
272 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu,
273 MAX_CPUMASK_BITS)) {
274 bitmap_and(seen_cpus, seen_cpus,
275 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
276 error_report("CPU(s) present in multiple NUMA nodes: %s",
Daniel P. Berrangea8f15a22015-08-26 12:17:12 +0100277 enumerate_cpus(seen_cpus, max_cpus));
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200278 exit(EXIT_FAILURE);
279 }
280 bitmap_or(seen_cpus, seen_cpus,
281 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
282 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200283
284 if (!bitmap_full(seen_cpus, max_cpus)) {
285 char *msg;
286 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
287 msg = enumerate_cpus(seen_cpus, max_cpus);
288 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
289 error_report("warning: All CPU(s) up to maxcpus should be described "
290 "in NUMA config");
291 g_free(msg);
292 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200293}
294
Igor Mammedov57924bc2015-03-19 17:09:21 +0000295void parse_numa_opts(MachineClass *mc)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800296{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300297 int i;
298
Markus Armbruster28d0de72015-03-13 13:35:14 +0100299 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, NULL, NULL)) {
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200300 exit(1);
301 }
302
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300303 assert(max_numa_nodeid <= MAX_NODES);
304
305 /* No support for sparse NUMA node IDs yet: */
306 for (i = max_numa_nodeid - 1; i >= 0; i--) {
307 /* Report large node IDs first, to make mistakes easier to spot */
308 if (!numa_info[i].present) {
309 error_report("numa: Node ID missing: %d", i);
310 exit(1);
311 }
312 }
313
314 /* This must be always true if all nodes are present: */
315 assert(nb_numa_nodes == max_numa_nodeid);
316
Wanlong Gao96d0e262014-05-14 17:43:05 +0800317 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800318 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800319
320 if (nb_numa_nodes > MAX_NODES) {
321 nb_numa_nodes = MAX_NODES;
322 }
323
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300324 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800325 * and distribute the available memory equally across all nodes
326 */
327 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800328 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800329 break;
330 }
331 }
332 if (i == nb_numa_nodes) {
333 uint64_t usedmem = 0;
334
Michael S. Tsirkind75e2f62014-06-24 07:43:37 +0300335 /* On Linux, each node's border has to be 8MB aligned,
Wanlong Gao96d0e262014-05-14 17:43:05 +0800336 * the final node gets the rest.
337 */
338 for (i = 0; i < nb_numa_nodes - 1; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800339 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
340 ~((1 << 23UL) - 1);
341 usedmem += numa_info[i].node_mem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800342 }
Wanlong Gao8c859012014-05-14 17:43:07 +0800343 numa_info[i].node_mem = ram_size - usedmem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800344 }
345
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800346 numa_total = 0;
347 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800348 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800349 }
350 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800351 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
352 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800353 numa_total, ram_size);
354 exit(1);
355 }
356
Wanlong Gao96d0e262014-05-14 17:43:05 +0800357 for (i = 0; i < nb_numa_nodes; i++) {
Bharata B Raofa9ea812015-06-29 13:50:25 +0530358 QLIST_INIT(&numa_info[i].addr);
359 }
360
Bharata B Raoabafabd2015-06-29 13:50:26 +0530361 numa_set_mem_ranges();
362
Bharata B Raofa9ea812015-06-29 13:50:25 +0530363 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800364 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800365 break;
366 }
367 }
Igor Mammedov57924bc2015-03-19 17:09:21 +0000368 /* Historically VCPUs were assigned in round-robin order to NUMA
369 * nodes. However it causes issues with guest not handling it nice
370 * in case where cores/threads from a multicore CPU appear on
371 * different nodes. So allow boards to override default distribution
372 * rule grouping VCPUs by socket so that VCPUs from the same socket
373 * would be on the same node.
Wanlong Gao96d0e262014-05-14 17:43:05 +0800374 */
375 if (i == nb_numa_nodes) {
376 for (i = 0; i < max_cpus; i++) {
Igor Mammedov57924bc2015-03-19 17:09:21 +0000377 unsigned node_id = i % nb_numa_nodes;
378 if (mc->cpu_index_to_socket_id) {
379 node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
380 }
381
382 set_bit(i, numa_info[node_id].node_cpu);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800383 }
384 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200385
386 validate_numa_cpus();
Bharata B Raoabafabd2015-06-29 13:50:26 +0530387 } else {
388 numa_set_mem_node_id(0, ram_size, 0);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800389 }
390}
391
Eduardo Habkostdde11112015-02-08 16:51:22 -0200392void numa_post_machine_init(void)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800393{
394 CPUState *cpu;
395 int i;
396
397 CPU_FOREACH(cpu) {
398 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800399 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800400 cpu->numa_node = i;
401 }
402 }
403 }
404}
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800405
Paolo Bonzini7febe362014-05-14 17:43:17 +0800406static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
407 const char *name,
408 uint64_t ram_size)
409{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800410 if (mem_path) {
411#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800412 Error *err = NULL;
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800413 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800414 mem_path, &err);
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200415 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100416 error_report_err(err);
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500417 if (mem_prealloc) {
418 exit(1);
419 }
420
421 /* Legacy behavior: if allocation failed, fall back to
422 * regular RAM allocation.
423 */
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200424 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800425 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800426#else
427 fprintf(stderr, "-mem-path not supported on this host\n");
428 exit(1);
429#endif
430 } else {
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200431 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800432 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800433 vmstate_register_ram_global(mr);
434}
435
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800436void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
437 const char *name,
438 uint64_t ram_size)
439{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800440 uint64_t addr = 0;
441 int i;
442
443 if (nb_numa_nodes == 0 || !have_memdevs) {
444 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
445 return;
446 }
447
448 memory_region_init(mr, owner, name, ram_size);
449 for (i = 0; i < MAX_NODES; i++) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800450 uint64_t size = numa_info[i].node_mem;
451 HostMemoryBackend *backend = numa_info[i].node_memdev;
452 if (!backend) {
453 continue;
454 }
Markus Armbruster007b0652015-09-11 15:04:45 +0200455 MemoryRegion *seg = host_memory_backend_get_memory(backend,
456 &error_fatal);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800457
Hu Tao0462fae2014-06-30 18:28:15 +0800458 if (memory_region_is_mapped(seg)) {
459 char *path = object_get_canonical_path_component(OBJECT(backend));
460 error_report("memory backend %s is used multiple times. Each "
461 "-numa option must use a different memdev value.",
462 path);
463 exit(1);
464 }
465
Paolo Bonzini7febe362014-05-14 17:43:17 +0800466 memory_region_add_subregion(mr, addr, seg);
467 vmstate_register_ram_global(seg);
468 addr += size;
469 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800470}
Hu Tao76b5d852014-06-16 18:05:41 +0800471
zhanghailiang5b009e42014-11-04 19:49:30 +0800472static void numa_stat_memory_devices(uint64_t node_mem[])
473{
474 MemoryDeviceInfoList *info_list = NULL;
475 MemoryDeviceInfoList **prev = &info_list;
476 MemoryDeviceInfoList *info;
477
478 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
479 for (info = info_list; info; info = info->next) {
480 MemoryDeviceInfo *value = info->value;
481
482 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600483 switch (value->type) {
zhanghailiang5b009e42014-11-04 19:49:30 +0800484 case MEMORY_DEVICE_INFO_KIND_DIMM:
Eric Blake32bafa82016-03-17 16:48:37 -0600485 node_mem[value->u.dimm.data->node] += value->u.dimm.data->size;
zhanghailiang5b009e42014-11-04 19:49:30 +0800486 break;
487 default:
488 break;
489 }
490 }
491 }
492 qapi_free_MemoryDeviceInfoList(info_list);
493}
494
495void query_numa_node_mem(uint64_t node_mem[])
496{
497 int i;
498
499 if (nb_numa_nodes <= 0) {
500 return;
501 }
502
503 numa_stat_memory_devices(node_mem);
504 for (i = 0; i < nb_numa_nodes; i++) {
505 node_mem[i] += numa_info[i].node_mem;
506 }
507}
508
Hu Tao76b5d852014-06-16 18:05:41 +0800509static int query_memdev(Object *obj, void *opaque)
510{
511 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800512 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800513
514 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800515 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800516
517 m->value = g_malloc0(sizeof(*m->value));
518
519 m->value->size = object_property_get_int(obj, "size",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100520 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800521 m->value->merge = object_property_get_bool(obj, "merge",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100522 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800523 m->value->dump = object_property_get_bool(obj, "dump",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100524 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800525 m->value->prealloc = object_property_get_bool(obj,
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100526 "prealloc",
527 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800528 m->value->policy = object_property_get_enum(obj,
529 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100530 "HostMemPolicy",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100531 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800532 object_property_get_uint16List(obj, "host-nodes",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100533 &m->value->host_nodes,
534 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800535
536 m->next = *list;
537 *list = m;
538 }
539
540 return 0;
Hu Tao76b5d852014-06-16 18:05:41 +0800541}
542
543MemdevList *qmp_query_memdev(Error **errp)
544{
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100545 Object *obj = object_get_objects_root();
Chen Fanecaf54a2014-08-18 14:46:35 +0800546 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800547
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100548 object_child_foreach(obj, query_memdev, &list);
Hu Tao76b5d852014-06-16 18:05:41 +0800549 return list;
Hu Tao76b5d852014-06-16 18:05:41 +0800550}