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