xref: /openbmc/qemu/hw/i386/pc.c (revision 3e038d7d)
1 /*
2  * QEMU PC System Emulator
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "hw/i386/x86.h"
28 #include "hw/i386/pc.h"
29 #include "hw/char/serial.h"
30 #include "hw/char/parallel.h"
31 #include "hw/i386/apic.h"
32 #include "hw/i386/topology.h"
33 #include "hw/i386/fw_cfg.h"
34 #include "hw/i386/vmport.h"
35 #include "sysemu/cpus.h"
36 #include "hw/block/fdc.h"
37 #include "hw/ide.h"
38 #include "hw/pci/pci.h"
39 #include "hw/pci/pci_bus.h"
40 #include "hw/nvram/fw_cfg.h"
41 #include "hw/timer/hpet.h"
42 #include "hw/firmware/smbios.h"
43 #include "hw/loader.h"
44 #include "elf.h"
45 #include "migration/vmstate.h"
46 #include "multiboot.h"
47 #include "hw/rtc/mc146818rtc.h"
48 #include "hw/intc/i8259.h"
49 #include "hw/dma/i8257.h"
50 #include "hw/timer/i8254.h"
51 #include "hw/input/i8042.h"
52 #include "hw/irq.h"
53 #include "hw/audio/pcspk.h"
54 #include "hw/pci/msi.h"
55 #include "hw/sysbus.h"
56 #include "sysemu/sysemu.h"
57 #include "sysemu/tcg.h"
58 #include "sysemu/numa.h"
59 #include "sysemu/kvm.h"
60 #include "sysemu/xen.h"
61 #include "sysemu/reset.h"
62 #include "sysemu/runstate.h"
63 #include "kvm/kvm_i386.h"
64 #include "hw/xen/xen.h"
65 #include "hw/xen/start_info.h"
66 #include "ui/qemu-spice.h"
67 #include "exec/memory.h"
68 #include "qemu/bitmap.h"
69 #include "qemu/config-file.h"
70 #include "qemu/error-report.h"
71 #include "qemu/option.h"
72 #include "qemu/cutils.h"
73 #include "hw/acpi/acpi.h"
74 #include "hw/acpi/cpu_hotplug.h"
75 #include "acpi-build.h"
76 #include "hw/mem/pc-dimm.h"
77 #include "hw/mem/nvdimm.h"
78 #include "qapi/error.h"
79 #include "qapi/qapi-visit-common.h"
80 #include "qapi/visitor.h"
81 #include "hw/core/cpu.h"
82 #include "hw/usb.h"
83 #include "hw/i386/intel_iommu.h"
84 #include "hw/net/ne2000-isa.h"
85 #include "standard-headers/asm-x86/bootparam.h"
86 #include "hw/virtio/virtio-pmem-pci.h"
87 #include "hw/virtio/virtio-mem-pci.h"
88 #include "hw/mem/memory-device.h"
89 #include "sysemu/replay.h"
90 #include "qapi/qmp/qerror.h"
91 #include "e820_memory_layout.h"
92 #include "fw_cfg.h"
93 #include "trace.h"
94 #include CONFIG_DEVICES
95 
96 GlobalProperty pc_compat_6_1[] = {};
97 const size_t pc_compat_6_1_len = G_N_ELEMENTS(pc_compat_6_1);
98 
99 GlobalProperty pc_compat_6_0[] = {
100     { "qemu64" "-" TYPE_X86_CPU, "family", "6" },
101     { "qemu64" "-" TYPE_X86_CPU, "model", "6" },
102     { "qemu64" "-" TYPE_X86_CPU, "stepping", "3" },
103     { TYPE_X86_CPU, "x-vendor-cpuid-only", "off" },
104     { "ICH9-LPC", "acpi-pci-hotplug-with-bridge-support", "off" },
105 };
106 const size_t pc_compat_6_0_len = G_N_ELEMENTS(pc_compat_6_0);
107 
108 GlobalProperty pc_compat_5_2[] = {
109     { "ICH9-LPC", "x-smi-cpu-hotunplug", "off" },
110 };
111 const size_t pc_compat_5_2_len = G_N_ELEMENTS(pc_compat_5_2);
112 
113 GlobalProperty pc_compat_5_1[] = {
114     { "ICH9-LPC", "x-smi-cpu-hotplug", "off" },
115     { TYPE_X86_CPU, "kvm-msi-ext-dest-id", "off" },
116 };
117 const size_t pc_compat_5_1_len = G_N_ELEMENTS(pc_compat_5_1);
118 
119 GlobalProperty pc_compat_5_0[] = {
120 };
121 const size_t pc_compat_5_0_len = G_N_ELEMENTS(pc_compat_5_0);
122 
123 GlobalProperty pc_compat_4_2[] = {
124     { "mch", "smbase-smram", "off" },
125 };
126 const size_t pc_compat_4_2_len = G_N_ELEMENTS(pc_compat_4_2);
127 
128 GlobalProperty pc_compat_4_1[] = {};
129 const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
130 
131 GlobalProperty pc_compat_4_0[] = {};
132 const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0);
133 
134 GlobalProperty pc_compat_3_1[] = {
135     { "intel-iommu", "dma-drain", "off" },
136     { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" },
137     { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" },
138     { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" },
139     { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" },
140     { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
141     { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" },
142     { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" },
143     { "EPYC" "-" TYPE_X86_CPU, "npt", "off" },
144     { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" },
145     { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" },
146     { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" },
147     { "Skylake-Client" "-" TYPE_X86_CPU,      "mpx", "on" },
148     { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
149     { "Skylake-Server" "-" TYPE_X86_CPU,      "mpx", "on" },
150     { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
151     { "Cascadelake-Server" "-" TYPE_X86_CPU,  "mpx", "on" },
152     { "Icelake-Client" "-" TYPE_X86_CPU,      "mpx", "on" },
153     { "Icelake-Server" "-" TYPE_X86_CPU,      "mpx", "on" },
154     { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
155     { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" },
156 };
157 const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
158 
159 GlobalProperty pc_compat_3_0[] = {
160     { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
161     { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
162     { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
163 };
164 const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
165 
166 GlobalProperty pc_compat_2_12[] = {
167     { TYPE_X86_CPU, "legacy-cache", "on" },
168     { TYPE_X86_CPU, "topoext", "off" },
169     { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
170     { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
171 };
172 const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
173 
174 GlobalProperty pc_compat_2_11[] = {
175     { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
176     { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
177 };
178 const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
179 
180 GlobalProperty pc_compat_2_10[] = {
181     { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
182     { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
183     { "q35-pcihost", "x-pci-hole64-fix", "off" },
184 };
185 const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
186 
187 GlobalProperty pc_compat_2_9[] = {
188     { "mch", "extended-tseg-mbytes", "0" },
189 };
190 const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
191 
192 GlobalProperty pc_compat_2_8[] = {
193     { TYPE_X86_CPU, "tcg-cpuid", "off" },
194     { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
195     { "ICH9-LPC", "x-smi-broadcast", "off" },
196     { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
197     { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
198 };
199 const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
200 
201 GlobalProperty pc_compat_2_7[] = {
202     { TYPE_X86_CPU, "l3-cache", "off" },
203     { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
204     { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
205     { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
206     { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
207     { "isa-pcspk", "migrate", "off" },
208 };
209 const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
210 
211 GlobalProperty pc_compat_2_6[] = {
212     { TYPE_X86_CPU, "cpuid-0xb", "off" },
213     { "vmxnet3", "romfile", "" },
214     { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
215     { "apic-common", "legacy-instance-id", "on", }
216 };
217 const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
218 
219 GlobalProperty pc_compat_2_5[] = {};
220 const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
221 
222 GlobalProperty pc_compat_2_4[] = {
223     PC_CPU_MODEL_IDS("2.4.0")
224     { "Haswell-" TYPE_X86_CPU, "abm", "off" },
225     { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
226     { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
227     { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
228     { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
229     { TYPE_X86_CPU, "check", "off" },
230     { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
231     { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
232     { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
233     { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
234     { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
235     { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
236     { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
237     { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
238 };
239 const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
240 
241 GlobalProperty pc_compat_2_3[] = {
242     PC_CPU_MODEL_IDS("2.3.0")
243     { TYPE_X86_CPU, "arat", "off" },
244     { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" },
245     { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" },
246     { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" },
247     { "n270" "-" TYPE_X86_CPU, "min-level", "5" },
248     { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" },
249     { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" },
250     { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" },
251     { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
252     { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
253     { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
254     { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
255     { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
256     { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
257     { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
258     { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
259     { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
260     { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
261     { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
262     { TYPE_X86_CPU, "kvm-no-smi-migration", "on" },
263 };
264 const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3);
265 
266 GlobalProperty pc_compat_2_2[] = {
267     PC_CPU_MODEL_IDS("2.2.0")
268     { "kvm64" "-" TYPE_X86_CPU, "vme", "off" },
269     { "kvm32" "-" TYPE_X86_CPU, "vme", "off" },
270     { "Conroe" "-" TYPE_X86_CPU, "vme", "off" },
271     { "Penryn" "-" TYPE_X86_CPU, "vme", "off" },
272     { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" },
273     { "Westmere" "-" TYPE_X86_CPU, "vme", "off" },
274     { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" },
275     { "Haswell" "-" TYPE_X86_CPU, "vme", "off" },
276     { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" },
277     { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" },
278     { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" },
279     { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" },
280     { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" },
281     { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" },
282     { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" },
283     { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" },
284     { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" },
285     { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" },
286 };
287 const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2);
288 
289 GlobalProperty pc_compat_2_1[] = {
290     PC_CPU_MODEL_IDS("2.1.0")
291     { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" },
292     { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" },
293 };
294 const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1);
295 
296 GlobalProperty pc_compat_2_0[] = {
297     PC_CPU_MODEL_IDS("2.0.0")
298     { "virtio-scsi-pci", "any_layout", "off" },
299     { "PIIX4_PM", "memory-hotplug-support", "off" },
300     { "apic", "version", "0x11" },
301     { "nec-usb-xhci", "superspeed-ports-first", "off" },
302     { "nec-usb-xhci", "force-pcie-endcap", "on" },
303     { "pci-serial", "prog_if", "0" },
304     { "pci-serial-2x", "prog_if", "0" },
305     { "pci-serial-4x", "prog_if", "0" },
306     { "virtio-net-pci", "guest_announce", "off" },
307     { "ICH9-LPC", "memory-hotplug-support", "off" },
308     { "xio3130-downstream", COMPAT_PROP_PCP, "off" },
309     { "ioh3420", COMPAT_PROP_PCP, "off" },
310 };
311 const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0);
312 
313 GlobalProperty pc_compat_1_7[] = {
314     PC_CPU_MODEL_IDS("1.7.0")
315     { TYPE_USB_DEVICE, "msos-desc", "no" },
316     { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" },
317     { "hpet", HPET_INTCAP, "4" },
318 };
319 const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7);
320 
321 GlobalProperty pc_compat_1_6[] = {
322     PC_CPU_MODEL_IDS("1.6.0")
323     { "e1000", "mitigation", "off" },
324     { "qemu64-" TYPE_X86_CPU, "model", "2" },
325     { "qemu32-" TYPE_X86_CPU, "model", "3" },
326     { "i440FX-pcihost", "short_root_bus", "1" },
327     { "q35-pcihost", "short_root_bus", "1" },
328 };
329 const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6);
330 
331 GlobalProperty pc_compat_1_5[] = {
332     PC_CPU_MODEL_IDS("1.5.0")
333     { "Conroe-" TYPE_X86_CPU, "model", "2" },
334     { "Conroe-" TYPE_X86_CPU, "min-level", "2" },
335     { "Penryn-" TYPE_X86_CPU, "model", "2" },
336     { "Penryn-" TYPE_X86_CPU, "min-level", "2" },
337     { "Nehalem-" TYPE_X86_CPU, "model", "2" },
338     { "Nehalem-" TYPE_X86_CPU, "min-level", "2" },
339     { "virtio-net-pci", "any_layout", "off" },
340     { TYPE_X86_CPU, "pmu", "on" },
341     { "i440FX-pcihost", "short_root_bus", "0" },
342     { "q35-pcihost", "short_root_bus", "0" },
343 };
344 const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5);
345 
346 GlobalProperty pc_compat_1_4[] = {
347     PC_CPU_MODEL_IDS("1.4.0")
348     { "scsi-hd", "discard_granularity", "0" },
349     { "scsi-cd", "discard_granularity", "0" },
350     { "ide-hd", "discard_granularity", "0" },
351     { "ide-cd", "discard_granularity", "0" },
352     { "virtio-blk-pci", "discard_granularity", "0" },
353     /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
354     { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
355     { "virtio-net-pci", "ctrl_guest_offloads", "off" },
356     { "e1000", "romfile", "pxe-e1000.rom" },
357     { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
358     { "pcnet", "romfile", "pxe-pcnet.rom" },
359     { "rtl8139", "romfile", "pxe-rtl8139.rom" },
360     { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
361     { "486-" TYPE_X86_CPU, "model", "0" },
362     { "n270" "-" TYPE_X86_CPU, "movbe", "off" },
363     { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" },
364 };
365 const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4);
366 
367 GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled)
368 {
369     GSIState *s;
370 
371     s = g_new0(GSIState, 1);
372     if (kvm_ioapic_in_kernel()) {
373         kvm_pc_setup_irq_routing(pci_enabled);
374     }
375     *irqs = qemu_allocate_irqs(gsi_handler, s, GSI_NUM_PINS);
376 
377     return s;
378 }
379 
380 static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
381                            unsigned size)
382 {
383 }
384 
385 static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
386 {
387     return 0xffffffffffffffffULL;
388 }
389 
390 /* MSDOS compatibility mode FPU exception support */
391 static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
392                            unsigned size)
393 {
394     if (tcg_enabled()) {
395         cpu_set_ignne();
396     }
397 }
398 
399 static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
400 {
401     return 0xffffffffffffffffULL;
402 }
403 
404 /* PC cmos mappings */
405 
406 #define REG_EQUIPMENT_BYTE          0x14
407 
408 static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
409                          int16_t cylinders, int8_t heads, int8_t sectors)
410 {
411     rtc_set_memory(s, type_ofs, 47);
412     rtc_set_memory(s, info_ofs, cylinders);
413     rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
414     rtc_set_memory(s, info_ofs + 2, heads);
415     rtc_set_memory(s, info_ofs + 3, 0xff);
416     rtc_set_memory(s, info_ofs + 4, 0xff);
417     rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
418     rtc_set_memory(s, info_ofs + 6, cylinders);
419     rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
420     rtc_set_memory(s, info_ofs + 8, sectors);
421 }
422 
423 /* convert boot_device letter to something recognizable by the bios */
424 static int boot_device2nibble(char boot_device)
425 {
426     switch(boot_device) {
427     case 'a':
428     case 'b':
429         return 0x01; /* floppy boot */
430     case 'c':
431         return 0x02; /* hard drive boot */
432     case 'd':
433         return 0x03; /* CD-ROM boot */
434     case 'n':
435         return 0x04; /* Network boot */
436     }
437     return 0;
438 }
439 
440 static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
441 {
442 #define PC_MAX_BOOT_DEVICES 3
443     int nbds, bds[3] = { 0, };
444     int i;
445 
446     nbds = strlen(boot_device);
447     if (nbds > PC_MAX_BOOT_DEVICES) {
448         error_setg(errp, "Too many boot devices for PC");
449         return;
450     }
451     for (i = 0; i < nbds; i++) {
452         bds[i] = boot_device2nibble(boot_device[i]);
453         if (bds[i] == 0) {
454             error_setg(errp, "Invalid boot device for PC: '%c'",
455                        boot_device[i]);
456             return;
457         }
458     }
459     rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
460     rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
461 }
462 
463 static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
464 {
465     set_boot_dev(opaque, boot_device, errp);
466 }
467 
468 static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
469 {
470     int val, nb, i;
471     FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
472                                    FLOPPY_DRIVE_TYPE_NONE };
473 
474     /* floppy type */
475     if (floppy) {
476         for (i = 0; i < 2; i++) {
477             fd_type[i] = isa_fdc_get_drive_type(floppy, i);
478         }
479     }
480     val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
481         cmos_get_fd_drive_type(fd_type[1]);
482     rtc_set_memory(rtc_state, 0x10, val);
483 
484     val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
485     nb = 0;
486     if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
487         nb++;
488     }
489     if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
490         nb++;
491     }
492     switch (nb) {
493     case 0:
494         break;
495     case 1:
496         val |= 0x01; /* 1 drive, ready for boot */
497         break;
498     case 2:
499         val |= 0x41; /* 2 drives, ready for boot */
500         break;
501     }
502     rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
503 }
504 
505 typedef struct pc_cmos_init_late_arg {
506     ISADevice *rtc_state;
507     BusState *idebus[2];
508 } pc_cmos_init_late_arg;
509 
510 typedef struct check_fdc_state {
511     ISADevice *floppy;
512     bool multiple;
513 } CheckFdcState;
514 
515 static int check_fdc(Object *obj, void *opaque)
516 {
517     CheckFdcState *state = opaque;
518     Object *fdc;
519     uint32_t iobase;
520     Error *local_err = NULL;
521 
522     fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
523     if (!fdc) {
524         return 0;
525     }
526 
527     iobase = object_property_get_uint(obj, "iobase", &local_err);
528     if (local_err || iobase != 0x3f0) {
529         error_free(local_err);
530         return 0;
531     }
532 
533     if (state->floppy) {
534         state->multiple = true;
535     } else {
536         state->floppy = ISA_DEVICE(obj);
537     }
538     return 0;
539 }
540 
541 static const char * const fdc_container_path[] = {
542     "/unattached", "/peripheral", "/peripheral-anon"
543 };
544 
545 /*
546  * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
547  * and ACPI objects.
548  */
549 ISADevice *pc_find_fdc0(void)
550 {
551     int i;
552     Object *container;
553     CheckFdcState state = { 0 };
554 
555     for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
556         container = container_get(qdev_get_machine(), fdc_container_path[i]);
557         object_child_foreach(container, check_fdc, &state);
558     }
559 
560     if (state.multiple) {
561         warn_report("multiple floppy disk controllers with "
562                     "iobase=0x3f0 have been found");
563         error_printf("the one being picked for CMOS setup might not reflect "
564                      "your intent");
565     }
566 
567     return state.floppy;
568 }
569 
570 static void pc_cmos_init_late(void *opaque)
571 {
572     pc_cmos_init_late_arg *arg = opaque;
573     ISADevice *s = arg->rtc_state;
574     int16_t cylinders;
575     int8_t heads, sectors;
576     int val;
577     int i, trans;
578 
579     val = 0;
580     if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
581                                            &cylinders, &heads, &sectors) >= 0) {
582         cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
583         val |= 0xf0;
584     }
585     if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
586                                            &cylinders, &heads, &sectors) >= 0) {
587         cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
588         val |= 0x0f;
589     }
590     rtc_set_memory(s, 0x12, val);
591 
592     val = 0;
593     for (i = 0; i < 4; i++) {
594         /* NOTE: ide_get_geometry() returns the physical
595            geometry.  It is always such that: 1 <= sects <= 63, 1
596            <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
597            geometry can be different if a translation is done. */
598         if (arg->idebus[i / 2] &&
599             ide_get_geometry(arg->idebus[i / 2], i % 2,
600                              &cylinders, &heads, &sectors) >= 0) {
601             trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
602             assert((trans & ~3) == 0);
603             val |= trans << (i * 2);
604         }
605     }
606     rtc_set_memory(s, 0x39, val);
607 
608     pc_cmos_init_floppy(s, pc_find_fdc0());
609 
610     qemu_unregister_reset(pc_cmos_init_late, opaque);
611 }
612 
613 void pc_cmos_init(PCMachineState *pcms,
614                   BusState *idebus0, BusState *idebus1,
615                   ISADevice *s)
616 {
617     int val;
618     static pc_cmos_init_late_arg arg;
619     X86MachineState *x86ms = X86_MACHINE(pcms);
620 
621     /* various important CMOS locations needed by PC/Bochs bios */
622 
623     /* memory size */
624     /* base memory (first MiB) */
625     val = MIN(x86ms->below_4g_mem_size / KiB, 640);
626     rtc_set_memory(s, 0x15, val);
627     rtc_set_memory(s, 0x16, val >> 8);
628     /* extended memory (next 64MiB) */
629     if (x86ms->below_4g_mem_size > 1 * MiB) {
630         val = (x86ms->below_4g_mem_size - 1 * MiB) / KiB;
631     } else {
632         val = 0;
633     }
634     if (val > 65535)
635         val = 65535;
636     rtc_set_memory(s, 0x17, val);
637     rtc_set_memory(s, 0x18, val >> 8);
638     rtc_set_memory(s, 0x30, val);
639     rtc_set_memory(s, 0x31, val >> 8);
640     /* memory between 16MiB and 4GiB */
641     if (x86ms->below_4g_mem_size > 16 * MiB) {
642         val = (x86ms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
643     } else {
644         val = 0;
645     }
646     if (val > 65535)
647         val = 65535;
648     rtc_set_memory(s, 0x34, val);
649     rtc_set_memory(s, 0x35, val >> 8);
650     /* memory above 4GiB */
651     val = x86ms->above_4g_mem_size / 65536;
652     rtc_set_memory(s, 0x5b, val);
653     rtc_set_memory(s, 0x5c, val >> 8);
654     rtc_set_memory(s, 0x5d, val >> 16);
655 
656     object_property_add_link(OBJECT(pcms), "rtc_state",
657                              TYPE_ISA_DEVICE,
658                              (Object **)&x86ms->rtc,
659                              object_property_allow_set_link,
660                              OBJ_PROP_LINK_STRONG);
661     object_property_set_link(OBJECT(pcms), "rtc_state", OBJECT(s),
662                              &error_abort);
663 
664     set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal);
665 
666     val = 0;
667     val |= 0x02; /* FPU is there */
668     val |= 0x04; /* PS/2 mouse installed */
669     rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
670 
671     /* hard drives and FDC */
672     arg.rtc_state = s;
673     arg.idebus[0] = idebus0;
674     arg.idebus[1] = idebus1;
675     qemu_register_reset(pc_cmos_init_late, &arg);
676 }
677 
678 static void handle_a20_line_change(void *opaque, int irq, int level)
679 {
680     X86CPU *cpu = opaque;
681 
682     /* XXX: send to all CPUs ? */
683     /* XXX: add logic to handle multiple A20 line sources */
684     x86_cpu_set_a20(cpu, level);
685 }
686 
687 #define NE2000_NB_MAX 6
688 
689 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
690                                               0x280, 0x380 };
691 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
692 
693 void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
694 {
695     static int nb_ne2k = 0;
696 
697     if (nb_ne2k == NE2000_NB_MAX)
698         return;
699     isa_ne2000_init(bus, ne2000_io[nb_ne2k],
700                     ne2000_irq[nb_ne2k], nd);
701     nb_ne2k++;
702 }
703 
704 void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
705 {
706     X86CPU *cpu = opaque;
707 
708     if (level) {
709         cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
710     }
711 }
712 
713 /*
714  * This function is very similar to smp_parse()
715  * in hw/core/machine.c but includes CPU die support.
716  */
717 static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
718 {
719     unsigned cpus    = config->has_cpus ? config->cpus : 0;
720     unsigned sockets = config->has_sockets ? config->sockets : 0;
721     unsigned dies    = config->has_dies ? config->dies : 1;
722     unsigned cores   = config->has_cores ? config->cores : 0;
723     unsigned threads = config->has_threads ? config->threads : 0;
724 
725     /* compute missing values, prefer sockets over cores over threads */
726     if (cpus == 0 || sockets == 0) {
727         cores = cores > 0 ? cores : 1;
728         threads = threads > 0 ? threads : 1;
729         if (cpus == 0) {
730             sockets = sockets > 0 ? sockets : 1;
731             cpus = cores * threads * dies * sockets;
732         } else {
733             ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
734             sockets = ms->smp.max_cpus / (cores * threads * dies);
735         }
736     } else if (cores == 0) {
737         threads = threads > 0 ? threads : 1;
738         cores = cpus / (sockets * dies * threads);
739         cores = cores > 0 ? cores : 1;
740     } else if (threads == 0) {
741         threads = cpus / (cores * dies * sockets);
742         threads = threads > 0 ? threads : 1;
743     } else if (sockets * dies * cores * threads < cpus) {
744         error_setg(errp, "cpu topology: "
745                    "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
746                    "smp_cpus (%u)",
747                    sockets, dies, cores, threads, cpus);
748         return;
749     }
750 
751     ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
752 
753     if (ms->smp.max_cpus < cpus) {
754         error_setg(errp, "maxcpus must be equal to or greater than smp");
755         return;
756     }
757 
758     if (sockets * dies * cores * threads != ms->smp.max_cpus) {
759         error_setg(errp, "Invalid CPU topology deprecated: "
760                    "sockets (%u) * dies (%u) * cores (%u) * threads (%u) "
761                    "!= maxcpus (%u)",
762                    sockets, dies, cores, threads,
763                    ms->smp.max_cpus);
764         return;
765     }
766 
767     ms->smp.cpus = cpus;
768     ms->smp.cores = cores;
769     ms->smp.threads = threads;
770     ms->smp.sockets = sockets;
771     ms->smp.dies = dies;
772 }
773 
774 static
775 void pc_machine_done(Notifier *notifier, void *data)
776 {
777     PCMachineState *pcms = container_of(notifier,
778                                         PCMachineState, machine_done);
779     X86MachineState *x86ms = X86_MACHINE(pcms);
780 
781     /* set the number of CPUs */
782     x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
783 
784     fw_cfg_add_extra_pci_roots(pcms->bus, x86ms->fw_cfg);
785 
786     acpi_setup();
787     if (x86ms->fw_cfg) {
788         fw_cfg_build_smbios(MACHINE(pcms), x86ms->fw_cfg);
789         fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg);
790         /* update FW_CFG_NB_CPUS to account for -device added CPUs */
791         fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
792     }
793 
794 
795     if (x86ms->apic_id_limit > 255 && !xen_enabled() &&
796         !kvm_irqchip_in_kernel()) {
797         error_report("current -smp configuration requires kernel "
798                      "irqchip support.");
799         exit(EXIT_FAILURE);
800     }
801 }
802 
803 void pc_guest_info_init(PCMachineState *pcms)
804 {
805     int i;
806     MachineState *ms = MACHINE(pcms);
807     X86MachineState *x86ms = X86_MACHINE(pcms);
808 
809     x86ms->apic_xrupt_override = true;
810     pcms->numa_nodes = ms->numa_state->num_nodes;
811     pcms->node_mem = g_malloc0(pcms->numa_nodes *
812                                     sizeof *pcms->node_mem);
813     for (i = 0; i < ms->numa_state->num_nodes; i++) {
814         pcms->node_mem[i] = ms->numa_state->nodes[i].node_mem;
815     }
816 
817     pcms->machine_done.notify = pc_machine_done;
818     qemu_add_machine_init_done_notifier(&pcms->machine_done);
819 }
820 
821 /* setup pci memory address space mapping into system address space */
822 void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
823                             MemoryRegion *pci_address_space)
824 {
825     /* Set to lower priority than RAM */
826     memory_region_add_subregion_overlap(system_memory, 0x0,
827                                         pci_address_space, -1);
828 }
829 
830 void xen_load_linux(PCMachineState *pcms)
831 {
832     int i;
833     FWCfgState *fw_cfg;
834     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
835     X86MachineState *x86ms = X86_MACHINE(pcms);
836 
837     assert(MACHINE(pcms)->kernel_filename != NULL);
838 
839     fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
840     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
841     rom_set_fw(fw_cfg);
842 
843     x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size,
844                    pcmc->pvh_enabled, pcmc->linuxboot_dma_enabled);
845     for (i = 0; i < nb_option_roms; i++) {
846         assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
847                !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
848                !strcmp(option_rom[i].name, "pvh.bin") ||
849                !strcmp(option_rom[i].name, "multiboot.bin"));
850         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
851     }
852     x86ms->fw_cfg = fw_cfg;
853 }
854 
855 void pc_memory_init(PCMachineState *pcms,
856                     MemoryRegion *system_memory,
857                     MemoryRegion *rom_memory,
858                     MemoryRegion **ram_memory)
859 {
860     int linux_boot, i;
861     MemoryRegion *option_rom_mr;
862     MemoryRegion *ram_below_4g, *ram_above_4g;
863     FWCfgState *fw_cfg;
864     MachineState *machine = MACHINE(pcms);
865     MachineClass *mc = MACHINE_GET_CLASS(machine);
866     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
867     X86MachineState *x86ms = X86_MACHINE(pcms);
868 
869     assert(machine->ram_size == x86ms->below_4g_mem_size +
870                                 x86ms->above_4g_mem_size);
871 
872     linux_boot = (machine->kernel_filename != NULL);
873 
874     /*
875      * Split single memory region and use aliases to address portions of it,
876      * done for backwards compatibility with older qemus.
877      */
878     *ram_memory = machine->ram;
879     ram_below_4g = g_malloc(sizeof(*ram_below_4g));
880     memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", machine->ram,
881                              0, x86ms->below_4g_mem_size);
882     memory_region_add_subregion(system_memory, 0, ram_below_4g);
883     e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM);
884     if (x86ms->above_4g_mem_size > 0) {
885         ram_above_4g = g_malloc(sizeof(*ram_above_4g));
886         memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g",
887                                  machine->ram,
888                                  x86ms->below_4g_mem_size,
889                                  x86ms->above_4g_mem_size);
890         memory_region_add_subregion(system_memory, 0x100000000ULL,
891                                     ram_above_4g);
892         e820_add_entry(0x100000000ULL, x86ms->above_4g_mem_size, E820_RAM);
893     }
894 
895     if (!pcmc->has_reserved_memory &&
896         (machine->ram_slots ||
897          (machine->maxram_size > machine->ram_size))) {
898 
899         error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
900                      mc->name);
901         exit(EXIT_FAILURE);
902     }
903 
904     /* always allocate the device memory information */
905     machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
906 
907     /* initialize device memory address space */
908     if (pcmc->has_reserved_memory &&
909         (machine->ram_size < machine->maxram_size)) {
910         ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
911 
912         if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
913             error_report("unsupported amount of memory slots: %"PRIu64,
914                          machine->ram_slots);
915             exit(EXIT_FAILURE);
916         }
917 
918         if (QEMU_ALIGN_UP(machine->maxram_size,
919                           TARGET_PAGE_SIZE) != machine->maxram_size) {
920             error_report("maximum memory size must by aligned to multiple of "
921                          "%d bytes", TARGET_PAGE_SIZE);
922             exit(EXIT_FAILURE);
923         }
924 
925         machine->device_memory->base =
926             ROUND_UP(0x100000000ULL + x86ms->above_4g_mem_size, 1 * GiB);
927 
928         if (pcmc->enforce_aligned_dimm) {
929             /* size device region assuming 1G page max alignment per slot */
930             device_mem_size += (1 * GiB) * machine->ram_slots;
931         }
932 
933         if ((machine->device_memory->base + device_mem_size) <
934             device_mem_size) {
935             error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
936                          machine->maxram_size);
937             exit(EXIT_FAILURE);
938         }
939 
940         memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
941                            "device-memory", device_mem_size);
942         memory_region_add_subregion(system_memory, machine->device_memory->base,
943                                     &machine->device_memory->mr);
944     }
945 
946     /* Initialize PC system firmware */
947     pc_system_firmware_init(pcms, rom_memory);
948 
949     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
950     memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
951                            &error_fatal);
952     if (pcmc->pci_enabled) {
953         memory_region_set_readonly(option_rom_mr, true);
954     }
955     memory_region_add_subregion_overlap(rom_memory,
956                                         PC_ROM_MIN_VGA,
957                                         option_rom_mr,
958                                         1);
959 
960     fw_cfg = fw_cfg_arch_create(machine,
961                                 x86ms->boot_cpus, x86ms->apic_id_limit);
962 
963     rom_set_fw(fw_cfg);
964 
965     if (pcmc->has_reserved_memory && machine->device_memory->base) {
966         uint64_t *val = g_malloc(sizeof(*val));
967         PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
968         uint64_t res_mem_end = machine->device_memory->base;
969 
970         if (!pcmc->broken_reserved_end) {
971             res_mem_end += memory_region_size(&machine->device_memory->mr);
972         }
973         *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
974         fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
975     }
976 
977     if (linux_boot) {
978         x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size,
979                        pcmc->pvh_enabled, pcmc->linuxboot_dma_enabled);
980     }
981 
982     for (i = 0; i < nb_option_roms; i++) {
983         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
984     }
985     x86ms->fw_cfg = fw_cfg;
986 
987     /* Init default IOAPIC address space */
988     x86ms->ioapic_as = &address_space_memory;
989 
990     /* Init ACPI memory hotplug IO base address */
991     pcms->memhp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
992 }
993 
994 /*
995  * The 64bit pci hole starts after "above 4G RAM" and
996  * potentially the space reserved for memory hotplug.
997  */
998 uint64_t pc_pci_hole64_start(void)
999 {
1000     PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1001     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1002     MachineState *ms = MACHINE(pcms);
1003     X86MachineState *x86ms = X86_MACHINE(pcms);
1004     uint64_t hole64_start = 0;
1005 
1006     if (pcmc->has_reserved_memory && ms->device_memory->base) {
1007         hole64_start = ms->device_memory->base;
1008         if (!pcmc->broken_reserved_end) {
1009             hole64_start += memory_region_size(&ms->device_memory->mr);
1010         }
1011     } else {
1012         hole64_start = 0x100000000ULL + x86ms->above_4g_mem_size;
1013     }
1014 
1015     return ROUND_UP(hole64_start, 1 * GiB);
1016 }
1017 
1018 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
1019 {
1020     DeviceState *dev = NULL;
1021 
1022     rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
1023     if (pci_bus) {
1024         PCIDevice *pcidev = pci_vga_init(pci_bus);
1025         dev = pcidev ? &pcidev->qdev : NULL;
1026     } else if (isa_bus) {
1027         ISADevice *isadev = isa_vga_init(isa_bus);
1028         dev = isadev ? DEVICE(isadev) : NULL;
1029     }
1030     rom_reset_order_override();
1031     return dev;
1032 }
1033 
1034 static const MemoryRegionOps ioport80_io_ops = {
1035     .write = ioport80_write,
1036     .read = ioport80_read,
1037     .endianness = DEVICE_NATIVE_ENDIAN,
1038     .impl = {
1039         .min_access_size = 1,
1040         .max_access_size = 1,
1041     },
1042 };
1043 
1044 static const MemoryRegionOps ioportF0_io_ops = {
1045     .write = ioportF0_write,
1046     .read = ioportF0_read,
1047     .endianness = DEVICE_NATIVE_ENDIAN,
1048     .impl = {
1049         .min_access_size = 1,
1050         .max_access_size = 1,
1051     },
1052 };
1053 
1054 static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport)
1055 {
1056     int i;
1057     DriveInfo *fd[MAX_FD];
1058     qemu_irq *a20_line;
1059     ISADevice *fdc, *i8042, *port92, *vmmouse;
1060 
1061     serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
1062     parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
1063 
1064     for (i = 0; i < MAX_FD; i++) {
1065         fd[i] = drive_get(IF_FLOPPY, 0, i);
1066         create_fdctrl |= !!fd[i];
1067     }
1068     if (create_fdctrl) {
1069         fdc = isa_new(TYPE_ISA_FDC);
1070         if (fdc) {
1071             isa_realize_and_unref(fdc, isa_bus, &error_fatal);
1072             isa_fdc_init_drives(fdc, fd);
1073         }
1074     }
1075 
1076     i8042 = isa_create_simple(isa_bus, "i8042");
1077     if (!no_vmport) {
1078         isa_create_simple(isa_bus, TYPE_VMPORT);
1079         vmmouse = isa_try_new("vmmouse");
1080     } else {
1081         vmmouse = NULL;
1082     }
1083     if (vmmouse) {
1084         object_property_set_link(OBJECT(vmmouse), "i8042", OBJECT(i8042),
1085                                  &error_abort);
1086         isa_realize_and_unref(vmmouse, isa_bus, &error_fatal);
1087     }
1088     port92 = isa_create_simple(isa_bus, TYPE_PORT92);
1089 
1090     a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1091     i8042_setup_a20_line(i8042, a20_line[0]);
1092     qdev_connect_gpio_out_named(DEVICE(port92),
1093                                 PORT92_A20_LINE, 0, a20_line[1]);
1094     g_free(a20_line);
1095 }
1096 
1097 void pc_basic_device_init(struct PCMachineState *pcms,
1098                           ISABus *isa_bus, qemu_irq *gsi,
1099                           ISADevice **rtc_state,
1100                           bool create_fdctrl,
1101                           uint32_t hpet_irqs)
1102 {
1103     int i;
1104     DeviceState *hpet = NULL;
1105     int pit_isa_irq = 0;
1106     qemu_irq pit_alt_irq = NULL;
1107     qemu_irq rtc_irq = NULL;
1108     ISADevice *pit = NULL;
1109     MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1110     MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
1111 
1112     memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
1113     memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
1114 
1115     memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
1116     memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
1117 
1118     /*
1119      * Check if an HPET shall be created.
1120      *
1121      * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1122      * when the HPET wants to take over. Thus we have to disable the latter.
1123      */
1124     if (pcms->hpet_enabled && (!kvm_irqchip_in_kernel() ||
1125                                kvm_has_pit_state2())) {
1126         hpet = qdev_try_new(TYPE_HPET);
1127         if (!hpet) {
1128             error_report("couldn't create HPET device");
1129             exit(1);
1130         }
1131         /*
1132          * For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7 and
1133          * earlier, use IRQ2 for compat. Otherwise, use IRQ16~23, IRQ8 and
1134          * IRQ2.
1135          */
1136         uint8_t compat = object_property_get_uint(OBJECT(hpet),
1137                 HPET_INTCAP, NULL);
1138         if (!compat) {
1139             qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
1140         }
1141         sysbus_realize_and_unref(SYS_BUS_DEVICE(hpet), &error_fatal);
1142         sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
1143 
1144         for (i = 0; i < GSI_NUM_PINS; i++) {
1145             sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
1146         }
1147         pit_isa_irq = -1;
1148         pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1149         rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
1150     }
1151     *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
1152 
1153     qemu_register_boot_set(pc_boot_set, *rtc_state);
1154 
1155     if (!xen_enabled() && pcms->pit_enabled) {
1156         if (kvm_pit_in_kernel()) {
1157             pit = kvm_pit_init(isa_bus, 0x40);
1158         } else {
1159             pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1160         }
1161         if (hpet) {
1162             /* connect PIT to output control line of the HPET */
1163             qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
1164         }
1165         pcspk_init(pcms->pcspk, isa_bus, pit);
1166     }
1167 
1168     i8257_dma_init(isa_bus, 0);
1169 
1170     /* Super I/O */
1171     pc_superio_init(isa_bus, create_fdctrl, pcms->vmport != ON_OFF_AUTO_ON);
1172 }
1173 
1174 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
1175 {
1176     int i;
1177 
1178     rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
1179     for (i = 0; i < nb_nics; i++) {
1180         NICInfo *nd = &nd_table[i];
1181         const char *model = nd->model ? nd->model : pcmc->default_nic_model;
1182 
1183         if (g_str_equal(model, "ne2k_isa")) {
1184             pc_init_ne2k_isa(isa_bus, nd);
1185         } else {
1186             pci_nic_init_nofail(nd, pci_bus, model, NULL);
1187         }
1188     }
1189     rom_reset_order_override();
1190 }
1191 
1192 void pc_i8259_create(ISABus *isa_bus, qemu_irq *i8259_irqs)
1193 {
1194     qemu_irq *i8259;
1195 
1196     if (kvm_pic_in_kernel()) {
1197         i8259 = kvm_i8259_init(isa_bus);
1198     } else if (xen_enabled()) {
1199         i8259 = xen_interrupt_controller_init();
1200     } else {
1201         i8259 = i8259_init(isa_bus, x86_allocate_cpu_irq());
1202     }
1203 
1204     for (size_t i = 0; i < ISA_NUM_IRQS; i++) {
1205         i8259_irqs[i] = i8259[i];
1206     }
1207 
1208     g_free(i8259);
1209 }
1210 
1211 static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
1212                                Error **errp)
1213 {
1214     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1215     const X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
1216     const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1217     const MachineState *ms = MACHINE(hotplug_dev);
1218     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1219     const uint64_t legacy_align = TARGET_PAGE_SIZE;
1220     Error *local_err = NULL;
1221 
1222     /*
1223      * When -no-acpi is used with Q35 machine type, no ACPI is built,
1224      * but pcms->acpi_dev is still created. Check !acpi_enabled in
1225      * addition to cover this case.
1226      */
1227     if (!x86ms->acpi_dev || !x86_machine_is_acpi_enabled(x86ms)) {
1228         error_setg(errp,
1229                    "memory hotplug is not enabled: missing acpi device or acpi disabled");
1230         return;
1231     }
1232 
1233     if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
1234         error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
1235         return;
1236     }
1237 
1238     hotplug_handler_pre_plug(x86ms->acpi_dev, dev, &local_err);
1239     if (local_err) {
1240         error_propagate(errp, local_err);
1241         return;
1242     }
1243 
1244     pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
1245                      pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
1246 }
1247 
1248 static void pc_memory_plug(HotplugHandler *hotplug_dev,
1249                            DeviceState *dev, Error **errp)
1250 {
1251     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1252     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
1253     MachineState *ms = MACHINE(hotplug_dev);
1254     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1255 
1256     pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms));
1257 
1258     if (is_nvdimm) {
1259         nvdimm_plug(ms->nvdimms_state);
1260     }
1261 
1262     hotplug_handler_plug(x86ms->acpi_dev, dev, &error_abort);
1263 }
1264 
1265 static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
1266                                      DeviceState *dev, Error **errp)
1267 {
1268     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
1269 
1270     /*
1271      * When -no-acpi is used with Q35 machine type, no ACPI is built,
1272      * but pcms->acpi_dev is still created. Check !acpi_enabled in
1273      * addition to cover this case.
1274      */
1275     if (!x86ms->acpi_dev || !x86_machine_is_acpi_enabled(x86ms)) {
1276         error_setg(errp,
1277                    "memory hotplug is not enabled: missing acpi device or acpi disabled");
1278         return;
1279     }
1280 
1281     if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
1282         error_setg(errp, "nvdimm device hot unplug is not supported yet.");
1283         return;
1284     }
1285 
1286     hotplug_handler_unplug_request(x86ms->acpi_dev, dev,
1287                                    errp);
1288 }
1289 
1290 static void pc_memory_unplug(HotplugHandler *hotplug_dev,
1291                              DeviceState *dev, Error **errp)
1292 {
1293     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1294     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
1295     Error *local_err = NULL;
1296 
1297     hotplug_handler_unplug(x86ms->acpi_dev, dev, &local_err);
1298     if (local_err) {
1299         goto out;
1300     }
1301 
1302     pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
1303     qdev_unrealize(dev);
1304  out:
1305     error_propagate(errp, local_err);
1306 }
1307 
1308 static void pc_virtio_md_pci_pre_plug(HotplugHandler *hotplug_dev,
1309                                       DeviceState *dev, Error **errp)
1310 {
1311     HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
1312     Error *local_err = NULL;
1313 
1314     if (!hotplug_dev2 && dev->hotplugged) {
1315         /*
1316          * Without a bus hotplug handler, we cannot control the plug/unplug
1317          * order. We should never reach this point when hotplugging on x86,
1318          * however, better add a safety net.
1319          */
1320         error_setg(errp, "hotplug of virtio based memory devices not supported"
1321                    " on this bus.");
1322         return;
1323     }
1324     /*
1325      * First, see if we can plug this memory device at all. If that
1326      * succeeds, branch of to the actual hotplug handler.
1327      */
1328     memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
1329                            &local_err);
1330     if (!local_err && hotplug_dev2) {
1331         hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err);
1332     }
1333     error_propagate(errp, local_err);
1334 }
1335 
1336 static void pc_virtio_md_pci_plug(HotplugHandler *hotplug_dev,
1337                                   DeviceState *dev, Error **errp)
1338 {
1339     HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
1340     Error *local_err = NULL;
1341 
1342     /*
1343      * Plug the memory device first and then branch off to the actual
1344      * hotplug handler. If that one fails, we can easily undo the memory
1345      * device bits.
1346      */
1347     memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
1348     if (hotplug_dev2) {
1349         hotplug_handler_plug(hotplug_dev2, dev, &local_err);
1350         if (local_err) {
1351             memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
1352         }
1353     }
1354     error_propagate(errp, local_err);
1355 }
1356 
1357 static void pc_virtio_md_pci_unplug_request(HotplugHandler *hotplug_dev,
1358                                             DeviceState *dev, Error **errp)
1359 {
1360     /* We don't support hot unplug of virtio based memory devices */
1361     error_setg(errp, "virtio based memory devices cannot be unplugged.");
1362 }
1363 
1364 static void pc_virtio_md_pci_unplug(HotplugHandler *hotplug_dev,
1365                                     DeviceState *dev, Error **errp)
1366 {
1367     /* We don't support hot unplug of virtio based memory devices */
1368 }
1369 
1370 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
1371                                           DeviceState *dev, Error **errp)
1372 {
1373     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1374         pc_memory_pre_plug(hotplug_dev, dev, errp);
1375     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1376         x86_cpu_pre_plug(hotplug_dev, dev, errp);
1377     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1378                object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1379         pc_virtio_md_pci_pre_plug(hotplug_dev, dev, errp);
1380     }
1381 }
1382 
1383 static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
1384                                       DeviceState *dev, Error **errp)
1385 {
1386     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1387         pc_memory_plug(hotplug_dev, dev, errp);
1388     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1389         x86_cpu_plug(hotplug_dev, dev, errp);
1390     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1391                object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1392         pc_virtio_md_pci_plug(hotplug_dev, dev, errp);
1393     }
1394 }
1395 
1396 static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
1397                                                 DeviceState *dev, Error **errp)
1398 {
1399     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1400         pc_memory_unplug_request(hotplug_dev, dev, errp);
1401     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1402         x86_cpu_unplug_request_cb(hotplug_dev, dev, errp);
1403     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1404                object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1405         pc_virtio_md_pci_unplug_request(hotplug_dev, dev, errp);
1406     } else {
1407         error_setg(errp, "acpi: device unplug request for not supported device"
1408                    " type: %s", object_get_typename(OBJECT(dev)));
1409     }
1410 }
1411 
1412 static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
1413                                         DeviceState *dev, Error **errp)
1414 {
1415     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1416         pc_memory_unplug(hotplug_dev, dev, errp);
1417     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1418         x86_cpu_unplug_cb(hotplug_dev, dev, errp);
1419     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1420                object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1421         pc_virtio_md_pci_unplug(hotplug_dev, dev, errp);
1422     } else {
1423         error_setg(errp, "acpi: device unplug for not supported device"
1424                    " type: %s", object_get_typename(OBJECT(dev)));
1425     }
1426 }
1427 
1428 static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
1429                                              DeviceState *dev)
1430 {
1431     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
1432         object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
1433         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1434         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1435         return HOTPLUG_HANDLER(machine);
1436     }
1437 
1438     return NULL;
1439 }
1440 
1441 static void
1442 pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
1443                                          const char *name, void *opaque,
1444                                          Error **errp)
1445 {
1446     MachineState *ms = MACHINE(obj);
1447     int64_t value = 0;
1448 
1449     if (ms->device_memory) {
1450         value = memory_region_size(&ms->device_memory->mr);
1451     }
1452 
1453     visit_type_int(v, name, &value, errp);
1454 }
1455 
1456 static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
1457                                   void *opaque, Error **errp)
1458 {
1459     PCMachineState *pcms = PC_MACHINE(obj);
1460     OnOffAuto vmport = pcms->vmport;
1461 
1462     visit_type_OnOffAuto(v, name, &vmport, errp);
1463 }
1464 
1465 static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
1466                                   void *opaque, Error **errp)
1467 {
1468     PCMachineState *pcms = PC_MACHINE(obj);
1469 
1470     visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
1471 }
1472 
1473 static bool pc_machine_get_smbus(Object *obj, Error **errp)
1474 {
1475     PCMachineState *pcms = PC_MACHINE(obj);
1476 
1477     return pcms->smbus_enabled;
1478 }
1479 
1480 static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
1481 {
1482     PCMachineState *pcms = PC_MACHINE(obj);
1483 
1484     pcms->smbus_enabled = value;
1485 }
1486 
1487 static bool pc_machine_get_sata(Object *obj, Error **errp)
1488 {
1489     PCMachineState *pcms = PC_MACHINE(obj);
1490 
1491     return pcms->sata_enabled;
1492 }
1493 
1494 static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
1495 {
1496     PCMachineState *pcms = PC_MACHINE(obj);
1497 
1498     pcms->sata_enabled = value;
1499 }
1500 
1501 static bool pc_machine_get_pit(Object *obj, Error **errp)
1502 {
1503     PCMachineState *pcms = PC_MACHINE(obj);
1504 
1505     return pcms->pit_enabled;
1506 }
1507 
1508 static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
1509 {
1510     PCMachineState *pcms = PC_MACHINE(obj);
1511 
1512     pcms->pit_enabled = value;
1513 }
1514 
1515 static bool pc_machine_get_hpet(Object *obj, Error **errp)
1516 {
1517     PCMachineState *pcms = PC_MACHINE(obj);
1518 
1519     return pcms->hpet_enabled;
1520 }
1521 
1522 static void pc_machine_set_hpet(Object *obj, bool value, Error **errp)
1523 {
1524     PCMachineState *pcms = PC_MACHINE(obj);
1525 
1526     pcms->hpet_enabled = value;
1527 }
1528 
1529 static bool pc_machine_get_default_bus_bypass_iommu(Object *obj, Error **errp)
1530 {
1531     PCMachineState *pcms = PC_MACHINE(obj);
1532 
1533     return pcms->default_bus_bypass_iommu;
1534 }
1535 
1536 static void pc_machine_set_default_bus_bypass_iommu(Object *obj, bool value,
1537                                                     Error **errp)
1538 {
1539     PCMachineState *pcms = PC_MACHINE(obj);
1540 
1541     pcms->default_bus_bypass_iommu = value;
1542 }
1543 
1544 static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
1545                                             const char *name, void *opaque,
1546                                             Error **errp)
1547 {
1548     PCMachineState *pcms = PC_MACHINE(obj);
1549     uint64_t value = pcms->max_ram_below_4g;
1550 
1551     visit_type_size(v, name, &value, errp);
1552 }
1553 
1554 static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
1555                                             const char *name, void *opaque,
1556                                             Error **errp)
1557 {
1558     PCMachineState *pcms = PC_MACHINE(obj);
1559     uint64_t value;
1560 
1561     if (!visit_type_size(v, name, &value, errp)) {
1562         return;
1563     }
1564     if (value > 4 * GiB) {
1565         error_setg(errp,
1566                    "Machine option 'max-ram-below-4g=%"PRIu64
1567                    "' expects size less than or equal to 4G", value);
1568         return;
1569     }
1570 
1571     if (value < 1 * MiB) {
1572         warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
1573                     "BIOS may not work with less than 1MiB", value);
1574     }
1575 
1576     pcms->max_ram_below_4g = value;
1577 }
1578 
1579 static void pc_machine_get_max_fw_size(Object *obj, Visitor *v,
1580                                        const char *name, void *opaque,
1581                                        Error **errp)
1582 {
1583     PCMachineState *pcms = PC_MACHINE(obj);
1584     uint64_t value = pcms->max_fw_size;
1585 
1586     visit_type_size(v, name, &value, errp);
1587 }
1588 
1589 static void pc_machine_set_max_fw_size(Object *obj, Visitor *v,
1590                                        const char *name, void *opaque,
1591                                        Error **errp)
1592 {
1593     PCMachineState *pcms = PC_MACHINE(obj);
1594     Error *error = NULL;
1595     uint64_t value;
1596 
1597     visit_type_size(v, name, &value, &error);
1598     if (error) {
1599         error_propagate(errp, error);
1600         return;
1601     }
1602 
1603     /*
1604     * We don't have a theoretically justifiable exact lower bound on the base
1605     * address of any flash mapping. In practice, the IO-APIC MMIO range is
1606     * [0xFEE00000..0xFEE01000] -- see IO_APIC_DEFAULT_ADDRESS --, leaving free
1607     * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in
1608     * size.
1609     */
1610     if (value > 16 * MiB) {
1611         error_setg(errp,
1612                    "User specified max allowed firmware size %" PRIu64 " is "
1613                    "greater than 16MiB. If combined firwmare size exceeds "
1614                    "16MiB the system may not boot, or experience intermittent"
1615                    "stability issues.",
1616                    value);
1617         return;
1618     }
1619 
1620     pcms->max_fw_size = value;
1621 }
1622 
1623 
1624 static void pc_machine_initfn(Object *obj)
1625 {
1626     PCMachineState *pcms = PC_MACHINE(obj);
1627 
1628 #ifdef CONFIG_VMPORT
1629     pcms->vmport = ON_OFF_AUTO_AUTO;
1630 #else
1631     pcms->vmport = ON_OFF_AUTO_OFF;
1632 #endif /* CONFIG_VMPORT */
1633     pcms->max_ram_below_4g = 0; /* use default */
1634     /* acpi build is enabled by default if machine supports it */
1635     pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
1636     pcms->smbus_enabled = true;
1637     pcms->sata_enabled = true;
1638     pcms->pit_enabled = true;
1639     pcms->max_fw_size = 8 * MiB;
1640 #ifdef CONFIG_HPET
1641     pcms->hpet_enabled = true;
1642 #endif
1643     pcms->default_bus_bypass_iommu = false;
1644 
1645     pc_system_flash_create(pcms);
1646     pcms->pcspk = isa_new(TYPE_PC_SPEAKER);
1647     object_property_add_alias(OBJECT(pcms), "pcspk-audiodev",
1648                               OBJECT(pcms->pcspk), "audiodev");
1649 }
1650 
1651 static void pc_machine_reset(MachineState *machine)
1652 {
1653     CPUState *cs;
1654     X86CPU *cpu;
1655 
1656     qemu_devices_reset();
1657 
1658     /* Reset APIC after devices have been reset to cancel
1659      * any changes that qemu_devices_reset() might have done.
1660      */
1661     CPU_FOREACH(cs) {
1662         cpu = X86_CPU(cs);
1663 
1664         if (cpu->apic_state) {
1665             device_legacy_reset(cpu->apic_state);
1666         }
1667     }
1668 }
1669 
1670 static void pc_machine_wakeup(MachineState *machine)
1671 {
1672     cpu_synchronize_all_states();
1673     pc_machine_reset(machine);
1674     cpu_synchronize_all_post_reset();
1675 }
1676 
1677 static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp)
1678 {
1679     X86IOMMUState *iommu = x86_iommu_get_default();
1680     IntelIOMMUState *intel_iommu;
1681 
1682     if (iommu &&
1683         object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) &&
1684         object_dynamic_cast((Object *)dev, "vfio-pci")) {
1685         intel_iommu = INTEL_IOMMU_DEVICE(iommu);
1686         if (!intel_iommu->caching_mode) {
1687             error_setg(errp, "Device assignment is not allowed without "
1688                        "enabling caching-mode=on for Intel IOMMU.");
1689             return false;
1690         }
1691     }
1692 
1693     return true;
1694 }
1695 
1696 static void pc_machine_class_init(ObjectClass *oc, void *data)
1697 {
1698     MachineClass *mc = MACHINE_CLASS(oc);
1699     PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
1700     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
1701 
1702     pcmc->pci_enabled = true;
1703     pcmc->has_acpi_build = true;
1704     pcmc->rsdp_in_ram = true;
1705     pcmc->smbios_defaults = true;
1706     pcmc->smbios_uuid_encoded = true;
1707     pcmc->gigabyte_align = true;
1708     pcmc->has_reserved_memory = true;
1709     pcmc->kvmclock_enabled = true;
1710     pcmc->enforce_aligned_dimm = true;
1711     /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
1712      * to be used at the moment, 32K should be enough for a while.  */
1713     pcmc->acpi_data_size = 0x20000 + 0x8000;
1714     pcmc->linuxboot_dma_enabled = true;
1715     pcmc->pvh_enabled = true;
1716     pcmc->kvmclock_create_always = true;
1717     assert(!mc->get_hotplug_handler);
1718     mc->get_hotplug_handler = pc_get_hotplug_handler;
1719     mc->hotplug_allowed = pc_hotplug_allowed;
1720     mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
1721     mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
1722     mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
1723     mc->auto_enable_numa_with_memhp = true;
1724     mc->auto_enable_numa_with_memdev = true;
1725     mc->has_hotpluggable_cpus = true;
1726     mc->default_boot_order = "cad";
1727     mc->smp_parse = pc_smp_parse;
1728     mc->block_default_type = IF_IDE;
1729     mc->max_cpus = 255;
1730     mc->reset = pc_machine_reset;
1731     mc->wakeup = pc_machine_wakeup;
1732     hc->pre_plug = pc_machine_device_pre_plug_cb;
1733     hc->plug = pc_machine_device_plug_cb;
1734     hc->unplug_request = pc_machine_device_unplug_request_cb;
1735     hc->unplug = pc_machine_device_unplug_cb;
1736     mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
1737     mc->nvdimm_supported = true;
1738     mc->default_ram_id = "pc.ram";
1739 
1740     object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
1741         pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
1742         NULL, NULL);
1743     object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
1744         "Maximum ram below the 4G boundary (32bit boundary)");
1745 
1746     object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
1747         pc_machine_get_device_memory_region_size, NULL,
1748         NULL, NULL);
1749 
1750     object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
1751         pc_machine_get_vmport, pc_machine_set_vmport,
1752         NULL, NULL);
1753     object_class_property_set_description(oc, PC_MACHINE_VMPORT,
1754         "Enable vmport (pc & q35)");
1755 
1756     object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
1757         pc_machine_get_smbus, pc_machine_set_smbus);
1758 
1759     object_class_property_add_bool(oc, PC_MACHINE_SATA,
1760         pc_machine_get_sata, pc_machine_set_sata);
1761 
1762     object_class_property_add_bool(oc, PC_MACHINE_PIT,
1763         pc_machine_get_pit, pc_machine_set_pit);
1764 
1765     object_class_property_add_bool(oc, "hpet",
1766         pc_machine_get_hpet, pc_machine_set_hpet);
1767 
1768     object_class_property_add_bool(oc, "default_bus_bypass_iommu",
1769         pc_machine_get_default_bus_bypass_iommu,
1770         pc_machine_set_default_bus_bypass_iommu);
1771 
1772     object_class_property_add(oc, PC_MACHINE_MAX_FW_SIZE, "size",
1773         pc_machine_get_max_fw_size, pc_machine_set_max_fw_size,
1774         NULL, NULL);
1775     object_class_property_set_description(oc, PC_MACHINE_MAX_FW_SIZE,
1776         "Maximum combined firmware size");
1777 }
1778 
1779 static const TypeInfo pc_machine_info = {
1780     .name = TYPE_PC_MACHINE,
1781     .parent = TYPE_X86_MACHINE,
1782     .abstract = true,
1783     .instance_size = sizeof(PCMachineState),
1784     .instance_init = pc_machine_initfn,
1785     .class_size = sizeof(PCMachineClass),
1786     .class_init = pc_machine_class_init,
1787     .interfaces = (InterfaceInfo[]) {
1788          { TYPE_HOTPLUG_HANDLER },
1789          { }
1790     },
1791 };
1792 
1793 static void pc_machine_register_types(void)
1794 {
1795     type_register_static(&pc_machine_info);
1796 }
1797 
1798 type_init(pc_machine_register_types)
1799