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