2 * QEMU PC System Emulator
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
28 #include "hw/i386/pc.h"
29 #include "hw/i386/apic.h"
30 #include "hw/pci/pci.h"
31 #include "hw/pci/pci_ids.h"
34 #include "hw/boards.h"
36 #include "sysemu/kvm.h"
37 #include "hw/kvm/clock.h"
38 #include "sysemu/sysemu.h"
39 #include "hw/sysbus.h"
40 #include "hw/cpu/icc_bus.h"
41 #include "sysemu/arch_init.h"
42 #include "sysemu/blockdev.h"
43 #include "hw/i2c/smbus.h"
44 #include "hw/xen/xen.h"
45 #include "exec/memory.h"
46 #include "exec/address-spaces.h"
47 #include "hw/acpi/acpi.h"
50 # include <xen/hvm/hvm_info_table.h>
55 static const int ide_iobase
[MAX_IDE_BUS
] = { 0x1f0, 0x170 };
56 static const int ide_iobase2
[MAX_IDE_BUS
] = { 0x3f6, 0x376 };
57 static const int ide_irq
[MAX_IDE_BUS
] = { 14, 15 };
59 static bool has_pvpanic
= true;
61 /* PC hardware initialisation */
62 static void pc_init1(MemoryRegion
*system_memory
,
63 MemoryRegion
*system_io
,
65 const char *boot_device
,
66 const char *kernel_filename
,
67 const char *kernel_cmdline
,
68 const char *initrd_filename
,
69 const char *cpu_model
,
74 ram_addr_t below_4g_mem_size
, above_4g_mem_size
;
77 PCII440FXState
*i440fx_state
;
84 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
85 BusState
*idebus
[MAX_IDE_BUS
];
88 MemoryRegion
*ram_memory
;
89 MemoryRegion
*pci_memory
;
90 MemoryRegion
*rom_memory
;
91 DeviceState
*icc_bridge
;
92 FWCfgState
*fw_cfg
= NULL
;
94 if (xen_enabled() && xen_hvm_init() != 0) {
95 fprintf(stderr
, "xen hardware virtual machine initialisation failed\n");
99 icc_bridge
= qdev_create(NULL
, TYPE_ICC_BRIDGE
);
100 object_property_add_child(qdev_get_machine(), "icc-bridge",
101 OBJECT(icc_bridge
), NULL
);
103 pc_cpus_init(cpu_model
, icc_bridge
);
104 pc_acpi_init("acpi-dsdt.aml");
106 if (kvm_enabled() && kvmclock_enabled
) {
110 if (ram_size
>= 0xe0000000 ) {
111 above_4g_mem_size
= ram_size
- 0xe0000000;
112 below_4g_mem_size
= 0xe0000000;
114 above_4g_mem_size
= 0;
115 below_4g_mem_size
= ram_size
;
119 pci_memory
= g_new(MemoryRegion
, 1);
120 memory_region_init(pci_memory
, "pci", INT64_MAX
);
121 rom_memory
= pci_memory
;
124 rom_memory
= system_memory
;
127 /* allocate ram and load rom/bios */
128 if (!xen_enabled()) {
129 fw_cfg
= pc_memory_init(system_memory
,
130 kernel_filename
, kernel_cmdline
, initrd_filename
,
131 below_4g_mem_size
, above_4g_mem_size
,
132 rom_memory
, &ram_memory
);
135 gsi_state
= g_malloc0(sizeof(*gsi_state
));
136 if (kvm_irqchip_in_kernel()) {
137 kvm_pc_setup_irq_routing(pci_enabled
);
138 gsi
= qemu_allocate_irqs(kvm_pc_gsi_handler
, gsi_state
,
141 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
145 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
146 system_memory
, system_io
, ram_size
,
148 0x100000000ULL
- below_4g_mem_size
,
149 0x100000000ULL
+ above_4g_mem_size
,
152 : ((uint64_t)1 << 62)),
153 pci_memory
, ram_memory
);
157 isa_bus
= isa_bus_new(NULL
, system_io
);
160 isa_bus_irqs(isa_bus
, gsi
);
162 if (kvm_irqchip_in_kernel()) {
163 i8259
= kvm_i8259_init(isa_bus
);
164 } else if (xen_enabled()) {
165 i8259
= xen_interrupt_controller_init();
167 cpu_irq
= pc_allocate_cpu_irq();
168 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
171 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
172 gsi_state
->i8259_irq
[i
] = i8259
[i
];
175 ioapic_init_gsi(gsi_state
, "i440fx");
177 qdev_init_nofail(icc_bridge
);
179 pc_register_ferr_irq(gsi
[13]);
181 pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
183 /* init basic PC hardware */
184 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
186 pc_nic_init(isa_bus
, pci_bus
);
188 ide_drive_get(hd
, MAX_IDE_BUS
);
192 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
194 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
196 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
197 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
199 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
201 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
203 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
204 idebus
[i
] = qdev_get_child_bus(DEVICE(dev
), "ide.0");
208 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
209 floppy
, idebus
[0], idebus
[1], rtc_state
);
211 if (pci_enabled
&& usb_enabled(false)) {
212 pci_create_simple(pci_bus
, piix3_devfn
+ 2, "piix3-usb-uhci");
215 if (pci_enabled
&& acpi_enabled
) {
218 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
,
219 x86_env_get_cpu(first_cpu
), 1);
220 /* TODO: Populate SPD eeprom data. */
221 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
223 kvm_enabled(), fw_cfg
);
224 smbus_eeprom_init(smbus
, 8, NULL
, 0);
228 pc_pci_device_init(pci_bus
);
232 pvpanic_init(isa_bus
);
236 static void pc_init_pci(QEMUMachineInitArgs
*args
)
238 ram_addr_t ram_size
= args
->ram_size
;
239 const char *cpu_model
= args
->cpu_model
;
240 const char *kernel_filename
= args
->kernel_filename
;
241 const char *kernel_cmdline
= args
->kernel_cmdline
;
242 const char *initrd_filename
= args
->initrd_filename
;
243 const char *boot_device
= args
->boot_device
;
244 pc_init1(get_system_memory(),
246 ram_size
, boot_device
,
247 kernel_filename
, kernel_cmdline
,
248 initrd_filename
, cpu_model
, 1, 1);
251 static void pc_init_pci_1_4(QEMUMachineInitArgs
*args
)
254 x86_cpu_compat_set_features("n270", FEAT_1_ECX
, 0, CPUID_EXT_MOVBE
);
258 static void pc_init_pci_1_3(QEMUMachineInitArgs
*args
)
260 enable_compat_apic_id_mode();
265 /* PC machine init function for pc-1.1 to pc-1.2 */
266 static void pc_init_pci_1_2(QEMUMachineInitArgs
*args
)
268 disable_kvm_pv_eoi();
269 enable_compat_apic_id_mode();
274 /* PC machine init function for pc-0.14 to pc-1.0 */
275 static void pc_init_pci_1_0(QEMUMachineInitArgs
*args
)
277 disable_kvm_pv_eoi();
278 enable_compat_apic_id_mode();
283 /* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
284 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs
*args
)
286 ram_addr_t ram_size
= args
->ram_size
;
287 const char *cpu_model
= args
->cpu_model
;
288 const char *kernel_filename
= args
->kernel_filename
;
289 const char *kernel_cmdline
= args
->kernel_cmdline
;
290 const char *initrd_filename
= args
->initrd_filename
;
291 const char *boot_device
= args
->boot_device
;
293 disable_kvm_pv_eoi();
294 enable_compat_apic_id_mode();
295 pc_init1(get_system_memory(),
297 ram_size
, boot_device
,
298 kernel_filename
, kernel_cmdline
,
299 initrd_filename
, cpu_model
, 1, 0);
302 static void pc_init_isa(QEMUMachineInitArgs
*args
)
304 ram_addr_t ram_size
= args
->ram_size
;
305 const char *cpu_model
= args
->cpu_model
;
306 const char *kernel_filename
= args
->kernel_filename
;
307 const char *kernel_cmdline
= args
->kernel_cmdline
;
308 const char *initrd_filename
= args
->initrd_filename
;
309 const char *boot_device
= args
->boot_device
;
311 if (cpu_model
== NULL
)
313 disable_kvm_pv_eoi();
314 enable_compat_apic_id_mode();
315 pc_init1(get_system_memory(),
317 ram_size
, boot_device
,
318 kernel_filename
, kernel_cmdline
,
319 initrd_filename
, cpu_model
, 0, 1);
323 static void pc_xen_hvm_init(QEMUMachineInitArgs
*args
)
329 bus
= pci_find_root_bus(0);
331 pci_create_simple(bus
, -1, "xen-platform");
336 static QEMUMachine pc_i440fx_machine_v1_6
= {
337 .name
= "pc-i440fx-1.6",
339 .desc
= "Standard PC (i440FX + PIIX, 1996)",
341 .hot_add_cpu
= pc_hot_add_cpu
,
344 DEFAULT_MACHINE_OPTIONS
,
347 static QEMUMachine pc_i440fx_machine_v1_5
= {
348 .name
= "pc-i440fx-1.5",
349 .desc
= "Standard PC (i440FX + PIIX, 1996)",
351 .hot_add_cpu
= pc_hot_add_cpu
,
353 .compat_props
= (GlobalProperty
[]) {
355 { /* end of list */ }
357 DEFAULT_MACHINE_OPTIONS
,
360 static QEMUMachine pc_i440fx_machine_v1_4
= {
361 .name
= "pc-i440fx-1.4",
362 .desc
= "Standard PC (i440FX + PIIX, 1996)",
363 .init
= pc_init_pci_1_4
,
365 .compat_props
= (GlobalProperty
[]) {
367 { /* end of list */ }
369 DEFAULT_MACHINE_OPTIONS
,
372 #define PC_COMPAT_1_3 \
375 .driver = "usb-tablet",\
376 .property = "usb_version",\
377 .value = stringify(1),\
379 .driver = "virtio-net-pci",\
380 .property = "ctrl_mac_addr",\
383 .driver = "virtio-net-pci", \
388 .property = "autonegotiation",\
392 static QEMUMachine pc_machine_v1_3
= {
394 .desc
= "Standard PC",
395 .init
= pc_init_pci_1_3
,
397 .compat_props
= (GlobalProperty
[]) {
399 { /* end of list */ }
401 DEFAULT_MACHINE_OPTIONS
,
404 #define PC_COMPAT_1_2 \
407 .driver = "nec-usb-xhci",\
411 .driver = "nec-usb-xhci",\
415 .driver = "ivshmem",\
416 .property = "use64",\
420 .property = "revision",\
421 .value = stringify(3),\
423 .driver = "qxl-vga",\
424 .property = "revision",\
425 .value = stringify(3),\
432 static QEMUMachine pc_machine_v1_2
= {
434 .desc
= "Standard PC",
435 .init
= pc_init_pci_1_2
,
437 .compat_props
= (GlobalProperty
[]) {
439 { /* end of list */ }
441 DEFAULT_MACHINE_OPTIONS
,
444 #define PC_COMPAT_1_1 \
447 .driver = "virtio-scsi-pci",\
448 .property = "hotplug",\
451 .driver = "virtio-scsi-pci",\
452 .property = "param_change",\
456 .property = "vgamem_mb",\
457 .value = stringify(8),\
459 .driver = "vmware-svga",\
460 .property = "vgamem_mb",\
461 .value = stringify(8),\
463 .driver = "qxl-vga",\
464 .property = "vgamem_mb",\
465 .value = stringify(8),\
468 .property = "vgamem_mb",\
469 .value = stringify(8),\
471 .driver = "virtio-blk-pci",\
472 .property = "config-wce",\
476 static QEMUMachine pc_machine_v1_1
= {
478 .desc
= "Standard PC",
479 .init
= pc_init_pci_1_2
,
481 .compat_props
= (GlobalProperty
[]) {
483 { /* end of list */ }
485 DEFAULT_MACHINE_OPTIONS
,
488 #define PC_COMPAT_1_0 \
491 .driver = "pc-sysfw",\
492 .property = "rom_only",\
493 .value = stringify(1),\
495 .driver = TYPE_ISA_FDC,\
496 .property = "check_media_rate",\
499 .driver = "virtio-balloon-pci",\
500 .property = "class",\
501 .value = stringify(PCI_CLASS_MEMORY_RAM),\
504 .property = "vapic",\
507 .driver = TYPE_USB_DEVICE,\
508 .property = "full-path",\
512 static QEMUMachine pc_machine_v1_0
= {
514 .desc
= "Standard PC",
515 .init
= pc_init_pci_1_0
,
517 .compat_props
= (GlobalProperty
[]) {
519 { /* end of list */ }
522 DEFAULT_MACHINE_OPTIONS
,
525 #define PC_COMPAT_0_15 \
528 static QEMUMachine pc_machine_v0_15
= {
530 .desc
= "Standard PC",
531 .init
= pc_init_pci_1_0
,
533 .compat_props
= (GlobalProperty
[]) {
535 { /* end of list */ }
537 .hw_version
= "0.15",
538 DEFAULT_MACHINE_OPTIONS
,
541 #define PC_COMPAT_0_14 \
544 .driver = "virtio-blk-pci",\
545 .property = "event_idx",\
548 .driver = "virtio-serial-pci",\
549 .property = "event_idx",\
552 .driver = "virtio-net-pci",\
553 .property = "event_idx",\
556 .driver = "virtio-balloon-pci",\
557 .property = "event_idx",\
561 static QEMUMachine pc_machine_v0_14
= {
563 .desc
= "Standard PC",
564 .init
= pc_init_pci_1_0
,
566 .compat_props
= (GlobalProperty
[]) {
570 .property
= "revision",
571 .value
= stringify(2),
574 .property
= "revision",
575 .value
= stringify(2),
577 { /* end of list */ }
579 .hw_version
= "0.14",
580 DEFAULT_MACHINE_OPTIONS
,
583 #define PC_COMPAT_0_13 \
586 .driver = TYPE_PCI_DEVICE,\
587 .property = "command_serr_enable",\
591 .property = "use_broken_id",\
592 .value = stringify(1),\
595 static QEMUMachine pc_machine_v0_13
= {
597 .desc
= "Standard PC",
598 .init
= pc_init_pci_no_kvmclock
,
600 .compat_props
= (GlobalProperty
[]) {
603 .driver
= "virtio-9p-pci",
604 .property
= "vectors",
605 .value
= stringify(0),
608 .property
= "rombar",
609 .value
= stringify(0),
611 .driver
= "vmware-svga",
612 .property
= "rombar",
613 .value
= stringify(0),
615 { /* end of list */ }
617 .hw_version
= "0.13",
618 DEFAULT_MACHINE_OPTIONS
,
621 #define PC_COMPAT_0_12 \
624 .driver = "virtio-serial-pci",\
625 .property = "max_ports",\
626 .value = stringify(1),\
628 .driver = "virtio-serial-pci",\
629 .property = "vectors",\
630 .value = stringify(0),\
632 .driver = "usb-mouse",\
633 .property = "serial",\
636 .driver = "usb-tablet",\
637 .property = "serial",\
640 .driver = "usb-kbd",\
641 .property = "serial",\
645 static QEMUMachine pc_machine_v0_12
= {
647 .desc
= "Standard PC",
648 .init
= pc_init_pci_no_kvmclock
,
650 .compat_props
= (GlobalProperty
[]) {
654 .property
= "rombar",
655 .value
= stringify(0),
657 .driver
= "vmware-svga",
658 .property
= "rombar",
659 .value
= stringify(0),
661 { /* end of list */ }
663 .hw_version
= "0.12",
664 DEFAULT_MACHINE_OPTIONS
,
667 #define PC_COMPAT_0_11 \
670 .driver = "virtio-blk-pci",\
671 .property = "vectors",\
672 .value = stringify(0),\
674 .driver = TYPE_PCI_DEVICE,\
675 .property = "rombar",\
676 .value = stringify(0),\
679 static QEMUMachine pc_machine_v0_11
= {
681 .desc
= "Standard PC, qemu 0.11",
682 .init
= pc_init_pci_no_kvmclock
,
684 .compat_props
= (GlobalProperty
[]) {
687 .driver
= "ide-drive",
691 .driver
= "scsi-disk",
695 { /* end of list */ }
697 .hw_version
= "0.11",
698 DEFAULT_MACHINE_OPTIONS
,
701 static QEMUMachine pc_machine_v0_10
= {
703 .desc
= "Standard PC, qemu 0.10",
704 .init
= pc_init_pci_no_kvmclock
,
706 .compat_props
= (GlobalProperty
[]) {
709 .driver
= "virtio-blk-pci",
711 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
713 .driver
= "virtio-serial-pci",
715 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
717 .driver
= "virtio-net-pci",
718 .property
= "vectors",
719 .value
= stringify(0),
721 .driver
= "ide-drive",
725 .driver
= "scsi-disk",
729 { /* end of list */ }
731 .hw_version
= "0.10",
732 DEFAULT_MACHINE_OPTIONS
,
735 static QEMUMachine isapc_machine
= {
737 .desc
= "ISA-only PC",
740 .compat_props
= (GlobalProperty
[]) {
742 .driver
= "pc-sysfw",
743 .property
= "rom_only",
744 .value
= stringify(1),
747 .driver
= "pc-sysfw",
748 .property
= "isapc_ram_fw",
749 .value
= stringify(1),
751 { /* end of list */ }
753 DEFAULT_MACHINE_OPTIONS
,
757 static QEMUMachine xenfv_machine
= {
759 .desc
= "Xen Fully-virtualized PC",
760 .init
= pc_xen_hvm_init
,
761 .max_cpus
= HVM_MAX_VCPUS
,
762 .default_machine_opts
= "accel=xen",
763 DEFAULT_MACHINE_OPTIONS
,
767 static void pc_machine_init(void)
769 qemu_register_machine(&pc_i440fx_machine_v1_6
);
770 qemu_register_machine(&pc_i440fx_machine_v1_5
);
771 qemu_register_machine(&pc_i440fx_machine_v1_4
);
772 qemu_register_machine(&pc_machine_v1_3
);
773 qemu_register_machine(&pc_machine_v1_2
);
774 qemu_register_machine(&pc_machine_v1_1
);
775 qemu_register_machine(&pc_machine_v1_0
);
776 qemu_register_machine(&pc_machine_v0_15
);
777 qemu_register_machine(&pc_machine_v0_14
);
778 qemu_register_machine(&pc_machine_v0_13
);
779 qemu_register_machine(&pc_machine_v0_12
);
780 qemu_register_machine(&pc_machine_v0_11
);
781 qemu_register_machine(&pc_machine_v0_10
);
782 qemu_register_machine(&isapc_machine
);
784 qemu_register_machine(&xenfv_machine
);
788 machine_init(pc_machine_init
);