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