]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/machine.c
machine: Move nvdimms state into struct MachineState
[mirror_qemu.git] / hw / core / machine.c
CommitLineData
36d20cb2
MA
1/*
2 * QEMU Machine
3 *
4 * Copyright (C) 2014 Red Hat Inc
5 *
6 * Authors:
7 * Marcel Apfelbaum <marcel.a@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
18c86e2b 13#include "qemu/osdep.h"
fc6b3cf9 14#include "qemu/units.h"
36d20cb2 15#include "hw/boards.h"
da34e65c 16#include "qapi/error.h"
9af23989 17#include "qapi/qapi-visit-common.h"
6b1b1440 18#include "qapi/visitor.h"
33cd52b5
AG
19#include "hw/sysbus.h"
20#include "sysemu/sysemu.h"
3bfe5716 21#include "sysemu/numa.h"
33cd52b5 22#include "qemu/error-report.h"
c6ff347c 23#include "sysemu/qtest.h"
edc24ccd 24#include "hw/pci/pci.h"
f6a0d06b 25#include "hw/mem/nvdimm.h"
6b1b1440 26
abd93cc7 27GlobalProperty hw_compat_3_1[] = {
6c36bddf
EH
28 { "pcie-root-port", "x-speed", "2_5" },
29 { "pcie-root-port", "x-width", "1" },
30 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
31 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
b6148757
MAL
32 { "tpm-crb", "ppi", "false" },
33 { "tpm-tis", "ppi", "false" },
b63e1050
GH
34 { "usb-kbd", "serial", "42" },
35 { "usb-mouse", "serial", "42" },
36 { "usb-kbd", "serial", "42" },
5c81161f
SG
37 { "virtio-blk-device", "discard", "false" },
38 { "virtio-blk-device", "write-zeroes", "false" },
abd93cc7
MAL
39};
40const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
41
ddb3235d
MAL
42GlobalProperty hw_compat_3_0[] = {};
43const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
44
0d47310b 45GlobalProperty hw_compat_2_12[] = {
6c36bddf
EH
46 { "migration", "decompress-error-check", "off" },
47 { "hda-audio", "use-timer", "false" },
48 { "cirrus-vga", "global-vmstate", "true" },
49 { "VGA", "global-vmstate", "true" },
50 { "vmware-svga", "global-vmstate", "true" },
51 { "qxl-vga", "global-vmstate", "true" },
0d47310b
MAL
52};
53const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
54
43df70a9 55GlobalProperty hw_compat_2_11[] = {
6c36bddf
EH
56 { "hpet", "hpet-offset-saved", "false" },
57 { "virtio-blk-pci", "vectors", "2" },
58 { "vhost-user-blk-pci", "vectors", "2" },
59 { "e1000", "migrate_tso_props", "off" },
43df70a9
MAL
60};
61const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
62
503224f4 63GlobalProperty hw_compat_2_10[] = {
6c36bddf
EH
64 { "virtio-mouse-device", "wheel-axis", "false" },
65 { "virtio-tablet-device", "wheel-axis", "false" },
503224f4
MAL
66};
67const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
68
3e803152 69GlobalProperty hw_compat_2_9[] = {
6c36bddf
EH
70 { "pci-bridge", "shpc", "off" },
71 { "intel-iommu", "pt", "off" },
72 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
73 { "pcie-root-port", "x-migrate-msix", "false" },
3e803152
MAL
74};
75const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
76
edc24ccd 77GlobalProperty hw_compat_2_8[] = {
6c36bddf
EH
78 { "fw_cfg_mem", "x-file-slots", "0x10" },
79 { "fw_cfg_io", "x-file-slots", "0x10" },
80 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
81 { "pci-bridge", "shpc", "on" },
82 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
83 { "virtio-pci", "x-pcie-deverr-init", "off" },
84 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
85 { "virtio-pci", "x-pcie-pm-init", "off" },
86 { "cirrus-vga", "vgamem_mb", "8" },
87 { "isa-cirrus-vga", "vgamem_mb", "8" },
edc24ccd
MAL
88};
89const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
90
5a995064 91GlobalProperty hw_compat_2_7[] = {
6c36bddf
EH
92 { "virtio-pci", "page-per-vq", "on" },
93 { "virtio-serial-device", "emergency-write", "off" },
94 { "ioapic", "version", "0x11" },
95 { "intel-iommu", "x-buggy-eim", "true" },
96 { "virtio-pci", "x-ignore-backend-features", "on" },
5a995064
MAL
97};
98const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
99
ff8f261f 100GlobalProperty hw_compat_2_6[] = {
6c36bddf 101 { "virtio-mmio", "format_transport_address", "off" },
53921bfd
EH
102 /* Optional because not all virtio-pci devices support legacy mode */
103 { "virtio-pci", "disable-modern", "on", .optional = true },
104 { "virtio-pci", "disable-legacy", "off", .optional = true },
ff8f261f
MAL
105};
106const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
107
fe759610 108GlobalProperty hw_compat_2_5[] = {
6c36bddf
EH
109 { "isa-fdc", "fallback", "144" },
110 { "pvscsi", "x-old-pci-configuration", "on" },
111 { "pvscsi", "x-disable-pcie", "on" },
112 { "vmxnet3", "x-old-msi-offsets", "on" },
113 { "vmxnet3", "x-disable-pcie", "on" },
fe759610
MAL
114};
115const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
116
2f99b9c2 117GlobalProperty hw_compat_2_4[] = {
6c36bddf
EH
118 { "virtio-blk-device", "scsi", "true" },
119 { "e1000", "extra_mac_registers", "off" },
120 { "virtio-pci", "x-disable-pcie", "on" },
121 { "virtio-pci", "migrate-extra", "off" },
122 { "fw_cfg_mem", "dma_enabled", "off" },
123 { "fw_cfg_io", "dma_enabled", "off" }
2f99b9c2
MAL
124};
125const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
126
8995dd90 127GlobalProperty hw_compat_2_3[] = {
6c36bddf
EH
128 { "virtio-blk-pci", "any_layout", "off" },
129 { "virtio-balloon-pci", "any_layout", "off" },
130 { "virtio-serial-pci", "any_layout", "off" },
131 { "virtio-9p-pci", "any_layout", "off" },
132 { "virtio-rng-pci", "any_layout", "off" },
133 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
134 { "migration", "send-configuration", "off" },
135 { "migration", "send-section-footer", "off" },
136 { "migration", "store-global-state", "off" },
8995dd90
MAL
137};
138const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
139
1c30044e
MAL
140GlobalProperty hw_compat_2_2[] = {};
141const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
142
c4fc5695 143GlobalProperty hw_compat_2_1[] = {
6c36bddf
EH
144 { "intel-hda", "old_msi_addr", "on" },
145 { "VGA", "qemu-extended-regs", "off" },
146 { "secondary-vga", "qemu-extended-regs", "off" },
147 { "virtio-scsi-pci", "any_layout", "off" },
148 { "usb-mouse", "usb_version", "1" },
149 { "usb-kbd", "usb_version", "1" },
150 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
c4fc5695
MAL
151};
152const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
153
6b1b1440
MA
154static char *machine_get_accel(Object *obj, Error **errp)
155{
156 MachineState *ms = MACHINE(obj);
157
158 return g_strdup(ms->accel);
159}
160
161static void machine_set_accel(Object *obj, const char *value, Error **errp)
162{
163 MachineState *ms = MACHINE(obj);
164
556068ee 165 g_free(ms->accel);
6b1b1440
MA
166 ms->accel = g_strdup(value);
167}
168
32c18a2d 169static void machine_set_kernel_irqchip(Object *obj, Visitor *v,
d7bce999 170 const char *name, void *opaque,
32c18a2d 171 Error **errp)
6b1b1440 172{
32c18a2d 173 Error *err = NULL;
6b1b1440 174 MachineState *ms = MACHINE(obj);
32c18a2d 175 OnOffSplit mode;
6b1b1440 176
51e72bc1 177 visit_type_OnOffSplit(v, name, &mode, &err);
32c18a2d
MG
178 if (err) {
179 error_propagate(errp, err);
180 return;
181 } else {
182 switch (mode) {
183 case ON_OFF_SPLIT_ON:
184 ms->kernel_irqchip_allowed = true;
185 ms->kernel_irqchip_required = true;
186 ms->kernel_irqchip_split = false;
187 break;
188 case ON_OFF_SPLIT_OFF:
189 ms->kernel_irqchip_allowed = false;
190 ms->kernel_irqchip_required = false;
191 ms->kernel_irqchip_split = false;
192 break;
193 case ON_OFF_SPLIT_SPLIT:
194 ms->kernel_irqchip_allowed = true;
195 ms->kernel_irqchip_required = true;
196 ms->kernel_irqchip_split = true;
197 break;
198 default:
78a39306
GK
199 /* The value was checked in visit_type_OnOffSplit() above. If
200 * we get here, then something is wrong in QEMU.
201 */
32c18a2d
MG
202 abort();
203 }
204 }
6b1b1440
MA
205}
206
207static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
d7bce999 208 const char *name, void *opaque,
6b1b1440
MA
209 Error **errp)
210{
211 MachineState *ms = MACHINE(obj);
212 int64_t value = ms->kvm_shadow_mem;
213
51e72bc1 214 visit_type_int(v, name, &value, errp);
6b1b1440
MA
215}
216
217static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
d7bce999 218 const char *name, void *opaque,
6b1b1440
MA
219 Error **errp)
220{
221 MachineState *ms = MACHINE(obj);
222 Error *error = NULL;
223 int64_t value;
224
51e72bc1 225 visit_type_int(v, name, &value, &error);
6b1b1440
MA
226 if (error) {
227 error_propagate(errp, error);
228 return;
229 }
230
231 ms->kvm_shadow_mem = value;
232}
233
234static char *machine_get_kernel(Object *obj, Error **errp)
235{
236 MachineState *ms = MACHINE(obj);
237
238 return g_strdup(ms->kernel_filename);
239}
240
241static void machine_set_kernel(Object *obj, const char *value, Error **errp)
242{
243 MachineState *ms = MACHINE(obj);
244
556068ee 245 g_free(ms->kernel_filename);
6b1b1440
MA
246 ms->kernel_filename = g_strdup(value);
247}
248
249static char *machine_get_initrd(Object *obj, Error **errp)
250{
251 MachineState *ms = MACHINE(obj);
252
253 return g_strdup(ms->initrd_filename);
254}
255
256static void machine_set_initrd(Object *obj, const char *value, Error **errp)
257{
258 MachineState *ms = MACHINE(obj);
259
556068ee 260 g_free(ms->initrd_filename);
6b1b1440
MA
261 ms->initrd_filename = g_strdup(value);
262}
263
264static char *machine_get_append(Object *obj, Error **errp)
265{
266 MachineState *ms = MACHINE(obj);
267
268 return g_strdup(ms->kernel_cmdline);
269}
270
271static void machine_set_append(Object *obj, const char *value, Error **errp)
272{
273 MachineState *ms = MACHINE(obj);
274
556068ee 275 g_free(ms->kernel_cmdline);
6b1b1440
MA
276 ms->kernel_cmdline = g_strdup(value);
277}
278
279static char *machine_get_dtb(Object *obj, Error **errp)
280{
281 MachineState *ms = MACHINE(obj);
282
283 return g_strdup(ms->dtb);
284}
285
286static void machine_set_dtb(Object *obj, const char *value, Error **errp)
287{
288 MachineState *ms = MACHINE(obj);
289
556068ee 290 g_free(ms->dtb);
6b1b1440
MA
291 ms->dtb = g_strdup(value);
292}
293
294static char *machine_get_dumpdtb(Object *obj, Error **errp)
295{
296 MachineState *ms = MACHINE(obj);
297
298 return g_strdup(ms->dumpdtb);
299}
300
301static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
302{
303 MachineState *ms = MACHINE(obj);
304
556068ee 305 g_free(ms->dumpdtb);
6b1b1440
MA
306 ms->dumpdtb = g_strdup(value);
307}
308
309static void machine_get_phandle_start(Object *obj, Visitor *v,
d7bce999
EB
310 const char *name, void *opaque,
311 Error **errp)
6b1b1440
MA
312{
313 MachineState *ms = MACHINE(obj);
314 int64_t value = ms->phandle_start;
315
51e72bc1 316 visit_type_int(v, name, &value, errp);
6b1b1440
MA
317}
318
319static void machine_set_phandle_start(Object *obj, Visitor *v,
d7bce999
EB
320 const char *name, void *opaque,
321 Error **errp)
6b1b1440
MA
322{
323 MachineState *ms = MACHINE(obj);
324 Error *error = NULL;
325 int64_t value;
326
51e72bc1 327 visit_type_int(v, name, &value, &error);
6b1b1440
MA
328 if (error) {
329 error_propagate(errp, error);
330 return;
331 }
332
333 ms->phandle_start = value;
334}
335
336static char *machine_get_dt_compatible(Object *obj, Error **errp)
337{
338 MachineState *ms = MACHINE(obj);
339
340 return g_strdup(ms->dt_compatible);
341}
342
343static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
344{
345 MachineState *ms = MACHINE(obj);
346
556068ee 347 g_free(ms->dt_compatible);
6b1b1440
MA
348 ms->dt_compatible = g_strdup(value);
349}
350
351static bool machine_get_dump_guest_core(Object *obj, Error **errp)
352{
353 MachineState *ms = MACHINE(obj);
354
355 return ms->dump_guest_core;
356}
357
358static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
359{
360 MachineState *ms = MACHINE(obj);
361
362 ms->dump_guest_core = value;
363}
364
365static bool machine_get_mem_merge(Object *obj, Error **errp)
366{
367 MachineState *ms = MACHINE(obj);
368
369 return ms->mem_merge;
370}
371
372static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
373{
374 MachineState *ms = MACHINE(obj);
375
376 ms->mem_merge = value;
377}
378
379static bool machine_get_usb(Object *obj, Error **errp)
380{
381 MachineState *ms = MACHINE(obj);
382
383 return ms->usb;
384}
385
386static void machine_set_usb(Object *obj, bool value, Error **errp)
387{
388 MachineState *ms = MACHINE(obj);
389
390 ms->usb = value;
c6e76503 391 ms->usb_disabled = !value;
6b1b1440
MA
392}
393
cfc58cf3
EH
394static bool machine_get_graphics(Object *obj, Error **errp)
395{
396 MachineState *ms = MACHINE(obj);
397
398 return ms->enable_graphics;
399}
400
401static void machine_set_graphics(Object *obj, bool value, Error **errp)
402{
403 MachineState *ms = MACHINE(obj);
404
405 ms->enable_graphics = value;
406}
407
79814179
TC
408static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
409{
410 MachineState *ms = MACHINE(obj);
411
412 return ms->igd_gfx_passthru;
413}
414
415static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
416{
417 MachineState *ms = MACHINE(obj);
418
419 ms->igd_gfx_passthru = value;
420}
421
6b1b1440
MA
422static char *machine_get_firmware(Object *obj, Error **errp)
423{
424 MachineState *ms = MACHINE(obj);
425
426 return g_strdup(ms->firmware);
427}
428
429static void machine_set_firmware(Object *obj, const char *value, Error **errp)
430{
431 MachineState *ms = MACHINE(obj);
432
556068ee 433 g_free(ms->firmware);
6b1b1440
MA
434 ms->firmware = g_strdup(value);
435}
436
9850c604
AG
437static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
438{
439 MachineState *ms = MACHINE(obj);
440
441 ms->suppress_vmdesc = value;
442}
443
444static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
445{
446 MachineState *ms = MACHINE(obj);
447
448 return ms->suppress_vmdesc;
449}
450
902c053d
GK
451static void machine_set_enforce_config_section(Object *obj, bool value,
452 Error **errp)
453{
454 MachineState *ms = MACHINE(obj);
455
91c082ad
TH
456 warn_report("enforce-config-section is deprecated, please use "
457 "-global migration.send-configuration=on|off instead");
458
902c053d
GK
459 ms->enforce_config_section = value;
460}
461
462static bool machine_get_enforce_config_section(Object *obj, Error **errp)
463{
464 MachineState *ms = MACHINE(obj);
465
466 return ms->enforce_config_section;
467}
468
db588194
BS
469static char *machine_get_memory_encryption(Object *obj, Error **errp)
470{
471 MachineState *ms = MACHINE(obj);
472
473 return g_strdup(ms->memory_encryption);
474}
475
476static void machine_set_memory_encryption(Object *obj, const char *value,
477 Error **errp)
478{
479 MachineState *ms = MACHINE(obj);
480
481 g_free(ms->memory_encryption);
482 ms->memory_encryption = g_strdup(value);
483}
484
f6a0d06b
EA
485static bool machine_get_nvdimm(Object *obj, Error **errp)
486{
487 MachineState *ms = MACHINE(obj);
488
489 return ms->nvdimms_state->is_enabled;
490}
491
492static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
493{
494 MachineState *ms = MACHINE(obj);
495
496 ms->nvdimms_state->is_enabled = value;
497}
498
499static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
500{
501 MachineState *ms = MACHINE(obj);
502
503 return g_strdup(ms->nvdimms_state->persistence_string);
504}
505
506static void machine_set_nvdimm_persistence(Object *obj, const char *value,
507 Error **errp)
508{
509 MachineState *ms = MACHINE(obj);
510 NVDIMMState *nvdimms_state = ms->nvdimms_state;
511
512 if (strcmp(value, "cpu") == 0) {
513 nvdimms_state->persistence = 3;
514 } else if (strcmp(value, "mem-ctrl") == 0) {
515 nvdimms_state->persistence = 2;
516 } else {
517 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
518 value);
519 return;
520 }
521
522 g_free(nvdimms_state->persistence_string);
523 nvdimms_state->persistence_string = g_strdup(value);
524}
525
0bd1909d 526void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
33cd52b5 527{
0bd1909d
EH
528 strList *item = g_new0(strList, 1);
529
530 item->value = g_strdup(type);
531 item->next = mc->allowed_dynamic_sysbus_devices;
532 mc->allowed_dynamic_sysbus_devices = item;
33cd52b5
AG
533}
534
0bd1909d 535static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
33cd52b5 536{
0bd1909d
EH
537 MachineState *machine = opaque;
538 MachineClass *mc = MACHINE_GET_CLASS(machine);
539 bool allowed = false;
540 strList *wl;
33cd52b5 541
0bd1909d
EH
542 for (wl = mc->allowed_dynamic_sysbus_devices;
543 !allowed && wl;
544 wl = wl->next) {
545 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
546 }
547
548 if (!allowed) {
549 error_report("Option '-device %s' cannot be handled by this machine",
550 object_class_get_name(object_get_class(OBJECT(sbdev))));
551 exit(1);
33cd52b5 552 }
0bd1909d
EH
553}
554
555static void machine_init_notify(Notifier *notifier, void *data)
556{
557 MachineState *machine = MACHINE(qdev_get_machine());
33cd52b5
AG
558
559 /*
0bd1909d
EH
560 * Loop through all dynamically created sysbus devices and check if they are
561 * all allowed. If a device is not allowed, error out.
33cd52b5 562 */
0bd1909d 563 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
33cd52b5
AG
564}
565
f2d672c2
IM
566HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
567{
568 int i;
f2d672c2 569 HotpluggableCPUList *head = NULL;
d342eb76
IM
570 MachineClass *mc = MACHINE_GET_CLASS(machine);
571
572 /* force board to initialize possible_cpus if it hasn't been done yet */
573 mc->possible_cpu_arch_ids(machine);
f2d672c2 574
f2d672c2 575 for (i = 0; i < machine->possible_cpus->len; i++) {
d342eb76 576 Object *cpu;
f2d672c2
IM
577 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
578 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
579
d342eb76 580 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
f2d672c2
IM
581 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
582 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
583 sizeof(*cpu_item->props));
584
585 cpu = machine->possible_cpus->cpus[i].cpu;
586 if (cpu) {
587 cpu_item->has_qom_path = true;
588 cpu_item->qom_path = object_get_canonical_path(cpu);
589 }
590 list_item->value = cpu_item;
591 list_item->next = head;
592 head = list_item;
593 }
594 return head;
595}
596
7c88e65d
IM
597/**
598 * machine_set_cpu_numa_node:
599 * @machine: machine object to modify
600 * @props: specifies which cpu objects to assign to
601 * numa node specified by @props.node_id
602 * @errp: if an error occurs, a pointer to an area to store the error
603 *
604 * Associate NUMA node specified by @props.node_id with cpu slots that
605 * match socket/core/thread-ids specified by @props. It's recommended to use
606 * query-hotpluggable-cpus.props values to specify affected cpu slots,
607 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
608 *
609 * However for CLI convenience it's possible to pass in subset of properties,
610 * which would affect all cpu slots that match it.
611 * Ex for pc machine:
612 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
613 * -numa cpu,node-id=0,socket_id=0 \
614 * -numa cpu,node-id=1,socket_id=1
615 * will assign all child cores of socket 0 to node 0 and
616 * of socket 1 to node 1.
617 *
618 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
619 * return error.
620 * Empty subset is disallowed and function will return with error in this case.
621 */
622void machine_set_cpu_numa_node(MachineState *machine,
623 const CpuInstanceProperties *props, Error **errp)
624{
625 MachineClass *mc = MACHINE_GET_CLASS(machine);
626 bool match = false;
627 int i;
628
629 if (!mc->possible_cpu_arch_ids) {
630 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
631 return;
632 }
633
634 /* disabling node mapping is not supported, forbid it */
635 assert(props->has_node_id);
636
637 /* force board to initialize possible_cpus if it hasn't been done yet */
638 mc->possible_cpu_arch_ids(machine);
639
640 for (i = 0; i < machine->possible_cpus->len; i++) {
641 CPUArchId *slot = &machine->possible_cpus->cpus[i];
642
643 /* reject unsupported by board properties */
644 if (props->has_thread_id && !slot->props.has_thread_id) {
645 error_setg(errp, "thread-id is not supported");
646 return;
647 }
648
649 if (props->has_core_id && !slot->props.has_core_id) {
650 error_setg(errp, "core-id is not supported");
651 return;
652 }
653
654 if (props->has_socket_id && !slot->props.has_socket_id) {
655 error_setg(errp, "socket-id is not supported");
656 return;
657 }
658
659 /* skip slots with explicit mismatch */
660 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
661 continue;
662 }
663
664 if (props->has_core_id && props->core_id != slot->props.core_id) {
665 continue;
666 }
667
668 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
669 continue;
670 }
671
672 /* reject assignment if slot is already assigned, for compatibility
673 * of legacy cpu_index mapping with SPAPR core based mapping do not
674 * error out if cpu thread and matched core have the same node-id */
675 if (slot->props.has_node_id &&
676 slot->props.node_id != props->node_id) {
677 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
678 slot->props.node_id);
679 return;
680 }
681
682 /* assign slot to node as it's matched '-numa cpu' key */
683 match = true;
684 slot->props.node_id = props->node_id;
685 slot->props.has_node_id = props->has_node_id;
686 }
687
688 if (!match) {
689 error_setg(errp, "no match found");
690 }
691}
692
076b35b5
ND
693static void machine_class_init(ObjectClass *oc, void *data)
694{
695 MachineClass *mc = MACHINE_CLASS(oc);
696
697 /* Default 128 MB as guest ram size */
d23b6caa 698 mc->default_ram_size = 128 * MiB;
71ae9e94 699 mc->rom_file_has_mr = true;
26b81df4 700
55641213
LV
701 /* numa node memory size aligned on 8MB by default.
702 * On Linux, each node's border has to be 8MB aligned
703 */
704 mc->numa_mem_align_shift = 23;
3bfe5716 705 mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
55641213 706
26b81df4
EH
707 object_class_property_add_str(oc, "accel",
708 machine_get_accel, machine_set_accel, &error_abort);
709 object_class_property_set_description(oc, "accel",
710 "Accelerator list", &error_abort);
711
e80200c5 712 object_class_property_add(oc, "kernel-irqchip", "on|off|split",
26b81df4
EH
713 NULL, machine_set_kernel_irqchip,
714 NULL, NULL, &error_abort);
715 object_class_property_set_description(oc, "kernel-irqchip",
716 "Configure KVM in-kernel irqchip", &error_abort);
717
718 object_class_property_add(oc, "kvm-shadow-mem", "int",
719 machine_get_kvm_shadow_mem, machine_set_kvm_shadow_mem,
720 NULL, NULL, &error_abort);
721 object_class_property_set_description(oc, "kvm-shadow-mem",
722 "KVM shadow MMU size", &error_abort);
723
724 object_class_property_add_str(oc, "kernel",
725 machine_get_kernel, machine_set_kernel, &error_abort);
726 object_class_property_set_description(oc, "kernel",
727 "Linux kernel image file", &error_abort);
728
729 object_class_property_add_str(oc, "initrd",
730 machine_get_initrd, machine_set_initrd, &error_abort);
731 object_class_property_set_description(oc, "initrd",
732 "Linux initial ramdisk file", &error_abort);
733
734 object_class_property_add_str(oc, "append",
735 machine_get_append, machine_set_append, &error_abort);
736 object_class_property_set_description(oc, "append",
737 "Linux kernel command line", &error_abort);
738
739 object_class_property_add_str(oc, "dtb",
740 machine_get_dtb, machine_set_dtb, &error_abort);
741 object_class_property_set_description(oc, "dtb",
742 "Linux kernel device tree file", &error_abort);
743
744 object_class_property_add_str(oc, "dumpdtb",
745 machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
746 object_class_property_set_description(oc, "dumpdtb",
747 "Dump current dtb to a file and quit", &error_abort);
748
749 object_class_property_add(oc, "phandle-start", "int",
750 machine_get_phandle_start, machine_set_phandle_start,
751 NULL, NULL, &error_abort);
752 object_class_property_set_description(oc, "phandle-start",
753 "The first phandle ID we may generate dynamically", &error_abort);
754
755 object_class_property_add_str(oc, "dt-compatible",
756 machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
757 object_class_property_set_description(oc, "dt-compatible",
758 "Overrides the \"compatible\" property of the dt root node",
759 &error_abort);
760
761 object_class_property_add_bool(oc, "dump-guest-core",
762 machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
763 object_class_property_set_description(oc, "dump-guest-core",
097f699c 764 "Include guest memory in a core dump", &error_abort);
26b81df4
EH
765
766 object_class_property_add_bool(oc, "mem-merge",
767 machine_get_mem_merge, machine_set_mem_merge, &error_abort);
768 object_class_property_set_description(oc, "mem-merge",
769 "Enable/disable memory merge support", &error_abort);
770
771 object_class_property_add_bool(oc, "usb",
772 machine_get_usb, machine_set_usb, &error_abort);
773 object_class_property_set_description(oc, "usb",
774 "Set on/off to enable/disable usb", &error_abort);
775
776 object_class_property_add_bool(oc, "graphics",
777 machine_get_graphics, machine_set_graphics, &error_abort);
778 object_class_property_set_description(oc, "graphics",
779 "Set on/off to enable/disable graphics emulation", &error_abort);
780
781 object_class_property_add_bool(oc, "igd-passthru",
782 machine_get_igd_gfx_passthru, machine_set_igd_gfx_passthru,
783 &error_abort);
784 object_class_property_set_description(oc, "igd-passthru",
785 "Set on/off to enable/disable igd passthrou", &error_abort);
786
787 object_class_property_add_str(oc, "firmware",
788 machine_get_firmware, machine_set_firmware,
789 &error_abort);
790 object_class_property_set_description(oc, "firmware",
791 "Firmware image", &error_abort);
792
793 object_class_property_add_bool(oc, "suppress-vmdesc",
794 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
795 &error_abort);
796 object_class_property_set_description(oc, "suppress-vmdesc",
797 "Set on to disable self-describing migration", &error_abort);
798
799 object_class_property_add_bool(oc, "enforce-config-section",
800 machine_get_enforce_config_section, machine_set_enforce_config_section,
801 &error_abort);
802 object_class_property_set_description(oc, "enforce-config-section",
803 "Set on to enforce configuration section migration", &error_abort);
db588194
BS
804
805 object_class_property_add_str(oc, "memory-encryption",
806 machine_get_memory_encryption, machine_set_memory_encryption,
807 &error_abort);
808 object_class_property_set_description(oc, "memory-encryption",
bfec23a0 809 "Set memory encryption object to use", &error_abort);
076b35b5
ND
810}
811
dcb3d601
EH
812static void machine_class_base_init(ObjectClass *oc, void *data)
813{
814 if (!object_class_is_abstract(oc)) {
98cec76a 815 MachineClass *mc = MACHINE_CLASS(oc);
dcb3d601
EH
816 const char *cname = object_class_get_name(oc);
817 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
98cec76a
EH
818 mc->name = g_strndup(cname,
819 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
b66bbee3 820 mc->compat_props = g_ptr_array_new();
dcb3d601
EH
821 }
822}
823
6b1b1440
MA
824static void machine_initfn(Object *obj)
825{
33cd52b5 826 MachineState *ms = MACHINE(obj);
b2fc91db 827 MachineClass *mc = MACHINE_GET_CLASS(obj);
33cd52b5 828
d8870d02 829 ms->kernel_irqchip_allowed = true;
b2fc91db 830 ms->kernel_irqchip_split = mc->default_kernel_irqchip_split;
4689b77b 831 ms->kvm_shadow_mem = -1;
47c8ca53 832 ms->dump_guest_core = true;
75cc7f01 833 ms->mem_merge = true;
cfc58cf3 834 ms->enable_graphics = true;
d8870d02 835
f6a0d06b
EA
836 if (mc->nvdimm_supported) {
837 Object *obj = OBJECT(ms);
838
839 ms->nvdimms_state = g_new0(NVDIMMState, 1);
840 object_property_add_bool(obj, "nvdimm",
841 machine_get_nvdimm, machine_set_nvdimm,
842 &error_abort);
843 object_property_set_description(obj, "nvdimm",
844 "Set on/off to enable/disable "
845 "NVDIMM instantiation", NULL);
846
847 object_property_add_str(obj, "nvdimm-persistence",
848 machine_get_nvdimm_persistence,
849 machine_set_nvdimm_persistence,
850 &error_abort);
851 object_property_set_description(obj, "nvdimm-persistence",
852 "Set NVDIMM persistence"
853 "Valid values are cpu, mem-ctrl",
854 NULL);
855 }
856
857
33cd52b5
AG
858 /* Register notifier when init is done for sysbus sanity checks */
859 ms->sysbus_notifier.notify = machine_init_notify;
860 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
6b1b1440
MA
861}
862
863static void machine_finalize(Object *obj)
864{
865 MachineState *ms = MACHINE(obj);
866
867 g_free(ms->accel);
868 g_free(ms->kernel_filename);
869 g_free(ms->initrd_filename);
870 g_free(ms->kernel_cmdline);
871 g_free(ms->dtb);
872 g_free(ms->dumpdtb);
873 g_free(ms->dt_compatible);
874 g_free(ms->firmware);
2ff4f67c 875 g_free(ms->device_memory);
f6a0d06b 876 g_free(ms->nvdimms_state);
6b1b1440 877}
36d20cb2 878
5e97b623
MA
879bool machine_usb(MachineState *machine)
880{
881 return machine->usb;
882}
883
d8870d02
MA
884bool machine_kernel_irqchip_allowed(MachineState *machine)
885{
886 return machine->kernel_irqchip_allowed;
887}
888
889bool machine_kernel_irqchip_required(MachineState *machine)
890{
891 return machine->kernel_irqchip_required;
892}
893
32c18a2d
MG
894bool machine_kernel_irqchip_split(MachineState *machine)
895{
896 return machine->kernel_irqchip_split;
897}
898
4689b77b
MA
899int machine_kvm_shadow_mem(MachineState *machine)
900{
901 return machine->kvm_shadow_mem;
902}
903
6cabe7fa
MA
904int machine_phandle_start(MachineState *machine)
905{
906 return machine->phandle_start;
907}
908
47c8ca53
MA
909bool machine_dump_guest_core(MachineState *machine)
910{
911 return machine->dump_guest_core;
912}
913
75cc7f01
MA
914bool machine_mem_merge(MachineState *machine)
915{
916 return machine->mem_merge;
917}
918
ec78f811
IM
919static char *cpu_slot_to_string(const CPUArchId *cpu)
920{
921 GString *s = g_string_new(NULL);
922 if (cpu->props.has_socket_id) {
923 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
924 }
925 if (cpu->props.has_core_id) {
926 if (s->len) {
927 g_string_append_printf(s, ", ");
928 }
929 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
930 }
931 if (cpu->props.has_thread_id) {
932 if (s->len) {
933 g_string_append_printf(s, ", ");
934 }
935 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
936 }
937 return g_string_free(s, false);
938}
939
7a3099fc 940static void machine_numa_finish_cpu_init(MachineState *machine)
ec78f811
IM
941{
942 int i;
60bed6a3 943 bool default_mapping;
ec78f811
IM
944 GString *s = g_string_new(NULL);
945 MachineClass *mc = MACHINE_GET_CLASS(machine);
946 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
947
948 assert(nb_numa_nodes);
60bed6a3
IM
949 for (i = 0; i < possible_cpus->len; i++) {
950 if (possible_cpus->cpus[i].props.has_node_id) {
951 break;
952 }
953 }
954 default_mapping = (i == possible_cpus->len);
955
ec78f811
IM
956 for (i = 0; i < possible_cpus->len; i++) {
957 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
958
ec78f811 959 if (!cpu_slot->props.has_node_id) {
d41f3e75
IM
960 /* fetch default mapping from board and enable it */
961 CpuInstanceProperties props = cpu_slot->props;
962
79e07936 963 props.node_id = mc->get_default_cpu_node_id(machine, i);
d41f3e75 964 if (!default_mapping) {
60bed6a3
IM
965 /* record slots with not set mapping,
966 * TODO: make it hard error in future */
967 char *cpu_str = cpu_slot_to_string(cpu_slot);
968 g_string_append_printf(s, "%sCPU %d [%s]",
969 s->len ? ", " : "", i, cpu_str);
970 g_free(cpu_str);
d41f3e75
IM
971
972 /* non mapped cpus used to fallback to node 0 */
973 props.node_id = 0;
60bed6a3 974 }
d41f3e75
IM
975
976 props.has_node_id = true;
977 machine_set_cpu_numa_node(machine, &props, &error_fatal);
ec78f811
IM
978 }
979 }
c6ff347c 980 if (s->len && !qtest_enabled()) {
3dc6f869
AF
981 warn_report("CPU(s) not present in any NUMA nodes: %s",
982 s->str);
983 warn_report("All CPU(s) up to maxcpus should be described "
984 "in NUMA config, ability to start up with partial NUMA "
985 "mappings is obsoleted and will be removed in future");
ec78f811
IM
986 }
987 g_string_free(s, true);
988}
989
482dfe9a
IM
990void machine_run_board_init(MachineState *machine)
991{
992 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
ec78f811 993
7747abf1 994 numa_complete_configuration(machine);
3aeaac8f 995 if (nb_numa_nodes) {
7a3099fc 996 machine_numa_finish_cpu_init(machine);
3aeaac8f 997 }
c9cf636d
AF
998
999 /* If the machine supports the valid_cpu_types check and the user
1000 * specified a CPU with -cpu check here that the user CPU is supported.
1001 */
1002 if (machine_class->valid_cpu_types && machine->cpu_type) {
1003 ObjectClass *class = object_class_by_name(machine->cpu_type);
1004 int i;
1005
1006 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1007 if (object_class_dynamic_cast(class,
1008 machine_class->valid_cpu_types[i])) {
1009 /* The user specificed CPU is in the valid field, we are
1010 * good to go.
1011 */
1012 break;
1013 }
1014 }
1015
1016 if (!machine_class->valid_cpu_types[i]) {
1017 /* The user specified CPU is not valid */
1018 error_report("Invalid CPU type: %s", machine->cpu_type);
1019 error_printf("The valid types are: %s",
1020 machine_class->valid_cpu_types[0]);
1021 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1022 error_printf(", %s", machine_class->valid_cpu_types[i]);
1023 }
1024 error_printf("\n");
1025
1026 exit(1);
1027 }
1028 }
1029
482dfe9a
IM
1030 machine_class->init(machine);
1031}
1032
36d20cb2
MA
1033static const TypeInfo machine_info = {
1034 .name = TYPE_MACHINE,
1035 .parent = TYPE_OBJECT,
1036 .abstract = true,
1037 .class_size = sizeof(MachineClass),
076b35b5 1038 .class_init = machine_class_init,
dcb3d601 1039 .class_base_init = machine_class_base_init,
36d20cb2 1040 .instance_size = sizeof(MachineState),
6b1b1440
MA
1041 .instance_init = machine_initfn,
1042 .instance_finalize = machine_finalize,
36d20cb2
MA
1043};
1044
1045static void machine_register_types(void)
1046{
1047 type_register_static(&machine_info);
1048}
1049
1050type_init(machine_register_types)