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