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