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