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