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