]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/pc.c
i386: Update stepping of Cascadelake-Server
[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"
83c9f4ca 27#include "hw/hw.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
EH
32#include "hw/i386/topology.h"
33#include "sysemu/cpus.h"
0d09e41a 34#include "hw/block/fdc.h"
83c9f4ca
PB
35#include "hw/ide.h"
36#include "hw/pci/pci.h"
2118196b 37#include "hw/pci/pci_bus.h"
0d09e41a
PB
38#include "hw/nvram/fw_cfg.h"
39#include "hw/timer/hpet.h"
a2eb5c0c 40#include "hw/firmware/smbios.h"
83c9f4ca 41#include "hw/loader.h"
ca20cf32 42#include "elf.h"
47b43a1f 43#include "multiboot.h"
0d09e41a 44#include "hw/timer/mc146818rtc.h"
55f613ac 45#include "hw/dma/i8257.h"
0d09e41a 46#include "hw/timer/i8254.h"
47973a2d 47#include "hw/input/i8042.h"
0d09e41a 48#include "hw/audio/pcspk.h"
83c9f4ca
PB
49#include "hw/pci/msi.h"
50#include "hw/sysbus.h"
9c17d615 51#include "sysemu/sysemu.h"
e35704ba 52#include "sysemu/numa.h"
9c17d615 53#include "sysemu/kvm.h"
b1c12027 54#include "sysemu/qtest.h"
1d31f66b 55#include "kvm_i386.h"
0d09e41a 56#include "hw/xen/xen.h"
a19cbfb3 57#include "ui/qemu-spice.h"
022c62cb
PB
58#include "exec/memory.h"
59#include "exec/address-spaces.h"
9c17d615 60#include "sysemu/arch_init.h"
1de7afc9 61#include "qemu/bitmap.h"
0c764a9d 62#include "qemu/config-file.h"
d49b6836 63#include "qemu/error-report.h"
922a01a0 64#include "qemu/option.h"
0445259b 65#include "hw/acpi/acpi.h"
5ff020b7 66#include "hw/acpi/cpu_hotplug.h"
c649983b 67#include "hw/boards.h"
72c194f7 68#include "acpi-build.h"
95bee274 69#include "hw/mem/pc-dimm.h"
e688df6b 70#include "qapi/error.h"
9af23989 71#include "qapi/qapi-visit-common.h"
bf1e8939 72#include "qapi/visitor.h"
15eafc2e 73#include "qom/cpu.h"
1255166b 74#include "hw/nmi.h"
a310e653 75#include "hw/usb.h"
60c5e104 76#include "hw/i386/intel_iommu.h"
489983d6 77#include "hw/net/ne2000-isa.h"
80cabfad 78
471fd342
BS
79/* debug PC/ISA interrupts */
80//#define DEBUG_IRQ
81
82#ifdef DEBUG_IRQ
83#define DPRINTF(fmt, ...) \
84 do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
85#else
86#define DPRINTF(fmt, ...)
87#endif
88
8a92ea2f 89#define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
b6f6e3d3 90#define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
6b35e7bf 91#define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
4c5b10b7 92#define FW_CFG_E820_TABLE (FW_CFG_ARCH_LOCAL + 3)
40ac17cd 93#define FW_CFG_HPET (FW_CFG_ARCH_LOCAL + 4)
80cabfad 94
4c5b10b7
JS
95#define E820_NR_ENTRIES 16
96
97struct e820_entry {
98 uint64_t address;
99 uint64_t length;
100 uint32_t type;
541dc0d4 101} QEMU_PACKED __attribute((__aligned__(4)));
4c5b10b7
JS
102
103struct e820_table {
104 uint32_t count;
105 struct e820_entry entry[E820_NR_ENTRIES];
541dc0d4 106} QEMU_PACKED __attribute((__aligned__(4)));
4c5b10b7 107
7d67110f
GH
108static struct e820_table e820_reserve;
109static struct e820_entry *e820_table;
110static unsigned e820_entries;
dd703b99 111struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
4c5b10b7 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" },
117 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
ecb85fe4
PB
118 { "Skylake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
119 { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
120 { "Skylake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
121 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
122 { "Cascadelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
123 { "Icelake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
124 { "Icelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
b0a19803 125 { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
abd93cc7
MAL
126};
127const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
128
ddb3235d 129GlobalProperty pc_compat_3_0[] = {
6c36bddf
EH
130 { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
131 { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
132 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
ddb3235d
MAL
133};
134const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
135
0d47310b 136GlobalProperty pc_compat_2_12[] = {
6c36bddf
EH
137 { TYPE_X86_CPU, "legacy-cache", "on" },
138 { TYPE_X86_CPU, "topoext", "off" },
139 { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
140 { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
0d47310b
MAL
141};
142const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
143
43df70a9 144GlobalProperty pc_compat_2_11[] = {
6c36bddf
EH
145 { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
146 { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
43df70a9
MAL
147};
148const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
149
503224f4 150GlobalProperty pc_compat_2_10[] = {
6c36bddf
EH
151 { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
152 { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
153 { "q35-pcihost", "x-pci-hole64-fix", "off" },
503224f4
MAL
154};
155const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
156
3e803152 157GlobalProperty pc_compat_2_9[] = {
6c36bddf 158 { "mch", "extended-tseg-mbytes", "0" },
3e803152
MAL
159};
160const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
161
edc24ccd 162GlobalProperty pc_compat_2_8[] = {
6c36bddf
EH
163 { TYPE_X86_CPU, "tcg-cpuid", "off" },
164 { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
165 { "ICH9-LPC", "x-smi-broadcast", "off" },
166 { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
167 { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
edc24ccd
MAL
168};
169const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
170
5a995064 171GlobalProperty pc_compat_2_7[] = {
6c36bddf
EH
172 { TYPE_X86_CPU, "l3-cache", "off" },
173 { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
174 { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
175 { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
176 { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
177 { "isa-pcspk", "migrate", "off" },
5a995064
MAL
178};
179const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
180
ff8f261f 181GlobalProperty pc_compat_2_6[] = {
6c36bddf
EH
182 { TYPE_X86_CPU, "cpuid-0xb", "off" },
183 { "vmxnet3", "romfile", "" },
184 { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
185 { "apic-common", "legacy-instance-id", "on", }
ff8f261f
MAL
186};
187const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
188
fe759610
MAL
189GlobalProperty pc_compat_2_5[] = {};
190const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
191
2f99b9c2
MAL
192GlobalProperty pc_compat_2_4[] = {
193 PC_CPU_MODEL_IDS("2.4.0")
6c36bddf
EH
194 { "Haswell-" TYPE_X86_CPU, "abm", "off" },
195 { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
196 { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
197 { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
198 { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
199 { TYPE_X86_CPU, "check", "off" },
200 { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
201 { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
202 { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
203 { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
204 { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
205 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
206 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
207 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
2f99b9c2
MAL
208};
209const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
210
8995dd90
MAL
211GlobalProperty pc_compat_2_3[] = {
212 PC_CPU_MODEL_IDS("2.3.0")
6c36bddf
EH
213 { TYPE_X86_CPU, "arat", "off" },
214 { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" },
215 { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" },
216 { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" },
217 { "n270" "-" TYPE_X86_CPU, "min-level", "5" },
218 { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" },
219 { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" },
220 { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" },
221 { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
222 { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
223 { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
224 { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
225 { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
226 { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
227 { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
228 { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
229 { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
230 { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
231 { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
232 { TYPE_X86_CPU, "kvm-no-smi-migration", "on" },
8995dd90
MAL
233};
234const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3);
235
1c30044e
MAL
236GlobalProperty pc_compat_2_2[] = {
237 PC_CPU_MODEL_IDS("2.2.0")
6c36bddf
EH
238 { "kvm64" "-" TYPE_X86_CPU, "vme", "off" },
239 { "kvm32" "-" TYPE_X86_CPU, "vme", "off" },
240 { "Conroe" "-" TYPE_X86_CPU, "vme", "off" },
241 { "Penryn" "-" TYPE_X86_CPU, "vme", "off" },
242 { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" },
243 { "Westmere" "-" TYPE_X86_CPU, "vme", "off" },
244 { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" },
245 { "Haswell" "-" TYPE_X86_CPU, "vme", "off" },
246 { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" },
247 { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" },
248 { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" },
249 { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" },
250 { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" },
251 { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" },
252 { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" },
253 { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" },
254 { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" },
255 { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" },
1c30044e
MAL
256};
257const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2);
258
c4fc5695
MAL
259GlobalProperty pc_compat_2_1[] = {
260 PC_CPU_MODEL_IDS("2.1.0")
6c36bddf
EH
261 { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" },
262 { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" },
c4fc5695
MAL
263};
264const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1);
265
a310e653
MAL
266GlobalProperty pc_compat_2_0[] = {
267 PC_CPU_MODEL_IDS("2.0.0")
6c36bddf
EH
268 { "virtio-scsi-pci", "any_layout", "off" },
269 { "PIIX4_PM", "memory-hotplug-support", "off" },
270 { "apic", "version", "0x11" },
271 { "nec-usb-xhci", "superspeed-ports-first", "off" },
272 { "nec-usb-xhci", "force-pcie-endcap", "on" },
273 { "pci-serial", "prog_if", "0" },
274 { "pci-serial-2x", "prog_if", "0" },
275 { "pci-serial-4x", "prog_if", "0" },
276 { "virtio-net-pci", "guest_announce", "off" },
277 { "ICH9-LPC", "memory-hotplug-support", "off" },
278 { "xio3130-downstream", COMPAT_PROP_PCP, "off" },
279 { "ioh3420", COMPAT_PROP_PCP, "off" },
a310e653
MAL
280};
281const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0);
282
283GlobalProperty pc_compat_1_7[] = {
284 PC_CPU_MODEL_IDS("1.7.0")
6c36bddf
EH
285 { TYPE_USB_DEVICE, "msos-desc", "no" },
286 { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" },
287 { "hpet", HPET_INTCAP, "4" },
a310e653
MAL
288};
289const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7);
290
291GlobalProperty pc_compat_1_6[] = {
292 PC_CPU_MODEL_IDS("1.6.0")
6c36bddf
EH
293 { "e1000", "mitigation", "off" },
294 { "qemu64-" TYPE_X86_CPU, "model", "2" },
295 { "qemu32-" TYPE_X86_CPU, "model", "3" },
296 { "i440FX-pcihost", "short_root_bus", "1" },
297 { "q35-pcihost", "short_root_bus", "1" },
a310e653
MAL
298};
299const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6);
300
301GlobalProperty pc_compat_1_5[] = {
302 PC_CPU_MODEL_IDS("1.5.0")
6c36bddf
EH
303 { "Conroe-" TYPE_X86_CPU, "model", "2" },
304 { "Conroe-" TYPE_X86_CPU, "min-level", "2" },
305 { "Penryn-" TYPE_X86_CPU, "model", "2" },
306 { "Penryn-" TYPE_X86_CPU, "min-level", "2" },
307 { "Nehalem-" TYPE_X86_CPU, "model", "2" },
308 { "Nehalem-" TYPE_X86_CPU, "min-level", "2" },
309 { "virtio-net-pci", "any_layout", "off" },
310 { TYPE_X86_CPU, "pmu", "on" },
311 { "i440FX-pcihost", "short_root_bus", "0" },
312 { "q35-pcihost", "short_root_bus", "0" },
a310e653
MAL
313};
314const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5);
315
316GlobalProperty pc_compat_1_4[] = {
317 PC_CPU_MODEL_IDS("1.4.0")
6c36bddf
EH
318 { "scsi-hd", "discard_granularity", "0" },
319 { "scsi-cd", "discard_granularity", "0" },
320 { "scsi-disk", "discard_granularity", "0" },
321 { "ide-hd", "discard_granularity", "0" },
322 { "ide-cd", "discard_granularity", "0" },
323 { "ide-drive", "discard_granularity", "0" },
324 { "virtio-blk-pci", "discard_granularity", "0" },
325 /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
326 { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
327 { "virtio-net-pci", "ctrl_guest_offloads", "off" },
328 { "e1000", "romfile", "pxe-e1000.rom" },
329 { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
330 { "pcnet", "romfile", "pxe-pcnet.rom" },
331 { "rtl8139", "romfile", "pxe-rtl8139.rom" },
332 { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
333 { "486-" TYPE_X86_CPU, "model", "0" },
334 { "n270" "-" TYPE_X86_CPU, "movbe", "off" },
335 { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" },
a310e653
MAL
336};
337const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4);
338
b881fbe9 339void gsi_handler(void *opaque, int n, int level)
1452411b 340{
b881fbe9 341 GSIState *s = opaque;
1452411b 342
b881fbe9
JK
343 DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
344 if (n < ISA_NUM_IRQS) {
345 qemu_set_irq(s->i8259_irq[n], level);
1632dc6a 346 }
b881fbe9 347 qemu_set_irq(s->ioapic_irq[n], level);
2e9947d2 348}
1452411b 349
258711c6
JG
350static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
351 unsigned size)
80cabfad
FB
352{
353}
354
c02e1eac
JG
355static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
356{
a6fc23e5 357 return 0xffffffffffffffffULL;
c02e1eac
JG
358}
359
f929aad6 360/* MSDOS compatibility mode FPU exception support */
d537cf6c 361static qemu_irq ferr_irq;
8e78eb28
IY
362
363void pc_register_ferr_irq(qemu_irq irq)
364{
365 ferr_irq = irq;
366}
367
f929aad6
FB
368/* XXX: add IGNNE support */
369void cpu_set_ferr(CPUX86State *s)
370{
d537cf6c 371 qemu_irq_raise(ferr_irq);
f929aad6
FB
372}
373
258711c6
JG
374static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
375 unsigned size)
f929aad6 376{
d537cf6c 377 qemu_irq_lower(ferr_irq);
f929aad6
FB
378}
379
c02e1eac
JG
380static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
381{
a6fc23e5 382 return 0xffffffffffffffffULL;
c02e1eac
JG
383}
384
28ab0e2e 385/* TSC handling */
28ab0e2e
FB
386uint64_t cpu_get_tsc(CPUX86State *env)
387{
4a1418e0 388 return cpu_get_ticks();
28ab0e2e
FB
389}
390
3de388f6 391/* IRQ handling */
4a8fa5dc 392int cpu_get_pic_interrupt(CPUX86State *env)
3de388f6 393{
02e51483 394 X86CPU *cpu = x86_env_get_cpu(env);
3de388f6
FB
395 int intno;
396
bb93e099
WL
397 if (!kvm_irqchip_in_kernel()) {
398 intno = apic_get_interrupt(cpu->apic_state);
399 if (intno >= 0) {
400 return intno;
401 }
402 /* read the irq from the PIC */
403 if (!apic_accept_pic_intr(cpu->apic_state)) {
404 return -1;
405 }
cf6d64bf 406 }
0e21e12b 407
3de388f6
FB
408 intno = pic_read_irq(isa_pic);
409 return intno;
410}
411
d537cf6c 412static void pic_irq_request(void *opaque, int irq, int level)
3de388f6 413{
182735ef
AF
414 CPUState *cs = first_cpu;
415 X86CPU *cpu = X86_CPU(cs);
a5b38b51 416
471fd342 417 DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
bb93e099 418 if (cpu->apic_state && !kvm_irqchip_in_kernel()) {
bdc44640 419 CPU_FOREACH(cs) {
182735ef 420 cpu = X86_CPU(cs);
02e51483
CF
421 if (apic_accept_pic_intr(cpu->apic_state)) {
422 apic_deliver_pic_intr(cpu->apic_state, level);
cf6d64bf 423 }
d5529471
AJ
424 }
425 } else {
d8ed887b 426 if (level) {
c3affe56 427 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
d8ed887b
AF
428 } else {
429 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
430 }
a5b38b51 431 }
3de388f6
FB
432}
433
b0a21b53
FB
434/* PC cmos mappings */
435
80cabfad
FB
436#define REG_EQUIPMENT_BYTE 0x14
437
bda05509 438int cmos_get_fd_drive_type(FloppyDriveType fd0)
777428f2
FB
439{
440 int val;
441
442 switch (fd0) {
2da44dd0 443 case FLOPPY_DRIVE_TYPE_144:
777428f2
FB
444 /* 1.44 Mb 3"5 drive */
445 val = 4;
446 break;
2da44dd0 447 case FLOPPY_DRIVE_TYPE_288:
777428f2
FB
448 /* 2.88 Mb 3"5 drive */
449 val = 5;
450 break;
2da44dd0 451 case FLOPPY_DRIVE_TYPE_120:
777428f2
FB
452 /* 1.2 Mb 5"5 drive */
453 val = 2;
454 break;
2da44dd0 455 case FLOPPY_DRIVE_TYPE_NONE:
777428f2
FB
456 default:
457 val = 0;
458 break;
459 }
460 return val;
461}
462
9139046c
MA
463static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
464 int16_t cylinders, int8_t heads, int8_t sectors)
ba6c2377 465{
ba6c2377
FB
466 rtc_set_memory(s, type_ofs, 47);
467 rtc_set_memory(s, info_ofs, cylinders);
468 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
469 rtc_set_memory(s, info_ofs + 2, heads);
470 rtc_set_memory(s, info_ofs + 3, 0xff);
471 rtc_set_memory(s, info_ofs + 4, 0xff);
472 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
473 rtc_set_memory(s, info_ofs + 6, cylinders);
474 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
475 rtc_set_memory(s, info_ofs + 8, sectors);
476}
477
6ac0e82d
AZ
478/* convert boot_device letter to something recognizable by the bios */
479static int boot_device2nibble(char boot_device)
480{
481 switch(boot_device) {
482 case 'a':
483 case 'b':
484 return 0x01; /* floppy boot */
485 case 'c':
486 return 0x02; /* hard drive boot */
487 case 'd':
488 return 0x03; /* CD-ROM boot */
489 case 'n':
490 return 0x04; /* Network boot */
491 }
492 return 0;
493}
494
ddcd5531 495static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
0ecdffbb
AJ
496{
497#define PC_MAX_BOOT_DEVICES 3
0ecdffbb
AJ
498 int nbds, bds[3] = { 0, };
499 int i;
500
501 nbds = strlen(boot_device);
502 if (nbds > PC_MAX_BOOT_DEVICES) {
ddcd5531
GA
503 error_setg(errp, "Too many boot devices for PC");
504 return;
0ecdffbb
AJ
505 }
506 for (i = 0; i < nbds; i++) {
507 bds[i] = boot_device2nibble(boot_device[i]);
508 if (bds[i] == 0) {
ddcd5531
GA
509 error_setg(errp, "Invalid boot device for PC: '%c'",
510 boot_device[i]);
511 return;
0ecdffbb
AJ
512 }
513 }
514 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
d9346e81 515 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
0ecdffbb
AJ
516}
517
ddcd5531 518static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
d9346e81 519{
ddcd5531 520 set_boot_dev(opaque, boot_device, errp);
d9346e81
MA
521}
522
7444ca4e
LE
523static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
524{
525 int val, nb, i;
2da44dd0
JS
526 FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
527 FLOPPY_DRIVE_TYPE_NONE };
7444ca4e
LE
528
529 /* floppy type */
530 if (floppy) {
531 for (i = 0; i < 2; i++) {
532 fd_type[i] = isa_fdc_get_drive_type(floppy, i);
533 }
534 }
535 val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
536 cmos_get_fd_drive_type(fd_type[1]);
537 rtc_set_memory(rtc_state, 0x10, val);
538
539 val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
540 nb = 0;
2da44dd0 541 if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
7444ca4e
LE
542 nb++;
543 }
2da44dd0 544 if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
7444ca4e
LE
545 nb++;
546 }
547 switch (nb) {
548 case 0:
549 break;
550 case 1:
551 val |= 0x01; /* 1 drive, ready for boot */
552 break;
553 case 2:
554 val |= 0x41; /* 2 drives, ready for boot */
555 break;
556 }
557 rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
558}
559
c0897e0c
MA
560typedef struct pc_cmos_init_late_arg {
561 ISADevice *rtc_state;
9139046c 562 BusState *idebus[2];
c0897e0c
MA
563} pc_cmos_init_late_arg;
564
b86f4613
LE
565typedef struct check_fdc_state {
566 ISADevice *floppy;
567 bool multiple;
568} CheckFdcState;
569
570static int check_fdc(Object *obj, void *opaque)
571{
572 CheckFdcState *state = opaque;
573 Object *fdc;
574 uint32_t iobase;
575 Error *local_err = NULL;
576
577 fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
578 if (!fdc) {
579 return 0;
580 }
581
1ea1572a 582 iobase = object_property_get_uint(obj, "iobase", &local_err);
b86f4613
LE
583 if (local_err || iobase != 0x3f0) {
584 error_free(local_err);
585 return 0;
586 }
587
588 if (state->floppy) {
589 state->multiple = true;
590 } else {
591 state->floppy = ISA_DEVICE(obj);
592 }
593 return 0;
594}
595
596static const char * const fdc_container_path[] = {
597 "/unattached", "/peripheral", "/peripheral-anon"
598};
599
424e4a87
RK
600/*
601 * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
602 * and ACPI objects.
603 */
604ISADevice *pc_find_fdc0(void)
605{
606 int i;
607 Object *container;
608 CheckFdcState state = { 0 };
609
610 for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
611 container = container_get(qdev_get_machine(), fdc_container_path[i]);
612 object_child_foreach(container, check_fdc, &state);
613 }
614
615 if (state.multiple) {
3dc6f869
AF
616 warn_report("multiple floppy disk controllers with "
617 "iobase=0x3f0 have been found");
433672b0 618 error_printf("the one being picked for CMOS setup might not reflect "
9e5d2c52 619 "your intent");
424e4a87
RK
620 }
621
622 return state.floppy;
623}
624
c0897e0c
MA
625static void pc_cmos_init_late(void *opaque)
626{
627 pc_cmos_init_late_arg *arg = opaque;
628 ISADevice *s = arg->rtc_state;
9139046c
MA
629 int16_t cylinders;
630 int8_t heads, sectors;
c0897e0c 631 int val;
2adc99b2 632 int i, trans;
c0897e0c 633
9139046c 634 val = 0;
272f0428
CP
635 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
636 &cylinders, &heads, &sectors) >= 0) {
9139046c
MA
637 cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
638 val |= 0xf0;
639 }
272f0428
CP
640 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
641 &cylinders, &heads, &sectors) >= 0) {
9139046c
MA
642 cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
643 val |= 0x0f;
644 }
645 rtc_set_memory(s, 0x12, val);
c0897e0c
MA
646
647 val = 0;
648 for (i = 0; i < 4; i++) {
9139046c
MA
649 /* NOTE: ide_get_geometry() returns the physical
650 geometry. It is always such that: 1 <= sects <= 63, 1
651 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
652 geometry can be different if a translation is done. */
272f0428
CP
653 if (arg->idebus[i / 2] &&
654 ide_get_geometry(arg->idebus[i / 2], i % 2,
9139046c 655 &cylinders, &heads, &sectors) >= 0) {
2adc99b2
MA
656 trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
657 assert((trans & ~3) == 0);
658 val |= trans << (i * 2);
c0897e0c
MA
659 }
660 }
661 rtc_set_memory(s, 0x39, val);
662
424e4a87 663 pc_cmos_init_floppy(s, pc_find_fdc0());
b86f4613 664
c0897e0c
MA
665 qemu_unregister_reset(pc_cmos_init_late, opaque);
666}
667
23d30407 668void pc_cmos_init(PCMachineState *pcms,
220a8846 669 BusState *idebus0, BusState *idebus1,
63ffb564 670 ISADevice *s)
80cabfad 671{
7444ca4e 672 int val;
c0897e0c 673 static pc_cmos_init_late_arg arg;
b0a21b53 674
b0a21b53 675 /* various important CMOS locations needed by PC/Bochs bios */
80cabfad
FB
676
677 /* memory size */
e89001f7 678 /* base memory (first MiB) */
d471bf3e 679 val = MIN(pcms->below_4g_mem_size / KiB, 640);
333190eb
FB
680 rtc_set_memory(s, 0x15, val);
681 rtc_set_memory(s, 0x16, val >> 8);
e89001f7 682 /* extended memory (next 64MiB) */
d471bf3e
PB
683 if (pcms->below_4g_mem_size > 1 * MiB) {
684 val = (pcms->below_4g_mem_size - 1 * MiB) / KiB;
e89001f7
MA
685 } else {
686 val = 0;
687 }
80cabfad
FB
688 if (val > 65535)
689 val = 65535;
b0a21b53
FB
690 rtc_set_memory(s, 0x17, val);
691 rtc_set_memory(s, 0x18, val >> 8);
692 rtc_set_memory(s, 0x30, val);
693 rtc_set_memory(s, 0x31, val >> 8);
e89001f7 694 /* memory between 16MiB and 4GiB */
d471bf3e
PB
695 if (pcms->below_4g_mem_size > 16 * MiB) {
696 val = (pcms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
e89001f7 697 } else {
9da98861 698 val = 0;
e89001f7 699 }
80cabfad
FB
700 if (val > 65535)
701 val = 65535;
b0a21b53
FB
702 rtc_set_memory(s, 0x34, val);
703 rtc_set_memory(s, 0x35, val >> 8);
e89001f7 704 /* memory above 4GiB */
88076854 705 val = pcms->above_4g_mem_size / 65536;
e89001f7
MA
706 rtc_set_memory(s, 0x5b, val);
707 rtc_set_memory(s, 0x5c, val >> 8);
708 rtc_set_memory(s, 0x5d, val >> 16);
3b46e624 709
23d30407 710 object_property_add_link(OBJECT(pcms), "rtc_state",
2d996150 711 TYPE_ISA_DEVICE,
ec68007a 712 (Object **)&pcms->rtc,
2d996150 713 object_property_allow_set_link,
265b578c 714 OBJ_PROP_LINK_STRONG, &error_abort);
23d30407 715 object_property_set_link(OBJECT(pcms), OBJECT(s),
2d996150 716 "rtc_state", &error_abort);
298e01b6 717
007b0657 718 set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal);
80cabfad 719
b0a21b53 720 val = 0;
b0a21b53
FB
721 val |= 0x02; /* FPU is there */
722 val |= 0x04; /* PS/2 mouse installed */
723 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
724
b86f4613 725 /* hard drives and FDC */
c0897e0c 726 arg.rtc_state = s;
9139046c
MA
727 arg.idebus[0] = idebus0;
728 arg.idebus[1] = idebus1;
c0897e0c 729 qemu_register_reset(pc_cmos_init_late, &arg);
80cabfad
FB
730}
731
a0881c64
AF
732#define TYPE_PORT92 "port92"
733#define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
734
4b78a802
BS
735/* port 92 stuff: could be split off */
736typedef struct Port92State {
a0881c64
AF
737 ISADevice parent_obj;
738
23af670e 739 MemoryRegion io;
4b78a802 740 uint8_t outport;
d812b3d6 741 qemu_irq a20_out;
4b78a802
BS
742} Port92State;
743
93ef4192
AG
744static void port92_write(void *opaque, hwaddr addr, uint64_t val,
745 unsigned size)
4b78a802
BS
746{
747 Port92State *s = opaque;
4700a316 748 int oldval = s->outport;
4b78a802 749
c5539cb4 750 DPRINTF("port92: write 0x%02" PRIx64 "\n", val);
4b78a802 751 s->outport = val;
d812b3d6 752 qemu_set_irq(s->a20_out, (val >> 1) & 1);
4700a316 753 if ((val & 1) && !(oldval & 1)) {
cf83f140 754 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
4b78a802
BS
755 }
756}
757
93ef4192
AG
758static uint64_t port92_read(void *opaque, hwaddr addr,
759 unsigned size)
4b78a802
BS
760{
761 Port92State *s = opaque;
762 uint32_t ret;
763
764 ret = s->outport;
765 DPRINTF("port92: read 0x%02x\n", ret);
766 return ret;
767}
768
d80fe99d 769static void port92_init(ISADevice *dev, qemu_irq a20_out)
4b78a802 770{
d80fe99d 771 qdev_connect_gpio_out_named(DEVICE(dev), PORT92_A20_LINE, 0, a20_out);
4b78a802
BS
772}
773
774static const VMStateDescription vmstate_port92_isa = {
775 .name = "port92",
776 .version_id = 1,
777 .minimum_version_id = 1,
d49805ae 778 .fields = (VMStateField[]) {
4b78a802
BS
779 VMSTATE_UINT8(outport, Port92State),
780 VMSTATE_END_OF_LIST()
781 }
782};
783
784static void port92_reset(DeviceState *d)
785{
a0881c64 786 Port92State *s = PORT92(d);
4b78a802
BS
787
788 s->outport &= ~1;
789}
790
23af670e 791static const MemoryRegionOps port92_ops = {
93ef4192
AG
792 .read = port92_read,
793 .write = port92_write,
794 .impl = {
795 .min_access_size = 1,
796 .max_access_size = 1,
797 },
798 .endianness = DEVICE_LITTLE_ENDIAN,
23af670e
RH
799};
800
db895a1e 801static void port92_initfn(Object *obj)
4b78a802 802{
db895a1e 803 Port92State *s = PORT92(obj);
4b78a802 804
1437c94b 805 memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1);
23af670e 806
4b78a802 807 s->outport = 0;
d812b3d6
EV
808
809 qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, PORT92_A20_LINE, 1);
db895a1e
AF
810}
811
812static void port92_realizefn(DeviceState *dev, Error **errp)
813{
814 ISADevice *isadev = ISA_DEVICE(dev);
815 Port92State *s = PORT92(dev);
816
817 isa_register_ioport(isadev, &s->io, 0x92);
4b78a802
BS
818}
819
8f04ee08
AL
820static void port92_class_initfn(ObjectClass *klass, void *data)
821{
39bffca2 822 DeviceClass *dc = DEVICE_CLASS(klass);
db895a1e 823
db895a1e 824 dc->realize = port92_realizefn;
39bffca2
AL
825 dc->reset = port92_reset;
826 dc->vmsd = &vmstate_port92_isa;
f3b17640
MA
827 /*
828 * Reason: unlike ordinary ISA devices, this one needs additional
829 * wiring: its A20 output line needs to be wired up by
830 * port92_init().
831 */
e90f2a8c 832 dc->user_creatable = false;
8f04ee08
AL
833}
834
8c43a6f0 835static const TypeInfo port92_info = {
a0881c64 836 .name = TYPE_PORT92,
39bffca2
AL
837 .parent = TYPE_ISA_DEVICE,
838 .instance_size = sizeof(Port92State),
db895a1e 839 .instance_init = port92_initfn,
39bffca2 840 .class_init = port92_class_initfn,
4b78a802
BS
841};
842
83f7d43a 843static void port92_register_types(void)
4b78a802 844{
39bffca2 845 type_register_static(&port92_info);
4b78a802 846}
83f7d43a
AF
847
848type_init(port92_register_types)
4b78a802 849
956a3e6b 850static void handle_a20_line_change(void *opaque, int irq, int level)
59b8ad81 851{
cc36a7a2 852 X86CPU *cpu = opaque;
e1a23744 853
956a3e6b 854 /* XXX: send to all CPUs ? */
4b78a802 855 /* XXX: add logic to handle multiple A20 line sources */
cc36a7a2 856 x86_cpu_set_a20(cpu, level);
e1a23744
FB
857}
858
4c5b10b7
JS
859int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
860{
7d67110f 861 int index = le32_to_cpu(e820_reserve.count);
4c5b10b7
JS
862 struct e820_entry *entry;
863
7d67110f
GH
864 if (type != E820_RAM) {
865 /* old FW_CFG_E820_TABLE entry -- reservations only */
866 if (index >= E820_NR_ENTRIES) {
867 return -EBUSY;
868 }
869 entry = &e820_reserve.entry[index++];
870
871 entry->address = cpu_to_le64(address);
872 entry->length = cpu_to_le64(length);
873 entry->type = cpu_to_le32(type);
874
875 e820_reserve.count = cpu_to_le32(index);
876 }
4c5b10b7 877
7d67110f 878 /* new "etc/e820" file -- include ram too */
ab3ad07f 879 e820_table = g_renew(struct e820_entry, e820_table, e820_entries + 1);
7d67110f
GH
880 e820_table[e820_entries].address = cpu_to_le64(address);
881 e820_table[e820_entries].length = cpu_to_le64(length);
882 e820_table[e820_entries].type = cpu_to_le32(type);
883 e820_entries++;
4c5b10b7 884
7d67110f 885 return e820_entries;
4c5b10b7
JS
886}
887
7bf8ef19
GS
888int e820_get_num_entries(void)
889{
890 return e820_entries;
891}
892
893bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length)
894{
895 if (idx < e820_entries && e820_table[idx].type == cpu_to_le32(type)) {
896 *address = le64_to_cpu(e820_table[idx].address);
897 *length = le64_to_cpu(e820_table[idx].length);
898 return true;
899 }
900 return false;
901}
902
54a40293
EH
903/* Enables contiguous-apic-ID mode, for compatibility */
904static bool compat_apic_id_mode;
905
906void enable_compat_apic_id_mode(void)
907{
908 compat_apic_id_mode = true;
909}
910
911/* Calculates initial APIC ID for a specific CPU index
912 *
913 * Currently we need to be able to calculate the APIC ID from the CPU index
914 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
915 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
916 * all CPUs up to max_cpus.
917 */
918static uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index)
919{
920 uint32_t correct_id;
921 static bool warned;
922
923 correct_id = x86_apicid_from_cpu_idx(smp_cores, smp_threads, cpu_index);
924 if (compat_apic_id_mode) {
b1c12027 925 if (cpu_index != correct_id && !warned && !qtest_enabled()) {
54a40293
EH
926 error_report("APIC IDs set in compatibility mode, "
927 "CPU topology won't match the configuration");
928 warned = true;
929 }
930 return cpu_index;
931 } else {
932 return correct_id;
933 }
934}
935
f2098f48 936static void pc_build_smbios(PCMachineState *pcms)
80cabfad 937{
c97294ec
GS
938 uint8_t *smbios_tables, *smbios_anchor;
939 size_t smbios_tables_len, smbios_anchor_len;
89cc4a27
WH
940 struct smbios_phys_mem_area *mem_array;
941 unsigned i, array_count;
38690a1c
IM
942 MachineState *ms = MACHINE(pcms);
943 X86CPU *cpu = X86_CPU(ms->possible_cpus->cpus[0].cpu);
f2098f48
IM
944
945 /* tell smbios about cpuid version and features */
946 smbios_set_cpuid(cpu->env.cpuid_version, cpu->env.features[FEAT_1_EDX]);
5fd0a9d4
WH
947
948 smbios_tables = smbios_get_table_legacy(&smbios_tables_len);
949 if (smbios_tables) {
f2098f48 950 fw_cfg_add_bytes(pcms->fw_cfg, FW_CFG_SMBIOS_ENTRIES,
5fd0a9d4
WH
951 smbios_tables, smbios_tables_len);
952 }
953
89cc4a27
WH
954 /* build the array of physical mem area from e820 table */
955 mem_array = g_malloc0(sizeof(*mem_array) * e820_get_num_entries());
956 for (i = 0, array_count = 0; i < e820_get_num_entries(); i++) {
957 uint64_t addr, len;
958
959 if (e820_get_entry(i, E820_RAM, &addr, &len)) {
960 mem_array[array_count].address = addr;
961 mem_array[array_count].length = len;
962 array_count++;
963 }
964 }
965 smbios_get_tables(mem_array, array_count,
966 &smbios_tables, &smbios_tables_len,
5fd0a9d4 967 &smbios_anchor, &smbios_anchor_len);
89cc4a27
WH
968 g_free(mem_array);
969
5fd0a9d4 970 if (smbios_anchor) {
f2098f48 971 fw_cfg_add_file(pcms->fw_cfg, "etc/smbios/smbios-tables",
5fd0a9d4 972 smbios_tables, smbios_tables_len);
f2098f48 973 fw_cfg_add_file(pcms->fw_cfg, "etc/smbios/smbios-anchor",
5fd0a9d4
WH
974 smbios_anchor, smbios_anchor_len);
975 }
976}
977
ebde2465 978static FWCfgState *bochs_bios_init(AddressSpace *as, PCMachineState *pcms)
5fd0a9d4
WH
979{
980 FWCfgState *fw_cfg;
11c2fd3e 981 uint64_t *numa_fw_cfg;
ea265072
IM
982 int i;
983 const CPUArchIdList *cpus;
984 MachineClass *mc = MACHINE_GET_CLASS(pcms);
3cce6243 985
305ae888 986 fw_cfg = fw_cfg_init_io_dma(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4, as);
e3cadac0 987 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
c886fc4c 988
1d934e89
EH
989 /* FW_CFG_MAX_CPUS is a bit confusing/problematic on x86:
990 *
a3abd0f2
IM
991 * For machine types prior to 1.8, SeaBIOS needs FW_CFG_MAX_CPUS for
992 * building MPTable, ACPI MADT, ACPI CPU hotplug and ACPI SRAT table,
993 * that tables are based on xAPIC ID and QEMU<->SeaBIOS interface
994 * for CPU hotplug also uses APIC ID and not "CPU index".
995 * This means that FW_CFG_MAX_CPUS is not the "maximum number of CPUs",
996 * but the "limit to the APIC ID values SeaBIOS may see".
1d934e89 997 *
a3abd0f2
IM
998 * So for compatibility reasons with old BIOSes we are stuck with
999 * "etc/max-cpus" actually being apic_id_limit
1d934e89 1000 */
ebde2465 1001 fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)pcms->apic_id_limit);
905fdcb5 1002 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
089da572
MA
1003 fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES,
1004 acpi_tables, acpi_tables_len);
9b5b76d4 1005 fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, kvm_allows_irq0_override());
b6f6e3d3 1006
089da572 1007 fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE,
7d67110f
GH
1008 &e820_reserve, sizeof(e820_reserve));
1009 fw_cfg_add_file(fw_cfg, "etc/e820", e820_table,
1010 sizeof(struct e820_entry) * e820_entries);
11c2fd3e 1011
089da572 1012 fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg));
11c2fd3e
AL
1013 /* allocate memory for the NUMA channel: one (64bit) word for the number
1014 * of nodes, one word for each VCPU->node and one word for each node to
1015 * hold the amount of memory.
1016 */
ebde2465 1017 numa_fw_cfg = g_new0(uint64_t, 1 + pcms->apic_id_limit + nb_numa_nodes);
11c2fd3e 1018 numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
ea265072
IM
1019 cpus = mc->possible_cpu_arch_ids(MACHINE(pcms));
1020 for (i = 0; i < cpus->len; i++) {
1021 unsigned int apic_id = cpus->cpus[i].arch_id;
ebde2465 1022 assert(apic_id < pcms->apic_id_limit);
d41f3e75 1023 numa_fw_cfg[apic_id + 1] = cpu_to_le64(cpus->cpus[i].props.node_id);
11c2fd3e
AL
1024 }
1025 for (i = 0; i < nb_numa_nodes; i++) {
ebde2465
IM
1026 numa_fw_cfg[pcms->apic_id_limit + 1 + i] =
1027 cpu_to_le64(numa_info[i].node_mem);
11c2fd3e 1028 }
089da572 1029 fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
ebde2465 1030 (1 + pcms->apic_id_limit + nb_numa_nodes) *
1d934e89 1031 sizeof(*numa_fw_cfg));
bf483392
AG
1032
1033 return fw_cfg;
80cabfad
FB
1034}
1035
642a4f96
TS
1036static long get_file_size(FILE *f)
1037{
1038 long where, size;
1039
1040 /* XXX: on Unix systems, using fstat() probably makes more sense */
1041
1042 where = ftell(f);
1043 fseek(f, 0, SEEK_END);
1044 size = ftell(f);
1045 fseek(f, where, SEEK_SET);
1046
1047 return size;
1048}
1049
3cbeb524
AB
1050/* setup_data types */
1051#define SETUP_NONE 0
1052#define SETUP_E820_EXT 1
1053#define SETUP_DTB 2
1054#define SETUP_PCI 3
1055#define SETUP_EFI 4
1056
1057struct setup_data {
1058 uint64_t next;
1059 uint32_t type;
1060 uint32_t len;
1061 uint8_t data[0];
1062} __attribute__((packed));
1063
df1f79fd
EH
1064static void load_linux(PCMachineState *pcms,
1065 FWCfgState *fw_cfg)
642a4f96
TS
1066{
1067 uint16_t protocol;
f3839fda 1068 int setup_size, kernel_size, cmdline_size;
3cbeb524 1069 int dtb_size, setup_data_offset;
642a4f96 1070 uint32_t initrd_max;
c24323dd 1071 uint8_t header[8192], *setup, *kernel;
a8170e5e 1072 hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
45a50b16 1073 FILE *f;
bf4e5d92 1074 char *vmode;
df1f79fd 1075 MachineState *machine = MACHINE(pcms);
cd4040ec 1076 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
3cbeb524 1077 struct setup_data *setup_data;
df1f79fd
EH
1078 const char *kernel_filename = machine->kernel_filename;
1079 const char *initrd_filename = machine->initrd_filename;
3cbeb524 1080 const char *dtb_filename = machine->dtb;
df1f79fd 1081 const char *kernel_cmdline = machine->kernel_cmdline;
642a4f96
TS
1082
1083 /* Align to 16 bytes as a paranoia measure */
1084 cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
1085
1086 /* load the kernel header */
1087 f = fopen(kernel_filename, "rb");
1088 if (!f || !(kernel_size = get_file_size(f)) ||
0f9d76e5
LG
1089 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
1090 MIN(ARRAY_SIZE(header), kernel_size)) {
1091 fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
1092 kernel_filename, strerror(errno));
1093 exit(1);
642a4f96
TS
1094 }
1095
1096 /* kernel protocol version */
bc4edd79 1097#if 0
642a4f96 1098 fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
bc4edd79 1099#endif
0f9d76e5
LG
1100 if (ldl_p(header+0x202) == 0x53726448) {
1101 protocol = lduw_p(header+0x206);
1102 } else {
1103 /* This looks like a multiboot kernel. If it is, let's stop
1104 treating it like a Linux kernel. */
52001445 1105 if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
0f9d76e5 1106 kernel_cmdline, kernel_size, header)) {
82663ee2 1107 return;
0f9d76e5
LG
1108 }
1109 protocol = 0;
f16408df 1110 }
642a4f96
TS
1111
1112 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
0f9d76e5
LG
1113 /* Low kernel */
1114 real_addr = 0x90000;
1115 cmdline_addr = 0x9a000 - cmdline_size;
1116 prot_addr = 0x10000;
642a4f96 1117 } else if (protocol < 0x202) {
0f9d76e5
LG
1118 /* High but ancient kernel */
1119 real_addr = 0x90000;
1120 cmdline_addr = 0x9a000 - cmdline_size;
1121 prot_addr = 0x100000;
642a4f96 1122 } else {
0f9d76e5
LG
1123 /* High and recent kernel */
1124 real_addr = 0x10000;
1125 cmdline_addr = 0x20000;
1126 prot_addr = 0x100000;
642a4f96
TS
1127 }
1128
bc4edd79 1129#if 0
642a4f96 1130 fprintf(stderr,
0f9d76e5
LG
1131 "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
1132 "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
1133 "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
1134 real_addr,
1135 cmdline_addr,
1136 prot_addr);
bc4edd79 1137#endif
642a4f96
TS
1138
1139 /* highest address for loading the initrd */
0f9d76e5
LG
1140 if (protocol >= 0x203) {
1141 initrd_max = ldl_p(header+0x22c);
1142 } else {
1143 initrd_max = 0x37ffffff;
1144 }
642a4f96 1145
cd4040ec
EH
1146 if (initrd_max >= pcms->below_4g_mem_size - pcmc->acpi_data_size) {
1147 initrd_max = pcms->below_4g_mem_size - pcmc->acpi_data_size - 1;
927766c7 1148 }
642a4f96 1149
57a46d05
AG
1150 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
1151 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
96f80586 1152 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
642a4f96
TS
1153
1154 if (protocol >= 0x202) {
0f9d76e5 1155 stl_p(header+0x228, cmdline_addr);
642a4f96 1156 } else {
0f9d76e5
LG
1157 stw_p(header+0x20, 0xA33F);
1158 stw_p(header+0x22, cmdline_addr-real_addr);
642a4f96
TS
1159 }
1160
bf4e5d92
PT
1161 /* handle vga= parameter */
1162 vmode = strstr(kernel_cmdline, "vga=");
1163 if (vmode) {
1164 unsigned int video_mode;
1165 /* skip "vga=" */
1166 vmode += 4;
1167 if (!strncmp(vmode, "normal", 6)) {
1168 video_mode = 0xffff;
1169 } else if (!strncmp(vmode, "ext", 3)) {
1170 video_mode = 0xfffe;
1171 } else if (!strncmp(vmode, "ask", 3)) {
1172 video_mode = 0xfffd;
1173 } else {
1174 video_mode = strtol(vmode, NULL, 0);
1175 }
1176 stw_p(header+0x1fa, video_mode);
1177 }
1178
642a4f96 1179 /* loader type */
5cbdb3a3 1180 /* High nybble = B reserved for QEMU; low nybble is revision number.
642a4f96
TS
1181 If this code is substantially changed, you may want to consider
1182 incrementing the revision. */
0f9d76e5
LG
1183 if (protocol >= 0x200) {
1184 header[0x210] = 0xB0;
1185 }
642a4f96
TS
1186 /* heap */
1187 if (protocol >= 0x201) {
0f9d76e5
LG
1188 header[0x211] |= 0x80; /* CAN_USE_HEAP */
1189 stw_p(header+0x224, cmdline_addr-real_addr-0x200);
642a4f96
TS
1190 }
1191
1192 /* load initrd */
1193 if (initrd_filename) {
c24323dd
PM
1194 gsize initrd_size;
1195 gchar *initrd_data;
1196 GError *gerr = NULL;
1197
0f9d76e5
LG
1198 if (protocol < 0x200) {
1199 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
1200 exit(1);
1201 }
642a4f96 1202
c24323dd
PM
1203 if (!g_file_get_contents(initrd_filename, &initrd_data,
1204 &initrd_size, &gerr)) {
7454e51d 1205 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
c24323dd 1206 initrd_filename, gerr->message);
d6fa4b77 1207 exit(1);
c24323dd
PM
1208 }
1209 if (initrd_size >= initrd_max) {
f3839fda 1210 fprintf(stderr, "qemu: initrd is too large, cannot support."
c24323dd
PM
1211 "(max: %"PRIu32", need %"PRId64")\n",
1212 initrd_max, (uint64_t)initrd_size);
f3839fda 1213 exit(1);
d6fa4b77
MK
1214 }
1215
45a50b16 1216 initrd_addr = (initrd_max-initrd_size) & ~4095;
57a46d05 1217
57a46d05
AG
1218 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
1219 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
1220 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
642a4f96 1221
0f9d76e5
LG
1222 stl_p(header+0x218, initrd_addr);
1223 stl_p(header+0x21c, initrd_size);
642a4f96
TS
1224 }
1225
45a50b16 1226 /* load kernel and setup */
642a4f96 1227 setup_size = header[0x1f1];
0f9d76e5
LG
1228 if (setup_size == 0) {
1229 setup_size = 4;
1230 }
642a4f96 1231 setup_size = (setup_size+1)*512;
ec5fd402
PB
1232 if (setup_size > kernel_size) {
1233 fprintf(stderr, "qemu: invalid kernel header\n");
1234 exit(1);
1235 }
45a50b16 1236 kernel_size -= setup_size;
642a4f96 1237
7267c094
AL
1238 setup = g_malloc(setup_size);
1239 kernel = g_malloc(kernel_size);
45a50b16 1240 fseek(f, 0, SEEK_SET);
5a41ecc5
KS
1241 if (fread(setup, 1, setup_size, f) != setup_size) {
1242 fprintf(stderr, "fread() failed\n");
1243 exit(1);
1244 }
1245 if (fread(kernel, 1, kernel_size, f) != kernel_size) {
1246 fprintf(stderr, "fread() failed\n");
1247 exit(1);
1248 }
642a4f96 1249 fclose(f);
3cbeb524
AB
1250
1251 /* append dtb to kernel */
1252 if (dtb_filename) {
1253 if (protocol < 0x209) {
1254 fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
1255 exit(1);
1256 }
1257
1258 dtb_size = get_image_size(dtb_filename);
1259 if (dtb_size <= 0) {
1260 fprintf(stderr, "qemu: error reading dtb %s: %s\n",
1261 dtb_filename, strerror(errno));
1262 exit(1);
1263 }
1264
1265 setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
1266 kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
1267 kernel = g_realloc(kernel, kernel_size);
1268
1269 stq_p(header+0x250, prot_addr + setup_data_offset);
1270
1271 setup_data = (struct setup_data *)(kernel + setup_data_offset);
1272 setup_data->next = 0;
1273 setup_data->type = cpu_to_le32(SETUP_DTB);
1274 setup_data->len = cpu_to_le32(dtb_size);
1275
1276 load_image_size(dtb_filename, setup_data->data, dtb_size);
1277 }
1278
45a50b16 1279 memcpy(setup, header, MIN(sizeof(header), setup_size));
57a46d05
AG
1280
1281 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
1282 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
1283 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
1284
1285 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
1286 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
1287 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
1288
98e753a6
IM
1289 option_rom[nb_option_roms].bootindex = 0;
1290 option_rom[nb_option_roms].name = "linuxboot.bin";
1291 if (pcmc->linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) {
b2a575a1 1292 option_rom[nb_option_roms].name = "linuxboot_dma.bin";
b2a575a1 1293 }
57a46d05 1294 nb_option_roms++;
642a4f96
TS
1295}
1296
b41a2cd1
FB
1297#define NE2000_NB_MAX 6
1298
675d6f82
BS
1299static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
1300 0x280, 0x380 };
1301static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
b41a2cd1 1302
48a18b3c 1303void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
a41b2ff2
PB
1304{
1305 static int nb_ne2k = 0;
1306
1307 if (nb_ne2k == NE2000_NB_MAX)
1308 return;
48a18b3c 1309 isa_ne2000_init(bus, ne2000_io[nb_ne2k],
9453c5bc 1310 ne2000_irq[nb_ne2k], nd);
a41b2ff2
PB
1311 nb_ne2k++;
1312}
1313
92a16d7a 1314DeviceState *cpu_get_current_apic(void)
0e26b7b8 1315{
4917cf44
AF
1316 if (current_cpu) {
1317 X86CPU *cpu = X86_CPU(current_cpu);
02e51483 1318 return cpu->apic_state;
0e26b7b8
BS
1319 } else {
1320 return NULL;
1321 }
1322}
1323
845773ab 1324void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
53b67b30 1325{
c3affe56 1326 X86CPU *cpu = opaque;
53b67b30
BS
1327
1328 if (level) {
c3affe56 1329 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
53b67b30
BS
1330 }
1331}
1332
074281d6 1333static void pc_new_cpu(const char *typename, int64_t apic_id, Error **errp)
31050930 1334{
074281d6 1335 Object *cpu = NULL;
31050930
IM
1336 Error *local_err = NULL;
1337
074281d6 1338 cpu = object_new(typename);
31050930 1339
c7b4efb4 1340 object_property_set_uint(cpu, apic_id, "apic-id", &local_err);
074281d6 1341 object_property_set_bool(cpu, true, "realized", &local_err);
31050930 1342
074281d6 1343 object_unref(cpu);
021c9d25 1344 error_propagate(errp, local_err);
31050930
IM
1345}
1346
c649983b
IM
1347void pc_hot_add_cpu(const int64_t id, Error **errp)
1348{
38690a1c 1349 MachineState *ms = MACHINE(qdev_get_machine());
c649983b 1350 int64_t apic_id = x86_cpu_apic_id_from_index(id);
0e3bd562 1351 Error *local_err = NULL;
c649983b 1352
8de433cb
IM
1353 if (id < 0) {
1354 error_setg(errp, "Invalid CPU id: %" PRIi64, id);
1355 return;
1356 }
1357
5ff020b7
EH
1358 if (apic_id >= ACPI_CPU_HOTPLUG_ID_LIMIT) {
1359 error_setg(errp, "Unable to add CPU: %" PRIi64
1360 ", resulting APIC ID (%" PRIi64 ") is too large",
1361 id, apic_id);
1362 return;
1363 }
1364
311ca98d 1365 pc_new_cpu(ms->cpu_type, apic_id, &local_err);
0e3bd562
AF
1366 if (local_err) {
1367 error_propagate(errp, local_err);
1368 return;
1369 }
c649983b
IM
1370}
1371
4884b7bf 1372void pc_cpus_init(PCMachineState *pcms)
70166477
IY
1373{
1374 int i;
c96a1c0b 1375 const CPUArchIdList *possible_cpus;
311ca98d 1376 MachineState *ms = MACHINE(pcms);
c96a1c0b 1377 MachineClass *mc = MACHINE_GET_CLASS(pcms);
70166477 1378
ebde2465
IM
1379 /* Calculates the limit to CPU APIC ID values
1380 *
1381 * Limit for the APIC ID value, so that all
1382 * CPU APIC IDs are < pcms->apic_id_limit.
1383 *
1384 * This is used for FW_CFG_MAX_CPUS. See comments on bochs_bios_init().
1385 */
1386 pcms->apic_id_limit = x86_cpu_apic_id_from_index(max_cpus - 1) + 1;
311ca98d 1387 possible_cpus = mc->possible_cpu_arch_ids(ms);
c96a1c0b 1388 for (i = 0; i < smp_cpus; i++) {
d342eb76
IM
1389 pc_new_cpu(possible_cpus->cpus[i].type, possible_cpus->cpus[i].arch_id,
1390 &error_fatal);
70166477
IY
1391 }
1392}
1393
217f1b4a
HZ
1394static void pc_build_feature_control_file(PCMachineState *pcms)
1395{
38690a1c
IM
1396 MachineState *ms = MACHINE(pcms);
1397 X86CPU *cpu = X86_CPU(ms->possible_cpus->cpus[0].cpu);
217f1b4a
HZ
1398 CPUX86State *env = &cpu->env;
1399 uint32_t unused, ecx, edx;
1400 uint64_t feature_control_bits = 0;
1401 uint64_t *val;
1402
1403 cpu_x86_cpuid(env, 1, 0, &unused, &unused, &ecx, &edx);
1404 if (ecx & CPUID_EXT_VMX) {
1405 feature_control_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1406 }
1407
1408 if ((edx & (CPUID_EXT2_MCE | CPUID_EXT2_MCA)) ==
1409 (CPUID_EXT2_MCE | CPUID_EXT2_MCA) &&
1410 (env->mcg_cap & MCG_LMCE_P)) {
1411 feature_control_bits |= FEATURE_CONTROL_LMCE;
1412 }
1413
1414 if (!feature_control_bits) {
1415 return;
1416 }
1417
1418 val = g_malloc(sizeof(*val));
1419 *val = cpu_to_le64(feature_control_bits | FEATURE_CONTROL_LOCKED);
1420 fw_cfg_add_file(pcms->fw_cfg, "etc/msr_feature_control", val, sizeof(*val));
1421}
1422
e3cadac0
IM
1423static void rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
1424{
1425 if (cpus_count > 0xff) {
1426 /* If the number of CPUs can't be represented in 8 bits, the
1427 * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
1428 * to make old BIOSes fail more predictably.
1429 */
1430 rtc_set_memory(rtc, 0x5f, 0);
1431 } else {
1432 rtc_set_memory(rtc, 0x5f, cpus_count - 1);
1433 }
1434}
1435
3459a625 1436static
9ebeed0c 1437void pc_machine_done(Notifier *notifier, void *data)
3459a625 1438{
9ebeed0c
EH
1439 PCMachineState *pcms = container_of(notifier,
1440 PCMachineState, machine_done);
1441 PCIBus *bus = pcms->bus;
2118196b 1442
ba157b69 1443 /* set the number of CPUs */
e3cadac0 1444 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
ba157b69 1445
2118196b
MA
1446 if (bus) {
1447 int extra_hosts = 0;
1448
1449 QLIST_FOREACH(bus, &bus->child, sibling) {
1450 /* look for expander root buses */
1451 if (pci_bus_is_root(bus)) {
1452 extra_hosts++;
1453 }
1454 }
f264d360 1455 if (extra_hosts && pcms->fw_cfg) {
2118196b
MA
1456 uint64_t *val = g_malloc(sizeof(*val));
1457 *val = cpu_to_le64(extra_hosts);
f264d360 1458 fw_cfg_add_file(pcms->fw_cfg,
2118196b
MA
1459 "etc/extra-pci-roots", val, sizeof(*val));
1460 }
1461 }
1462
bb292f5a 1463 acpi_setup();
6d42eefa 1464 if (pcms->fw_cfg) {
f2098f48 1465 pc_build_smbios(pcms);
217f1b4a 1466 pc_build_feature_control_file(pcms);
e3cadac0
IM
1467 /* update FW_CFG_NB_CPUS to account for -device added CPUs */
1468 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
6d42eefa 1469 }
60c5e104 1470
1a26f466 1471 if (pcms->apic_id_limit > 255 && !xen_enabled()) {
60c5e104
IM
1472 IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
1473
a924b3d8 1474 if (!iommu || !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu)) ||
60c5e104
IM
1475 iommu->intr_eim != ON_OFF_AUTO_ON) {
1476 error_report("current -smp configuration requires "
1477 "Extended Interrupt Mode enabled. "
1478 "You can add an IOMMU using: "
1479 "-device intel-iommu,intremap=on,eim=on");
1480 exit(EXIT_FAILURE);
1481 }
1482 }
3459a625
MT
1483}
1484
e4e8ba04 1485void pc_guest_info_init(PCMachineState *pcms)
3459a625 1486{
1f3aba37 1487 int i;
b20c9bd5 1488
dd4c2f01
EH
1489 pcms->apic_xrupt_override = kvm_allows_irq0_override();
1490 pcms->numa_nodes = nb_numa_nodes;
1491 pcms->node_mem = g_malloc0(pcms->numa_nodes *
1492 sizeof *pcms->node_mem);
8c85901e 1493 for (i = 0; i < nb_numa_nodes; i++) {
dd4c2f01 1494 pcms->node_mem[i] = numa_info[i].node_mem;
8c85901e
WG
1495 }
1496
9ebeed0c
EH
1497 pcms->machine_done.notify = pc_machine_done;
1498 qemu_add_machine_init_done_notifier(&pcms->machine_done);
3459a625
MT
1499}
1500
83d08f26
MT
1501/* setup pci memory address space mapping into system address space */
1502void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
1503 MemoryRegion *pci_address_space)
39848901 1504{
83d08f26
MT
1505 /* Set to lower priority than RAM */
1506 memory_region_add_subregion_overlap(system_memory, 0x0,
1507 pci_address_space, -1);
39848901
IM
1508}
1509
f7e4dd6c
GH
1510void pc_acpi_init(const char *default_dsdt)
1511{
c5a98cf3 1512 char *filename;
f7e4dd6c
GH
1513
1514 if (acpi_tables != NULL) {
1515 /* manually set via -acpitable, leave it alone */
1516 return;
1517 }
1518
1519 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, default_dsdt);
1520 if (filename == NULL) {
2ab4b135 1521 warn_report("failed to find %s", default_dsdt);
c5a98cf3 1522 } else {
5bdb59a2
MA
1523 QemuOpts *opts = qemu_opts_create(qemu_find_opts("acpi"), NULL, 0,
1524 &error_abort);
c5a98cf3 1525 Error *err = NULL;
f7e4dd6c 1526
5bdb59a2 1527 qemu_opt_set(opts, "file", filename, &error_abort);
0c764a9d 1528
1a4b2666 1529 acpi_table_add_builtin(opts, &err);
c5a98cf3 1530 if (err) {
88f83f35 1531 warn_reportf_err(err, "failed to load %s: ", filename);
c5a98cf3 1532 }
c5a98cf3 1533 g_free(filename);
f7e4dd6c 1534 }
f7e4dd6c
GH
1535}
1536
7bc35e0f 1537void xen_load_linux(PCMachineState *pcms)
b33a5bbf
CL
1538{
1539 int i;
1540 FWCfgState *fw_cfg;
1541
df1f79fd 1542 assert(MACHINE(pcms)->kernel_filename != NULL);
b33a5bbf 1543
305ae888 1544 fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
e3cadac0 1545 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
b33a5bbf
CL
1546 rom_set_fw(fw_cfg);
1547
df1f79fd 1548 load_linux(pcms, fw_cfg);
b33a5bbf
CL
1549 for (i = 0; i < nb_option_roms; i++) {
1550 assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
b2a575a1 1551 !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
b33a5bbf
CL
1552 !strcmp(option_rom[i].name, "multiboot.bin"));
1553 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1554 }
f264d360 1555 pcms->fw_cfg = fw_cfg;
b33a5bbf
CL
1556}
1557
5934e216
EH
1558void pc_memory_init(PCMachineState *pcms,
1559 MemoryRegion *system_memory,
1560 MemoryRegion *rom_memory,
1561 MemoryRegion **ram_memory)
80cabfad 1562{
cbc5b5f3
JJ
1563 int linux_boot, i;
1564 MemoryRegion *ram, *option_rom_mr;
00cb2a99 1565 MemoryRegion *ram_below_4g, *ram_above_4g;
a88b362c 1566 FWCfgState *fw_cfg;
62b160c0 1567 MachineState *machine = MACHINE(pcms);
16a9e8a5 1568 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
d592d303 1569
c8d163bc
EH
1570 assert(machine->ram_size == pcms->below_4g_mem_size +
1571 pcms->above_4g_mem_size);
9521d42b
PB
1572
1573 linux_boot = (machine->kernel_filename != NULL);
80cabfad 1574
00cb2a99 1575 /* Allocate RAM. We allocate it as a single memory region and use
66a0a2cb 1576 * aliases to address portions of it, mostly for backwards compatibility
00cb2a99
AK
1577 * with older qemus that used qemu_ram_alloc().
1578 */
7267c094 1579 ram = g_malloc(sizeof(*ram));
9521d42b
PB
1580 memory_region_allocate_system_memory(ram, NULL, "pc.ram",
1581 machine->ram_size);
ae0a5466 1582 *ram_memory = ram;
7267c094 1583 ram_below_4g = g_malloc(sizeof(*ram_below_4g));
2c9b15ca 1584 memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
c8d163bc 1585 0, pcms->below_4g_mem_size);
00cb2a99 1586 memory_region_add_subregion(system_memory, 0, ram_below_4g);
c8d163bc
EH
1587 e820_add_entry(0, pcms->below_4g_mem_size, E820_RAM);
1588 if (pcms->above_4g_mem_size > 0) {
7267c094 1589 ram_above_4g = g_malloc(sizeof(*ram_above_4g));
2c9b15ca 1590 memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
c8d163bc
EH
1591 pcms->below_4g_mem_size,
1592 pcms->above_4g_mem_size);
00cb2a99
AK
1593 memory_region_add_subregion(system_memory, 0x100000000ULL,
1594 ram_above_4g);
c8d163bc 1595 e820_add_entry(0x100000000ULL, pcms->above_4g_mem_size, E820_RAM);
bbe80adf 1596 }
82b36dc3 1597
bb292f5a 1598 if (!pcmc->has_reserved_memory &&
ca8336f3 1599 (machine->ram_slots ||
9521d42b 1600 (machine->maxram_size > machine->ram_size))) {
ca8336f3
IM
1601 MachineClass *mc = MACHINE_GET_CLASS(machine);
1602
1603 error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
1604 mc->name);
1605 exit(EXIT_FAILURE);
1606 }
1607
b0c14ec4
DH
1608 /* always allocate the device memory information */
1609 machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
1610
f2ffbe2b 1611 /* initialize device memory address space */
bb292f5a 1612 if (pcmc->has_reserved_memory &&
9521d42b 1613 (machine->ram_size < machine->maxram_size)) {
f2ffbe2b 1614 ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
619d11e4 1615
a0cc8856
IM
1616 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
1617 error_report("unsupported amount of memory slots: %"PRIu64,
1618 machine->ram_slots);
1619 exit(EXIT_FAILURE);
1620 }
1621
f2c38522
PK
1622 if (QEMU_ALIGN_UP(machine->maxram_size,
1623 TARGET_PAGE_SIZE) != machine->maxram_size) {
1624 error_report("maximum memory size must by aligned to multiple of "
1625 "%d bytes", TARGET_PAGE_SIZE);
1626 exit(EXIT_FAILURE);
1627 }
1628
b0c14ec4 1629 machine->device_memory->base =
d471bf3e 1630 ROUND_UP(0x100000000ULL + pcms->above_4g_mem_size, 1 * GiB);
619d11e4 1631
16a9e8a5 1632 if (pcmc->enforce_aligned_dimm) {
f2ffbe2b 1633 /* size device region assuming 1G page max alignment per slot */
d471bf3e 1634 device_mem_size += (1 * GiB) * machine->ram_slots;
085f8e88
IM
1635 }
1636
f2ffbe2b
DH
1637 if ((machine->device_memory->base + device_mem_size) <
1638 device_mem_size) {
619d11e4
IM
1639 error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
1640 machine->maxram_size);
1641 exit(EXIT_FAILURE);
1642 }
1643
b0c14ec4 1644 memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
f2ffbe2b 1645 "device-memory", device_mem_size);
b0c14ec4
DH
1646 memory_region_add_subregion(system_memory, machine->device_memory->base,
1647 &machine->device_memory->mr);
619d11e4 1648 }
cbc5b5f3
JJ
1649
1650 /* Initialize PC system firmware */
5db3f0de 1651 pc_system_firmware_init(rom_memory, !pcmc->pci_enabled);
00cb2a99 1652
7267c094 1653 option_rom_mr = g_malloc(sizeof(*option_rom_mr));
98a99ce0 1654 memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
f8ed85ac 1655 &error_fatal);
208fa0e4
IM
1656 if (pcmc->pci_enabled) {
1657 memory_region_set_readonly(option_rom_mr, true);
1658 }
4463aee6 1659 memory_region_add_subregion_overlap(rom_memory,
00cb2a99
AK
1660 PC_ROM_MIN_VGA,
1661 option_rom_mr,
1662 1);
f753ff16 1663
ebde2465 1664 fw_cfg = bochs_bios_init(&address_space_memory, pcms);
c886fc4c 1665
8832cb80 1666 rom_set_fw(fw_cfg);
1d108d97 1667
b0c14ec4 1668 if (pcmc->has_reserved_memory && machine->device_memory->base) {
de268e13 1669 uint64_t *val = g_malloc(sizeof(*val));
2f8b5008 1670 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
b0c14ec4 1671 uint64_t res_mem_end = machine->device_memory->base;
2f8b5008
IM
1672
1673 if (!pcmc->broken_reserved_end) {
b0c14ec4 1674 res_mem_end += memory_region_size(&machine->device_memory->mr);
2f8b5008 1675 }
d471bf3e 1676 *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
de268e13
IM
1677 fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
1678 }
1679
f753ff16 1680 if (linux_boot) {
df1f79fd 1681 load_linux(pcms, fw_cfg);
f753ff16
PB
1682 }
1683
1684 for (i = 0; i < nb_option_roms; i++) {
2e55e842 1685 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
406c8df3 1686 }
f264d360 1687 pcms->fw_cfg = fw_cfg;
cb135f59
PX
1688
1689 /* Init default IOAPIC address space */
1690 pcms->ioapic_as = &address_space_memory;
3d53f5c3
IY
1691}
1692
9fa99d25
MA
1693/*
1694 * The 64bit pci hole starts after "above 4G RAM" and
1695 * potentially the space reserved for memory hotplug.
1696 */
1697uint64_t pc_pci_hole64_start(void)
1698{
1699 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1700 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
b0c14ec4 1701 MachineState *ms = MACHINE(pcms);
9fa99d25
MA
1702 uint64_t hole64_start = 0;
1703
b0c14ec4
DH
1704 if (pcmc->has_reserved_memory && ms->device_memory->base) {
1705 hole64_start = ms->device_memory->base;
9fa99d25 1706 if (!pcmc->broken_reserved_end) {
b0c14ec4 1707 hole64_start += memory_region_size(&ms->device_memory->mr);
9fa99d25
MA
1708 }
1709 } else {
1710 hole64_start = 0x100000000ULL + pcms->above_4g_mem_size;
1711 }
1712
d471bf3e 1713 return ROUND_UP(hole64_start, 1 * GiB);
9fa99d25
MA
1714}
1715
0b0cc076 1716qemu_irq pc_allocate_cpu_irq(void)
845773ab 1717{
0b0cc076 1718 return qemu_allocate_irq(pic_irq_request, NULL, 0);
845773ab
IY
1719}
1720
48a18b3c 1721DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
765d7908 1722{
ad6d45fa
AL
1723 DeviceState *dev = NULL;
1724
bab47d9a 1725 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
16094b75
AJ
1726 if (pci_bus) {
1727 PCIDevice *pcidev = pci_vga_init(pci_bus);
1728 dev = pcidev ? &pcidev->qdev : NULL;
1729 } else if (isa_bus) {
1730 ISADevice *isadev = isa_vga_init(isa_bus);
4a17cc4f 1731 dev = isadev ? DEVICE(isadev) : NULL;
765d7908 1732 }
bab47d9a 1733 rom_reset_order_override();
ad6d45fa 1734 return dev;
765d7908
IY
1735}
1736
258711c6
JG
1737static const MemoryRegionOps ioport80_io_ops = {
1738 .write = ioport80_write,
c02e1eac 1739 .read = ioport80_read,
258711c6
JG
1740 .endianness = DEVICE_NATIVE_ENDIAN,
1741 .impl = {
1742 .min_access_size = 1,
1743 .max_access_size = 1,
1744 },
1745};
1746
1747static const MemoryRegionOps ioportF0_io_ops = {
1748 .write = ioportF0_write,
c02e1eac 1749 .read = ioportF0_read,
258711c6
JG
1750 .endianness = DEVICE_NATIVE_ENDIAN,
1751 .impl = {
1752 .min_access_size = 1,
1753 .max_access_size = 1,
1754 },
1755};
1756
ac64273c
PMD
1757static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport)
1758{
1759 int i;
1760 DriveInfo *fd[MAX_FD];
1761 qemu_irq *a20_line;
1762 ISADevice *i8042, *port92, *vmmouse;
1763
def337ff 1764 serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
ac64273c
PMD
1765 parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
1766
1767 for (i = 0; i < MAX_FD; i++) {
1768 fd[i] = drive_get(IF_FLOPPY, 0, i);
1769 create_fdctrl |= !!fd[i];
1770 }
1771 if (create_fdctrl) {
1772 fdctrl_init_isa(isa_bus, fd);
1773 }
1774
1775 i8042 = isa_create_simple(isa_bus, "i8042");
1776 if (!no_vmport) {
1777 vmport_init(isa_bus);
1778 vmmouse = isa_try_create(isa_bus, "vmmouse");
1779 } else {
1780 vmmouse = NULL;
1781 }
1782 if (vmmouse) {
1783 DeviceState *dev = DEVICE(vmmouse);
1784 qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
1785 qdev_init_nofail(dev);
1786 }
1787 port92 = isa_create_simple(isa_bus, "port92");
1788
1789 a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1790 i8042_setup_a20_line(i8042, a20_line[0]);
1791 port92_init(port92, a20_line[1]);
1792 g_free(a20_line);
1793}
1794
48a18b3c 1795void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
1611977c 1796 ISADevice **rtc_state,
fd53c87c 1797 bool create_fdctrl,
7a10ef51 1798 bool no_vmport,
feddd2fd 1799 bool has_pit,
3a87d009 1800 uint32_t hpet_irqs)
ffe513da
IY
1801{
1802 int i;
ce967e2f
JK
1803 DeviceState *hpet = NULL;
1804 int pit_isa_irq = 0;
1805 qemu_irq pit_alt_irq = NULL;
7d932dfd 1806 qemu_irq rtc_irq = NULL;
ac64273c 1807 ISADevice *pit = NULL;
258711c6
JG
1808 MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1809 MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
ffe513da 1810
2c9b15ca 1811 memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
258711c6 1812 memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
ffe513da 1813
2c9b15ca 1814 memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
258711c6 1815 memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
ffe513da 1816
5d17c0d2
JK
1817 /*
1818 * Check if an HPET shall be created.
1819 *
1820 * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1821 * when the HPET wants to take over. Thus we have to disable the latter.
1822 */
1823 if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
7a10ef51 1824 /* In order to set property, here not using sysbus_try_create_simple */
51116102 1825 hpet = qdev_try_create(NULL, TYPE_HPET);
dd703b99 1826 if (hpet) {
7a10ef51
LPF
1827 /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
1828 * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
1829 * IRQ8 and IRQ2.
1830 */
5d7fb0f2 1831 uint8_t compat = object_property_get_uint(OBJECT(hpet),
7a10ef51
LPF
1832 HPET_INTCAP, NULL);
1833 if (!compat) {
1834 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
1835 }
1836 qdev_init_nofail(hpet);
1837 sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
1838
b881fbe9 1839 for (i = 0; i < GSI_NUM_PINS; i++) {
1356b98d 1840 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
dd703b99 1841 }
ce967e2f
JK
1842 pit_isa_irq = -1;
1843 pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1844 rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
822557eb 1845 }
ffe513da 1846 }
6c646a11 1847 *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
7d932dfd
JK
1848
1849 qemu_register_boot_set(pc_boot_set, *rtc_state);
1850
feddd2fd 1851 if (!xen_enabled() && has_pit) {
15eafc2e 1852 if (kvm_pit_in_kernel()) {
c2d8d311
SS
1853 pit = kvm_pit_init(isa_bus, 0x40);
1854 } else {
acf695ec 1855 pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
c2d8d311
SS
1856 }
1857 if (hpet) {
1858 /* connect PIT to output control line of the HPET */
4a17cc4f 1859 qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
c2d8d311
SS
1860 }
1861 pcspk_init(isa_bus, pit);
ce967e2f 1862 }
ffe513da 1863
55f613ac 1864 i8257_dma_init(isa_bus, 0);
ffe513da 1865
ac64273c
PMD
1866 /* Super I/O */
1867 pc_superio_init(isa_bus, create_fdctrl, no_vmport);
ffe513da
IY
1868}
1869
4b9c264b 1870void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
9011a1a7
IY
1871{
1872 int i;
1873
bab47d9a 1874 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
9011a1a7
IY
1875 for (i = 0; i < nb_nics; i++) {
1876 NICInfo *nd = &nd_table[i];
4b9c264b 1877 const char *model = nd->model ? nd->model : pcmc->default_nic_model;
9011a1a7 1878
4b9c264b 1879 if (g_str_equal(model, "ne2k_isa")) {
9011a1a7
IY
1880 pc_init_ne2k_isa(isa_bus, nd);
1881 } else {
4b9c264b 1882 pci_nic_init_nofail(nd, pci_bus, model, NULL);
9011a1a7
IY
1883 }
1884 }
bab47d9a 1885 rom_reset_order_override();
9011a1a7
IY
1886}
1887
a39e3564
JB
1888void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
1889{
1890 DeviceState *dev;
1891 SysBusDevice *d;
1892 unsigned int i;
1893
15eafc2e 1894 if (kvm_ioapic_in_kernel()) {
34bec7a8 1895 dev = qdev_create(NULL, TYPE_KVM_IOAPIC);
a39e3564 1896 } else {
34bec7a8 1897 dev = qdev_create(NULL, TYPE_IOAPIC);
a39e3564
JB
1898 }
1899 if (parent_name) {
1900 object_property_add_child(object_resolve_path(parent_name, NULL),
1901 "ioapic", OBJECT(dev), NULL);
1902 }
1903 qdev_init_nofail(dev);
1356b98d 1904 d = SYS_BUS_DEVICE(dev);
3a4a4697 1905 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
a39e3564
JB
1906
1907 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1908 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
1909 }
1910}
d5747cac 1911
d468115b
DH
1912static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
1913 Error **errp)
1914{
1915 const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
b0e62443 1916 const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
d468115b 1917 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
b0e62443 1918 const uint64_t legacy_align = TARGET_PAGE_SIZE;
d468115b
DH
1919
1920 /*
1921 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1922 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1923 * addition to cover this case.
1924 */
1925 if (!pcms->acpi_dev || !acpi_enabled) {
1926 error_setg(errp,
1927 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1928 return;
1929 }
1930
1931 if (is_nvdimm && !pcms->acpi_nvdimm_state.is_enabled) {
1932 error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
1933 return;
1934 }
8f1ffe5b 1935
fd3416f5 1936 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
b0e62443 1937 pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
d468115b
DH
1938}
1939
bb6e2f7a
DH
1940static void pc_memory_plug(HotplugHandler *hotplug_dev,
1941 DeviceState *dev, Error **errp)
95bee274 1942{
3fbcdc27 1943 HotplugHandlerClass *hhc;
95bee274
IM
1944 Error *local_err = NULL;
1945 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
7f3cf2d6 1946 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
95bee274 1947
fd3416f5 1948 pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms), &local_err);
43bbb49e 1949 if (local_err) {
b8865591
IM
1950 goto out;
1951 }
1952
7f3cf2d6 1953 if (is_nvdimm) {
284197e4 1954 nvdimm_plug(&pcms->acpi_nvdimm_state);
c7f8d0f3
XG
1955 }
1956
3fbcdc27 1957 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
8e23184b 1958 hhc->plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &error_abort);
95bee274
IM
1959out:
1960 error_propagate(errp, local_err);
1961}
1962
bb6e2f7a
DH
1963static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
1964 DeviceState *dev, Error **errp)
64fec58e
TC
1965{
1966 HotplugHandlerClass *hhc;
1967 Error *local_err = NULL;
1968 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1969
8cd91ace
HZ
1970 /*
1971 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1972 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1973 * addition to cover this case.
1974 */
1975 if (!pcms->acpi_dev || !acpi_enabled) {
64fec58e 1976 error_setg(&local_err,
8cd91ace 1977 "memory hotplug is not enabled: missing acpi device or acpi disabled");
64fec58e
TC
1978 goto out;
1979 }
1980
b097cc52
XG
1981 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
1982 error_setg(&local_err,
1983 "nvdimm device hot unplug is not supported yet.");
1984 goto out;
1985 }
1986
64fec58e
TC
1987 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
1988 hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1989
1990out:
1991 error_propagate(errp, local_err);
1992}
1993
bb6e2f7a
DH
1994static void pc_memory_unplug(HotplugHandler *hotplug_dev,
1995 DeviceState *dev, Error **errp)
f7d3e29d
TC
1996{
1997 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
f7d3e29d
TC
1998 HotplugHandlerClass *hhc;
1999 Error *local_err = NULL;
2000
2001 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
2002 hhc->unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2003
2004 if (local_err) {
2005 goto out;
2006 }
2007
fd3416f5 2008 pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
f7d3e29d
TC
2009 object_unparent(OBJECT(dev));
2010
2011 out:
2012 error_propagate(errp, local_err);
2013}
2014
3811ef14
IM
2015static int pc_apic_cmp(const void *a, const void *b)
2016{
2017 CPUArchId *apic_a = (CPUArchId *)a;
2018 CPUArchId *apic_b = (CPUArchId *)b;
2019
2020 return apic_a->arch_id - apic_b->arch_id;
2021}
2022
7baef5cf 2023/* returns pointer to CPUArchId descriptor that matches CPU's apic_id
38690a1c 2024 * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
b12227af 2025 * entry corresponding to CPU's apic_id returns NULL.
7baef5cf 2026 */
1ea69c0e 2027static CPUArchId *pc_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
7baef5cf 2028{
7baef5cf
IM
2029 CPUArchId apic_id, *found_cpu;
2030
1ea69c0e 2031 apic_id.arch_id = id;
38690a1c
IM
2032 found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
2033 ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
7baef5cf
IM
2034 pc_apic_cmp);
2035 if (found_cpu && idx) {
38690a1c 2036 *idx = found_cpu - ms->possible_cpus->cpus;
7baef5cf
IM
2037 }
2038 return found_cpu;
2039}
2040
5279569e
GZ
2041static void pc_cpu_plug(HotplugHandler *hotplug_dev,
2042 DeviceState *dev, Error **errp)
2043{
7baef5cf 2044 CPUArchId *found_cpu;
5279569e
GZ
2045 HotplugHandlerClass *hhc;
2046 Error *local_err = NULL;
1ea69c0e 2047 X86CPU *cpu = X86_CPU(dev);
5279569e
GZ
2048 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2049
a44a49db
IM
2050 if (pcms->acpi_dev) {
2051 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
2052 hhc->plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2053 if (local_err) {
2054 goto out;
2055 }
5279569e
GZ
2056 }
2057
e3cadac0
IM
2058 /* increment the number of CPUs */
2059 pcms->boot_cpus++;
26ef65be 2060 if (pcms->rtc) {
e3cadac0 2061 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
26ef65be
IM
2062 }
2063 if (pcms->fw_cfg) {
e3cadac0 2064 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
2d996150
GZ
2065 }
2066
1ea69c0e 2067 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
8aba3842 2068 found_cpu->cpu = OBJECT(dev);
5279569e
GZ
2069out:
2070 error_propagate(errp, local_err);
2071}
8872c25a
IM
2072static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
2073 DeviceState *dev, Error **errp)
2074{
73360e27 2075 int idx = -1;
8872c25a
IM
2076 HotplugHandlerClass *hhc;
2077 Error *local_err = NULL;
1ea69c0e 2078 X86CPU *cpu = X86_CPU(dev);
8872c25a
IM
2079 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2080
75ba2ddb
IM
2081 if (!pcms->acpi_dev) {
2082 error_setg(&local_err, "CPU hot unplug not supported without ACPI");
2083 goto out;
2084 }
2085
1ea69c0e 2086 pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
73360e27
IM
2087 assert(idx != -1);
2088 if (idx == 0) {
2089 error_setg(&local_err, "Boot CPU is unpluggable");
2090 goto out;
2091 }
2092
8872c25a
IM
2093 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
2094 hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2095
2096 if (local_err) {
2097 goto out;
2098 }
2099
2100 out:
2101 error_propagate(errp, local_err);
2102
2103}
2104
2105static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
2106 DeviceState *dev, Error **errp)
2107{
8fe6374e 2108 CPUArchId *found_cpu;
8872c25a
IM
2109 HotplugHandlerClass *hhc;
2110 Error *local_err = NULL;
1ea69c0e 2111 X86CPU *cpu = X86_CPU(dev);
8872c25a
IM
2112 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2113
2114 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
2115 hhc->unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2116
2117 if (local_err) {
2118 goto out;
2119 }
2120
1ea69c0e 2121 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
8fe6374e
IM
2122 found_cpu->cpu = NULL;
2123 object_unparent(OBJECT(dev));
8872c25a 2124
e3cadac0
IM
2125 /* decrement the number of CPUs */
2126 pcms->boot_cpus--;
2127 /* Update the number of CPUs in CMOS */
2128 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
2129 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
8872c25a
IM
2130 out:
2131 error_propagate(errp, local_err);
2132}
5279569e 2133
4ec60c76
IM
2134static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
2135 DeviceState *dev, Error **errp)
2136{
2137 int idx;
a15d2728 2138 CPUState *cs;
e8f7b83e 2139 CPUArchId *cpu_slot;
d89c2b8b 2140 X86CPUTopoInfo topo;
4ec60c76 2141 X86CPU *cpu = X86_CPU(dev);
6970c5ff 2142 MachineState *ms = MACHINE(hotplug_dev);
4ec60c76 2143 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
4ec60c76 2144
6970c5ff
IM
2145 if(!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
2146 error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
2147 ms->cpu_type);
2148 return;
2149 }
2150
e8f7b83e
IM
2151 /* if APIC ID is not set, set it based on socket/core/thread properties */
2152 if (cpu->apic_id == UNASSIGNED_APIC_ID) {
2153 int max_socket = (max_cpus - 1) / smp_threads / smp_cores;
2154
2155 if (cpu->socket_id < 0) {
2156 error_setg(errp, "CPU socket-id is not set");
2157 return;
2158 } else if (cpu->socket_id > max_socket) {
2159 error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
2160 cpu->socket_id, max_socket);
2161 return;
2162 }
2163 if (cpu->core_id < 0) {
2164 error_setg(errp, "CPU core-id is not set");
2165 return;
2166 } else if (cpu->core_id > (smp_cores - 1)) {
2167 error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
2168 cpu->core_id, smp_cores - 1);
2169 return;
2170 }
2171 if (cpu->thread_id < 0) {
2172 error_setg(errp, "CPU thread-id is not set");
2173 return;
2174 } else if (cpu->thread_id > (smp_threads - 1)) {
2175 error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
2176 cpu->thread_id, smp_threads - 1);
2177 return;
2178 }
2179
2180 topo.pkg_id = cpu->socket_id;
2181 topo.core_id = cpu->core_id;
2182 topo.smt_id = cpu->thread_id;
2183 cpu->apic_id = apicid_from_topo_ids(smp_cores, smp_threads, &topo);
2184 }
2185
1ea69c0e 2186 cpu_slot = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
4ec60c76 2187 if (!cpu_slot) {
38690a1c
IM
2188 MachineState *ms = MACHINE(pcms);
2189
e8f7b83e
IM
2190 x86_topo_ids_from_apicid(cpu->apic_id, smp_cores, smp_threads, &topo);
2191 error_setg(errp, "Invalid CPU [socket: %u, core: %u, thread: %u] with"
2192 " APIC ID %" PRIu32 ", valid index range 0:%d",
2193 topo.pkg_id, topo.core_id, topo.smt_id, cpu->apic_id,
38690a1c 2194 ms->possible_cpus->len - 1);
4ec60c76
IM
2195 return;
2196 }
2197
2198 if (cpu_slot->cpu) {
2199 error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
2200 idx, cpu->apic_id);
2201 return;
2202 }
d89c2b8b
IM
2203
2204 /* if 'address' properties socket-id/core-id/thread-id are not set, set them
c5514d0e 2205 * so that machine_query_hotpluggable_cpus would show correct values
d89c2b8b
IM
2206 */
2207 /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
2208 * once -smp refactoring is complete and there will be CPU private
2209 * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
2210 x86_topo_ids_from_apicid(cpu->apic_id, smp_cores, smp_threads, &topo);
2211 if (cpu->socket_id != -1 && cpu->socket_id != topo.pkg_id) {
2212 error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
2213 " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, topo.pkg_id);
2214 return;
2215 }
2216 cpu->socket_id = topo.pkg_id;
2217
2218 if (cpu->core_id != -1 && cpu->core_id != topo.core_id) {
2219 error_setg(errp, "property core-id: %u doesn't match set apic-id:"
2220 " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, topo.core_id);
2221 return;
2222 }
2223 cpu->core_id = topo.core_id;
2224
2225 if (cpu->thread_id != -1 && cpu->thread_id != topo.smt_id) {
2226 error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
2227 " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, topo.smt_id);
2228 return;
2229 }
2230 cpu->thread_id = topo.smt_id;
a15d2728 2231
e9688fab
RK
2232 if (cpu->hyperv_vpindex && !kvm_hv_vpindex_settable()) {
2233 error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
2234 return;
2235 }
2236
a15d2728
IM
2237 cs = CPU(cpu);
2238 cs->cpu_index = idx;
93b2a8cb 2239
a0ceb640 2240 numa_cpu_pre_plug(cpu_slot, dev, errp);
4ec60c76
IM
2241}
2242
2243static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
2244 DeviceState *dev, Error **errp)
2245{
d468115b
DH
2246 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2247 pc_memory_pre_plug(hotplug_dev, dev, errp);
2248 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
4ec60c76
IM
2249 pc_cpu_pre_plug(hotplug_dev, dev, errp);
2250 }
2251}
2252
95bee274
IM
2253static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
2254 DeviceState *dev, Error **errp)
2255{
2256 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
bb6e2f7a 2257 pc_memory_plug(hotplug_dev, dev, errp);
5279569e
GZ
2258 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2259 pc_cpu_plug(hotplug_dev, dev, errp);
95bee274
IM
2260 }
2261}
2262
d9c5c5b8
TC
2263static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
2264 DeviceState *dev, Error **errp)
2265{
64fec58e 2266 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
bb6e2f7a 2267 pc_memory_unplug_request(hotplug_dev, dev, errp);
8872c25a
IM
2268 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2269 pc_cpu_unplug_request_cb(hotplug_dev, dev, errp);
64fec58e
TC
2270 } else {
2271 error_setg(errp, "acpi: device unplug request for not supported device"
2272 " type: %s", object_get_typename(OBJECT(dev)));
2273 }
d9c5c5b8
TC
2274}
2275
232391c1
TC
2276static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
2277 DeviceState *dev, Error **errp)
2278{
f7d3e29d 2279 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
bb6e2f7a 2280 pc_memory_unplug(hotplug_dev, dev, errp);
8872c25a
IM
2281 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2282 pc_cpu_unplug_cb(hotplug_dev, dev, errp);
f7d3e29d
TC
2283 } else {
2284 error_setg(errp, "acpi: device unplug for not supported device"
2285 " type: %s", object_get_typename(OBJECT(dev)));
2286 }
232391c1
TC
2287}
2288
285816d7 2289static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
95bee274
IM
2290 DeviceState *dev)
2291{
5279569e
GZ
2292 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
2293 object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
95bee274
IM
2294 return HOTPLUG_HANDLER(machine);
2295 }
2296
38aefb57 2297 return NULL;
95bee274
IM
2298}
2299
bf1e8939 2300static void
f2ffbe2b
DH
2301pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
2302 const char *name, void *opaque,
2303 Error **errp)
bf1e8939 2304{
b0c14ec4
DH
2305 MachineState *ms = MACHINE(obj);
2306 int64_t value = memory_region_size(&ms->device_memory->mr);
bf1e8939 2307
51e72bc1 2308 visit_type_int(v, name, &value, errp);
bf1e8939
IM
2309}
2310
c87b1520 2311static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
d7bce999
EB
2312 const char *name, void *opaque,
2313 Error **errp)
c87b1520
DS
2314{
2315 PCMachineState *pcms = PC_MACHINE(obj);
2316 uint64_t value = pcms->max_ram_below_4g;
2317
51e72bc1 2318 visit_type_size(v, name, &value, errp);
c87b1520
DS
2319}
2320
2321static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
d7bce999
EB
2322 const char *name, void *opaque,
2323 Error **errp)
c87b1520
DS
2324{
2325 PCMachineState *pcms = PC_MACHINE(obj);
2326 Error *error = NULL;
2327 uint64_t value;
2328
51e72bc1 2329 visit_type_size(v, name, &value, &error);
c87b1520
DS
2330 if (error) {
2331 error_propagate(errp, error);
2332 return;
2333 }
d471bf3e 2334 if (value > 4 * GiB) {
455b0fde
EB
2335 error_setg(&error,
2336 "Machine option 'max-ram-below-4g=%"PRIu64
2337 "' expects size less than or equal to 4G", value);
c87b1520
DS
2338 error_propagate(errp, error);
2339 return;
2340 }
2341
d471bf3e 2342 if (value < 1 * MiB) {
9e5d2c52
AF
2343 warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
2344 "BIOS may not work with less than 1MiB", value);
c87b1520
DS
2345 }
2346
2347 pcms->max_ram_below_4g = value;
2348}
2349
d7bce999
EB
2350static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
2351 void *opaque, Error **errp)
9b23cfb7
DDAG
2352{
2353 PCMachineState *pcms = PC_MACHINE(obj);
d1048bef 2354 OnOffAuto vmport = pcms->vmport;
9b23cfb7 2355
51e72bc1 2356 visit_type_OnOffAuto(v, name, &vmport, errp);
9b23cfb7
DDAG
2357}
2358
d7bce999
EB
2359static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
2360 void *opaque, Error **errp)
9b23cfb7
DDAG
2361{
2362 PCMachineState *pcms = PC_MACHINE(obj);
2363
51e72bc1 2364 visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
9b23cfb7
DDAG
2365}
2366
355023f2
PB
2367bool pc_machine_is_smm_enabled(PCMachineState *pcms)
2368{
2369 bool smm_available = false;
2370
2371 if (pcms->smm == ON_OFF_AUTO_OFF) {
2372 return false;
2373 }
2374
2375 if (tcg_enabled() || qtest_enabled()) {
2376 smm_available = true;
2377 } else if (kvm_enabled()) {
2378 smm_available = kvm_has_smm();
2379 }
2380
2381 if (smm_available) {
2382 return true;
2383 }
2384
2385 if (pcms->smm == ON_OFF_AUTO_ON) {
2386 error_report("System Management Mode not supported by this hypervisor.");
2387 exit(1);
2388 }
2389 return false;
2390}
2391
d7bce999
EB
2392static void pc_machine_get_smm(Object *obj, Visitor *v, const char *name,
2393 void *opaque, Error **errp)
355023f2
PB
2394{
2395 PCMachineState *pcms = PC_MACHINE(obj);
2396 OnOffAuto smm = pcms->smm;
2397
51e72bc1 2398 visit_type_OnOffAuto(v, name, &smm, errp);
355023f2
PB
2399}
2400
d7bce999
EB
2401static void pc_machine_set_smm(Object *obj, Visitor *v, const char *name,
2402 void *opaque, Error **errp)
355023f2
PB
2403{
2404 PCMachineState *pcms = PC_MACHINE(obj);
2405
51e72bc1 2406 visit_type_OnOffAuto(v, name, &pcms->smm, errp);
355023f2
PB
2407}
2408
87252e1b
XG
2409static bool pc_machine_get_nvdimm(Object *obj, Error **errp)
2410{
2411 PCMachineState *pcms = PC_MACHINE(obj);
2412
5fe79386 2413 return pcms->acpi_nvdimm_state.is_enabled;
87252e1b
XG
2414}
2415
2416static void pc_machine_set_nvdimm(Object *obj, bool value, Error **errp)
2417{
2418 PCMachineState *pcms = PC_MACHINE(obj);
2419
5fe79386 2420 pcms->acpi_nvdimm_state.is_enabled = value;
87252e1b
XG
2421}
2422
11c39b5c 2423static char *pc_machine_get_nvdimm_persistence(Object *obj, Error **errp)
9ab3aad2
RZ
2424{
2425 PCMachineState *pcms = PC_MACHINE(obj);
9ab3aad2 2426
11c39b5c 2427 return g_strdup(pcms->acpi_nvdimm_state.persistence_string);
9ab3aad2
RZ
2428}
2429
11c39b5c 2430static void pc_machine_set_nvdimm_persistence(Object *obj, const char *value,
9ab3aad2
RZ
2431 Error **errp)
2432{
2433 PCMachineState *pcms = PC_MACHINE(obj);
11c39b5c
RZ
2434 AcpiNVDIMMState *nvdimm_state = &pcms->acpi_nvdimm_state;
2435
2436 if (strcmp(value, "cpu") == 0)
2437 nvdimm_state->persistence = 3;
2438 else if (strcmp(value, "mem-ctrl") == 0)
2439 nvdimm_state->persistence = 2;
2440 else {
d319e05d
MA
2441 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
2442 value);
2443 return;
9ab3aad2
RZ
2444 }
2445
11c39b5c
RZ
2446 g_free(nvdimm_state->persistence_string);
2447 nvdimm_state->persistence_string = g_strdup(value);
9ab3aad2
RZ
2448}
2449
be232eb0
CP
2450static bool pc_machine_get_smbus(Object *obj, Error **errp)
2451{
2452 PCMachineState *pcms = PC_MACHINE(obj);
2453
f5878b03 2454 return pcms->smbus_enabled;
be232eb0
CP
2455}
2456
2457static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
2458{
2459 PCMachineState *pcms = PC_MACHINE(obj);
2460
f5878b03 2461 pcms->smbus_enabled = value;
be232eb0
CP
2462}
2463
272f0428
CP
2464static bool pc_machine_get_sata(Object *obj, Error **errp)
2465{
2466 PCMachineState *pcms = PC_MACHINE(obj);
2467
f5878b03 2468 return pcms->sata_enabled;
272f0428
CP
2469}
2470
2471static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
2472{
2473 PCMachineState *pcms = PC_MACHINE(obj);
2474
f5878b03 2475 pcms->sata_enabled = value;
272f0428
CP
2476}
2477
feddd2fd
CP
2478static bool pc_machine_get_pit(Object *obj, Error **errp)
2479{
2480 PCMachineState *pcms = PC_MACHINE(obj);
2481
f5878b03 2482 return pcms->pit_enabled;
feddd2fd
CP
2483}
2484
2485static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
2486{
2487 PCMachineState *pcms = PC_MACHINE(obj);
2488
f5878b03 2489 pcms->pit_enabled = value;
feddd2fd
CP
2490}
2491
bf1e8939
IM
2492static void pc_machine_initfn(Object *obj)
2493{
c87b1520
DS
2494 PCMachineState *pcms = PC_MACHINE(obj);
2495
5ec7d098 2496 pcms->max_ram_below_4g = 0; /* use default */
355023f2 2497 pcms->smm = ON_OFF_AUTO_AUTO;
d1048bef 2498 pcms->vmport = ON_OFF_AUTO_AUTO;
87252e1b 2499 /* nvdimm is disabled on default. */
5fe79386 2500 pcms->acpi_nvdimm_state.is_enabled = false;
021746c1
WL
2501 /* acpi build is enabled by default if machine supports it */
2502 pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
f5878b03
CM
2503 pcms->smbus_enabled = true;
2504 pcms->sata_enabled = true;
2505 pcms->pit_enabled = true;
bf1e8939
IM
2506}
2507
ae50c55a
ZG
2508static void pc_machine_reset(void)
2509{
2510 CPUState *cs;
2511 X86CPU *cpu;
2512
2513 qemu_devices_reset();
2514
2515 /* Reset APIC after devices have been reset to cancel
2516 * any changes that qemu_devices_reset() might have done.
2517 */
2518 CPU_FOREACH(cs) {
2519 cpu = X86_CPU(cs);
2520
2521 if (cpu->apic_state) {
2522 device_reset(cpu->apic_state);
2523 }
2524 }
2525}
2526
ea089eeb
IM
2527static CpuInstanceProperties
2528pc_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
fb43b73b 2529{
ea089eeb
IM
2530 MachineClass *mc = MACHINE_GET_CLASS(ms);
2531 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
2532
2533 assert(cpu_index < possible_cpus->len);
2534 return possible_cpus->cpus[cpu_index].props;
fb43b73b
IM
2535}
2536
79e07936
IM
2537static int64_t pc_get_default_cpu_node_id(const MachineState *ms, int idx)
2538{
2539 X86CPUTopoInfo topo;
2540
2541 assert(idx < ms->possible_cpus->len);
2542 x86_topo_ids_from_apicid(ms->possible_cpus->cpus[idx].arch_id,
2543 smp_cores, smp_threads, &topo);
2544 return topo.pkg_id % nb_numa_nodes;
2545}
2546
c96a1c0b 2547static const CPUArchIdList *pc_possible_cpu_arch_ids(MachineState *ms)
3811ef14 2548{
c96a1c0b
IM
2549 int i;
2550
2551 if (ms->possible_cpus) {
2552 /*
2553 * make sure that max_cpus hasn't changed since the first use, i.e.
2554 * -smp hasn't been parsed after it
2555 */
2556 assert(ms->possible_cpus->len == max_cpus);
2557 return ms->possible_cpus;
2558 }
2559
2560 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
2561 sizeof(CPUArchId) * max_cpus);
2562 ms->possible_cpus->len = max_cpus;
2563 for (i = 0; i < ms->possible_cpus->len; i++) {
c67ae933
IM
2564 X86CPUTopoInfo topo;
2565
d342eb76 2566 ms->possible_cpus->cpus[i].type = ms->cpu_type;
f2d672c2 2567 ms->possible_cpus->cpus[i].vcpus_count = 1;
c96a1c0b 2568 ms->possible_cpus->cpus[i].arch_id = x86_cpu_apic_id_from_index(i);
c67ae933
IM
2569 x86_topo_ids_from_apicid(ms->possible_cpus->cpus[i].arch_id,
2570 smp_cores, smp_threads, &topo);
2571 ms->possible_cpus->cpus[i].props.has_socket_id = true;
2572 ms->possible_cpus->cpus[i].props.socket_id = topo.pkg_id;
2573 ms->possible_cpus->cpus[i].props.has_core_id = true;
2574 ms->possible_cpus->cpus[i].props.core_id = topo.core_id;
2575 ms->possible_cpus->cpus[i].props.has_thread_id = true;
2576 ms->possible_cpus->cpus[i].props.thread_id = topo.smt_id;
c96a1c0b
IM
2577 }
2578 return ms->possible_cpus;
3811ef14
IM
2579}
2580
1255166b
BD
2581static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
2582{
2583 /* cpu index isn't used */
2584 CPUState *cs;
2585
2586 CPU_FOREACH(cs) {
2587 X86CPU *cpu = X86_CPU(cs);
2588
2589 if (!cpu->apic_state) {
2590 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
2591 } else {
2592 apic_deliver_nmi(cpu->apic_state);
2593 }
2594 }
2595}
2596
95bee274
IM
2597static void pc_machine_class_init(ObjectClass *oc, void *data)
2598{
2599 MachineClass *mc = MACHINE_CLASS(oc);
2600 PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
2601 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
1255166b 2602 NMIClass *nc = NMI_CLASS(oc);
95bee274 2603
7102fa70
EH
2604 pcmc->pci_enabled = true;
2605 pcmc->has_acpi_build = true;
2606 pcmc->rsdp_in_ram = true;
2607 pcmc->smbios_defaults = true;
2608 pcmc->smbios_uuid_encoded = true;
2609 pcmc->gigabyte_align = true;
2610 pcmc->has_reserved_memory = true;
2611 pcmc->kvmclock_enabled = true;
16a9e8a5 2612 pcmc->enforce_aligned_dimm = true;
cd4040ec
EH
2613 /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
2614 * to be used at the moment, 32K should be enough for a while. */
2615 pcmc->acpi_data_size = 0x20000 + 0x8000;
36f96c4b 2616 pcmc->save_tsc_khz = true;
98e753a6 2617 pcmc->linuxboot_dma_enabled = true;
debbdc00 2618 assert(!mc->get_hotplug_handler);
285816d7 2619 mc->get_hotplug_handler = pc_get_hotplug_handler;
ea089eeb 2620 mc->cpu_index_to_instance_props = pc_cpu_index_to_props;
79e07936 2621 mc->get_default_cpu_node_id = pc_get_default_cpu_node_id;
3811ef14 2622 mc->possible_cpu_arch_ids = pc_possible_cpu_arch_ids;
7b8be49d 2623 mc->auto_enable_numa_with_memhp = true;
c5514d0e 2624 mc->has_hotpluggable_cpus = true;
41742767 2625 mc->default_boot_order = "cad";
4458fb3a 2626 mc->hot_add_cpu = pc_hot_add_cpu;
2059839b 2627 mc->block_default_type = IF_IDE;
4458fb3a 2628 mc->max_cpus = 255;
ae50c55a 2629 mc->reset = pc_machine_reset;
4ec60c76 2630 hc->pre_plug = pc_machine_device_pre_plug_cb;
95bee274 2631 hc->plug = pc_machine_device_plug_cb;
d9c5c5b8 2632 hc->unplug_request = pc_machine_device_unplug_request_cb;
232391c1 2633 hc->unplug = pc_machine_device_unplug_cb;
1255166b 2634 nc->nmi_monitor_handler = x86_nmi;
311ca98d 2635 mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
0efc257d 2636
f2ffbe2b
DH
2637 object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
2638 pc_machine_get_device_memory_region_size, NULL,
0efc257d
EH
2639 NULL, NULL, &error_abort);
2640
2641 object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
2642 pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
2643 NULL, NULL, &error_abort);
2644
2645 object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
2646 "Maximum ram below the 4G boundary (32bit boundary)", &error_abort);
2647
2648 object_class_property_add(oc, PC_MACHINE_SMM, "OnOffAuto",
2649 pc_machine_get_smm, pc_machine_set_smm,
2650 NULL, NULL, &error_abort);
2651 object_class_property_set_description(oc, PC_MACHINE_SMM,
2652 "Enable SMM (pc & q35)", &error_abort);
2653
2654 object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
2655 pc_machine_get_vmport, pc_machine_set_vmport,
2656 NULL, NULL, &error_abort);
2657 object_class_property_set_description(oc, PC_MACHINE_VMPORT,
2658 "Enable vmport (pc & q35)", &error_abort);
2659
2660 object_class_property_add_bool(oc, PC_MACHINE_NVDIMM,
2661 pc_machine_get_nvdimm, pc_machine_set_nvdimm, &error_abort);
be232eb0 2662
11c39b5c
RZ
2663 object_class_property_add_str(oc, PC_MACHINE_NVDIMM_PERSIST,
2664 pc_machine_get_nvdimm_persistence,
2665 pc_machine_set_nvdimm_persistence, &error_abort);
9ab3aad2 2666
be232eb0
CP
2667 object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
2668 pc_machine_get_smbus, pc_machine_set_smbus, &error_abort);
272f0428
CP
2669
2670 object_class_property_add_bool(oc, PC_MACHINE_SATA,
2671 pc_machine_get_sata, pc_machine_set_sata, &error_abort);
feddd2fd
CP
2672
2673 object_class_property_add_bool(oc, PC_MACHINE_PIT,
2674 pc_machine_get_pit, pc_machine_set_pit, &error_abort);
95bee274
IM
2675}
2676
d5747cac
IM
2677static const TypeInfo pc_machine_info = {
2678 .name = TYPE_PC_MACHINE,
2679 .parent = TYPE_MACHINE,
2680 .abstract = true,
2681 .instance_size = sizeof(PCMachineState),
bf1e8939 2682 .instance_init = pc_machine_initfn,
d5747cac 2683 .class_size = sizeof(PCMachineClass),
95bee274
IM
2684 .class_init = pc_machine_class_init,
2685 .interfaces = (InterfaceInfo[]) {
2686 { TYPE_HOTPLUG_HANDLER },
1255166b 2687 { TYPE_NMI },
95bee274
IM
2688 { }
2689 },
d5747cac
IM
2690};
2691
2692static void pc_machine_register_types(void)
2693{
2694 type_register_static(&pc_machine_info);
2695}
2696
2697type_init(pc_machine_register_types)