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