blob: 6fc2393ddd803726bed28af7415d8276f0c34941 [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"
Paolo Bonzini0987d732016-12-21 00:31:36 +080028#include "exec/ramlist.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080029#include "qemu/bitmap.h"
30#include "qom/cpu.h"
Wanlong Gao2b631ec2014-05-14 17:43:06 +080031#include "qemu/error-report.h"
32#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
Wanlong Gao00421092014-05-14 17:43:08 +080033#include "qapi-visit.h"
34#include "qapi/opts-visitor.h"
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +080035#include "hw/boards.h"
Paolo Bonzini7febe362014-05-14 17:43:17 +080036#include "sysemu/hostmem.h"
Hu Tao76b5d852014-06-16 18:05:41 +080037#include "qmp-commands.h"
zhanghailiang5b009e42014-11-04 19:49:30 +080038#include "hw/mem/pc-dimm.h"
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -020039#include "qemu/option.h"
40#include "qemu/config-file.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080041
Wanlong Gao00421092014-05-14 17:43:08 +080042QemuOptsList 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 */
47};
48
Paolo Bonzini7febe362014-05-14 17:43:17 +080049static int have_memdevs = -1;
Eduardo Habkost25712ff2015-02-08 16:51:19 -020050static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
51 * For all nodes, nodeid < max_numa_nodeid
52 */
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020053int nb_numa_nodes;
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020054NodeInfo numa_info[MAX_NODES];
Paolo Bonzini7febe362014-05-14 17:43:17 +080055
Bharata B Raofa9ea812015-06-29 13:50:25 +053056void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
57{
Bharata B Rao672558d2015-07-09 20:57:36 +053058 struct numa_addr_range *range;
Bharata B Raofa9ea812015-06-29 13:50:25 +053059
Bharata B Raoabafabd2015-06-29 13:50:26 +053060 /*
61 * Memory-less nodes can come here with 0 size in which case,
62 * there is nothing to do.
63 */
64 if (!size) {
65 return;
66 }
67
Bharata B Rao672558d2015-07-09 20:57:36 +053068 range = g_malloc0(sizeof(*range));
Bharata B Raofa9ea812015-06-29 13:50:25 +053069 range->mem_start = addr;
70 range->mem_end = addr + size - 1;
71 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
72}
73
74void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
75{
76 struct numa_addr_range *range, *next;
77
78 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
79 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
80 QLIST_REMOVE(range, entry);
81 g_free(range);
82 return;
83 }
84 }
85}
86
Bharata B Raoabafabd2015-06-29 13:50:26 +053087static void numa_set_mem_ranges(void)
88{
89 int i;
90 ram_addr_t mem_start = 0;
91
92 /*
93 * Deduce start address of each node and use it to store
94 * the address range info in numa_info address range list
95 */
96 for (i = 0; i < nb_numa_nodes; i++) {
97 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
98 mem_start += numa_info[i].node_mem;
99 }
100}
101
Bharata B Raoe75e2a12015-06-29 13:50:27 +0530102/*
103 * Check if @addr falls under NUMA @node.
104 */
105static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
106{
107 struct numa_addr_range *range;
108
109 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
110 if (addr >= range->mem_start && addr <= range->mem_end) {
111 return true;
112 }
113 }
114 return false;
115}
116
117/*
118 * Given an address, return the index of the NUMA node to which the
119 * address belongs to.
120 */
121uint32_t numa_get_node(ram_addr_t addr, Error **errp)
122{
123 uint32_t i;
124
125 /* For non NUMA configurations, check if the addr falls under node 0 */
126 if (!nb_numa_nodes) {
127 if (numa_addr_belongs_to_node(addr, 0)) {
128 return 0;
129 }
130 }
131
132 for (i = 0; i < nb_numa_nodes; i++) {
133 if (numa_addr_belongs_to_node(addr, i)) {
134 return i;
135 }
136 }
137
138 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
139 "NUMA node", addr);
140 return -1;
141}
142
Wanlong Gao00421092014-05-14 17:43:08 +0800143static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800144{
Wanlong Gao00421092014-05-14 17:43:08 +0800145 uint16_t nodenr;
146 uint16List *cpus = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800147
Wanlong Gao00421092014-05-14 17:43:08 +0800148 if (node->has_nodeid) {
149 nodenr = node->nodeid;
150 } else {
151 nodenr = nb_numa_nodes;
152 }
153
154 if (nodenr >= MAX_NODES) {
155 error_setg(errp, "Max number of NUMA nodes reached: %"
Gonglei01bbbcf2015-02-25 12:22:30 +0800156 PRIu16 "", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800157 return;
158 }
159
Eduardo Habkost1945b9d2014-06-26 18:33:19 -0300160 if (numa_info[nodenr].present) {
161 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
162 return;
163 }
164
Wanlong Gao00421092014-05-14 17:43:08 +0800165 for (cpus = node->cpus; cpus; cpus = cpus->next) {
Eduardo Habkost8979c942015-02-09 17:28:52 -0200166 if (cpus->value >= max_cpus) {
167 error_setg(errp,
168 "CPU index (%" PRIu16 ")"
169 " should be smaller than maxcpus (%d)",
170 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +0800171 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800172 }
Wanlong Gao00421092014-05-14 17:43:08 +0800173 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800174 }
175
Paolo Bonzini7febe362014-05-14 17:43:17 +0800176 if (node->has_mem && node->has_memdev) {
Gonglei01bbbcf2015-02-25 12:22:30 +0800177 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800178 return;
179 }
180
181 if (have_memdevs == -1) {
182 have_memdevs = node->has_memdev;
183 }
184 if (node->has_memdev != have_memdevs) {
185 error_setg(errp, "qemu: memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800186 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800187 return;
188 }
189
Wanlong Gao00421092014-05-14 17:43:08 +0800190 if (node->has_mem) {
191 uint64_t mem_size = node->mem;
192 const char *mem_str = qemu_opt_get(opts, "mem");
193 /* Fix up legacy suffix-less format */
194 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
195 mem_size <<= 20;
196 }
197 numa_info[nodenr].node_mem = mem_size;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800198 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800199 if (node->has_memdev) {
200 Object *o;
201 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
202 if (!o) {
203 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
204 return;
205 }
206
207 object_ref(o);
208 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
209 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
210 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300211 numa_info[nodenr].present = true;
212 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800213}
214
Markus Armbruster28d0de72015-03-13 13:35:14 +0100215static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800216{
Wanlong Gao00421092014-05-14 17:43:08 +0800217 NumaOptions *object = NULL;
218 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800219
Wanlong Gao00421092014-05-14 17:43:08 +0800220 {
Eric Blake09204ea2016-06-09 10:48:36 -0600221 Visitor *v = opts_visitor_new(opts);
222 visit_type_NumaOptions(v, NULL, &object, &err);
223 visit_free(v);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800224 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800225
Wanlong Gao00421092014-05-14 17:43:08 +0800226 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200227 goto end;
Wanlong Gao00421092014-05-14 17:43:08 +0800228 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800229
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600230 switch (object->type) {
Markus Armbrusterd081a492017-02-21 21:46:26 +0100231 case NUMA_OPTIONS_TYPE_NODE:
232 numa_node_parse(&object->u.node, opts, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800233 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200234 goto end;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800235 }
236 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800237 break;
238 default:
239 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800240 }
Wanlong Gao00421092014-05-14 17:43:08 +0800241
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200242end:
Eric Blake96a16162016-02-23 14:14:33 -0700243 qapi_free_NumaOptions(object);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200244 if (err) {
245 error_report_err(err);
246 return -1;
247 }
Wanlong Gao00421092014-05-14 17:43:08 +0800248
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200249 return 0;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800250}
251
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200252static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
253{
254 int cpu;
255 bool first = true;
256 GString *s = g_string_new(NULL);
257
258 for (cpu = find_first_bit(cpus, max_cpus);
259 cpu < max_cpus;
260 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
261 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
262 first = false;
263 }
264 return g_string_free(s, FALSE);
265}
266
267static void validate_numa_cpus(void)
268{
269 int i;
Igor Mammedovcdda2012016-11-18 12:02:54 +0100270 unsigned long *seen_cpus = bitmap_new(max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200271
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200272 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100273 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu, max_cpus)) {
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200274 bitmap_and(seen_cpus, seen_cpus,
Igor Mammedovcdda2012016-11-18 12:02:54 +0100275 numa_info[i].node_cpu, max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200276 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));
Igor Mammedovcdda2012016-11-18 12:02:54 +0100278 g_free(seen_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200279 exit(EXIT_FAILURE);
280 }
281 bitmap_or(seen_cpus, seen_cpus,
Igor Mammedovcdda2012016-11-18 12:02:54 +0100282 numa_info[i].node_cpu, max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200283 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200284
285 if (!bitmap_full(seen_cpus, max_cpus)) {
286 char *msg;
287 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
288 msg = enumerate_cpus(seen_cpus, max_cpus);
289 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
290 error_report("warning: All CPU(s) up to maxcpus should be described "
291 "in NUMA config");
292 g_free(msg);
293 }
Igor Mammedovcdda2012016-11-18 12:02:54 +0100294 g_free(seen_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200295}
296
Igor Mammedov57924bc2015-03-19 17:09:21 +0000297void parse_numa_opts(MachineClass *mc)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800298{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300299 int i;
300
Igor Mammedovcdda2012016-11-18 12:02:54 +0100301 for (i = 0; i < MAX_NODES; i++) {
302 numa_info[i].node_cpu = bitmap_new(max_cpus);
303 }
304
Markus Armbruster28d0de72015-03-13 13:35:14 +0100305 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, NULL, NULL)) {
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200306 exit(1);
307 }
308
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300309 assert(max_numa_nodeid <= MAX_NODES);
310
311 /* No support for sparse NUMA node IDs yet: */
312 for (i = max_numa_nodeid - 1; i >= 0; i--) {
313 /* Report large node IDs first, to make mistakes easier to spot */
314 if (!numa_info[i].present) {
315 error_report("numa: Node ID missing: %d", i);
316 exit(1);
317 }
318 }
319
320 /* This must be always true if all nodes are present: */
321 assert(nb_numa_nodes == max_numa_nodeid);
322
Wanlong Gao96d0e262014-05-14 17:43:05 +0800323 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800324 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800325
326 if (nb_numa_nodes > MAX_NODES) {
327 nb_numa_nodes = MAX_NODES;
328 }
329
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300330 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800331 * and distribute the available memory equally across all nodes
332 */
333 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800334 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800335 break;
336 }
337 }
338 if (i == nb_numa_nodes) {
339 uint64_t usedmem = 0;
340
Laurent Vivier55641212017-03-21 11:25:42 +0100341 /* Align each node according to the alignment
342 * requirements of the machine class
Wanlong Gao96d0e262014-05-14 17:43:05 +0800343 */
344 for (i = 0; i < nb_numa_nodes - 1; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800345 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
Laurent Vivier55641212017-03-21 11:25:42 +0100346 ~((1 << mc->numa_mem_align_shift) - 1);
Wanlong Gao8c859012014-05-14 17:43:07 +0800347 usedmem += numa_info[i].node_mem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800348 }
Wanlong Gao8c859012014-05-14 17:43:07 +0800349 numa_info[i].node_mem = ram_size - usedmem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800350 }
351
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800352 numa_total = 0;
353 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800354 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800355 }
356 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800357 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
358 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800359 numa_total, ram_size);
360 exit(1);
361 }
362
Wanlong Gao96d0e262014-05-14 17:43:05 +0800363 for (i = 0; i < nb_numa_nodes; i++) {
Bharata B Raofa9ea812015-06-29 13:50:25 +0530364 QLIST_INIT(&numa_info[i].addr);
365 }
366
Bharata B Raoabafabd2015-06-29 13:50:26 +0530367 numa_set_mem_ranges();
368
Bharata B Raofa9ea812015-06-29 13:50:25 +0530369 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100370 if (!bitmap_empty(numa_info[i].node_cpu, max_cpus)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800371 break;
372 }
373 }
Igor Mammedov57924bc2015-03-19 17:09:21 +0000374 /* Historically VCPUs were assigned in round-robin order to NUMA
375 * nodes. However it causes issues with guest not handling it nice
376 * in case where cores/threads from a multicore CPU appear on
377 * different nodes. So allow boards to override default distribution
378 * rule grouping VCPUs by socket so that VCPUs from the same socket
379 * would be on the same node.
Wanlong Gao96d0e262014-05-14 17:43:05 +0800380 */
381 if (i == nb_numa_nodes) {
382 for (i = 0; i < max_cpus; i++) {
Igor Mammedov57924bc2015-03-19 17:09:21 +0000383 unsigned node_id = i % nb_numa_nodes;
384 if (mc->cpu_index_to_socket_id) {
385 node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
386 }
387
388 set_bit(i, numa_info[node_id].node_cpu);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800389 }
390 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200391
392 validate_numa_cpus();
Bharata B Raoabafabd2015-06-29 13:50:26 +0530393 } else {
394 numa_set_mem_node_id(0, ram_size, 0);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800395 }
396}
397
Eduardo Habkostdde11112015-02-08 16:51:22 -0200398void numa_post_machine_init(void)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800399{
400 CPUState *cpu;
401 int i;
402
403 CPU_FOREACH(cpu) {
404 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100405 assert(cpu->cpu_index < max_cpus);
Wanlong Gao8c859012014-05-14 17:43:07 +0800406 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800407 cpu->numa_node = i;
408 }
409 }
410 }
411}
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800412
Paolo Bonzini7febe362014-05-14 17:43:17 +0800413static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
414 const char *name,
415 uint64_t ram_size)
416{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800417 if (mem_path) {
418#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800419 Error *err = NULL;
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800420 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800421 mem_path, &err);
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200422 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100423 error_report_err(err);
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500424 if (mem_prealloc) {
425 exit(1);
426 }
427
428 /* Legacy behavior: if allocation failed, fall back to
429 * regular RAM allocation.
430 */
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200431 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800432 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800433#else
434 fprintf(stderr, "-mem-path not supported on this host\n");
435 exit(1);
436#endif
437 } else {
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200438 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800439 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800440 vmstate_register_ram_global(mr);
441}
442
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800443void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
444 const char *name,
445 uint64_t ram_size)
446{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800447 uint64_t addr = 0;
448 int i;
449
450 if (nb_numa_nodes == 0 || !have_memdevs) {
451 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
452 return;
453 }
454
455 memory_region_init(mr, owner, name, ram_size);
456 for (i = 0; i < MAX_NODES; i++) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800457 uint64_t size = numa_info[i].node_mem;
458 HostMemoryBackend *backend = numa_info[i].node_memdev;
459 if (!backend) {
460 continue;
461 }
Markus Armbruster007b0652015-09-11 15:04:45 +0200462 MemoryRegion *seg = host_memory_backend_get_memory(backend,
463 &error_fatal);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800464
Hu Tao0462fae2014-06-30 18:28:15 +0800465 if (memory_region_is_mapped(seg)) {
466 char *path = object_get_canonical_path_component(OBJECT(backend));
467 error_report("memory backend %s is used multiple times. Each "
468 "-numa option must use a different memdev value.",
469 path);
470 exit(1);
471 }
472
Greg Kurz0b217572016-07-19 10:28:35 +0200473 host_memory_backend_set_mapped(backend, true);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800474 memory_region_add_subregion(mr, addr, seg);
475 vmstate_register_ram_global(seg);
476 addr += size;
477 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800478}
Hu Tao76b5d852014-06-16 18:05:41 +0800479
zhanghailiang5b009e42014-11-04 19:49:30 +0800480static void numa_stat_memory_devices(uint64_t node_mem[])
481{
482 MemoryDeviceInfoList *info_list = NULL;
483 MemoryDeviceInfoList **prev = &info_list;
484 MemoryDeviceInfoList *info;
485
486 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
487 for (info = info_list; info; info = info->next) {
488 MemoryDeviceInfo *value = info->value;
489
490 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600491 switch (value->type) {
zhanghailiang5b009e42014-11-04 19:49:30 +0800492 case MEMORY_DEVICE_INFO_KIND_DIMM:
Eric Blake32bafa82016-03-17 16:48:37 -0600493 node_mem[value->u.dimm.data->node] += value->u.dimm.data->size;
zhanghailiang5b009e42014-11-04 19:49:30 +0800494 break;
495 default:
496 break;
497 }
498 }
499 }
500 qapi_free_MemoryDeviceInfoList(info_list);
501}
502
503void query_numa_node_mem(uint64_t node_mem[])
504{
505 int i;
506
507 if (nb_numa_nodes <= 0) {
508 return;
509 }
510
511 numa_stat_memory_devices(node_mem);
512 for (i = 0; i < nb_numa_nodes; i++) {
513 node_mem[i] += numa_info[i].node_mem;
514 }
515}
516
Hu Tao76b5d852014-06-16 18:05:41 +0800517static int query_memdev(Object *obj, void *opaque)
518{
519 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800520 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800521
522 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800523 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800524
525 m->value = g_malloc0(sizeof(*m->value));
526
Igor Mammedove1ff3c62017-01-10 13:53:15 +0100527 m->value->id = object_property_get_str(obj, "id", NULL);
528 m->value->has_id = !!m->value->id;
529
Hu Tao76b5d852014-06-16 18:05:41 +0800530 m->value->size = object_property_get_int(obj, "size",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100531 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800532 m->value->merge = object_property_get_bool(obj, "merge",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100533 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800534 m->value->dump = object_property_get_bool(obj, "dump",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100535 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800536 m->value->prealloc = object_property_get_bool(obj,
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100537 "prealloc",
538 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800539 m->value->policy = object_property_get_enum(obj,
540 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100541 "HostMemPolicy",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100542 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800543 object_property_get_uint16List(obj, "host-nodes",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100544 &m->value->host_nodes,
545 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800546
547 m->next = *list;
548 *list = m;
549 }
550
551 return 0;
Hu Tao76b5d852014-06-16 18:05:41 +0800552}
553
554MemdevList *qmp_query_memdev(Error **errp)
555{
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100556 Object *obj = object_get_objects_root();
Chen Fanecaf54a2014-08-18 14:46:35 +0800557 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800558
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100559 object_child_foreach(obj, query_memdev, &list);
Hu Tao76b5d852014-06-16 18:05:41 +0800560 return list;
Hu Tao76b5d852014-06-16 18:05:41 +0800561}
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200562
563int numa_get_node_for_cpu(int idx)
564{
565 int i;
566
Igor Mammedovcdda2012016-11-18 12:02:54 +0100567 assert(idx < max_cpus);
568
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200569 for (i = 0; i < nb_numa_nodes; i++) {
570 if (test_bit(idx, numa_info[i].node_cpu)) {
571 break;
572 }
573 }
574 return i;
575}
Paolo Bonzini0987d732016-12-21 00:31:36 +0800576
577void ram_block_notifier_add(RAMBlockNotifier *n)
578{
579 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
580}
581
582void ram_block_notifier_remove(RAMBlockNotifier *n)
583{
584 QLIST_REMOVE(n, next);
585}
586
587void ram_block_notify_add(void *host, size_t size)
588{
589 RAMBlockNotifier *notifier;
590
591 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
592 notifier->ram_block_added(notifier, host, size);
593 }
594}
595
596void ram_block_notify_remove(void *host, size_t size)
597{
598 RAMBlockNotifier *notifier;
599
600 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
601 notifier->ram_block_removed(notifier, host, size);
602 }
603}