hw/dma: Add SiFive platform DMA controller emulation
[qemu/ar7.git] / hw / core / machine.c
blobea26d612374de592d4c3d82d2050a78e3d05a28e
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"
29 #include "migration/vmstate.h"
31 GlobalProperty hw_compat_5_1[] = {
32 { "vhost-scsi", "num_queues", "1"},
33 { "vhost-user-blk", "num-queues", "1"},
34 { "vhost-user-scsi", "num_queues", "1"},
35 { "virtio-blk-device", "num-queues", "1"},
36 { "virtio-scsi-device", "num_queues", "1"},
38 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
40 GlobalProperty hw_compat_5_0[] = {
41 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
42 { "virtio-balloon-device", "page-poison", "false" },
43 { "vmport", "x-read-set-eax", "off" },
44 { "vmport", "x-signal-unsupported-cmd", "off" },
45 { "vmport", "x-report-vmx-type", "off" },
46 { "vmport", "x-cmds-v2", "off" },
48 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
50 GlobalProperty hw_compat_4_2[] = {
51 { "virtio-blk-device", "queue-size", "128"},
52 { "virtio-scsi-device", "virtqueue_size", "128"},
53 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
54 { "virtio-blk-device", "seg-max-adjust", "off"},
55 { "virtio-scsi-device", "seg_max_adjust", "off"},
56 { "vhost-blk-device", "seg_max_adjust", "off"},
57 { "usb-host", "suppress-remote-wake", "off" },
58 { "usb-redir", "suppress-remote-wake", "off" },
59 { "qxl", "revision", "4" },
60 { "qxl-vga", "revision", "4" },
61 { "fw_cfg", "acpi-mr-restore", "false" },
63 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
65 GlobalProperty hw_compat_4_1[] = {
66 { "virtio-pci", "x-pcie-flr-init", "off" },
67 { "virtio-device", "use-disabled-flag", "false" },
69 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
71 GlobalProperty hw_compat_4_0[] = {
72 { "VGA", "edid", "false" },
73 { "secondary-vga", "edid", "false" },
74 { "bochs-display", "edid", "false" },
75 { "virtio-vga", "edid", "false" },
76 { "virtio-gpu-device", "edid", "false" },
77 { "virtio-device", "use-started", "false" },
78 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
79 { "pl031", "migrate-tick-offset", "false" },
81 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
83 GlobalProperty hw_compat_3_1[] = {
84 { "pcie-root-port", "x-speed", "2_5" },
85 { "pcie-root-port", "x-width", "1" },
86 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
87 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
88 { "tpm-crb", "ppi", "false" },
89 { "tpm-tis", "ppi", "false" },
90 { "usb-kbd", "serial", "42" },
91 { "usb-mouse", "serial", "42" },
92 { "usb-tablet", "serial", "42" },
93 { "virtio-blk-device", "discard", "false" },
94 { "virtio-blk-device", "write-zeroes", "false" },
95 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
96 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
98 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
100 GlobalProperty hw_compat_3_0[] = {};
101 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
103 GlobalProperty hw_compat_2_12[] = {
104 { "migration", "decompress-error-check", "off" },
105 { "hda-audio", "use-timer", "false" },
106 { "cirrus-vga", "global-vmstate", "true" },
107 { "VGA", "global-vmstate", "true" },
108 { "vmware-svga", "global-vmstate", "true" },
109 { "qxl-vga", "global-vmstate", "true" },
111 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
113 GlobalProperty hw_compat_2_11[] = {
114 { "hpet", "hpet-offset-saved", "false" },
115 { "virtio-blk-pci", "vectors", "2" },
116 { "vhost-user-blk-pci", "vectors", "2" },
117 { "e1000", "migrate_tso_props", "off" },
119 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
121 GlobalProperty hw_compat_2_10[] = {
122 { "virtio-mouse-device", "wheel-axis", "false" },
123 { "virtio-tablet-device", "wheel-axis", "false" },
125 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
127 GlobalProperty hw_compat_2_9[] = {
128 { "pci-bridge", "shpc", "off" },
129 { "intel-iommu", "pt", "off" },
130 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
131 { "pcie-root-port", "x-migrate-msix", "false" },
133 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
135 GlobalProperty hw_compat_2_8[] = {
136 { "fw_cfg_mem", "x-file-slots", "0x10" },
137 { "fw_cfg_io", "x-file-slots", "0x10" },
138 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
139 { "pci-bridge", "shpc", "on" },
140 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
141 { "virtio-pci", "x-pcie-deverr-init", "off" },
142 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
143 { "virtio-pci", "x-pcie-pm-init", "off" },
144 { "cirrus-vga", "vgamem_mb", "8" },
145 { "isa-cirrus-vga", "vgamem_mb", "8" },
147 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
149 GlobalProperty hw_compat_2_7[] = {
150 { "virtio-pci", "page-per-vq", "on" },
151 { "virtio-serial-device", "emergency-write", "off" },
152 { "ioapic", "version", "0x11" },
153 { "intel-iommu", "x-buggy-eim", "true" },
154 { "virtio-pci", "x-ignore-backend-features", "on" },
156 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
158 GlobalProperty hw_compat_2_6[] = {
159 { "virtio-mmio", "format_transport_address", "off" },
160 /* Optional because not all virtio-pci devices support legacy mode */
161 { "virtio-pci", "disable-modern", "on", .optional = true },
162 { "virtio-pci", "disable-legacy", "off", .optional = true },
164 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
166 GlobalProperty hw_compat_2_5[] = {
167 { "isa-fdc", "fallback", "144" },
168 { "pvscsi", "x-old-pci-configuration", "on" },
169 { "pvscsi", "x-disable-pcie", "on" },
170 { "vmxnet3", "x-old-msi-offsets", "on" },
171 { "vmxnet3", "x-disable-pcie", "on" },
173 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
175 GlobalProperty hw_compat_2_4[] = {
176 /* Optional because the 'scsi' property is Linux-only */
177 { "virtio-blk-device", "scsi", "true", .optional = true },
178 { "e1000", "extra_mac_registers", "off" },
179 { "virtio-pci", "x-disable-pcie", "on" },
180 { "virtio-pci", "migrate-extra", "off" },
181 { "fw_cfg_mem", "dma_enabled", "off" },
182 { "fw_cfg_io", "dma_enabled", "off" }
184 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
186 GlobalProperty hw_compat_2_3[] = {
187 { "virtio-blk-pci", "any_layout", "off" },
188 { "virtio-balloon-pci", "any_layout", "off" },
189 { "virtio-serial-pci", "any_layout", "off" },
190 { "virtio-9p-pci", "any_layout", "off" },
191 { "virtio-rng-pci", "any_layout", "off" },
192 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
193 { "migration", "send-configuration", "off" },
194 { "migration", "send-section-footer", "off" },
195 { "migration", "store-global-state", "off" },
197 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
199 GlobalProperty hw_compat_2_2[] = {};
200 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
202 GlobalProperty hw_compat_2_1[] = {
203 { "intel-hda", "old_msi_addr", "on" },
204 { "VGA", "qemu-extended-regs", "off" },
205 { "secondary-vga", "qemu-extended-regs", "off" },
206 { "virtio-scsi-pci", "any_layout", "off" },
207 { "usb-mouse", "usb_version", "1" },
208 { "usb-kbd", "usb_version", "1" },
209 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
211 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
213 static char *machine_get_kernel(Object *obj, Error **errp)
215 MachineState *ms = MACHINE(obj);
217 return g_strdup(ms->kernel_filename);
220 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
222 MachineState *ms = MACHINE(obj);
224 g_free(ms->kernel_filename);
225 ms->kernel_filename = g_strdup(value);
228 static char *machine_get_initrd(Object *obj, Error **errp)
230 MachineState *ms = MACHINE(obj);
232 return g_strdup(ms->initrd_filename);
235 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
237 MachineState *ms = MACHINE(obj);
239 g_free(ms->initrd_filename);
240 ms->initrd_filename = g_strdup(value);
243 static char *machine_get_append(Object *obj, Error **errp)
245 MachineState *ms = MACHINE(obj);
247 return g_strdup(ms->kernel_cmdline);
250 static void machine_set_append(Object *obj, const char *value, Error **errp)
252 MachineState *ms = MACHINE(obj);
254 g_free(ms->kernel_cmdline);
255 ms->kernel_cmdline = g_strdup(value);
258 static char *machine_get_dtb(Object *obj, Error **errp)
260 MachineState *ms = MACHINE(obj);
262 return g_strdup(ms->dtb);
265 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
267 MachineState *ms = MACHINE(obj);
269 g_free(ms->dtb);
270 ms->dtb = g_strdup(value);
273 static char *machine_get_dumpdtb(Object *obj, Error **errp)
275 MachineState *ms = MACHINE(obj);
277 return g_strdup(ms->dumpdtb);
280 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
282 MachineState *ms = MACHINE(obj);
284 g_free(ms->dumpdtb);
285 ms->dumpdtb = g_strdup(value);
288 static void machine_get_phandle_start(Object *obj, Visitor *v,
289 const char *name, void *opaque,
290 Error **errp)
292 MachineState *ms = MACHINE(obj);
293 int64_t value = ms->phandle_start;
295 visit_type_int(v, name, &value, errp);
298 static void machine_set_phandle_start(Object *obj, Visitor *v,
299 const char *name, void *opaque,
300 Error **errp)
302 MachineState *ms = MACHINE(obj);
303 int64_t value;
305 if (!visit_type_int(v, name, &value, errp)) {
306 return;
309 ms->phandle_start = value;
312 static char *machine_get_dt_compatible(Object *obj, Error **errp)
314 MachineState *ms = MACHINE(obj);
316 return g_strdup(ms->dt_compatible);
319 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
321 MachineState *ms = MACHINE(obj);
323 g_free(ms->dt_compatible);
324 ms->dt_compatible = g_strdup(value);
327 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
329 MachineState *ms = MACHINE(obj);
331 return ms->dump_guest_core;
334 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
336 MachineState *ms = MACHINE(obj);
338 ms->dump_guest_core = value;
341 static bool machine_get_mem_merge(Object *obj, Error **errp)
343 MachineState *ms = MACHINE(obj);
345 return ms->mem_merge;
348 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
350 MachineState *ms = MACHINE(obj);
352 ms->mem_merge = value;
355 static bool machine_get_usb(Object *obj, Error **errp)
357 MachineState *ms = MACHINE(obj);
359 return ms->usb;
362 static void machine_set_usb(Object *obj, bool value, Error **errp)
364 MachineState *ms = MACHINE(obj);
366 ms->usb = value;
367 ms->usb_disabled = !value;
370 static bool machine_get_graphics(Object *obj, Error **errp)
372 MachineState *ms = MACHINE(obj);
374 return ms->enable_graphics;
377 static void machine_set_graphics(Object *obj, bool value, Error **errp)
379 MachineState *ms = MACHINE(obj);
381 ms->enable_graphics = value;
384 static char *machine_get_firmware(Object *obj, Error **errp)
386 MachineState *ms = MACHINE(obj);
388 return g_strdup(ms->firmware);
391 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
393 MachineState *ms = MACHINE(obj);
395 g_free(ms->firmware);
396 ms->firmware = g_strdup(value);
399 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
401 MachineState *ms = MACHINE(obj);
403 ms->suppress_vmdesc = value;
406 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
408 MachineState *ms = MACHINE(obj);
410 return ms->suppress_vmdesc;
413 static void machine_set_enforce_config_section(Object *obj, bool value,
414 Error **errp)
416 MachineState *ms = MACHINE(obj);
418 warn_report("enforce-config-section is deprecated, please use "
419 "-global migration.send-configuration=on|off instead");
421 ms->enforce_config_section = value;
424 static bool machine_get_enforce_config_section(Object *obj, Error **errp)
426 MachineState *ms = MACHINE(obj);
428 return ms->enforce_config_section;
431 static char *machine_get_memory_encryption(Object *obj, Error **errp)
433 MachineState *ms = MACHINE(obj);
435 return g_strdup(ms->memory_encryption);
438 static void machine_set_memory_encryption(Object *obj, const char *value,
439 Error **errp)
441 MachineState *ms = MACHINE(obj);
443 g_free(ms->memory_encryption);
444 ms->memory_encryption = g_strdup(value);
447 * With memory encryption, the host can't see the real contents of RAM,
448 * so there's no point in it trying to merge areas.
450 if (value) {
451 machine_set_mem_merge(obj, false, errp);
455 static bool machine_get_nvdimm(Object *obj, Error **errp)
457 MachineState *ms = MACHINE(obj);
459 return ms->nvdimms_state->is_enabled;
462 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
464 MachineState *ms = MACHINE(obj);
466 ms->nvdimms_state->is_enabled = value;
469 static bool machine_get_hmat(Object *obj, Error **errp)
471 MachineState *ms = MACHINE(obj);
473 return ms->numa_state->hmat_enabled;
476 static void machine_set_hmat(Object *obj, bool value, Error **errp)
478 MachineState *ms = MACHINE(obj);
480 ms->numa_state->hmat_enabled = value;
483 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
485 MachineState *ms = MACHINE(obj);
487 return g_strdup(ms->nvdimms_state->persistence_string);
490 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
491 Error **errp)
493 MachineState *ms = MACHINE(obj);
494 NVDIMMState *nvdimms_state = ms->nvdimms_state;
496 if (strcmp(value, "cpu") == 0) {
497 nvdimms_state->persistence = 3;
498 } else if (strcmp(value, "mem-ctrl") == 0) {
499 nvdimms_state->persistence = 2;
500 } else {
501 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
502 value);
503 return;
506 g_free(nvdimms_state->persistence_string);
507 nvdimms_state->persistence_string = g_strdup(value);
510 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
512 strList *item = g_new0(strList, 1);
514 item->value = g_strdup(type);
515 item->next = mc->allowed_dynamic_sysbus_devices;
516 mc->allowed_dynamic_sysbus_devices = item;
519 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
521 MachineState *machine = opaque;
522 MachineClass *mc = MACHINE_GET_CLASS(machine);
523 bool allowed = false;
524 strList *wl;
526 for (wl = mc->allowed_dynamic_sysbus_devices;
527 !allowed && wl;
528 wl = wl->next) {
529 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
532 if (!allowed) {
533 error_report("Option '-device %s' cannot be handled by this machine",
534 object_class_get_name(object_get_class(OBJECT(sbdev))));
535 exit(1);
539 static char *machine_get_memdev(Object *obj, Error **errp)
541 MachineState *ms = MACHINE(obj);
543 return g_strdup(ms->ram_memdev_id);
546 static void machine_set_memdev(Object *obj, const char *value, Error **errp)
548 MachineState *ms = MACHINE(obj);
550 g_free(ms->ram_memdev_id);
551 ms->ram_memdev_id = g_strdup(value);
555 static void machine_init_notify(Notifier *notifier, void *data)
557 MachineState *machine = MACHINE(qdev_get_machine());
560 * Loop through all dynamically created sysbus devices and check if they are
561 * all allowed. If a device is not allowed, error out.
563 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
566 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
568 int i;
569 HotpluggableCPUList *head = NULL;
570 MachineClass *mc = MACHINE_GET_CLASS(machine);
572 /* force board to initialize possible_cpus if it hasn't been done yet */
573 mc->possible_cpu_arch_ids(machine);
575 for (i = 0; i < machine->possible_cpus->len; i++) {
576 Object *cpu;
577 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
578 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
580 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
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));
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);
590 list_item->value = cpu_item;
591 list_item->next = head;
592 head = list_item;
594 return head;
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
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.
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.
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.
622 void machine_set_cpu_numa_node(MachineState *machine,
623 const CpuInstanceProperties *props, Error **errp)
625 MachineClass *mc = MACHINE_GET_CLASS(machine);
626 NodeInfo *numa_info = machine->numa_state->nodes;
627 bool match = false;
628 int i;
630 if (!mc->possible_cpu_arch_ids) {
631 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
632 return;
635 /* disabling node mapping is not supported, forbid it */
636 assert(props->has_node_id);
638 /* force board to initialize possible_cpus if it hasn't been done yet */
639 mc->possible_cpu_arch_ids(machine);
641 for (i = 0; i < machine->possible_cpus->len; i++) {
642 CPUArchId *slot = &machine->possible_cpus->cpus[i];
644 /* reject unsupported by board properties */
645 if (props->has_thread_id && !slot->props.has_thread_id) {
646 error_setg(errp, "thread-id is not supported");
647 return;
650 if (props->has_core_id && !slot->props.has_core_id) {
651 error_setg(errp, "core-id is not supported");
652 return;
655 if (props->has_socket_id && !slot->props.has_socket_id) {
656 error_setg(errp, "socket-id is not supported");
657 return;
660 if (props->has_die_id && !slot->props.has_die_id) {
661 error_setg(errp, "die-id is not supported");
662 return;
665 /* skip slots with explicit mismatch */
666 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
667 continue;
670 if (props->has_core_id && props->core_id != slot->props.core_id) {
671 continue;
674 if (props->has_die_id && props->die_id != slot->props.die_id) {
675 continue;
678 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
679 continue;
682 /* reject assignment if slot is already assigned, for compatibility
683 * of legacy cpu_index mapping with SPAPR core based mapping do not
684 * error out if cpu thread and matched core have the same node-id */
685 if (slot->props.has_node_id &&
686 slot->props.node_id != props->node_id) {
687 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
688 slot->props.node_id);
689 return;
692 /* assign slot to node as it's matched '-numa cpu' key */
693 match = true;
694 slot->props.node_id = props->node_id;
695 slot->props.has_node_id = props->has_node_id;
697 if (machine->numa_state->hmat_enabled) {
698 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
699 (props->node_id != numa_info[props->node_id].initiator)) {
700 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
701 " should be itself", props->node_id);
702 return;
704 numa_info[props->node_id].has_cpu = true;
705 numa_info[props->node_id].initiator = props->node_id;
709 if (!match) {
710 error_setg(errp, "no match found");
714 static void smp_parse(MachineState *ms, QemuOpts *opts)
716 if (opts) {
717 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
718 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
719 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
720 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
722 /* compute missing values, prefer sockets over cores over threads */
723 if (cpus == 0 || sockets == 0) {
724 cores = cores > 0 ? cores : 1;
725 threads = threads > 0 ? threads : 1;
726 if (cpus == 0) {
727 sockets = sockets > 0 ? sockets : 1;
728 cpus = cores * threads * sockets;
729 } else {
730 ms->smp.max_cpus =
731 qemu_opt_get_number(opts, "maxcpus", cpus);
732 sockets = ms->smp.max_cpus / (cores * threads);
734 } else if (cores == 0) {
735 threads = threads > 0 ? threads : 1;
736 cores = cpus / (sockets * threads);
737 cores = cores > 0 ? cores : 1;
738 } else if (threads == 0) {
739 threads = cpus / (cores * sockets);
740 threads = threads > 0 ? threads : 1;
741 } else if (sockets * cores * threads < cpus) {
742 error_report("cpu topology: "
743 "sockets (%u) * cores (%u) * threads (%u) < "
744 "smp_cpus (%u)",
745 sockets, cores, threads, cpus);
746 exit(1);
749 ms->smp.max_cpus =
750 qemu_opt_get_number(opts, "maxcpus", cpus);
752 if (ms->smp.max_cpus < cpus) {
753 error_report("maxcpus must be equal to or greater than smp");
754 exit(1);
757 if (sockets * cores * threads > ms->smp.max_cpus) {
758 error_report("cpu topology: "
759 "sockets (%u) * cores (%u) * threads (%u) > "
760 "maxcpus (%u)",
761 sockets, cores, threads,
762 ms->smp.max_cpus);
763 exit(1);
766 if (sockets * cores * threads != ms->smp.max_cpus) {
767 warn_report("Invalid CPU topology deprecated: "
768 "sockets (%u) * cores (%u) * threads (%u) "
769 "!= maxcpus (%u)",
770 sockets, cores, threads,
771 ms->smp.max_cpus);
774 ms->smp.cpus = cpus;
775 ms->smp.cores = cores;
776 ms->smp.threads = threads;
777 ms->smp.sockets = sockets;
780 if (ms->smp.cpus > 1) {
781 Error *blocker = NULL;
782 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
783 replay_add_blocker(blocker);
787 static void machine_class_init(ObjectClass *oc, void *data)
789 MachineClass *mc = MACHINE_CLASS(oc);
791 /* Default 128 MB as guest ram size */
792 mc->default_ram_size = 128 * MiB;
793 mc->rom_file_has_mr = true;
794 mc->smp_parse = smp_parse;
796 /* numa node memory size aligned on 8MB by default.
797 * On Linux, each node's border has to be 8MB aligned
799 mc->numa_mem_align_shift = 23;
800 mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
802 object_class_property_add_str(oc, "kernel",
803 machine_get_kernel, machine_set_kernel);
804 object_class_property_set_description(oc, "kernel",
805 "Linux kernel image file");
807 object_class_property_add_str(oc, "initrd",
808 machine_get_initrd, machine_set_initrd);
809 object_class_property_set_description(oc, "initrd",
810 "Linux initial ramdisk file");
812 object_class_property_add_str(oc, "append",
813 machine_get_append, machine_set_append);
814 object_class_property_set_description(oc, "append",
815 "Linux kernel command line");
817 object_class_property_add_str(oc, "dtb",
818 machine_get_dtb, machine_set_dtb);
819 object_class_property_set_description(oc, "dtb",
820 "Linux kernel device tree file");
822 object_class_property_add_str(oc, "dumpdtb",
823 machine_get_dumpdtb, machine_set_dumpdtb);
824 object_class_property_set_description(oc, "dumpdtb",
825 "Dump current dtb to a file and quit");
827 object_class_property_add(oc, "phandle-start", "int",
828 machine_get_phandle_start, machine_set_phandle_start,
829 NULL, NULL);
830 object_class_property_set_description(oc, "phandle-start",
831 "The first phandle ID we may generate dynamically");
833 object_class_property_add_str(oc, "dt-compatible",
834 machine_get_dt_compatible, machine_set_dt_compatible);
835 object_class_property_set_description(oc, "dt-compatible",
836 "Overrides the \"compatible\" property of the dt root node");
838 object_class_property_add_bool(oc, "dump-guest-core",
839 machine_get_dump_guest_core, machine_set_dump_guest_core);
840 object_class_property_set_description(oc, "dump-guest-core",
841 "Include guest memory in a core dump");
843 object_class_property_add_bool(oc, "mem-merge",
844 machine_get_mem_merge, machine_set_mem_merge);
845 object_class_property_set_description(oc, "mem-merge",
846 "Enable/disable memory merge support");
848 object_class_property_add_bool(oc, "usb",
849 machine_get_usb, machine_set_usb);
850 object_class_property_set_description(oc, "usb",
851 "Set on/off to enable/disable usb");
853 object_class_property_add_bool(oc, "graphics",
854 machine_get_graphics, machine_set_graphics);
855 object_class_property_set_description(oc, "graphics",
856 "Set on/off to enable/disable graphics emulation");
858 object_class_property_add_str(oc, "firmware",
859 machine_get_firmware, machine_set_firmware);
860 object_class_property_set_description(oc, "firmware",
861 "Firmware image");
863 object_class_property_add_bool(oc, "suppress-vmdesc",
864 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
865 object_class_property_set_description(oc, "suppress-vmdesc",
866 "Set on to disable self-describing migration");
868 object_class_property_add_bool(oc, "enforce-config-section",
869 machine_get_enforce_config_section, machine_set_enforce_config_section);
870 object_class_property_set_description(oc, "enforce-config-section",
871 "Set on to enforce configuration section migration");
873 object_class_property_add_str(oc, "memory-encryption",
874 machine_get_memory_encryption, machine_set_memory_encryption);
875 object_class_property_set_description(oc, "memory-encryption",
876 "Set memory encryption object to use");
879 static void machine_class_base_init(ObjectClass *oc, void *data)
881 if (!object_class_is_abstract(oc)) {
882 MachineClass *mc = MACHINE_CLASS(oc);
883 const char *cname = object_class_get_name(oc);
884 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
885 mc->name = g_strndup(cname,
886 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
887 mc->compat_props = g_ptr_array_new();
891 static void machine_initfn(Object *obj)
893 MachineState *ms = MACHINE(obj);
894 MachineClass *mc = MACHINE_GET_CLASS(obj);
896 ms->dump_guest_core = true;
897 ms->mem_merge = true;
898 ms->enable_graphics = true;
900 if (mc->nvdimm_supported) {
901 Object *obj = OBJECT(ms);
903 ms->nvdimms_state = g_new0(NVDIMMState, 1);
904 object_property_add_bool(obj, "nvdimm",
905 machine_get_nvdimm, machine_set_nvdimm);
906 object_property_set_description(obj, "nvdimm",
907 "Set on/off to enable/disable "
908 "NVDIMM instantiation");
910 object_property_add_str(obj, "nvdimm-persistence",
911 machine_get_nvdimm_persistence,
912 machine_set_nvdimm_persistence);
913 object_property_set_description(obj, "nvdimm-persistence",
914 "Set NVDIMM persistence"
915 "Valid values are cpu, mem-ctrl");
918 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
919 ms->numa_state = g_new0(NumaState, 1);
920 object_property_add_bool(obj, "hmat",
921 machine_get_hmat, machine_set_hmat);
922 object_property_set_description(obj, "hmat",
923 "Set on/off to enable/disable "
924 "ACPI Heterogeneous Memory Attribute "
925 "Table (HMAT)");
928 object_property_add_str(obj, "memory-backend",
929 machine_get_memdev, machine_set_memdev);
930 object_property_set_description(obj, "memory-backend",
931 "Set RAM backend"
932 "Valid value is ID of hostmem based backend");
934 /* Register notifier when init is done for sysbus sanity checks */
935 ms->sysbus_notifier.notify = machine_init_notify;
936 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
939 static void machine_finalize(Object *obj)
941 MachineState *ms = MACHINE(obj);
943 g_free(ms->kernel_filename);
944 g_free(ms->initrd_filename);
945 g_free(ms->kernel_cmdline);
946 g_free(ms->dtb);
947 g_free(ms->dumpdtb);
948 g_free(ms->dt_compatible);
949 g_free(ms->firmware);
950 g_free(ms->device_memory);
951 g_free(ms->nvdimms_state);
952 g_free(ms->numa_state);
955 bool machine_usb(MachineState *machine)
957 return machine->usb;
960 int machine_phandle_start(MachineState *machine)
962 return machine->phandle_start;
965 bool machine_dump_guest_core(MachineState *machine)
967 return machine->dump_guest_core;
970 bool machine_mem_merge(MachineState *machine)
972 return machine->mem_merge;
975 static char *cpu_slot_to_string(const CPUArchId *cpu)
977 GString *s = g_string_new(NULL);
978 if (cpu->props.has_socket_id) {
979 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
981 if (cpu->props.has_die_id) {
982 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
984 if (cpu->props.has_core_id) {
985 if (s->len) {
986 g_string_append_printf(s, ", ");
988 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
990 if (cpu->props.has_thread_id) {
991 if (s->len) {
992 g_string_append_printf(s, ", ");
994 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
996 return g_string_free(s, false);
999 static void numa_validate_initiator(NumaState *numa_state)
1001 int i;
1002 NodeInfo *numa_info = numa_state->nodes;
1004 for (i = 0; i < numa_state->num_nodes; i++) {
1005 if (numa_info[i].initiator == MAX_NODES) {
1006 error_report("The initiator of NUMA node %d is missing, use "
1007 "'-numa node,initiator' option to declare it", i);
1008 exit(1);
1011 if (!numa_info[numa_info[i].initiator].present) {
1012 error_report("NUMA node %" PRIu16 " is missing, use "
1013 "'-numa node' option to declare it first",
1014 numa_info[i].initiator);
1015 exit(1);
1018 if (!numa_info[numa_info[i].initiator].has_cpu) {
1019 error_report("The initiator of NUMA node %d is invalid", i);
1020 exit(1);
1025 static void machine_numa_finish_cpu_init(MachineState *machine)
1027 int i;
1028 bool default_mapping;
1029 GString *s = g_string_new(NULL);
1030 MachineClass *mc = MACHINE_GET_CLASS(machine);
1031 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1033 assert(machine->numa_state->num_nodes);
1034 for (i = 0; i < possible_cpus->len; i++) {
1035 if (possible_cpus->cpus[i].props.has_node_id) {
1036 break;
1039 default_mapping = (i == possible_cpus->len);
1041 for (i = 0; i < possible_cpus->len; i++) {
1042 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1044 if (!cpu_slot->props.has_node_id) {
1045 /* fetch default mapping from board and enable it */
1046 CpuInstanceProperties props = cpu_slot->props;
1048 props.node_id = mc->get_default_cpu_node_id(machine, i);
1049 if (!default_mapping) {
1050 /* record slots with not set mapping,
1051 * TODO: make it hard error in future */
1052 char *cpu_str = cpu_slot_to_string(cpu_slot);
1053 g_string_append_printf(s, "%sCPU %d [%s]",
1054 s->len ? ", " : "", i, cpu_str);
1055 g_free(cpu_str);
1057 /* non mapped cpus used to fallback to node 0 */
1058 props.node_id = 0;
1061 props.has_node_id = true;
1062 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1066 if (machine->numa_state->hmat_enabled) {
1067 numa_validate_initiator(machine->numa_state);
1070 if (s->len && !qtest_enabled()) {
1071 warn_report("CPU(s) not present in any NUMA nodes: %s",
1072 s->str);
1073 warn_report("All CPU(s) up to maxcpus should be described "
1074 "in NUMA config, ability to start up with partial NUMA "
1075 "mappings is obsoleted and will be removed in future");
1077 g_string_free(s, true);
1080 MemoryRegion *machine_consume_memdev(MachineState *machine,
1081 HostMemoryBackend *backend)
1083 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1085 if (memory_region_is_mapped(ret)) {
1086 error_report("memory backend %s can't be used multiple times.",
1087 object_get_canonical_path_component(OBJECT(backend)));
1088 exit(EXIT_FAILURE);
1090 host_memory_backend_set_mapped(backend, true);
1091 vmstate_register_ram_global(ret);
1092 return ret;
1095 void machine_run_board_init(MachineState *machine)
1097 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1099 if (machine->ram_memdev_id) {
1100 Object *o;
1101 o = object_resolve_path_type(machine->ram_memdev_id,
1102 TYPE_MEMORY_BACKEND, NULL);
1103 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1106 if (machine->numa_state) {
1107 numa_complete_configuration(machine);
1108 if (machine->numa_state->num_nodes) {
1109 machine_numa_finish_cpu_init(machine);
1113 /* If the machine supports the valid_cpu_types check and the user
1114 * specified a CPU with -cpu check here that the user CPU is supported.
1116 if (machine_class->valid_cpu_types && machine->cpu_type) {
1117 ObjectClass *class = object_class_by_name(machine->cpu_type);
1118 int i;
1120 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1121 if (object_class_dynamic_cast(class,
1122 machine_class->valid_cpu_types[i])) {
1123 /* The user specificed CPU is in the valid field, we are
1124 * good to go.
1126 break;
1130 if (!machine_class->valid_cpu_types[i]) {
1131 /* The user specified CPU is not valid */
1132 error_report("Invalid CPU type: %s", machine->cpu_type);
1133 error_printf("The valid types are: %s",
1134 machine_class->valid_cpu_types[0]);
1135 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1136 error_printf(", %s", machine_class->valid_cpu_types[i]);
1138 error_printf("\n");
1140 exit(1);
1144 machine_class->init(machine);
1147 static const TypeInfo machine_info = {
1148 .name = TYPE_MACHINE,
1149 .parent = TYPE_OBJECT,
1150 .abstract = true,
1151 .class_size = sizeof(MachineClass),
1152 .class_init = machine_class_init,
1153 .class_base_init = machine_class_base_init,
1154 .instance_size = sizeof(MachineState),
1155 .instance_init = machine_initfn,
1156 .instance_finalize = machine_finalize,
1159 static void machine_register_types(void)
1161 type_register_static(&machine_info);
1164 type_init(machine_register_types)