blob: 8af9c916ac74042894faef32b370c699fbc56fe9 [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
25#include "sysemu/sysemu.h"
26#include "exec/cpu-common.h"
27#include "qemu/bitmap.h"
28#include "qom/cpu.h"
Wanlong Gao2b631ec2014-05-14 17:43:06 +080029#include "qemu/error-report.h"
30#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
Wanlong Gao00421092014-05-14 17:43:08 +080031#include "qapi-visit.h"
32#include "qapi/opts-visitor.h"
33#include "qapi/dealloc-visitor.h"
34#include "qapi/qmp/qerror.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"
Wanlong Gao96d0e262014-05-14 17:43:05 +080037
Wanlong Gao00421092014-05-14 17:43:08 +080038QemuOptsList qemu_numa_opts = {
39 .name = "numa",
40 .implied_opt_name = "type",
41 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
42 .desc = { { 0 } } /* validated with OptsVisitor */
43};
44
Paolo Bonzini7febe362014-05-14 17:43:17 +080045static int have_memdevs = -1;
46
Wanlong Gao00421092014-05-14 17:43:08 +080047static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +080048{
Wanlong Gao00421092014-05-14 17:43:08 +080049 uint16_t nodenr;
50 uint16List *cpus = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +080051
Wanlong Gao00421092014-05-14 17:43:08 +080052 if (node->has_nodeid) {
53 nodenr = node->nodeid;
54 } else {
55 nodenr = nb_numa_nodes;
56 }
57
58 if (nodenr >= MAX_NODES) {
59 error_setg(errp, "Max number of NUMA nodes reached: %"
60 PRIu16 "\n", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +080061 return;
62 }
63
Wanlong Gao00421092014-05-14 17:43:08 +080064 for (cpus = node->cpus; cpus; cpus = cpus->next) {
65 if (cpus->value > MAX_CPUMASK_BITS) {
66 error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
67 cpus->value, MAX_CPUMASK_BITS);
68 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +080069 }
Wanlong Gao00421092014-05-14 17:43:08 +080070 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +080071 }
72
Paolo Bonzini7febe362014-05-14 17:43:17 +080073 if (node->has_mem && node->has_memdev) {
74 error_setg(errp, "qemu: cannot specify both mem= and memdev=\n");
75 return;
76 }
77
78 if (have_memdevs == -1) {
79 have_memdevs = node->has_memdev;
80 }
81 if (node->has_memdev != have_memdevs) {
82 error_setg(errp, "qemu: memdev option must be specified for either "
83 "all or no nodes\n");
84 return;
85 }
86
Wanlong Gao00421092014-05-14 17:43:08 +080087 if (node->has_mem) {
88 uint64_t mem_size = node->mem;
89 const char *mem_str = qemu_opt_get(opts, "mem");
90 /* Fix up legacy suffix-less format */
91 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
92 mem_size <<= 20;
93 }
94 numa_info[nodenr].node_mem = mem_size;
Wanlong Gao96d0e262014-05-14 17:43:05 +080095 }
Paolo Bonzini7febe362014-05-14 17:43:17 +080096 if (node->has_memdev) {
97 Object *o;
98 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
99 if (!o) {
100 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
101 return;
102 }
103
104 object_ref(o);
105 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
106 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
107 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800108}
109
Wanlong Gao00421092014-05-14 17:43:08 +0800110int numa_init_func(QemuOpts *opts, void *opaque)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800111{
Wanlong Gao00421092014-05-14 17:43:08 +0800112 NumaOptions *object = NULL;
113 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800114
Wanlong Gao00421092014-05-14 17:43:08 +0800115 {
116 OptsVisitor *ov = opts_visitor_new(opts);
117 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
118 opts_visitor_cleanup(ov);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800119 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800120
Wanlong Gao00421092014-05-14 17:43:08 +0800121 if (err) {
122 goto error;
123 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800124
Wanlong Gao00421092014-05-14 17:43:08 +0800125 switch (object->kind) {
126 case NUMA_OPTIONS_KIND_NODE:
127 numa_node_parse(object->node, opts, &err);
128 if (err) {
129 goto error;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800130 }
131 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800132 break;
133 default:
134 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800135 }
Wanlong Gao00421092014-05-14 17:43:08 +0800136
137 return 0;
138
139error:
140 qerror_report_err(err);
141 error_free(err);
142
143 if (object) {
144 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
145 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
146 &object, NULL, NULL);
147 qapi_dealloc_visitor_cleanup(dv);
148 }
149
150 return -1;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800151}
152
153void set_numa_nodes(void)
154{
155 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800156 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800157 int i;
158
159 if (nb_numa_nodes > MAX_NODES) {
160 nb_numa_nodes = MAX_NODES;
161 }
162
163 /* If no memory size if given for any node, assume the default case
164 * and distribute the available memory equally across all nodes
165 */
166 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800167 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800168 break;
169 }
170 }
171 if (i == nb_numa_nodes) {
172 uint64_t usedmem = 0;
173
174 /* On Linux, the each node's border has to be 8MB aligned,
175 * the final node gets the rest.
176 */
177 for (i = 0; i < nb_numa_nodes - 1; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800178 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
179 ~((1 << 23UL) - 1);
180 usedmem += numa_info[i].node_mem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800181 }
Wanlong Gao8c859012014-05-14 17:43:07 +0800182 numa_info[i].node_mem = ram_size - usedmem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800183 }
184
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800185 numa_total = 0;
186 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800187 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800188 }
189 if (numa_total != ram_size) {
190 error_report("total memory for NUMA nodes (%" PRIu64 ")"
191 " should equal RAM size (" RAM_ADDR_FMT ")",
192 numa_total, ram_size);
193 exit(1);
194 }
195
Wanlong Gao96d0e262014-05-14 17:43:05 +0800196 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800197 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800198 break;
199 }
200 }
201 /* assigning the VCPUs round-robin is easier to implement, guest OSes
202 * must cope with this anyway, because there are BIOSes out there in
203 * real machines which also use this scheme.
204 */
205 if (i == nb_numa_nodes) {
206 for (i = 0; i < max_cpus; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800207 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800208 }
209 }
210 }
211}
212
213void set_numa_modes(void)
214{
215 CPUState *cpu;
216 int i;
217
218 CPU_FOREACH(cpu) {
219 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800220 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800221 cpu->numa_node = i;
222 }
223 }
224 }
225}
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800226
Paolo Bonzini7febe362014-05-14 17:43:17 +0800227static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
228 const char *name,
229 uint64_t ram_size)
230{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800231 if (mem_path) {
232#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800233 Error *err = NULL;
234 memory_region_init_ram_from_file(mr, owner, name, ram_size,
235 mem_path, &err);
236
237 /* Legacy behavior: if allocation failed, fall back to
238 * regular RAM allocation.
239 */
240 if (!memory_region_size(mr)) {
241 qerror_report_err(err);
242 error_free(err);
243 memory_region_init_ram(mr, owner, name, ram_size);
244 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800245#else
246 fprintf(stderr, "-mem-path not supported on this host\n");
247 exit(1);
248#endif
249 } else {
250 memory_region_init_ram(mr, owner, name, ram_size);
251 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800252 vmstate_register_ram_global(mr);
253}
254
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800255void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
256 const char *name,
257 uint64_t ram_size)
258{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800259 uint64_t addr = 0;
260 int i;
261
262 if (nb_numa_nodes == 0 || !have_memdevs) {
263 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
264 return;
265 }
266
267 memory_region_init(mr, owner, name, ram_size);
268 for (i = 0; i < MAX_NODES; i++) {
269 Error *local_err = NULL;
270 uint64_t size = numa_info[i].node_mem;
271 HostMemoryBackend *backend = numa_info[i].node_memdev;
272 if (!backend) {
273 continue;
274 }
275 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
276 if (local_err) {
277 qerror_report_err(local_err);
278 exit(1);
279 }
280
281 memory_region_add_subregion(mr, addr, seg);
282 vmstate_register_ram_global(seg);
283 addr += size;
284 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800285}