]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/pc.c
qerror: Move #include out of qerror.h
[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 */
83c9f4ca 24#include "hw/hw.h"
0d09e41a
PB
25#include "hw/i386/pc.h"
26#include "hw/char/serial.h"
27#include "hw/i386/apic.h"
54a40293
EH
28#include "hw/i386/topology.h"
29#include "sysemu/cpus.h"
0d09e41a 30#include "hw/block/fdc.h"
83c9f4ca
PB
31#include "hw/ide.h"
32#include "hw/pci/pci.h"
2118196b 33#include "hw/pci/pci_bus.h"
83c9089e 34#include "monitor/monitor.h"
0d09e41a
PB
35#include "hw/nvram/fw_cfg.h"
36#include "hw/timer/hpet.h"
37#include "hw/i386/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"
53a89e26 63#include "hw/cpu/icc_bus.h"
c649983b 64#include "hw/boards.h"
39848901 65#include "hw/pci/pci_host.h"
72c194f7 66#include "acpi-build.h"
95bee274 67#include "hw/mem/pc-dimm.h"
2e1ac493 68#include "trace.h"
bf1e8939 69#include "qapi/visitor.h"
d1048bef 70#include "qapi-visit.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
438f92ee
MT
82/* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables
83 * (128K) and other BIOS datastructures (less than 4K reported to be used at
84 * the moment, 32K should be enough for a while). */
e0bcc42e 85static unsigned acpi_data_size = 0x20000 + 0x8000;
927766c7
MT
86void pc_set_legacy_acpi_data_size(void)
87{
88 acpi_data_size = 0x10000;
89}
90
3cce6243 91#define BIOS_CFG_IOPORT 0x510
8a92ea2f 92#define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
b6f6e3d3 93#define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
6b35e7bf 94#define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
4c5b10b7 95#define FW_CFG_E820_TABLE (FW_CFG_ARCH_LOCAL + 3)
40ac17cd 96#define FW_CFG_HPET (FW_CFG_ARCH_LOCAL + 4)
80cabfad 97
4c5b10b7
JS
98#define E820_NR_ENTRIES 16
99
100struct e820_entry {
101 uint64_t address;
102 uint64_t length;
103 uint32_t type;
541dc0d4 104} QEMU_PACKED __attribute((__aligned__(4)));
4c5b10b7
JS
105
106struct e820_table {
107 uint32_t count;
108 struct e820_entry entry[E820_NR_ENTRIES];
541dc0d4 109} QEMU_PACKED __attribute((__aligned__(4)));
4c5b10b7 110
7d67110f
GH
111static struct e820_table e820_reserve;
112static struct e820_entry *e820_table;
113static unsigned e820_entries;
dd703b99 114struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
4c5b10b7 115
b881fbe9 116void gsi_handler(void *opaque, int n, int level)
1452411b 117{
b881fbe9 118 GSIState *s = opaque;
1452411b 119
b881fbe9
JK
120 DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
121 if (n < ISA_NUM_IRQS) {
122 qemu_set_irq(s->i8259_irq[n], level);
1632dc6a 123 }
b881fbe9 124 qemu_set_irq(s->ioapic_irq[n], level);
2e9947d2 125}
1452411b 126
258711c6
JG
127static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
128 unsigned size)
80cabfad
FB
129{
130}
131
c02e1eac
JG
132static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
133{
a6fc23e5 134 return 0xffffffffffffffffULL;
c02e1eac
JG
135}
136
f929aad6 137/* MSDOS compatibility mode FPU exception support */
d537cf6c 138static qemu_irq ferr_irq;
8e78eb28
IY
139
140void pc_register_ferr_irq(qemu_irq irq)
141{
142 ferr_irq = irq;
143}
144
f929aad6
FB
145/* XXX: add IGNNE support */
146void cpu_set_ferr(CPUX86State *s)
147{
d537cf6c 148 qemu_irq_raise(ferr_irq);
f929aad6
FB
149}
150
258711c6
JG
151static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
152 unsigned size)
f929aad6 153{
d537cf6c 154 qemu_irq_lower(ferr_irq);
f929aad6
FB
155}
156
c02e1eac
JG
157static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
158{
a6fc23e5 159 return 0xffffffffffffffffULL;
c02e1eac
JG
160}
161
28ab0e2e 162/* TSC handling */
28ab0e2e
FB
163uint64_t cpu_get_tsc(CPUX86State *env)
164{
4a1418e0 165 return cpu_get_ticks();
28ab0e2e
FB
166}
167
3de388f6 168/* IRQ handling */
4a8fa5dc 169int cpu_get_pic_interrupt(CPUX86State *env)
3de388f6 170{
02e51483 171 X86CPU *cpu = x86_env_get_cpu(env);
3de388f6
FB
172 int intno;
173
02e51483 174 intno = apic_get_interrupt(cpu->apic_state);
3de388f6 175 if (intno >= 0) {
3de388f6
FB
176 return intno;
177 }
3de388f6 178 /* read the irq from the PIC */
02e51483 179 if (!apic_accept_pic_intr(cpu->apic_state)) {
0e21e12b 180 return -1;
cf6d64bf 181 }
0e21e12b 182
3de388f6
FB
183 intno = pic_read_irq(isa_pic);
184 return intno;
185}
186
d537cf6c 187static void pic_irq_request(void *opaque, int irq, int level)
3de388f6 188{
182735ef
AF
189 CPUState *cs = first_cpu;
190 X86CPU *cpu = X86_CPU(cs);
a5b38b51 191
471fd342 192 DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
02e51483 193 if (cpu->apic_state) {
bdc44640 194 CPU_FOREACH(cs) {
182735ef 195 cpu = X86_CPU(cs);
02e51483
CF
196 if (apic_accept_pic_intr(cpu->apic_state)) {
197 apic_deliver_pic_intr(cpu->apic_state, level);
cf6d64bf 198 }
d5529471
AJ
199 }
200 } else {
d8ed887b 201 if (level) {
c3affe56 202 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
d8ed887b
AF
203 } else {
204 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
205 }
a5b38b51 206 }
3de388f6
FB
207}
208
b0a21b53
FB
209/* PC cmos mappings */
210
80cabfad
FB
211#define REG_EQUIPMENT_BYTE 0x14
212
d288c7ba 213static int cmos_get_fd_drive_type(FDriveType fd0)
777428f2
FB
214{
215 int val;
216
217 switch (fd0) {
d288c7ba 218 case FDRIVE_DRV_144:
777428f2
FB
219 /* 1.44 Mb 3"5 drive */
220 val = 4;
221 break;
d288c7ba 222 case FDRIVE_DRV_288:
777428f2
FB
223 /* 2.88 Mb 3"5 drive */
224 val = 5;
225 break;
d288c7ba 226 case FDRIVE_DRV_120:
777428f2
FB
227 /* 1.2 Mb 5"5 drive */
228 val = 2;
229 break;
d288c7ba 230 case FDRIVE_DRV_NONE:
777428f2
FB
231 default:
232 val = 0;
233 break;
234 }
235 return val;
236}
237
9139046c
MA
238static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
239 int16_t cylinders, int8_t heads, int8_t sectors)
ba6c2377 240{
ba6c2377
FB
241 rtc_set_memory(s, type_ofs, 47);
242 rtc_set_memory(s, info_ofs, cylinders);
243 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
244 rtc_set_memory(s, info_ofs + 2, heads);
245 rtc_set_memory(s, info_ofs + 3, 0xff);
246 rtc_set_memory(s, info_ofs + 4, 0xff);
247 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
248 rtc_set_memory(s, info_ofs + 6, cylinders);
249 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
250 rtc_set_memory(s, info_ofs + 8, sectors);
251}
252
6ac0e82d
AZ
253/* convert boot_device letter to something recognizable by the bios */
254static int boot_device2nibble(char boot_device)
255{
256 switch(boot_device) {
257 case 'a':
258 case 'b':
259 return 0x01; /* floppy boot */
260 case 'c':
261 return 0x02; /* hard drive boot */
262 case 'd':
263 return 0x03; /* CD-ROM boot */
264 case 'n':
265 return 0x04; /* Network boot */
266 }
267 return 0;
268}
269
ddcd5531 270static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
0ecdffbb
AJ
271{
272#define PC_MAX_BOOT_DEVICES 3
0ecdffbb
AJ
273 int nbds, bds[3] = { 0, };
274 int i;
275
276 nbds = strlen(boot_device);
277 if (nbds > PC_MAX_BOOT_DEVICES) {
ddcd5531
GA
278 error_setg(errp, "Too many boot devices for PC");
279 return;
0ecdffbb
AJ
280 }
281 for (i = 0; i < nbds; i++) {
282 bds[i] = boot_device2nibble(boot_device[i]);
283 if (bds[i] == 0) {
ddcd5531
GA
284 error_setg(errp, "Invalid boot device for PC: '%c'",
285 boot_device[i]);
286 return;
0ecdffbb
AJ
287 }
288 }
289 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
d9346e81 290 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
0ecdffbb
AJ
291}
292
ddcd5531 293static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
d9346e81 294{
ddcd5531 295 set_boot_dev(opaque, boot_device, errp);
d9346e81
MA
296}
297
c0897e0c
MA
298typedef struct pc_cmos_init_late_arg {
299 ISADevice *rtc_state;
9139046c 300 BusState *idebus[2];
c0897e0c
MA
301} pc_cmos_init_late_arg;
302
303static void pc_cmos_init_late(void *opaque)
304{
305 pc_cmos_init_late_arg *arg = opaque;
306 ISADevice *s = arg->rtc_state;
9139046c
MA
307 int16_t cylinders;
308 int8_t heads, sectors;
c0897e0c 309 int val;
2adc99b2 310 int i, trans;
c0897e0c 311
9139046c
MA
312 val = 0;
313 if (ide_get_geometry(arg->idebus[0], 0,
314 &cylinders, &heads, &sectors) >= 0) {
315 cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
316 val |= 0xf0;
317 }
318 if (ide_get_geometry(arg->idebus[0], 1,
319 &cylinders, &heads, &sectors) >= 0) {
320 cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
321 val |= 0x0f;
322 }
323 rtc_set_memory(s, 0x12, val);
c0897e0c
MA
324
325 val = 0;
326 for (i = 0; i < 4; i++) {
9139046c
MA
327 /* NOTE: ide_get_geometry() returns the physical
328 geometry. It is always such that: 1 <= sects <= 63, 1
329 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
330 geometry can be different if a translation is done. */
331 if (ide_get_geometry(arg->idebus[i / 2], i % 2,
332 &cylinders, &heads, &sectors) >= 0) {
2adc99b2
MA
333 trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
334 assert((trans & ~3) == 0);
335 val |= trans << (i * 2);
c0897e0c
MA
336 }
337 }
338 rtc_set_memory(s, 0x39, val);
339
340 qemu_unregister_reset(pc_cmos_init_late, opaque);
341}
342
845773ab 343void pc_cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
2d996150 344 const char *boot_device, MachineState *machine,
34d4260e 345 ISADevice *floppy, BusState *idebus0, BusState *idebus1,
63ffb564 346 ISADevice *s)
80cabfad 347{
61a8d649 348 int val, nb, i;
980bda8b 349 FDriveType fd_type[2] = { FDRIVE_DRV_NONE, FDRIVE_DRV_NONE };
c0897e0c 350 static pc_cmos_init_late_arg arg;
2d996150 351 PCMachineState *pc_machine = PC_MACHINE(machine);
ddcd5531 352 Error *local_err = NULL;
b0a21b53 353
b0a21b53 354 /* various important CMOS locations needed by PC/Bochs bios */
80cabfad
FB
355
356 /* memory size */
e89001f7
MA
357 /* base memory (first MiB) */
358 val = MIN(ram_size / 1024, 640);
333190eb
FB
359 rtc_set_memory(s, 0x15, val);
360 rtc_set_memory(s, 0x16, val >> 8);
e89001f7
MA
361 /* extended memory (next 64MiB) */
362 if (ram_size > 1024 * 1024) {
363 val = (ram_size - 1024 * 1024) / 1024;
364 } else {
365 val = 0;
366 }
80cabfad
FB
367 if (val > 65535)
368 val = 65535;
b0a21b53
FB
369 rtc_set_memory(s, 0x17, val);
370 rtc_set_memory(s, 0x18, val >> 8);
371 rtc_set_memory(s, 0x30, val);
372 rtc_set_memory(s, 0x31, val >> 8);
e89001f7
MA
373 /* memory between 16MiB and 4GiB */
374 if (ram_size > 16 * 1024 * 1024) {
375 val = (ram_size - 16 * 1024 * 1024) / 65536;
376 } else {
9da98861 377 val = 0;
e89001f7 378 }
80cabfad
FB
379 if (val > 65535)
380 val = 65535;
b0a21b53
FB
381 rtc_set_memory(s, 0x34, val);
382 rtc_set_memory(s, 0x35, val >> 8);
e89001f7
MA
383 /* memory above 4GiB */
384 val = above_4g_mem_size / 65536;
385 rtc_set_memory(s, 0x5b, val);
386 rtc_set_memory(s, 0x5c, val >> 8);
387 rtc_set_memory(s, 0x5d, val >> 16);
3b46e624 388
298e01b6
AJ
389 /* set the number of CPU */
390 rtc_set_memory(s, 0x5f, smp_cpus - 1);
2d996150
GZ
391
392 object_property_add_link(OBJECT(machine), "rtc_state",
393 TYPE_ISA_DEVICE,
394 (Object **)&pc_machine->rtc,
395 object_property_allow_set_link,
396 OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort);
397 object_property_set_link(OBJECT(machine), OBJECT(s),
398 "rtc_state", &error_abort);
298e01b6 399
ddcd5531
GA
400 set_boot_dev(s, boot_device, &local_err);
401 if (local_err) {
565f65d2 402 error_report_err(local_err);
28c5af54
JM
403 exit(1);
404 }
80cabfad 405
b41a2cd1 406 /* floppy type */
34d4260e 407 if (floppy) {
34d4260e 408 for (i = 0; i < 2; i++) {
61a8d649 409 fd_type[i] = isa_fdc_get_drive_type(floppy, i);
63ffb564
BS
410 }
411 }
412 val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
413 cmos_get_fd_drive_type(fd_type[1]);
b0a21b53 414 rtc_set_memory(s, 0x10, val);
3b46e624 415
b0a21b53 416 val = 0;
b41a2cd1 417 nb = 0;
63ffb564 418 if (fd_type[0] < FDRIVE_DRV_NONE) {
80cabfad 419 nb++;
d288c7ba 420 }
63ffb564 421 if (fd_type[1] < FDRIVE_DRV_NONE) {
80cabfad 422 nb++;
d288c7ba 423 }
80cabfad
FB
424 switch (nb) {
425 case 0:
426 break;
427 case 1:
b0a21b53 428 val |= 0x01; /* 1 drive, ready for boot */
80cabfad
FB
429 break;
430 case 2:
b0a21b53 431 val |= 0x41; /* 2 drives, ready for boot */
80cabfad
FB
432 break;
433 }
b0a21b53
FB
434 val |= 0x02; /* FPU is there */
435 val |= 0x04; /* PS/2 mouse installed */
436 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
437
ba6c2377 438 /* hard drives */
c0897e0c 439 arg.rtc_state = s;
9139046c
MA
440 arg.idebus[0] = idebus0;
441 arg.idebus[1] = idebus1;
c0897e0c 442 qemu_register_reset(pc_cmos_init_late, &arg);
80cabfad
FB
443}
444
a0881c64
AF
445#define TYPE_PORT92 "port92"
446#define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
447
4b78a802
BS
448/* port 92 stuff: could be split off */
449typedef struct Port92State {
a0881c64
AF
450 ISADevice parent_obj;
451
23af670e 452 MemoryRegion io;
4b78a802
BS
453 uint8_t outport;
454 qemu_irq *a20_out;
455} Port92State;
456
93ef4192
AG
457static void port92_write(void *opaque, hwaddr addr, uint64_t val,
458 unsigned size)
4b78a802
BS
459{
460 Port92State *s = opaque;
4700a316 461 int oldval = s->outport;
4b78a802 462
c5539cb4 463 DPRINTF("port92: write 0x%02" PRIx64 "\n", val);
4b78a802
BS
464 s->outport = val;
465 qemu_set_irq(*s->a20_out, (val >> 1) & 1);
4700a316 466 if ((val & 1) && !(oldval & 1)) {
4b78a802
BS
467 qemu_system_reset_request();
468 }
469}
470
93ef4192
AG
471static uint64_t port92_read(void *opaque, hwaddr addr,
472 unsigned size)
4b78a802
BS
473{
474 Port92State *s = opaque;
475 uint32_t ret;
476
477 ret = s->outport;
478 DPRINTF("port92: read 0x%02x\n", ret);
479 return ret;
480}
481
482static void port92_init(ISADevice *dev, qemu_irq *a20_out)
483{
a0881c64 484 Port92State *s = PORT92(dev);
4b78a802
BS
485
486 s->a20_out = a20_out;
487}
488
489static const VMStateDescription vmstate_port92_isa = {
490 .name = "port92",
491 .version_id = 1,
492 .minimum_version_id = 1,
d49805ae 493 .fields = (VMStateField[]) {
4b78a802
BS
494 VMSTATE_UINT8(outport, Port92State),
495 VMSTATE_END_OF_LIST()
496 }
497};
498
499static void port92_reset(DeviceState *d)
500{
a0881c64 501 Port92State *s = PORT92(d);
4b78a802
BS
502
503 s->outport &= ~1;
504}
505
23af670e 506static const MemoryRegionOps port92_ops = {
93ef4192
AG
507 .read = port92_read,
508 .write = port92_write,
509 .impl = {
510 .min_access_size = 1,
511 .max_access_size = 1,
512 },
513 .endianness = DEVICE_LITTLE_ENDIAN,
23af670e
RH
514};
515
db895a1e 516static void port92_initfn(Object *obj)
4b78a802 517{
db895a1e 518 Port92State *s = PORT92(obj);
4b78a802 519
1437c94b 520 memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1);
23af670e 521
4b78a802 522 s->outport = 0;
db895a1e
AF
523}
524
525static void port92_realizefn(DeviceState *dev, Error **errp)
526{
527 ISADevice *isadev = ISA_DEVICE(dev);
528 Port92State *s = PORT92(dev);
529
530 isa_register_ioport(isadev, &s->io, 0x92);
4b78a802
BS
531}
532
8f04ee08
AL
533static void port92_class_initfn(ObjectClass *klass, void *data)
534{
39bffca2 535 DeviceClass *dc = DEVICE_CLASS(klass);
db895a1e 536
db895a1e 537 dc->realize = port92_realizefn;
39bffca2
AL
538 dc->reset = port92_reset;
539 dc->vmsd = &vmstate_port92_isa;
f3b17640
MA
540 /*
541 * Reason: unlike ordinary ISA devices, this one needs additional
542 * wiring: its A20 output line needs to be wired up by
543 * port92_init().
544 */
545 dc->cannot_instantiate_with_device_add_yet = true;
8f04ee08
AL
546}
547
8c43a6f0 548static const TypeInfo port92_info = {
a0881c64 549 .name = TYPE_PORT92,
39bffca2
AL
550 .parent = TYPE_ISA_DEVICE,
551 .instance_size = sizeof(Port92State),
db895a1e 552 .instance_init = port92_initfn,
39bffca2 553 .class_init = port92_class_initfn,
4b78a802
BS
554};
555
83f7d43a 556static void port92_register_types(void)
4b78a802 557{
39bffca2 558 type_register_static(&port92_info);
4b78a802 559}
83f7d43a
AF
560
561type_init(port92_register_types)
4b78a802 562
956a3e6b 563static void handle_a20_line_change(void *opaque, int irq, int level)
59b8ad81 564{
cc36a7a2 565 X86CPU *cpu = opaque;
e1a23744 566
956a3e6b 567 /* XXX: send to all CPUs ? */
4b78a802 568 /* XXX: add logic to handle multiple A20 line sources */
cc36a7a2 569 x86_cpu_set_a20(cpu, level);
e1a23744
FB
570}
571
4c5b10b7
JS
572int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
573{
7d67110f 574 int index = le32_to_cpu(e820_reserve.count);
4c5b10b7
JS
575 struct e820_entry *entry;
576
7d67110f
GH
577 if (type != E820_RAM) {
578 /* old FW_CFG_E820_TABLE entry -- reservations only */
579 if (index >= E820_NR_ENTRIES) {
580 return -EBUSY;
581 }
582 entry = &e820_reserve.entry[index++];
583
584 entry->address = cpu_to_le64(address);
585 entry->length = cpu_to_le64(length);
586 entry->type = cpu_to_le32(type);
587
588 e820_reserve.count = cpu_to_le32(index);
589 }
4c5b10b7 590
7d67110f 591 /* new "etc/e820" file -- include ram too */
ab3ad07f 592 e820_table = g_renew(struct e820_entry, e820_table, e820_entries + 1);
7d67110f
GH
593 e820_table[e820_entries].address = cpu_to_le64(address);
594 e820_table[e820_entries].length = cpu_to_le64(length);
595 e820_table[e820_entries].type = cpu_to_le32(type);
596 e820_entries++;
4c5b10b7 597
7d67110f 598 return e820_entries;
4c5b10b7
JS
599}
600
7bf8ef19
GS
601int e820_get_num_entries(void)
602{
603 return e820_entries;
604}
605
606bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length)
607{
608 if (idx < e820_entries && e820_table[idx].type == cpu_to_le32(type)) {
609 *address = le64_to_cpu(e820_table[idx].address);
610 *length = le64_to_cpu(e820_table[idx].length);
611 return true;
612 }
613 return false;
614}
615
54a40293
EH
616/* Enables contiguous-apic-ID mode, for compatibility */
617static bool compat_apic_id_mode;
618
619void enable_compat_apic_id_mode(void)
620{
621 compat_apic_id_mode = true;
622}
623
624/* Calculates initial APIC ID for a specific CPU index
625 *
626 * Currently we need to be able to calculate the APIC ID from the CPU index
627 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
628 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
629 * all CPUs up to max_cpus.
630 */
631static uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index)
632{
633 uint32_t correct_id;
634 static bool warned;
635
636 correct_id = x86_apicid_from_cpu_idx(smp_cores, smp_threads, cpu_index);
637 if (compat_apic_id_mode) {
b1c12027 638 if (cpu_index != correct_id && !warned && !qtest_enabled()) {
54a40293
EH
639 error_report("APIC IDs set in compatibility mode, "
640 "CPU topology won't match the configuration");
641 warned = true;
642 }
643 return cpu_index;
644 } else {
645 return correct_id;
646 }
647}
648
1d934e89
EH
649/* Calculates the limit to CPU APIC ID values
650 *
651 * This function returns the limit for the APIC ID value, so that all
652 * CPU APIC IDs are < pc_apic_id_limit().
653 *
654 * This is used for FW_CFG_MAX_CPUS. See comments on bochs_bios_init().
655 */
656static unsigned int pc_apic_id_limit(unsigned int max_cpus)
657{
658 return x86_cpu_apic_id_from_index(max_cpus - 1) + 1;
659}
660
a88b362c 661static FWCfgState *bochs_bios_init(void)
80cabfad 662{
a88b362c 663 FWCfgState *fw_cfg;
c97294ec
GS
664 uint8_t *smbios_tables, *smbios_anchor;
665 size_t smbios_tables_len, smbios_anchor_len;
11c2fd3e
AL
666 uint64_t *numa_fw_cfg;
667 int i, j;
1d934e89 668 unsigned int apic_id_limit = pc_apic_id_limit(max_cpus);
3cce6243 669
66708822 670 fw_cfg = fw_cfg_init_io(BIOS_CFG_IOPORT);
1d934e89
EH
671 /* FW_CFG_MAX_CPUS is a bit confusing/problematic on x86:
672 *
673 * SeaBIOS needs FW_CFG_MAX_CPUS for CPU hotplug, but the CPU hotplug
674 * QEMU<->SeaBIOS interface is not based on the "CPU index", but on the APIC
675 * ID of hotplugged CPUs[1]. This means that FW_CFG_MAX_CPUS is not the
676 * "maximum number of CPUs", but the "limit to the APIC ID values SeaBIOS
677 * may see".
678 *
679 * So, this means we must not use max_cpus, here, but the maximum possible
680 * APIC ID value, plus one.
681 *
682 * [1] The only kind of "CPU identifier" used between SeaBIOS and QEMU is
683 * the APIC ID, not the "CPU index"
684 */
685 fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)apic_id_limit);
905fdcb5 686 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
089da572
MA
687 fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES,
688 acpi_tables, acpi_tables_len);
9b5b76d4 689 fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, kvm_allows_irq0_override());
b6f6e3d3 690
c97294ec
GS
691 smbios_tables = smbios_get_table_legacy(&smbios_tables_len);
692 if (smbios_tables) {
b6f6e3d3 693 fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
c97294ec
GS
694 smbios_tables, smbios_tables_len);
695 }
696
697 smbios_get_tables(&smbios_tables, &smbios_tables_len,
698 &smbios_anchor, &smbios_anchor_len);
699 if (smbios_anchor) {
700 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-tables",
701 smbios_tables, smbios_tables_len);
702 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-anchor",
703 smbios_anchor, smbios_anchor_len);
704 }
705
089da572 706 fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE,
7d67110f
GH
707 &e820_reserve, sizeof(e820_reserve));
708 fw_cfg_add_file(fw_cfg, "etc/e820", e820_table,
709 sizeof(struct e820_entry) * e820_entries);
11c2fd3e 710
089da572 711 fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg));
11c2fd3e
AL
712 /* allocate memory for the NUMA channel: one (64bit) word for the number
713 * of nodes, one word for each VCPU->node and one word for each node to
714 * hold the amount of memory.
715 */
1d934e89 716 numa_fw_cfg = g_new0(uint64_t, 1 + apic_id_limit + nb_numa_nodes);
11c2fd3e 717 numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
991dfefd 718 for (i = 0; i < max_cpus; i++) {
1d934e89
EH
719 unsigned int apic_id = x86_cpu_apic_id_from_index(i);
720 assert(apic_id < apic_id_limit);
11c2fd3e 721 for (j = 0; j < nb_numa_nodes; j++) {
8c85901e 722 if (test_bit(i, numa_info[j].node_cpu)) {
1d934e89 723 numa_fw_cfg[apic_id + 1] = cpu_to_le64(j);
11c2fd3e
AL
724 break;
725 }
726 }
727 }
728 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 729 numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(numa_info[i].node_mem);
11c2fd3e 730 }
089da572 731 fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
1d934e89
EH
732 (1 + apic_id_limit + nb_numa_nodes) *
733 sizeof(*numa_fw_cfg));
bf483392
AG
734
735 return fw_cfg;
80cabfad
FB
736}
737
642a4f96
TS
738static long get_file_size(FILE *f)
739{
740 long where, size;
741
742 /* XXX: on Unix systems, using fstat() probably makes more sense */
743
744 where = ftell(f);
745 fseek(f, 0, SEEK_END);
746 size = ftell(f);
747 fseek(f, where, SEEK_SET);
748
749 return size;
750}
751
a88b362c 752static void load_linux(FWCfgState *fw_cfg,
4fc9af53 753 const char *kernel_filename,
0f9d76e5
LG
754 const char *initrd_filename,
755 const char *kernel_cmdline,
a8170e5e 756 hwaddr max_ram_size)
642a4f96
TS
757{
758 uint16_t protocol;
5cea8590 759 int setup_size, kernel_size, initrd_size = 0, cmdline_size;
642a4f96 760 uint32_t initrd_max;
57a46d05 761 uint8_t header[8192], *setup, *kernel, *initrd_data;
a8170e5e 762 hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
45a50b16 763 FILE *f;
bf4e5d92 764 char *vmode;
642a4f96
TS
765
766 /* Align to 16 bytes as a paranoia measure */
767 cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
768
769 /* load the kernel header */
770 f = fopen(kernel_filename, "rb");
771 if (!f || !(kernel_size = get_file_size(f)) ||
0f9d76e5
LG
772 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
773 MIN(ARRAY_SIZE(header), kernel_size)) {
774 fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
775 kernel_filename, strerror(errno));
776 exit(1);
642a4f96
TS
777 }
778
779 /* kernel protocol version */
bc4edd79 780#if 0
642a4f96 781 fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
bc4edd79 782#endif
0f9d76e5
LG
783 if (ldl_p(header+0x202) == 0x53726448) {
784 protocol = lduw_p(header+0x206);
785 } else {
786 /* This looks like a multiboot kernel. If it is, let's stop
787 treating it like a Linux kernel. */
52001445 788 if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
0f9d76e5 789 kernel_cmdline, kernel_size, header)) {
82663ee2 790 return;
0f9d76e5
LG
791 }
792 protocol = 0;
f16408df 793 }
642a4f96
TS
794
795 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
0f9d76e5
LG
796 /* Low kernel */
797 real_addr = 0x90000;
798 cmdline_addr = 0x9a000 - cmdline_size;
799 prot_addr = 0x10000;
642a4f96 800 } else if (protocol < 0x202) {
0f9d76e5
LG
801 /* High but ancient kernel */
802 real_addr = 0x90000;
803 cmdline_addr = 0x9a000 - cmdline_size;
804 prot_addr = 0x100000;
642a4f96 805 } else {
0f9d76e5
LG
806 /* High and recent kernel */
807 real_addr = 0x10000;
808 cmdline_addr = 0x20000;
809 prot_addr = 0x100000;
642a4f96
TS
810 }
811
bc4edd79 812#if 0
642a4f96 813 fprintf(stderr,
0f9d76e5
LG
814 "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
815 "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
816 "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
817 real_addr,
818 cmdline_addr,
819 prot_addr);
bc4edd79 820#endif
642a4f96
TS
821
822 /* highest address for loading the initrd */
0f9d76e5
LG
823 if (protocol >= 0x203) {
824 initrd_max = ldl_p(header+0x22c);
825 } else {
826 initrd_max = 0x37ffffff;
827 }
642a4f96 828
927766c7
MT
829 if (initrd_max >= max_ram_size - acpi_data_size) {
830 initrd_max = max_ram_size - acpi_data_size - 1;
831 }
642a4f96 832
57a46d05
AG
833 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
834 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
96f80586 835 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
642a4f96
TS
836
837 if (protocol >= 0x202) {
0f9d76e5 838 stl_p(header+0x228, cmdline_addr);
642a4f96 839 } else {
0f9d76e5
LG
840 stw_p(header+0x20, 0xA33F);
841 stw_p(header+0x22, cmdline_addr-real_addr);
642a4f96
TS
842 }
843
bf4e5d92
PT
844 /* handle vga= parameter */
845 vmode = strstr(kernel_cmdline, "vga=");
846 if (vmode) {
847 unsigned int video_mode;
848 /* skip "vga=" */
849 vmode += 4;
850 if (!strncmp(vmode, "normal", 6)) {
851 video_mode = 0xffff;
852 } else if (!strncmp(vmode, "ext", 3)) {
853 video_mode = 0xfffe;
854 } else if (!strncmp(vmode, "ask", 3)) {
855 video_mode = 0xfffd;
856 } else {
857 video_mode = strtol(vmode, NULL, 0);
858 }
859 stw_p(header+0x1fa, video_mode);
860 }
861
642a4f96 862 /* loader type */
5cbdb3a3 863 /* High nybble = B reserved for QEMU; low nybble is revision number.
642a4f96
TS
864 If this code is substantially changed, you may want to consider
865 incrementing the revision. */
0f9d76e5
LG
866 if (protocol >= 0x200) {
867 header[0x210] = 0xB0;
868 }
642a4f96
TS
869 /* heap */
870 if (protocol >= 0x201) {
0f9d76e5
LG
871 header[0x211] |= 0x80; /* CAN_USE_HEAP */
872 stw_p(header+0x224, cmdline_addr-real_addr-0x200);
642a4f96
TS
873 }
874
875 /* load initrd */
876 if (initrd_filename) {
0f9d76e5
LG
877 if (protocol < 0x200) {
878 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
879 exit(1);
880 }
642a4f96 881
0f9d76e5 882 initrd_size = get_image_size(initrd_filename);
d6fa4b77 883 if (initrd_size < 0) {
7454e51d
MT
884 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
885 initrd_filename, strerror(errno));
d6fa4b77
MK
886 exit(1);
887 }
888
45a50b16 889 initrd_addr = (initrd_max-initrd_size) & ~4095;
57a46d05 890
7267c094 891 initrd_data = g_malloc(initrd_size);
57a46d05
AG
892 load_image(initrd_filename, initrd_data);
893
894 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
895 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
896 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
642a4f96 897
0f9d76e5
LG
898 stl_p(header+0x218, initrd_addr);
899 stl_p(header+0x21c, initrd_size);
642a4f96
TS
900 }
901
45a50b16 902 /* load kernel and setup */
642a4f96 903 setup_size = header[0x1f1];
0f9d76e5
LG
904 if (setup_size == 0) {
905 setup_size = 4;
906 }
642a4f96 907 setup_size = (setup_size+1)*512;
45a50b16 908 kernel_size -= setup_size;
642a4f96 909
7267c094
AL
910 setup = g_malloc(setup_size);
911 kernel = g_malloc(kernel_size);
45a50b16 912 fseek(f, 0, SEEK_SET);
5a41ecc5
KS
913 if (fread(setup, 1, setup_size, f) != setup_size) {
914 fprintf(stderr, "fread() failed\n");
915 exit(1);
916 }
917 if (fread(kernel, 1, kernel_size, f) != kernel_size) {
918 fprintf(stderr, "fread() failed\n");
919 exit(1);
920 }
642a4f96 921 fclose(f);
45a50b16 922 memcpy(setup, header, MIN(sizeof(header), setup_size));
57a46d05
AG
923
924 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
925 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
926 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
927
928 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
929 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
930 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
931
2e55e842
GN
932 option_rom[nb_option_roms].name = "linuxboot.bin";
933 option_rom[nb_option_roms].bootindex = 0;
57a46d05 934 nb_option_roms++;
642a4f96
TS
935}
936
b41a2cd1
FB
937#define NE2000_NB_MAX 6
938
675d6f82
BS
939static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
940 0x280, 0x380 };
941static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
b41a2cd1 942
48a18b3c 943void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
a41b2ff2
PB
944{
945 static int nb_ne2k = 0;
946
947 if (nb_ne2k == NE2000_NB_MAX)
948 return;
48a18b3c 949 isa_ne2000_init(bus, ne2000_io[nb_ne2k],
9453c5bc 950 ne2000_irq[nb_ne2k], nd);
a41b2ff2
PB
951 nb_ne2k++;
952}
953
92a16d7a 954DeviceState *cpu_get_current_apic(void)
0e26b7b8 955{
4917cf44
AF
956 if (current_cpu) {
957 X86CPU *cpu = X86_CPU(current_cpu);
02e51483 958 return cpu->apic_state;
0e26b7b8
BS
959 } else {
960 return NULL;
961 }
962}
963
845773ab 964void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
53b67b30 965{
c3affe56 966 X86CPU *cpu = opaque;
53b67b30
BS
967
968 if (level) {
c3affe56 969 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
53b67b30
BS
970 }
971}
972
62fc403f
IM
973static X86CPU *pc_new_cpu(const char *cpu_model, int64_t apic_id,
974 DeviceState *icc_bridge, Error **errp)
31050930 975{
e1570d00 976 X86CPU *cpu = NULL;
31050930
IM
977 Error *local_err = NULL;
978
e1570d00
EH
979 if (icc_bridge == NULL) {
980 error_setg(&local_err, "Invalid icc-bridge value");
981 goto out;
982 }
983
984 cpu = cpu_x86_create(cpu_model, &local_err);
cd7b87ff 985 if (local_err != NULL) {
e1570d00 986 goto out;
31050930
IM
987 }
988
e1570d00 989 qdev_set_parent_bus(DEVICE(cpu), qdev_get_child_bus(icc_bridge, "icc"));
e1570d00 990
31050930
IM
991 object_property_set_int(OBJECT(cpu), apic_id, "apic-id", &local_err);
992 object_property_set_bool(OBJECT(cpu), true, "realized", &local_err);
993
e1570d00 994out:
31050930 995 if (local_err) {
31050930 996 error_propagate(errp, local_err);
cd7b87ff
AF
997 object_unref(OBJECT(cpu));
998 cpu = NULL;
31050930
IM
999 }
1000 return cpu;
1001}
1002
c649983b
IM
1003static const char *current_cpu_model;
1004
1005void pc_hot_add_cpu(const int64_t id, Error **errp)
1006{
1007 DeviceState *icc_bridge;
0e3bd562 1008 X86CPU *cpu;
c649983b 1009 int64_t apic_id = x86_cpu_apic_id_from_index(id);
0e3bd562 1010 Error *local_err = NULL;
c649983b 1011
8de433cb
IM
1012 if (id < 0) {
1013 error_setg(errp, "Invalid CPU id: %" PRIi64, id);
1014 return;
1015 }
1016
c649983b
IM
1017 if (cpu_exists(apic_id)) {
1018 error_setg(errp, "Unable to add CPU: %" PRIi64
1019 ", it already exists", id);
1020 return;
1021 }
1022
1023 if (id >= max_cpus) {
1024 error_setg(errp, "Unable to add CPU: %" PRIi64
1025 ", max allowed: %d", id, max_cpus - 1);
1026 return;
1027 }
1028
5ff020b7
EH
1029 if (apic_id >= ACPI_CPU_HOTPLUG_ID_LIMIT) {
1030 error_setg(errp, "Unable to add CPU: %" PRIi64
1031 ", resulting APIC ID (%" PRIi64 ") is too large",
1032 id, apic_id);
1033 return;
1034 }
1035
c649983b
IM
1036 icc_bridge = DEVICE(object_resolve_path_type("icc-bridge",
1037 TYPE_ICC_BRIDGE, NULL));
0e3bd562
AF
1038 cpu = pc_new_cpu(current_cpu_model, apic_id, icc_bridge, &local_err);
1039 if (local_err) {
1040 error_propagate(errp, local_err);
1041 return;
1042 }
1043 object_unref(OBJECT(cpu));
c649983b
IM
1044}
1045
62fc403f 1046void pc_cpus_init(const char *cpu_model, DeviceState *icc_bridge)
70166477
IY
1047{
1048 int i;
53a89e26 1049 X86CPU *cpu = NULL;
31050930 1050 Error *error = NULL;
f03bd716 1051 unsigned long apic_id_limit;
70166477
IY
1052
1053 /* init CPUs */
1054 if (cpu_model == NULL) {
1055#ifdef TARGET_X86_64
1056 cpu_model = "qemu64";
1057#else
1058 cpu_model = "qemu32";
1059#endif
1060 }
c649983b 1061 current_cpu_model = cpu_model;
70166477 1062
f03bd716
EH
1063 apic_id_limit = pc_apic_id_limit(max_cpus);
1064 if (apic_id_limit > ACPI_CPU_HOTPLUG_ID_LIMIT) {
1065 error_report("max_cpus is too large. APIC ID of last CPU is %lu",
1066 apic_id_limit - 1);
1067 exit(1);
1068 }
1069
bdeec802 1070 for (i = 0; i < smp_cpus; i++) {
53a89e26
IM
1071 cpu = pc_new_cpu(cpu_model, x86_cpu_apic_id_from_index(i),
1072 icc_bridge, &error);
31050930 1073 if (error) {
565f65d2 1074 error_report_err(error);
bdeec802
IM
1075 exit(1);
1076 }
0e3bd562 1077 object_unref(OBJECT(cpu));
70166477 1078 }
53a89e26
IM
1079
1080 /* map APIC MMIO area if CPU has APIC */
02e51483 1081 if (cpu && cpu->apic_state) {
53a89e26
IM
1082 /* XXX: what if the base changes? */
1083 sysbus_mmio_map_overlap(SYS_BUS_DEVICE(icc_bridge), 0,
1084 APIC_DEFAULT_ADDRESS, 0x1000);
1085 }
c97294ec
GS
1086
1087 /* tell smbios about cpuid version and features */
1088 smbios_set_cpuid(cpu->env.cpuid_version, cpu->env.features[FEAT_1_EDX]);
70166477
IY
1089}
1090
f8c457b8
MT
1091/* pci-info ROM file. Little endian format */
1092typedef struct PcRomPciInfo {
1093 uint64_t w32_min;
1094 uint64_t w32_max;
1095 uint64_t w64_min;
1096 uint64_t w64_max;
1097} PcRomPciInfo;
1098
3459a625
MT
1099typedef struct PcGuestInfoState {
1100 PcGuestInfo info;
1101 Notifier machine_done;
1102} PcGuestInfoState;
1103
1104static
1105void pc_guest_info_machine_done(Notifier *notifier, void *data)
1106{
1107 PcGuestInfoState *guest_info_state = container_of(notifier,
1108 PcGuestInfoState,
1109 machine_done);
2118196b
MA
1110 PCIBus *bus = find_i440fx();
1111
1112 if (bus) {
1113 int extra_hosts = 0;
1114
1115 QLIST_FOREACH(bus, &bus->child, sibling) {
1116 /* look for expander root buses */
1117 if (pci_bus_is_root(bus)) {
1118 extra_hosts++;
1119 }
1120 }
1121 if (extra_hosts && guest_info_state->info.fw_cfg) {
1122 uint64_t *val = g_malloc(sizeof(*val));
1123 *val = cpu_to_le64(extra_hosts);
1124 fw_cfg_add_file(guest_info_state->info.fw_cfg,
1125 "etc/extra-pci-roots", val, sizeof(*val));
1126 }
1127 }
1128
72c194f7 1129 acpi_setup(&guest_info_state->info);
3459a625
MT
1130}
1131
1132PcGuestInfo *pc_guest_info_init(ram_addr_t below_4g_mem_size,
1133 ram_addr_t above_4g_mem_size)
1134{
1135 PcGuestInfoState *guest_info_state = g_malloc0(sizeof *guest_info_state);
1136 PcGuestInfo *guest_info = &guest_info_state->info;
b20c9bd5
MT
1137 int i, j;
1138
f30ee8a9 1139 guest_info->ram_size_below_4g = below_4g_mem_size;
b20c9bd5
MT
1140 guest_info->ram_size = below_4g_mem_size + above_4g_mem_size;
1141 guest_info->apic_id_limit = pc_apic_id_limit(max_cpus);
1142 guest_info->apic_xrupt_override = kvm_allows_irq0_override();
1143 guest_info->numa_nodes = nb_numa_nodes;
8c85901e 1144 guest_info->node_mem = g_malloc0(guest_info->numa_nodes *
b20c9bd5 1145 sizeof *guest_info->node_mem);
8c85901e
WG
1146 for (i = 0; i < nb_numa_nodes; i++) {
1147 guest_info->node_mem[i] = numa_info[i].node_mem;
1148 }
1149
b20c9bd5
MT
1150 guest_info->node_cpu = g_malloc0(guest_info->apic_id_limit *
1151 sizeof *guest_info->node_cpu);
1152
1153 for (i = 0; i < max_cpus; i++) {
1154 unsigned int apic_id = x86_cpu_apic_id_from_index(i);
1155 assert(apic_id < guest_info->apic_id_limit);
1156 for (j = 0; j < nb_numa_nodes; j++) {
8c85901e 1157 if (test_bit(i, numa_info[j].node_cpu)) {
b20c9bd5
MT
1158 guest_info->node_cpu[apic_id] = j;
1159 break;
1160 }
1161 }
1162 }
3459a625 1163
3459a625
MT
1164 guest_info_state->machine_done.notify = pc_guest_info_machine_done;
1165 qemu_add_machine_init_done_notifier(&guest_info_state->machine_done);
1166 return guest_info;
1167}
1168
83d08f26
MT
1169/* setup pci memory address space mapping into system address space */
1170void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
1171 MemoryRegion *pci_address_space)
39848901 1172{
83d08f26
MT
1173 /* Set to lower priority than RAM */
1174 memory_region_add_subregion_overlap(system_memory, 0x0,
1175 pci_address_space, -1);
39848901
IM
1176}
1177
f7e4dd6c
GH
1178void pc_acpi_init(const char *default_dsdt)
1179{
c5a98cf3 1180 char *filename;
f7e4dd6c
GH
1181
1182 if (acpi_tables != NULL) {
1183 /* manually set via -acpitable, leave it alone */
1184 return;
1185 }
1186
1187 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, default_dsdt);
1188 if (filename == NULL) {
1189 fprintf(stderr, "WARNING: failed to find %s\n", default_dsdt);
c5a98cf3 1190 } else {
5bdb59a2
MA
1191 QemuOpts *opts = qemu_opts_create(qemu_find_opts("acpi"), NULL, 0,
1192 &error_abort);
c5a98cf3 1193 Error *err = NULL;
f7e4dd6c 1194
5bdb59a2 1195 qemu_opt_set(opts, "file", filename, &error_abort);
0c764a9d 1196
1a4b2666 1197 acpi_table_add_builtin(opts, &err);
c5a98cf3 1198 if (err) {
4a44d85e
SA
1199 error_report("WARNING: failed to load %s: %s", filename,
1200 error_get_pretty(err));
c5a98cf3
LE
1201 error_free(err);
1202 }
c5a98cf3 1203 g_free(filename);
f7e4dd6c 1204 }
f7e4dd6c
GH
1205}
1206
b33a5bbf
CL
1207FWCfgState *xen_load_linux(const char *kernel_filename,
1208 const char *kernel_cmdline,
1209 const char *initrd_filename,
1210 ram_addr_t below_4g_mem_size,
1211 PcGuestInfo *guest_info)
1212{
1213 int i;
1214 FWCfgState *fw_cfg;
1215
1216 assert(kernel_filename != NULL);
1217
66708822 1218 fw_cfg = fw_cfg_init_io(BIOS_CFG_IOPORT);
b33a5bbf
CL
1219 rom_set_fw(fw_cfg);
1220
1221 load_linux(fw_cfg, kernel_filename, initrd_filename,
1222 kernel_cmdline, below_4g_mem_size);
1223 for (i = 0; i < nb_option_roms; i++) {
1224 assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
1225 !strcmp(option_rom[i].name, "multiboot.bin"));
1226 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1227 }
1228 guest_info->fw_cfg = fw_cfg;
1229 return fw_cfg;
1230}
1231
9521d42b
PB
1232FWCfgState *pc_memory_init(MachineState *machine,
1233 MemoryRegion *system_memory,
a88b362c
LE
1234 ram_addr_t below_4g_mem_size,
1235 ram_addr_t above_4g_mem_size,
1236 MemoryRegion *rom_memory,
3459a625
MT
1237 MemoryRegion **ram_memory,
1238 PcGuestInfo *guest_info)
80cabfad 1239{
cbc5b5f3
JJ
1240 int linux_boot, i;
1241 MemoryRegion *ram, *option_rom_mr;
00cb2a99 1242 MemoryRegion *ram_below_4g, *ram_above_4g;
a88b362c 1243 FWCfgState *fw_cfg;
619d11e4 1244 PCMachineState *pcms = PC_MACHINE(machine);
d592d303 1245
9521d42b
PB
1246 assert(machine->ram_size == below_4g_mem_size + above_4g_mem_size);
1247
1248 linux_boot = (machine->kernel_filename != NULL);
80cabfad 1249
00cb2a99 1250 /* Allocate RAM. We allocate it as a single memory region and use
66a0a2cb 1251 * aliases to address portions of it, mostly for backwards compatibility
00cb2a99
AK
1252 * with older qemus that used qemu_ram_alloc().
1253 */
7267c094 1254 ram = g_malloc(sizeof(*ram));
9521d42b
PB
1255 memory_region_allocate_system_memory(ram, NULL, "pc.ram",
1256 machine->ram_size);
ae0a5466 1257 *ram_memory = ram;
7267c094 1258 ram_below_4g = g_malloc(sizeof(*ram_below_4g));
2c9b15ca 1259 memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
00cb2a99
AK
1260 0, below_4g_mem_size);
1261 memory_region_add_subregion(system_memory, 0, ram_below_4g);
7db16f24 1262 e820_add_entry(0, below_4g_mem_size, E820_RAM);
bbe80adf 1263 if (above_4g_mem_size > 0) {
7267c094 1264 ram_above_4g = g_malloc(sizeof(*ram_above_4g));
2c9b15ca 1265 memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
00cb2a99
AK
1266 below_4g_mem_size, above_4g_mem_size);
1267 memory_region_add_subregion(system_memory, 0x100000000ULL,
1268 ram_above_4g);
0624c7f9 1269 e820_add_entry(0x100000000ULL, above_4g_mem_size, E820_RAM);
bbe80adf 1270 }
82b36dc3 1271
ca8336f3
IM
1272 if (!guest_info->has_reserved_memory &&
1273 (machine->ram_slots ||
9521d42b 1274 (machine->maxram_size > machine->ram_size))) {
ca8336f3
IM
1275 MachineClass *mc = MACHINE_GET_CLASS(machine);
1276
1277 error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
1278 mc->name);
1279 exit(EXIT_FAILURE);
1280 }
1281
619d11e4 1282 /* initialize hotplug memory address space */
de268e13 1283 if (guest_info->has_reserved_memory &&
9521d42b 1284 (machine->ram_size < machine->maxram_size)) {
619d11e4 1285 ram_addr_t hotplug_mem_size =
9521d42b 1286 machine->maxram_size - machine->ram_size;
619d11e4 1287
a0cc8856
IM
1288 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
1289 error_report("unsupported amount of memory slots: %"PRIu64,
1290 machine->ram_slots);
1291 exit(EXIT_FAILURE);
1292 }
1293
f2c38522
PK
1294 if (QEMU_ALIGN_UP(machine->maxram_size,
1295 TARGET_PAGE_SIZE) != machine->maxram_size) {
1296 error_report("maximum memory size must by aligned to multiple of "
1297 "%d bytes", TARGET_PAGE_SIZE);
1298 exit(EXIT_FAILURE);
1299 }
1300
619d11e4
IM
1301 pcms->hotplug_memory_base =
1302 ROUND_UP(0x100000000ULL + above_4g_mem_size, 1ULL << 30);
1303
085f8e88
IM
1304 if (pcms->enforce_aligned_dimm) {
1305 /* size hotplug region assuming 1G page max alignment per slot */
1306 hotplug_mem_size += (1ULL << 30) * machine->ram_slots;
1307 }
1308
619d11e4
IM
1309 if ((pcms->hotplug_memory_base + hotplug_mem_size) <
1310 hotplug_mem_size) {
1311 error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
1312 machine->maxram_size);
1313 exit(EXIT_FAILURE);
1314 }
1315
1316 memory_region_init(&pcms->hotplug_memory, OBJECT(pcms),
1317 "hotplug-memory", hotplug_mem_size);
1318 memory_region_add_subregion(system_memory, pcms->hotplug_memory_base,
1319 &pcms->hotplug_memory);
1320 }
cbc5b5f3
JJ
1321
1322 /* Initialize PC system firmware */
6dd2a5c9 1323 pc_system_firmware_init(rom_memory, guest_info->isapc_ram_fw);
00cb2a99 1324
7267c094 1325 option_rom_mr = g_malloc(sizeof(*option_rom_mr));
49946538
HT
1326 memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
1327 &error_abort);
c5705a77 1328 vmstate_register_ram_global(option_rom_mr);
4463aee6 1329 memory_region_add_subregion_overlap(rom_memory,
00cb2a99
AK
1330 PC_ROM_MIN_VGA,
1331 option_rom_mr,
1332 1);
f753ff16 1333
bf483392 1334 fw_cfg = bochs_bios_init();
8832cb80 1335 rom_set_fw(fw_cfg);
1d108d97 1336
de268e13
IM
1337 if (guest_info->has_reserved_memory && pcms->hotplug_memory_base) {
1338 uint64_t *val = g_malloc(sizeof(*val));
1339 *val = cpu_to_le64(ROUND_UP(pcms->hotplug_memory_base, 0x1ULL << 30));
1340 fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
1341 }
1342
f753ff16 1343 if (linux_boot) {
9521d42b
PB
1344 load_linux(fw_cfg, machine->kernel_filename, machine->initrd_filename,
1345 machine->kernel_cmdline, below_4g_mem_size);
f753ff16
PB
1346 }
1347
1348 for (i = 0; i < nb_option_roms; i++) {
2e55e842 1349 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
406c8df3 1350 }
3459a625 1351 guest_info->fw_cfg = fw_cfg;
459ae5ea 1352 return fw_cfg;
3d53f5c3
IY
1353}
1354
0b0cc076 1355qemu_irq pc_allocate_cpu_irq(void)
845773ab 1356{
0b0cc076 1357 return qemu_allocate_irq(pic_irq_request, NULL, 0);
845773ab
IY
1358}
1359
48a18b3c 1360DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
765d7908 1361{
ad6d45fa
AL
1362 DeviceState *dev = NULL;
1363
16094b75
AJ
1364 if (pci_bus) {
1365 PCIDevice *pcidev = pci_vga_init(pci_bus);
1366 dev = pcidev ? &pcidev->qdev : NULL;
1367 } else if (isa_bus) {
1368 ISADevice *isadev = isa_vga_init(isa_bus);
4a17cc4f 1369 dev = isadev ? DEVICE(isadev) : NULL;
765d7908 1370 }
ad6d45fa 1371 return dev;
765d7908
IY
1372}
1373
4556bd8b
BS
1374static void cpu_request_exit(void *opaque, int irq, int level)
1375{
4917cf44 1376 CPUState *cpu = current_cpu;
4556bd8b 1377
4917cf44
AF
1378 if (cpu && level) {
1379 cpu_exit(cpu);
4556bd8b
BS
1380 }
1381}
1382
258711c6
JG
1383static const MemoryRegionOps ioport80_io_ops = {
1384 .write = ioport80_write,
c02e1eac 1385 .read = ioport80_read,
258711c6
JG
1386 .endianness = DEVICE_NATIVE_ENDIAN,
1387 .impl = {
1388 .min_access_size = 1,
1389 .max_access_size = 1,
1390 },
1391};
1392
1393static const MemoryRegionOps ioportF0_io_ops = {
1394 .write = ioportF0_write,
c02e1eac 1395 .read = ioportF0_read,
258711c6
JG
1396 .endianness = DEVICE_NATIVE_ENDIAN,
1397 .impl = {
1398 .min_access_size = 1,
1399 .max_access_size = 1,
1400 },
1401};
1402
48a18b3c 1403void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
1611977c 1404 ISADevice **rtc_state,
fd53c87c 1405 bool create_fdctrl,
34d4260e 1406 ISADevice **floppy,
7a10ef51
LPF
1407 bool no_vmport,
1408 uint32 hpet_irqs)
ffe513da
IY
1409{
1410 int i;
1411 DriveInfo *fd[MAX_FD];
ce967e2f
JK
1412 DeviceState *hpet = NULL;
1413 int pit_isa_irq = 0;
1414 qemu_irq pit_alt_irq = NULL;
7d932dfd 1415 qemu_irq rtc_irq = NULL;
956a3e6b 1416 qemu_irq *a20_line;
c2d8d311 1417 ISADevice *i8042, *port92, *vmmouse, *pit = NULL;
4556bd8b 1418 qemu_irq *cpu_exit_irq;
258711c6
JG
1419 MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1420 MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
ffe513da 1421
2c9b15ca 1422 memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
258711c6 1423 memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
ffe513da 1424
2c9b15ca 1425 memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
258711c6 1426 memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
ffe513da 1427
5d17c0d2
JK
1428 /*
1429 * Check if an HPET shall be created.
1430 *
1431 * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1432 * when the HPET wants to take over. Thus we have to disable the latter.
1433 */
1434 if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
7a10ef51 1435 /* In order to set property, here not using sysbus_try_create_simple */
51116102 1436 hpet = qdev_try_create(NULL, TYPE_HPET);
dd703b99 1437 if (hpet) {
7a10ef51
LPF
1438 /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
1439 * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
1440 * IRQ8 and IRQ2.
1441 */
1442 uint8_t compat = object_property_get_int(OBJECT(hpet),
1443 HPET_INTCAP, NULL);
1444 if (!compat) {
1445 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
1446 }
1447 qdev_init_nofail(hpet);
1448 sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
1449
b881fbe9 1450 for (i = 0; i < GSI_NUM_PINS; i++) {
1356b98d 1451 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
dd703b99 1452 }
ce967e2f
JK
1453 pit_isa_irq = -1;
1454 pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1455 rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
822557eb 1456 }
ffe513da 1457 }
48a18b3c 1458 *rtc_state = rtc_init(isa_bus, 2000, rtc_irq);
7d932dfd
JK
1459
1460 qemu_register_boot_set(pc_boot_set, *rtc_state);
1461
c2d8d311
SS
1462 if (!xen_enabled()) {
1463 if (kvm_irqchip_in_kernel()) {
1464 pit = kvm_pit_init(isa_bus, 0x40);
1465 } else {
1466 pit = pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1467 }
1468 if (hpet) {
1469 /* connect PIT to output control line of the HPET */
4a17cc4f 1470 qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
c2d8d311
SS
1471 }
1472 pcspk_init(isa_bus, pit);
ce967e2f 1473 }
ffe513da 1474
b6607a1a 1475 serial_hds_isa_init(isa_bus, MAX_SERIAL_PORTS);
07dc7880 1476 parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
ffe513da 1477
182735ef 1478 a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
48a18b3c 1479 i8042 = isa_create_simple(isa_bus, "i8042");
4b78a802 1480 i8042_setup_a20_line(i8042, &a20_line[0]);
1611977c 1481 if (!no_vmport) {
48a18b3c
HP
1482 vmport_init(isa_bus);
1483 vmmouse = isa_try_create(isa_bus, "vmmouse");
1611977c
AP
1484 } else {
1485 vmmouse = NULL;
1486 }
86d86414 1487 if (vmmouse) {
4a17cc4f
AF
1488 DeviceState *dev = DEVICE(vmmouse);
1489 qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
1490 qdev_init_nofail(dev);
86d86414 1491 }
48a18b3c 1492 port92 = isa_create_simple(isa_bus, "port92");
4b78a802 1493 port92_init(port92, &a20_line[1]);
956a3e6b 1494
4556bd8b
BS
1495 cpu_exit_irq = qemu_allocate_irqs(cpu_request_exit, NULL, 1);
1496 DMA_init(0, cpu_exit_irq);
ffe513da
IY
1497
1498 for(i = 0; i < MAX_FD; i++) {
1499 fd[i] = drive_get(IF_FLOPPY, 0, i);
936a7c1c 1500 create_fdctrl |= !!fd[i];
ffe513da 1501 }
fd53c87c 1502 *floppy = create_fdctrl ? fdctrl_init_isa(isa_bus, fd) : NULL;
ffe513da
IY
1503}
1504
9011a1a7
IY
1505void pc_nic_init(ISABus *isa_bus, PCIBus *pci_bus)
1506{
1507 int i;
1508
1509 for (i = 0; i < nb_nics; i++) {
1510 NICInfo *nd = &nd_table[i];
1511
1512 if (!pci_bus || (nd->model && strcmp(nd->model, "ne2k_isa") == 0)) {
1513 pc_init_ne2k_isa(isa_bus, nd);
1514 } else {
29b358f9 1515 pci_nic_init_nofail(nd, pci_bus, "e1000", NULL);
9011a1a7
IY
1516 }
1517 }
1518}
1519
845773ab 1520void pc_pci_device_init(PCIBus *pci_bus)
e3a5cf42
IY
1521{
1522 int max_bus;
1523 int bus;
1524
1525 max_bus = drive_get_max_bus(IF_SCSI);
1526 for (bus = 0; bus <= max_bus; bus++) {
1527 pci_create_simple(pci_bus, -1, "lsi53c895a");
1528 }
1529}
a39e3564
JB
1530
1531void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
1532{
1533 DeviceState *dev;
1534 SysBusDevice *d;
1535 unsigned int i;
1536
1537 if (kvm_irqchip_in_kernel()) {
1538 dev = qdev_create(NULL, "kvm-ioapic");
1539 } else {
1540 dev = qdev_create(NULL, "ioapic");
1541 }
1542 if (parent_name) {
1543 object_property_add_child(object_resolve_path(parent_name, NULL),
1544 "ioapic", OBJECT(dev), NULL);
1545 }
1546 qdev_init_nofail(dev);
1356b98d 1547 d = SYS_BUS_DEVICE(dev);
3a4a4697 1548 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
a39e3564
JB
1549
1550 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1551 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
1552 }
1553}
d5747cac 1554
95bee274
IM
1555static void pc_dimm_plug(HotplugHandler *hotplug_dev,
1556 DeviceState *dev, Error **errp)
1557{
0cd03d89 1558 int slot;
3fbcdc27 1559 HotplugHandlerClass *hhc;
95bee274
IM
1560 Error *local_err = NULL;
1561 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
0cd03d89 1562 MachineState *machine = MACHINE(hotplug_dev);
95bee274
IM
1563 PCDIMMDevice *dimm = PC_DIMM(dev);
1564 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
1565 MemoryRegion *mr = ddc->get_memory_region(dimm);
b03541fa 1566 uint64_t existing_dimms_capacity = 0;
92a37a04 1567 uint64_t align = TARGET_PAGE_SIZE;
34dde136
IM
1568 uint64_t addr;
1569
1570 addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
95bee274
IM
1571 if (local_err) {
1572 goto out;
1573 }
1574
91aa70ab
IM
1575 if (memory_region_get_alignment(mr) && pcms->enforce_aligned_dimm) {
1576 align = memory_region_get_alignment(mr);
1577 }
1578
0b312571
IM
1579 addr = pc_dimm_get_free_addr(pcms->hotplug_memory_base,
1580 memory_region_size(&pcms->hotplug_memory),
92a37a04 1581 !addr ? NULL : &addr, align,
0b312571
IM
1582 memory_region_size(mr), &local_err);
1583 if (local_err) {
1584 goto out;
1585 }
0cd03d89 1586
37153450
BR
1587 existing_dimms_capacity = pc_existing_dimms_capacity(&local_err);
1588 if (local_err) {
b03541fa
IM
1589 goto out;
1590 }
1591
1592 if (existing_dimms_capacity + memory_region_size(mr) >
1593 machine->maxram_size - machine->ram_size) {
1594 error_setg(&local_err, "not enough space, currently 0x%" PRIx64
759048ac
BR
1595 " in use of total hot pluggable 0x" RAM_ADDR_FMT,
1596 existing_dimms_capacity,
1597 machine->maxram_size - machine->ram_size);
b03541fa
IM
1598 goto out;
1599 }
1600
0b312571 1601 object_property_set_int(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err);
0cd03d89
IM
1602 if (local_err) {
1603 goto out;
1604 }
2e1ac493 1605 trace_mhp_pc_dimm_assigned_address(addr);
0cd03d89
IM
1606
1607 slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, &local_err);
1608 if (local_err) {
1609 goto out;
1610 }
1611
1612 slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot,
1613 machine->ram_slots, &local_err);
1614 if (local_err) {
1615 goto out;
1616 }
1617 object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &local_err);
1618 if (local_err) {
1619 goto out;
1620 }
2e1ac493 1621 trace_mhp_pc_dimm_assigned_slot(slot);
0b312571 1622
3fbcdc27
IM
1623 if (!pcms->acpi_dev) {
1624 error_setg(&local_err,
1625 "memory hotplug is not enabled: missing acpi device");
1626 goto out;
1627 }
1628
b8865591
IM
1629 if (kvm_enabled() && !kvm_has_free_slot(machine)) {
1630 error_setg(&local_err, "hypervisor has no free memory slots left");
1631 goto out;
1632 }
1633
95bee274
IM
1634 memory_region_add_subregion(&pcms->hotplug_memory,
1635 addr - pcms->hotplug_memory_base, mr);
1636 vmstate_register_ram(mr, dev);
3fbcdc27
IM
1637
1638 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
1639 hhc->plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
95bee274
IM
1640out:
1641 error_propagate(errp, local_err);
1642}
1643
64fec58e
TC
1644static void pc_dimm_unplug_request(HotplugHandler *hotplug_dev,
1645 DeviceState *dev, Error **errp)
1646{
1647 HotplugHandlerClass *hhc;
1648 Error *local_err = NULL;
1649 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1650
1651 if (!pcms->acpi_dev) {
1652 error_setg(&local_err,
1653 "memory hotplug is not enabled: missing acpi device");
1654 goto out;
1655 }
1656
1657 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
1658 hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1659
1660out:
1661 error_propagate(errp, local_err);
1662}
1663
f7d3e29d
TC
1664static void pc_dimm_unplug(HotplugHandler *hotplug_dev,
1665 DeviceState *dev, Error **errp)
1666{
1667 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1668 PCDIMMDevice *dimm = PC_DIMM(dev);
1669 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
1670 MemoryRegion *mr = ddc->get_memory_region(dimm);
1671 HotplugHandlerClass *hhc;
1672 Error *local_err = NULL;
1673
1674 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
1675 hhc->unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1676
1677 if (local_err) {
1678 goto out;
1679 }
1680
1681 memory_region_del_subregion(&pcms->hotplug_memory, mr);
1682 vmstate_unregister_ram(mr, dev);
1683
1684 object_unparent(OBJECT(dev));
1685
1686 out:
1687 error_propagate(errp, local_err);
1688}
1689
5279569e
GZ
1690static void pc_cpu_plug(HotplugHandler *hotplug_dev,
1691 DeviceState *dev, Error **errp)
1692{
1693 HotplugHandlerClass *hhc;
1694 Error *local_err = NULL;
1695 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1696
1697 if (!dev->hotplugged) {
1698 goto out;
1699 }
1700
1701 if (!pcms->acpi_dev) {
1702 error_setg(&local_err,
1703 "cpu hotplug is not enabled: missing acpi device");
1704 goto out;
1705 }
1706
1707 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
1708 hhc->plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2d996150
GZ
1709 if (local_err) {
1710 goto out;
1711 }
1712
1713 /* increment the number of CPUs */
1714 rtc_set_memory(pcms->rtc, 0x5f, rtc_get_memory(pcms->rtc, 0x5f) + 1);
5279569e
GZ
1715out:
1716 error_propagate(errp, local_err);
1717}
1718
95bee274
IM
1719static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
1720 DeviceState *dev, Error **errp)
1721{
1722 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1723 pc_dimm_plug(hotplug_dev, dev, errp);
5279569e
GZ
1724 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1725 pc_cpu_plug(hotplug_dev, dev, errp);
95bee274
IM
1726 }
1727}
1728
d9c5c5b8
TC
1729static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
1730 DeviceState *dev, Error **errp)
1731{
64fec58e
TC
1732 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1733 pc_dimm_unplug_request(hotplug_dev, dev, errp);
1734 } else {
1735 error_setg(errp, "acpi: device unplug request for not supported device"
1736 " type: %s", object_get_typename(OBJECT(dev)));
1737 }
d9c5c5b8
TC
1738}
1739
232391c1
TC
1740static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
1741 DeviceState *dev, Error **errp)
1742{
f7d3e29d
TC
1743 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1744 pc_dimm_unplug(hotplug_dev, dev, errp);
1745 } else {
1746 error_setg(errp, "acpi: device unplug for not supported device"
1747 " type: %s", object_get_typename(OBJECT(dev)));
1748 }
232391c1
TC
1749}
1750
95bee274
IM
1751static HotplugHandler *pc_get_hotpug_handler(MachineState *machine,
1752 DeviceState *dev)
1753{
1754 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
1755
5279569e
GZ
1756 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
1757 object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
95bee274
IM
1758 return HOTPLUG_HANDLER(machine);
1759 }
1760
1761 return pcmc->get_hotplug_handler ?
1762 pcmc->get_hotplug_handler(machine, dev) : NULL;
1763}
1764
bf1e8939
IM
1765static void
1766pc_machine_get_hotplug_memory_region_size(Object *obj, Visitor *v, void *opaque,
1767 const char *name, Error **errp)
1768{
1769 PCMachineState *pcms = PC_MACHINE(obj);
1770 int64_t value = memory_region_size(&pcms->hotplug_memory);
1771
1772 visit_type_int(v, &value, name, errp);
1773}
1774
c87b1520
DS
1775static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
1776 void *opaque, const char *name,
1777 Error **errp)
1778{
1779 PCMachineState *pcms = PC_MACHINE(obj);
1780 uint64_t value = pcms->max_ram_below_4g;
1781
1782 visit_type_size(v, &value, name, errp);
1783}
1784
1785static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
1786 void *opaque, const char *name,
1787 Error **errp)
1788{
1789 PCMachineState *pcms = PC_MACHINE(obj);
1790 Error *error = NULL;
1791 uint64_t value;
1792
1793 visit_type_size(v, &value, name, &error);
1794 if (error) {
1795 error_propagate(errp, error);
1796 return;
1797 }
1798 if (value > (1ULL << 32)) {
1799 error_set(&error, ERROR_CLASS_GENERIC_ERROR,
1800 "Machine option 'max-ram-below-4g=%"PRIu64
1801 "' expects size less than or equal to 4G", value);
1802 error_propagate(errp, error);
1803 return;
1804 }
1805
1806 if (value < (1ULL << 20)) {
1807 error_report("Warning: small max_ram_below_4g(%"PRIu64
1808 ") less than 1M. BIOS may not work..",
1809 value);
1810 }
1811
1812 pcms->max_ram_below_4g = value;
1813}
1814
d1048bef
DS
1815static void pc_machine_get_vmport(Object *obj, Visitor *v, void *opaque,
1816 const char *name, Error **errp)
9b23cfb7
DDAG
1817{
1818 PCMachineState *pcms = PC_MACHINE(obj);
d1048bef 1819 OnOffAuto vmport = pcms->vmport;
9b23cfb7 1820
d1048bef 1821 visit_type_OnOffAuto(v, &vmport, name, errp);
9b23cfb7
DDAG
1822}
1823
d1048bef
DS
1824static void pc_machine_set_vmport(Object *obj, Visitor *v, void *opaque,
1825 const char *name, Error **errp)
9b23cfb7
DDAG
1826{
1827 PCMachineState *pcms = PC_MACHINE(obj);
1828
d1048bef 1829 visit_type_OnOffAuto(v, &pcms->vmport, name, errp);
9b23cfb7
DDAG
1830}
1831
91aa70ab
IM
1832static bool pc_machine_get_aligned_dimm(Object *obj, Error **errp)
1833{
1834 PCMachineState *pcms = PC_MACHINE(obj);
1835
1836 return pcms->enforce_aligned_dimm;
1837}
1838
bf1e8939
IM
1839static void pc_machine_initfn(Object *obj)
1840{
c87b1520
DS
1841 PCMachineState *pcms = PC_MACHINE(obj);
1842
bf1e8939
IM
1843 object_property_add(obj, PC_MACHINE_MEMHP_REGION_SIZE, "int",
1844 pc_machine_get_hotplug_memory_region_size,
1845 NULL, NULL, NULL, NULL);
49d2e648 1846
c87b1520
DS
1847 pcms->max_ram_below_4g = 1ULL << 32; /* 4G */
1848 object_property_add(obj, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
1849 pc_machine_get_max_ram_below_4g,
1850 pc_machine_set_max_ram_below_4g,
1851 NULL, NULL, NULL);
49d2e648
MA
1852 object_property_set_description(obj, PC_MACHINE_MAX_RAM_BELOW_4G,
1853 "Maximum ram below the 4G boundary (32bit boundary)",
1854 NULL);
91aa70ab 1855
d1048bef
DS
1856 pcms->vmport = ON_OFF_AUTO_AUTO;
1857 object_property_add(obj, PC_MACHINE_VMPORT, "OnOffAuto",
1858 pc_machine_get_vmport,
1859 pc_machine_set_vmport,
1860 NULL, NULL, NULL);
49d2e648
MA
1861 object_property_set_description(obj, PC_MACHINE_VMPORT,
1862 "Enable vmport (pc & q35)",
1863 NULL);
91aa70ab
IM
1864
1865 pcms->enforce_aligned_dimm = true;
1866 object_property_add_bool(obj, PC_MACHINE_ENFORCE_ALIGNED_DIMM,
1867 pc_machine_get_aligned_dimm,
1868 NULL, NULL);
bf1e8939
IM
1869}
1870
fb43b73b
IM
1871static unsigned pc_cpu_index_to_socket_id(unsigned cpu_index)
1872{
1873 unsigned pkg_id, core_id, smt_id;
1874 x86_topo_ids_from_idx(smp_cores, smp_threads, cpu_index,
1875 &pkg_id, &core_id, &smt_id);
1876 return pkg_id;
1877}
1878
95bee274
IM
1879static void pc_machine_class_init(ObjectClass *oc, void *data)
1880{
1881 MachineClass *mc = MACHINE_CLASS(oc);
1882 PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
1883 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
1884
1885 pcmc->get_hotplug_handler = mc->get_hotplug_handler;
1886 mc->get_hotplug_handler = pc_get_hotpug_handler;
fb43b73b 1887 mc->cpu_index_to_socket_id = pc_cpu_index_to_socket_id;
95bee274 1888 hc->plug = pc_machine_device_plug_cb;
d9c5c5b8 1889 hc->unplug_request = pc_machine_device_unplug_request_cb;
232391c1 1890 hc->unplug = pc_machine_device_unplug_cb;
95bee274
IM
1891}
1892
d5747cac
IM
1893static const TypeInfo pc_machine_info = {
1894 .name = TYPE_PC_MACHINE,
1895 .parent = TYPE_MACHINE,
1896 .abstract = true,
1897 .instance_size = sizeof(PCMachineState),
bf1e8939 1898 .instance_init = pc_machine_initfn,
d5747cac 1899 .class_size = sizeof(PCMachineClass),
95bee274
IM
1900 .class_init = pc_machine_class_init,
1901 .interfaces = (InterfaceInfo[]) {
1902 { TYPE_HOTPLUG_HANDLER },
1903 { }
1904 },
d5747cac
IM
1905};
1906
1907static void pc_machine_register_types(void)
1908{
1909 type_register_static(&pc_machine_info);
1910}
1911
1912type_init(pc_machine_register_types)