compat: disable edid on virtio-gpu base device
[qemu.git] / hw / core / machine.c
blob32d1ca9abc5a2025c81481de1d17552398188e73
1 /*
2 * QEMU Machine
4 * Copyright (C) 2014 Red Hat Inc
6 * Authors:
7 * Marcel Apfelbaum <marcel.a@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qemu/option.h"
15 #include "qapi/qmp/qerror.h"
16 #include "sysemu/replay.h"
17 #include "qemu/units.h"
18 #include "hw/boards.h"
19 #include "qapi/error.h"
20 #include "qapi/qapi-visit-common.h"
21 #include "qapi/visitor.h"
22 #include "hw/sysbus.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/numa.h"
25 #include "qemu/error-report.h"
26 #include "sysemu/qtest.h"
27 #include "hw/pci/pci.h"
28 #include "hw/mem/nvdimm.h"
30 GlobalProperty hw_compat_4_0[] = {
31 { "VGA", "edid", "false" },
32 { "secondary-vga", "edid", "false" },
33 { "bochs-display", "edid", "false" },
34 { "virtio-vga", "edid", "false" },
35 { "virtio-gpu", "edid", "false" },
36 { "virtio-device", "use-started", "false" },
37 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
38 { "pl031", "migrate-tick-offset", "false" },
40 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
42 GlobalProperty hw_compat_3_1[] = {
43 { "pcie-root-port", "x-speed", "2_5" },
44 { "pcie-root-port", "x-width", "1" },
45 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
46 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
47 { "tpm-crb", "ppi", "false" },
48 { "tpm-tis", "ppi", "false" },
49 { "usb-kbd", "serial", "42" },
50 { "usb-mouse", "serial", "42" },
51 { "usb-tablet", "serial", "42" },
52 { "virtio-blk-device", "discard", "false" },
53 { "virtio-blk-device", "write-zeroes", "false" },
54 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
55 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
57 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
59 GlobalProperty hw_compat_3_0[] = {};
60 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
62 GlobalProperty hw_compat_2_12[] = {
63 { "migration", "decompress-error-check", "off" },
64 { "hda-audio", "use-timer", "false" },
65 { "cirrus-vga", "global-vmstate", "true" },
66 { "VGA", "global-vmstate", "true" },
67 { "vmware-svga", "global-vmstate", "true" },
68 { "qxl-vga", "global-vmstate", "true" },
70 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
72 GlobalProperty hw_compat_2_11[] = {
73 { "hpet", "hpet-offset-saved", "false" },
74 { "virtio-blk-pci", "vectors", "2" },
75 { "vhost-user-blk-pci", "vectors", "2" },
76 { "e1000", "migrate_tso_props", "off" },
78 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
80 GlobalProperty hw_compat_2_10[] = {
81 { "virtio-mouse-device", "wheel-axis", "false" },
82 { "virtio-tablet-device", "wheel-axis", "false" },
84 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
86 GlobalProperty hw_compat_2_9[] = {
87 { "pci-bridge", "shpc", "off" },
88 { "intel-iommu", "pt", "off" },
89 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
90 { "pcie-root-port", "x-migrate-msix", "false" },
92 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
94 GlobalProperty hw_compat_2_8[] = {
95 { "fw_cfg_mem", "x-file-slots", "0x10" },
96 { "fw_cfg_io", "x-file-slots", "0x10" },
97 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
98 { "pci-bridge", "shpc", "on" },
99 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
100 { "virtio-pci", "x-pcie-deverr-init", "off" },
101 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
102 { "virtio-pci", "x-pcie-pm-init", "off" },
103 { "cirrus-vga", "vgamem_mb", "8" },
104 { "isa-cirrus-vga", "vgamem_mb", "8" },
106 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
108 GlobalProperty hw_compat_2_7[] = {
109 { "virtio-pci", "page-per-vq", "on" },
110 { "virtio-serial-device", "emergency-write", "off" },
111 { "ioapic", "version", "0x11" },
112 { "intel-iommu", "x-buggy-eim", "true" },
113 { "virtio-pci", "x-ignore-backend-features", "on" },
115 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
117 GlobalProperty hw_compat_2_6[] = {
118 { "virtio-mmio", "format_transport_address", "off" },
119 /* Optional because not all virtio-pci devices support legacy mode */
120 { "virtio-pci", "disable-modern", "on", .optional = true },
121 { "virtio-pci", "disable-legacy", "off", .optional = true },
123 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
125 GlobalProperty hw_compat_2_5[] = {
126 { "isa-fdc", "fallback", "144" },
127 { "pvscsi", "x-old-pci-configuration", "on" },
128 { "pvscsi", "x-disable-pcie", "on" },
129 { "vmxnet3", "x-old-msi-offsets", "on" },
130 { "vmxnet3", "x-disable-pcie", "on" },
132 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
134 GlobalProperty hw_compat_2_4[] = {
135 { "virtio-blk-device", "scsi", "true" },
136 { "e1000", "extra_mac_registers", "off" },
137 { "virtio-pci", "x-disable-pcie", "on" },
138 { "virtio-pci", "migrate-extra", "off" },
139 { "fw_cfg_mem", "dma_enabled", "off" },
140 { "fw_cfg_io", "dma_enabled", "off" }
142 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
144 GlobalProperty hw_compat_2_3[] = {
145 { "virtio-blk-pci", "any_layout", "off" },
146 { "virtio-balloon-pci", "any_layout", "off" },
147 { "virtio-serial-pci", "any_layout", "off" },
148 { "virtio-9p-pci", "any_layout", "off" },
149 { "virtio-rng-pci", "any_layout", "off" },
150 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
151 { "migration", "send-configuration", "off" },
152 { "migration", "send-section-footer", "off" },
153 { "migration", "store-global-state", "off" },
155 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
157 GlobalProperty hw_compat_2_2[] = {};
158 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
160 GlobalProperty hw_compat_2_1[] = {
161 { "intel-hda", "old_msi_addr", "on" },
162 { "VGA", "qemu-extended-regs", "off" },
163 { "secondary-vga", "qemu-extended-regs", "off" },
164 { "virtio-scsi-pci", "any_layout", "off" },
165 { "usb-mouse", "usb_version", "1" },
166 { "usb-kbd", "usb_version", "1" },
167 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
169 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
171 static char *machine_get_accel(Object *obj, Error **errp)
173 MachineState *ms = MACHINE(obj);
175 return g_strdup(ms->accel);
178 static void machine_set_accel(Object *obj, const char *value, Error **errp)
180 MachineState *ms = MACHINE(obj);
182 g_free(ms->accel);
183 ms->accel = g_strdup(value);
186 static void machine_set_kernel_irqchip(Object *obj, Visitor *v,
187 const char *name, void *opaque,
188 Error **errp)
190 Error *err = NULL;
191 MachineState *ms = MACHINE(obj);
192 OnOffSplit mode;
194 visit_type_OnOffSplit(v, name, &mode, &err);
195 if (err) {
196 error_propagate(errp, err);
197 return;
198 } else {
199 switch (mode) {
200 case ON_OFF_SPLIT_ON:
201 ms->kernel_irqchip_allowed = true;
202 ms->kernel_irqchip_required = true;
203 ms->kernel_irqchip_split = false;
204 break;
205 case ON_OFF_SPLIT_OFF:
206 ms->kernel_irqchip_allowed = false;
207 ms->kernel_irqchip_required = false;
208 ms->kernel_irqchip_split = false;
209 break;
210 case ON_OFF_SPLIT_SPLIT:
211 ms->kernel_irqchip_allowed = true;
212 ms->kernel_irqchip_required = true;
213 ms->kernel_irqchip_split = true;
214 break;
215 default:
216 /* The value was checked in visit_type_OnOffSplit() above. If
217 * we get here, then something is wrong in QEMU.
219 abort();
224 static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
225 const char *name, void *opaque,
226 Error **errp)
228 MachineState *ms = MACHINE(obj);
229 int64_t value = ms->kvm_shadow_mem;
231 visit_type_int(v, name, &value, errp);
234 static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
235 const char *name, void *opaque,
236 Error **errp)
238 MachineState *ms = MACHINE(obj);
239 Error *error = NULL;
240 int64_t value;
242 visit_type_int(v, name, &value, &error);
243 if (error) {
244 error_propagate(errp, error);
245 return;
248 ms->kvm_shadow_mem = value;
251 static char *machine_get_kernel(Object *obj, Error **errp)
253 MachineState *ms = MACHINE(obj);
255 return g_strdup(ms->kernel_filename);
258 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
260 MachineState *ms = MACHINE(obj);
262 g_free(ms->kernel_filename);
263 ms->kernel_filename = g_strdup(value);
266 static char *machine_get_initrd(Object *obj, Error **errp)
268 MachineState *ms = MACHINE(obj);
270 return g_strdup(ms->initrd_filename);
273 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
275 MachineState *ms = MACHINE(obj);
277 g_free(ms->initrd_filename);
278 ms->initrd_filename = g_strdup(value);
281 static char *machine_get_append(Object *obj, Error **errp)
283 MachineState *ms = MACHINE(obj);
285 return g_strdup(ms->kernel_cmdline);
288 static void machine_set_append(Object *obj, const char *value, Error **errp)
290 MachineState *ms = MACHINE(obj);
292 g_free(ms->kernel_cmdline);
293 ms->kernel_cmdline = g_strdup(value);
296 static char *machine_get_dtb(Object *obj, Error **errp)
298 MachineState *ms = MACHINE(obj);
300 return g_strdup(ms->dtb);
303 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
305 MachineState *ms = MACHINE(obj);
307 g_free(ms->dtb);
308 ms->dtb = g_strdup(value);
311 static char *machine_get_dumpdtb(Object *obj, Error **errp)
313 MachineState *ms = MACHINE(obj);
315 return g_strdup(ms->dumpdtb);
318 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
320 MachineState *ms = MACHINE(obj);
322 g_free(ms->dumpdtb);
323 ms->dumpdtb = g_strdup(value);
326 static void machine_get_phandle_start(Object *obj, Visitor *v,
327 const char *name, void *opaque,
328 Error **errp)
330 MachineState *ms = MACHINE(obj);
331 int64_t value = ms->phandle_start;
333 visit_type_int(v, name, &value, errp);
336 static void machine_set_phandle_start(Object *obj, Visitor *v,
337 const char *name, void *opaque,
338 Error **errp)
340 MachineState *ms = MACHINE(obj);
341 Error *error = NULL;
342 int64_t value;
344 visit_type_int(v, name, &value, &error);
345 if (error) {
346 error_propagate(errp, error);
347 return;
350 ms->phandle_start = value;
353 static char *machine_get_dt_compatible(Object *obj, Error **errp)
355 MachineState *ms = MACHINE(obj);
357 return g_strdup(ms->dt_compatible);
360 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
362 MachineState *ms = MACHINE(obj);
364 g_free(ms->dt_compatible);
365 ms->dt_compatible = g_strdup(value);
368 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
370 MachineState *ms = MACHINE(obj);
372 return ms->dump_guest_core;
375 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
377 MachineState *ms = MACHINE(obj);
379 ms->dump_guest_core = value;
382 static bool machine_get_mem_merge(Object *obj, Error **errp)
384 MachineState *ms = MACHINE(obj);
386 return ms->mem_merge;
389 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
391 MachineState *ms = MACHINE(obj);
393 ms->mem_merge = value;
396 static bool machine_get_usb(Object *obj, Error **errp)
398 MachineState *ms = MACHINE(obj);
400 return ms->usb;
403 static void machine_set_usb(Object *obj, bool value, Error **errp)
405 MachineState *ms = MACHINE(obj);
407 ms->usb = value;
408 ms->usb_disabled = !value;
411 static bool machine_get_graphics(Object *obj, Error **errp)
413 MachineState *ms = MACHINE(obj);
415 return ms->enable_graphics;
418 static void machine_set_graphics(Object *obj, bool value, Error **errp)
420 MachineState *ms = MACHINE(obj);
422 ms->enable_graphics = value;
425 static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
427 MachineState *ms = MACHINE(obj);
429 return ms->igd_gfx_passthru;
432 static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
434 MachineState *ms = MACHINE(obj);
436 ms->igd_gfx_passthru = value;
439 static char *machine_get_firmware(Object *obj, Error **errp)
441 MachineState *ms = MACHINE(obj);
443 return g_strdup(ms->firmware);
446 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
448 MachineState *ms = MACHINE(obj);
450 g_free(ms->firmware);
451 ms->firmware = g_strdup(value);
454 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
456 MachineState *ms = MACHINE(obj);
458 ms->suppress_vmdesc = value;
461 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
463 MachineState *ms = MACHINE(obj);
465 return ms->suppress_vmdesc;
468 static void machine_set_enforce_config_section(Object *obj, bool value,
469 Error **errp)
471 MachineState *ms = MACHINE(obj);
473 warn_report("enforce-config-section is deprecated, please use "
474 "-global migration.send-configuration=on|off instead");
476 ms->enforce_config_section = value;
479 static bool machine_get_enforce_config_section(Object *obj, Error **errp)
481 MachineState *ms = MACHINE(obj);
483 return ms->enforce_config_section;
486 static char *machine_get_memory_encryption(Object *obj, Error **errp)
488 MachineState *ms = MACHINE(obj);
490 return g_strdup(ms->memory_encryption);
493 static void machine_set_memory_encryption(Object *obj, const char *value,
494 Error **errp)
496 MachineState *ms = MACHINE(obj);
498 g_free(ms->memory_encryption);
499 ms->memory_encryption = g_strdup(value);
502 static bool machine_get_nvdimm(Object *obj, Error **errp)
504 MachineState *ms = MACHINE(obj);
506 return ms->nvdimms_state->is_enabled;
509 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
511 MachineState *ms = MACHINE(obj);
513 ms->nvdimms_state->is_enabled = value;
516 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
518 MachineState *ms = MACHINE(obj);
520 return g_strdup(ms->nvdimms_state->persistence_string);
523 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
524 Error **errp)
526 MachineState *ms = MACHINE(obj);
527 NVDIMMState *nvdimms_state = ms->nvdimms_state;
529 if (strcmp(value, "cpu") == 0) {
530 nvdimms_state->persistence = 3;
531 } else if (strcmp(value, "mem-ctrl") == 0) {
532 nvdimms_state->persistence = 2;
533 } else {
534 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
535 value);
536 return;
539 g_free(nvdimms_state->persistence_string);
540 nvdimms_state->persistence_string = g_strdup(value);
543 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
545 strList *item = g_new0(strList, 1);
547 item->value = g_strdup(type);
548 item->next = mc->allowed_dynamic_sysbus_devices;
549 mc->allowed_dynamic_sysbus_devices = item;
552 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
554 MachineState *machine = opaque;
555 MachineClass *mc = MACHINE_GET_CLASS(machine);
556 bool allowed = false;
557 strList *wl;
559 for (wl = mc->allowed_dynamic_sysbus_devices;
560 !allowed && wl;
561 wl = wl->next) {
562 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
565 if (!allowed) {
566 error_report("Option '-device %s' cannot be handled by this machine",
567 object_class_get_name(object_get_class(OBJECT(sbdev))));
568 exit(1);
572 static void machine_init_notify(Notifier *notifier, void *data)
574 MachineState *machine = MACHINE(qdev_get_machine());
577 * Loop through all dynamically created sysbus devices and check if they are
578 * all allowed. If a device is not allowed, error out.
580 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
583 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
585 int i;
586 HotpluggableCPUList *head = NULL;
587 MachineClass *mc = MACHINE_GET_CLASS(machine);
589 /* force board to initialize possible_cpus if it hasn't been done yet */
590 mc->possible_cpu_arch_ids(machine);
592 for (i = 0; i < machine->possible_cpus->len; i++) {
593 Object *cpu;
594 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
595 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
597 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
598 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
599 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
600 sizeof(*cpu_item->props));
602 cpu = machine->possible_cpus->cpus[i].cpu;
603 if (cpu) {
604 cpu_item->has_qom_path = true;
605 cpu_item->qom_path = object_get_canonical_path(cpu);
607 list_item->value = cpu_item;
608 list_item->next = head;
609 head = list_item;
611 return head;
615 * machine_set_cpu_numa_node:
616 * @machine: machine object to modify
617 * @props: specifies which cpu objects to assign to
618 * numa node specified by @props.node_id
619 * @errp: if an error occurs, a pointer to an area to store the error
621 * Associate NUMA node specified by @props.node_id with cpu slots that
622 * match socket/core/thread-ids specified by @props. It's recommended to use
623 * query-hotpluggable-cpus.props values to specify affected cpu slots,
624 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
626 * However for CLI convenience it's possible to pass in subset of properties,
627 * which would affect all cpu slots that match it.
628 * Ex for pc machine:
629 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
630 * -numa cpu,node-id=0,socket_id=0 \
631 * -numa cpu,node-id=1,socket_id=1
632 * will assign all child cores of socket 0 to node 0 and
633 * of socket 1 to node 1.
635 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
636 * return error.
637 * Empty subset is disallowed and function will return with error in this case.
639 void machine_set_cpu_numa_node(MachineState *machine,
640 const CpuInstanceProperties *props, Error **errp)
642 MachineClass *mc = MACHINE_GET_CLASS(machine);
643 bool match = false;
644 int i;
646 if (!mc->possible_cpu_arch_ids) {
647 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
648 return;
651 /* disabling node mapping is not supported, forbid it */
652 assert(props->has_node_id);
654 /* force board to initialize possible_cpus if it hasn't been done yet */
655 mc->possible_cpu_arch_ids(machine);
657 for (i = 0; i < machine->possible_cpus->len; i++) {
658 CPUArchId *slot = &machine->possible_cpus->cpus[i];
660 /* reject unsupported by board properties */
661 if (props->has_thread_id && !slot->props.has_thread_id) {
662 error_setg(errp, "thread-id is not supported");
663 return;
666 if (props->has_core_id && !slot->props.has_core_id) {
667 error_setg(errp, "core-id is not supported");
668 return;
671 if (props->has_socket_id && !slot->props.has_socket_id) {
672 error_setg(errp, "socket-id is not supported");
673 return;
676 if (props->has_die_id && !slot->props.has_die_id) {
677 error_setg(errp, "die-id is not supported");
678 return;
681 /* skip slots with explicit mismatch */
682 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
683 continue;
686 if (props->has_core_id && props->core_id != slot->props.core_id) {
687 continue;
690 if (props->has_die_id && props->die_id != slot->props.die_id) {
691 continue;
694 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
695 continue;
698 /* reject assignment if slot is already assigned, for compatibility
699 * of legacy cpu_index mapping with SPAPR core based mapping do not
700 * error out if cpu thread and matched core have the same node-id */
701 if (slot->props.has_node_id &&
702 slot->props.node_id != props->node_id) {
703 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
704 slot->props.node_id);
705 return;
708 /* assign slot to node as it's matched '-numa cpu' key */
709 match = true;
710 slot->props.node_id = props->node_id;
711 slot->props.has_node_id = props->has_node_id;
714 if (!match) {
715 error_setg(errp, "no match found");
719 static void smp_parse(MachineState *ms, QemuOpts *opts)
721 if (opts) {
722 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
723 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
724 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
725 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
727 /* compute missing values, prefer sockets over cores over threads */
728 if (cpus == 0 || sockets == 0) {
729 cores = cores > 0 ? cores : 1;
730 threads = threads > 0 ? threads : 1;
731 if (cpus == 0) {
732 sockets = sockets > 0 ? sockets : 1;
733 cpus = cores * threads * sockets;
734 } else {
735 ms->smp.max_cpus =
736 qemu_opt_get_number(opts, "maxcpus", cpus);
737 sockets = ms->smp.max_cpus / (cores * threads);
739 } else if (cores == 0) {
740 threads = threads > 0 ? threads : 1;
741 cores = cpus / (sockets * threads);
742 cores = cores > 0 ? cores : 1;
743 } else if (threads == 0) {
744 threads = cpus / (cores * sockets);
745 threads = threads > 0 ? threads : 1;
746 } else if (sockets * cores * threads < cpus) {
747 error_report("cpu topology: "
748 "sockets (%u) * cores (%u) * threads (%u) < "
749 "smp_cpus (%u)",
750 sockets, cores, threads, cpus);
751 exit(1);
754 ms->smp.max_cpus =
755 qemu_opt_get_number(opts, "maxcpus", cpus);
757 if (ms->smp.max_cpus < cpus) {
758 error_report("maxcpus must be equal to or greater than smp");
759 exit(1);
762 if (sockets * cores * threads > ms->smp.max_cpus) {
763 error_report("cpu topology: "
764 "sockets (%u) * cores (%u) * threads (%u) > "
765 "maxcpus (%u)",
766 sockets, cores, threads,
767 ms->smp.max_cpus);
768 exit(1);
771 if (sockets * cores * threads != ms->smp.max_cpus) {
772 warn_report("Invalid CPU topology deprecated: "
773 "sockets (%u) * cores (%u) * threads (%u) "
774 "!= maxcpus (%u)",
775 sockets, cores, threads,
776 ms->smp.max_cpus);
779 ms->smp.cpus = cpus;
780 ms->smp.cores = cores;
781 ms->smp.threads = threads;
784 if (ms->smp.cpus > 1) {
785 Error *blocker = NULL;
786 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
787 replay_add_blocker(blocker);
791 static void machine_class_init(ObjectClass *oc, void *data)
793 MachineClass *mc = MACHINE_CLASS(oc);
795 /* Default 128 MB as guest ram size */
796 mc->default_ram_size = 128 * MiB;
797 mc->rom_file_has_mr = true;
798 mc->smp_parse = smp_parse;
800 /* numa node memory size aligned on 8MB by default.
801 * On Linux, each node's border has to be 8MB aligned
803 mc->numa_mem_align_shift = 23;
804 mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
806 object_class_property_add_str(oc, "accel",
807 machine_get_accel, machine_set_accel, &error_abort);
808 object_class_property_set_description(oc, "accel",
809 "Accelerator list", &error_abort);
811 object_class_property_add(oc, "kernel-irqchip", "on|off|split",
812 NULL, machine_set_kernel_irqchip,
813 NULL, NULL, &error_abort);
814 object_class_property_set_description(oc, "kernel-irqchip",
815 "Configure KVM in-kernel irqchip", &error_abort);
817 object_class_property_add(oc, "kvm-shadow-mem", "int",
818 machine_get_kvm_shadow_mem, machine_set_kvm_shadow_mem,
819 NULL, NULL, &error_abort);
820 object_class_property_set_description(oc, "kvm-shadow-mem",
821 "KVM shadow MMU size", &error_abort);
823 object_class_property_add_str(oc, "kernel",
824 machine_get_kernel, machine_set_kernel, &error_abort);
825 object_class_property_set_description(oc, "kernel",
826 "Linux kernel image file", &error_abort);
828 object_class_property_add_str(oc, "initrd",
829 machine_get_initrd, machine_set_initrd, &error_abort);
830 object_class_property_set_description(oc, "initrd",
831 "Linux initial ramdisk file", &error_abort);
833 object_class_property_add_str(oc, "append",
834 machine_get_append, machine_set_append, &error_abort);
835 object_class_property_set_description(oc, "append",
836 "Linux kernel command line", &error_abort);
838 object_class_property_add_str(oc, "dtb",
839 machine_get_dtb, machine_set_dtb, &error_abort);
840 object_class_property_set_description(oc, "dtb",
841 "Linux kernel device tree file", &error_abort);
843 object_class_property_add_str(oc, "dumpdtb",
844 machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
845 object_class_property_set_description(oc, "dumpdtb",
846 "Dump current dtb to a file and quit", &error_abort);
848 object_class_property_add(oc, "phandle-start", "int",
849 machine_get_phandle_start, machine_set_phandle_start,
850 NULL, NULL, &error_abort);
851 object_class_property_set_description(oc, "phandle-start",
852 "The first phandle ID we may generate dynamically", &error_abort);
854 object_class_property_add_str(oc, "dt-compatible",
855 machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
856 object_class_property_set_description(oc, "dt-compatible",
857 "Overrides the \"compatible\" property of the dt root node",
858 &error_abort);
860 object_class_property_add_bool(oc, "dump-guest-core",
861 machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
862 object_class_property_set_description(oc, "dump-guest-core",
863 "Include guest memory in a core dump", &error_abort);
865 object_class_property_add_bool(oc, "mem-merge",
866 machine_get_mem_merge, machine_set_mem_merge, &error_abort);
867 object_class_property_set_description(oc, "mem-merge",
868 "Enable/disable memory merge support", &error_abort);
870 object_class_property_add_bool(oc, "usb",
871 machine_get_usb, machine_set_usb, &error_abort);
872 object_class_property_set_description(oc, "usb",
873 "Set on/off to enable/disable usb", &error_abort);
875 object_class_property_add_bool(oc, "graphics",
876 machine_get_graphics, machine_set_graphics, &error_abort);
877 object_class_property_set_description(oc, "graphics",
878 "Set on/off to enable/disable graphics emulation", &error_abort);
880 object_class_property_add_bool(oc, "igd-passthru",
881 machine_get_igd_gfx_passthru, machine_set_igd_gfx_passthru,
882 &error_abort);
883 object_class_property_set_description(oc, "igd-passthru",
884 "Set on/off to enable/disable igd passthrou", &error_abort);
886 object_class_property_add_str(oc, "firmware",
887 machine_get_firmware, machine_set_firmware,
888 &error_abort);
889 object_class_property_set_description(oc, "firmware",
890 "Firmware image", &error_abort);
892 object_class_property_add_bool(oc, "suppress-vmdesc",
893 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
894 &error_abort);
895 object_class_property_set_description(oc, "suppress-vmdesc",
896 "Set on to disable self-describing migration", &error_abort);
898 object_class_property_add_bool(oc, "enforce-config-section",
899 machine_get_enforce_config_section, machine_set_enforce_config_section,
900 &error_abort);
901 object_class_property_set_description(oc, "enforce-config-section",
902 "Set on to enforce configuration section migration", &error_abort);
904 object_class_property_add_str(oc, "memory-encryption",
905 machine_get_memory_encryption, machine_set_memory_encryption,
906 &error_abort);
907 object_class_property_set_description(oc, "memory-encryption",
908 "Set memory encryption object to use", &error_abort);
911 static void machine_class_base_init(ObjectClass *oc, void *data)
913 if (!object_class_is_abstract(oc)) {
914 MachineClass *mc = MACHINE_CLASS(oc);
915 const char *cname = object_class_get_name(oc);
916 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
917 mc->name = g_strndup(cname,
918 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
919 mc->compat_props = g_ptr_array_new();
923 static void machine_initfn(Object *obj)
925 MachineState *ms = MACHINE(obj);
926 MachineClass *mc = MACHINE_GET_CLASS(obj);
928 ms->kernel_irqchip_allowed = true;
929 ms->kernel_irqchip_split = mc->default_kernel_irqchip_split;
930 ms->kvm_shadow_mem = -1;
931 ms->dump_guest_core = true;
932 ms->mem_merge = true;
933 ms->enable_graphics = true;
935 if (mc->nvdimm_supported) {
936 Object *obj = OBJECT(ms);
938 ms->nvdimms_state = g_new0(NVDIMMState, 1);
939 object_property_add_bool(obj, "nvdimm",
940 machine_get_nvdimm, machine_set_nvdimm,
941 &error_abort);
942 object_property_set_description(obj, "nvdimm",
943 "Set on/off to enable/disable "
944 "NVDIMM instantiation", NULL);
946 object_property_add_str(obj, "nvdimm-persistence",
947 machine_get_nvdimm_persistence,
948 machine_set_nvdimm_persistence,
949 &error_abort);
950 object_property_set_description(obj, "nvdimm-persistence",
951 "Set NVDIMM persistence"
952 "Valid values are cpu, mem-ctrl",
953 NULL);
957 /* Register notifier when init is done for sysbus sanity checks */
958 ms->sysbus_notifier.notify = machine_init_notify;
959 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
962 static void machine_finalize(Object *obj)
964 MachineState *ms = MACHINE(obj);
966 g_free(ms->accel);
967 g_free(ms->kernel_filename);
968 g_free(ms->initrd_filename);
969 g_free(ms->kernel_cmdline);
970 g_free(ms->dtb);
971 g_free(ms->dumpdtb);
972 g_free(ms->dt_compatible);
973 g_free(ms->firmware);
974 g_free(ms->device_memory);
975 g_free(ms->nvdimms_state);
978 bool machine_usb(MachineState *machine)
980 return machine->usb;
983 bool machine_kernel_irqchip_allowed(MachineState *machine)
985 return machine->kernel_irqchip_allowed;
988 bool machine_kernel_irqchip_required(MachineState *machine)
990 return machine->kernel_irqchip_required;
993 bool machine_kernel_irqchip_split(MachineState *machine)
995 return machine->kernel_irqchip_split;
998 int machine_kvm_shadow_mem(MachineState *machine)
1000 return machine->kvm_shadow_mem;
1003 int machine_phandle_start(MachineState *machine)
1005 return machine->phandle_start;
1008 bool machine_dump_guest_core(MachineState *machine)
1010 return machine->dump_guest_core;
1013 bool machine_mem_merge(MachineState *machine)
1015 return machine->mem_merge;
1018 static char *cpu_slot_to_string(const CPUArchId *cpu)
1020 GString *s = g_string_new(NULL);
1021 if (cpu->props.has_socket_id) {
1022 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1024 if (cpu->props.has_die_id) {
1025 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1027 if (cpu->props.has_core_id) {
1028 if (s->len) {
1029 g_string_append_printf(s, ", ");
1031 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1033 if (cpu->props.has_thread_id) {
1034 if (s->len) {
1035 g_string_append_printf(s, ", ");
1037 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1039 return g_string_free(s, false);
1042 static void machine_numa_finish_cpu_init(MachineState *machine)
1044 int i;
1045 bool default_mapping;
1046 GString *s = g_string_new(NULL);
1047 MachineClass *mc = MACHINE_GET_CLASS(machine);
1048 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1050 assert(nb_numa_nodes);
1051 for (i = 0; i < possible_cpus->len; i++) {
1052 if (possible_cpus->cpus[i].props.has_node_id) {
1053 break;
1056 default_mapping = (i == possible_cpus->len);
1058 for (i = 0; i < possible_cpus->len; i++) {
1059 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1061 if (!cpu_slot->props.has_node_id) {
1062 /* fetch default mapping from board and enable it */
1063 CpuInstanceProperties props = cpu_slot->props;
1065 props.node_id = mc->get_default_cpu_node_id(machine, i);
1066 if (!default_mapping) {
1067 /* record slots with not set mapping,
1068 * TODO: make it hard error in future */
1069 char *cpu_str = cpu_slot_to_string(cpu_slot);
1070 g_string_append_printf(s, "%sCPU %d [%s]",
1071 s->len ? ", " : "", i, cpu_str);
1072 g_free(cpu_str);
1074 /* non mapped cpus used to fallback to node 0 */
1075 props.node_id = 0;
1078 props.has_node_id = true;
1079 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1082 if (s->len && !qtest_enabled()) {
1083 warn_report("CPU(s) not present in any NUMA nodes: %s",
1084 s->str);
1085 warn_report("All CPU(s) up to maxcpus should be described "
1086 "in NUMA config, ability to start up with partial NUMA "
1087 "mappings is obsoleted and will be removed in future");
1089 g_string_free(s, true);
1092 void machine_run_board_init(MachineState *machine)
1094 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1096 numa_complete_configuration(machine);
1097 if (nb_numa_nodes) {
1098 machine_numa_finish_cpu_init(machine);
1101 /* If the machine supports the valid_cpu_types check and the user
1102 * specified a CPU with -cpu check here that the user CPU is supported.
1104 if (machine_class->valid_cpu_types && machine->cpu_type) {
1105 ObjectClass *class = object_class_by_name(machine->cpu_type);
1106 int i;
1108 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1109 if (object_class_dynamic_cast(class,
1110 machine_class->valid_cpu_types[i])) {
1111 /* The user specificed CPU is in the valid field, we are
1112 * good to go.
1114 break;
1118 if (!machine_class->valid_cpu_types[i]) {
1119 /* The user specified CPU is not valid */
1120 error_report("Invalid CPU type: %s", machine->cpu_type);
1121 error_printf("The valid types are: %s",
1122 machine_class->valid_cpu_types[0]);
1123 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1124 error_printf(", %s", machine_class->valid_cpu_types[i]);
1126 error_printf("\n");
1128 exit(1);
1132 machine_class->init(machine);
1135 static const TypeInfo machine_info = {
1136 .name = TYPE_MACHINE,
1137 .parent = TYPE_OBJECT,
1138 .abstract = true,
1139 .class_size = sizeof(MachineClass),
1140 .class_init = machine_class_init,
1141 .class_base_init = machine_class_base_init,
1142 .instance_size = sizeof(MachineState),
1143 .instance_init = machine_initfn,
1144 .instance_finalize = machine_finalize,
1147 static void machine_register_types(void)
1149 type_register_static(&machine_info);
1152 type_init(machine_register_types)