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/loader.h"
29 #include "hw/i386/pc.h"
30 #include "hw/i386/apic.h"
31 #include "hw/i386/smbios.h"
32 #include "hw/pci/pci.h"
33 #include "hw/pci/pci_ids.h"
36 #include "hw/boards.h"
38 #include "sysemu/kvm.h"
39 #include "hw/kvm/clock.h"
40 #include "sysemu/sysemu.h"
41 #include "hw/sysbus.h"
42 #include "hw/cpu/icc_bus.h"
43 #include "sysemu/arch_init.h"
44 #include "sysemu/blockdev.h"
45 #include "hw/i2c/smbus.h"
46 #include "hw/xen/xen.h"
47 #include "exec/memory.h"
48 #include "exec/address-spaces.h"
49 #include "hw/acpi/acpi.h"
52 # include <xen/hvm/hvm_info_table.h>
57 static const int ide_iobase[MAX_IDE_BUS] = { 0x1f0, 0x170 };
58 static const int ide_iobase2[MAX_IDE_BUS] = { 0x3f6, 0x376 };
59 static const int ide_irq[MAX_IDE_BUS] = { 14, 15 };
61 static bool has_pci_info;
62 static bool has_acpi_build = true;
63 static bool smbios_defaults = true;
64 static bool smbios_legacy_mode;
65 /* Make sure that guest addresses aligned at 1Gbyte boundaries get mapped to
66 * host addresses aligned at 1Gbyte boundaries. This way we can use 1GByte
69 static bool gigabyte_align = true;
71 /* PC hardware initialisation */
72 static void pc_init1(QEMUMachineInitArgs *args,
76 MemoryRegion *system_memory = get_system_memory();
77 MemoryRegion *system_io = get_system_io();
79 ram_addr_t below_4g_mem_size, above_4g_mem_size;
82 PCII440FXState *i440fx_state;
89 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
90 BusState *idebus[MAX_IDE_BUS];
93 MemoryRegion *ram_memory;
94 MemoryRegion *pci_memory;
95 MemoryRegion *rom_memory;
96 DeviceState *icc_bridge;
97 FWCfgState *fw_cfg = NULL;
98 PcGuestInfo *guest_info;
100 if (xen_enabled() && xen_hvm_init(&ram_memory) != 0) {
101 fprintf(stderr, "xen hardware virtual machine initialisation failed\n");
105 icc_bridge = qdev_create(NULL, TYPE_ICC_BRIDGE);
106 object_property_add_child(qdev_get_machine(), "icc-bridge",
107 OBJECT(icc_bridge), NULL);
109 pc_cpus_init(args->cpu_model, icc_bridge);
111 if (kvm_enabled() && kvmclock_enabled) {
115 /* Check whether RAM fits below 4G (leaving 1/2 GByte for IO memory).
116 * If it doesn't, we need to split it in chunks below and above 4G.
117 * In any case, try to make sure that guest addresses aligned at
118 * 1G boundaries get mapped to host addresses aligned at 1G boundaries.
119 * For old machine types, use whatever split we used historically to avoid
120 * breaking migration.
122 if (args->ram_size >= 0xe0000000) {
123 ram_addr_t lowmem = gigabyte_align ? 0xc0000000 : 0xe0000000;
124 above_4g_mem_size = args->ram_size - lowmem;
125 below_4g_mem_size = lowmem;
127 above_4g_mem_size = 0;
128 below_4g_mem_size = args->ram_size;
132 pci_memory = g_new(MemoryRegion, 1);
133 memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
134 rom_memory = pci_memory;
137 rom_memory = system_memory;
140 guest_info = pc_guest_info_init(below_4g_mem_size, above_4g_mem_size);
142 guest_info->has_acpi_build = has_acpi_build;
144 guest_info->has_pci_info = has_pci_info;
145 guest_info->isapc_ram_fw = !pci_enabled;
147 if (smbios_defaults) {
148 /* These values are guest ABI, do not change */
149 smbios_set_defaults("QEMU", "Standard PC (i440FX + PIIX, 1996)",
150 args->machine->name, smbios_legacy_mode);
153 /* allocate ram and load rom/bios */
154 if (!xen_enabled()) {
155 fw_cfg = pc_memory_init(system_memory,
156 args->kernel_filename, args->kernel_cmdline,
157 args->initrd_filename,
158 below_4g_mem_size, above_4g_mem_size,
159 rom_memory, &ram_memory, guest_info);
162 gsi_state = g_malloc0(sizeof(*gsi_state));
163 if (kvm_irqchip_in_kernel()) {
164 kvm_pc_setup_irq_routing(pci_enabled);
165 gsi = qemu_allocate_irqs(kvm_pc_gsi_handler, gsi_state,
168 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
172 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
173 system_memory, system_io, args->ram_size,
176 pci_memory, ram_memory);
180 isa_bus = isa_bus_new(NULL, system_io);
183 isa_bus_irqs(isa_bus, gsi);
185 if (kvm_irqchip_in_kernel()) {
186 i8259 = kvm_i8259_init(isa_bus);
187 } else if (xen_enabled()) {
188 i8259 = xen_interrupt_controller_init();
190 cpu_irq = pc_allocate_cpu_irq();
191 i8259 = i8259_init(isa_bus, cpu_irq[0]);
194 for (i = 0; i < ISA_NUM_IRQS; i++) {
195 gsi_state->i8259_irq[i] = i8259[i];
198 ioapic_init_gsi(gsi_state, "i440fx");
200 qdev_init_nofail(icc_bridge);
202 pc_register_ferr_irq(gsi[13]);
204 pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
206 /* init basic PC hardware */
207 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled(),
210 pc_nic_init(isa_bus, pci_bus);
212 ide_drive_get(hd, MAX_IDE_BUS);
216 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
218 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
220 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
221 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
223 for(i = 0; i < MAX_IDE_BUS; i++) {
225 char busname[] = "ide.0";
226 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
228 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
230 * The ide bus name is ide.0 for the first bus and ide.1 for the
233 busname[4] = '0' + i;
234 idebus[i] = qdev_get_child_bus(DEVICE(dev), busname);
238 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, args->boot_order,
239 floppy, idebus[0], idebus[1], rtc_state);
241 if (pci_enabled && usb_enabled(false)) {
242 pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
245 if (pci_enabled && acpi_enabled) {
248 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
249 /* TODO: Populate SPD eeprom data. */
250 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
252 kvm_enabled(), fw_cfg);
253 smbus_eeprom_init(smbus, 8, NULL, 0);
257 pc_pci_device_init(pci_bus);
261 static void pc_init_pci(QEMUMachineInitArgs *args)
263 pc_init1(args, 1, 1);
266 static void pc_compat_2_0(QEMUMachineInitArgs *args)
268 smbios_legacy_mode = true;
271 static void pc_compat_1_7(QEMUMachineInitArgs *args)
274 smbios_defaults = false;
275 gigabyte_align = false;
276 option_rom_has_mr = true;
277 x86_cpu_compat_disable_kvm_features(FEAT_1_ECX, CPUID_EXT_X2APIC);
280 static void pc_compat_1_6(QEMUMachineInitArgs *args)
283 has_pci_info = false;
284 rom_file_has_mr = false;
285 has_acpi_build = false;
288 static void pc_compat_1_5(QEMUMachineInitArgs *args)
293 static void pc_compat_1_4(QEMUMachineInitArgs *args)
296 x86_cpu_compat_set_features("n270", FEAT_1_ECX, 0, CPUID_EXT_MOVBE);
297 x86_cpu_compat_set_features("Westmere", FEAT_1_ECX, 0, CPUID_EXT_PCLMULQDQ);
300 static void pc_compat_1_3(QEMUMachineInitArgs *args)
303 enable_compat_apic_id_mode();
306 /* PC compat function for pc-0.14 to pc-1.2 */
307 static void pc_compat_1_2(QEMUMachineInitArgs *args)
310 x86_cpu_compat_disable_kvm_features(FEAT_KVM, KVM_FEATURE_PV_EOI);
313 static void pc_init_pci_2_0(QEMUMachineInitArgs *args)
319 static void pc_init_pci_1_7(QEMUMachineInitArgs *args)
325 static void pc_init_pci_1_6(QEMUMachineInitArgs *args)
331 static void pc_init_pci_1_5(QEMUMachineInitArgs *args)
337 static void pc_init_pci_1_4(QEMUMachineInitArgs *args)
343 static void pc_init_pci_1_3(QEMUMachineInitArgs *args)
349 /* PC machine init function for pc-0.14 to pc-1.2 */
350 static void pc_init_pci_1_2(QEMUMachineInitArgs *args)
356 /* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
357 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
359 has_pci_info = false;
360 has_acpi_build = false;
361 smbios_defaults = false;
362 x86_cpu_compat_disable_kvm_features(FEAT_KVM, KVM_FEATURE_PV_EOI);
363 enable_compat_apic_id_mode();
364 pc_init1(args, 1, 0);
367 static void pc_init_isa(QEMUMachineInitArgs *args)
369 has_pci_info = false;
370 has_acpi_build = false;
371 smbios_defaults = false;
372 if (!args->cpu_model) {
373 args->cpu_model = "486";
375 x86_cpu_compat_disable_kvm_features(FEAT_KVM, KVM_FEATURE_PV_EOI);
376 enable_compat_apic_id_mode();
377 pc_init1(args, 0, 1);
381 static void pc_xen_hvm_init(QEMUMachineInitArgs *args)
387 bus = pci_find_primary_bus();
389 pci_create_simple(bus, -1, "xen-platform");
394 #define PC_I440FX_MACHINE_OPTIONS \
395 PC_DEFAULT_MACHINE_OPTIONS, \
396 .desc = "Standard PC (i440FX + PIIX, 1996)", \
397 .hot_add_cpu = pc_hot_add_cpu
399 #define PC_I440FX_2_1_MACHINE_OPTIONS \
400 PC_I440FX_MACHINE_OPTIONS, \
401 .default_machine_opts = "firmware=bios-256k.bin"
403 static QEMUMachine pc_i440fx_machine_v2_1 = {
404 PC_I440FX_2_1_MACHINE_OPTIONS,
405 .name = "pc-i440fx-2.1",
411 #define PC_I440FX_2_0_MACHINE_OPTIONS PC_I440FX_2_1_MACHINE_OPTIONS
413 static QEMUMachine pc_i440fx_machine_v2_0 = {
414 PC_I440FX_2_0_MACHINE_OPTIONS,
415 .name = "pc-i440fx-2.0",
416 .init = pc_init_pci_2_0,
419 #define PC_I440FX_1_7_MACHINE_OPTIONS PC_I440FX_MACHINE_OPTIONS
421 static QEMUMachine pc_i440fx_machine_v1_7 = {
422 PC_I440FX_1_7_MACHINE_OPTIONS,
423 .name = "pc-i440fx-1.7",
424 .init = pc_init_pci_1_7,
425 .compat_props = (GlobalProperty[]) {
427 { /* end of list */ }
431 #define PC_I440FX_1_6_MACHINE_OPTIONS PC_I440FX_MACHINE_OPTIONS
433 static QEMUMachine pc_i440fx_machine_v1_6 = {
434 PC_I440FX_1_6_MACHINE_OPTIONS,
435 .name = "pc-i440fx-1.6",
436 .init = pc_init_pci_1_6,
437 .compat_props = (GlobalProperty[]) {
439 { /* end of list */ }
443 static QEMUMachine pc_i440fx_machine_v1_5 = {
444 PC_I440FX_1_6_MACHINE_OPTIONS,
445 .name = "pc-i440fx-1.5",
446 .init = pc_init_pci_1_5,
447 .compat_props = (GlobalProperty[]) {
449 { /* end of list */ }
453 #define PC_I440FX_1_4_MACHINE_OPTIONS \
454 PC_I440FX_1_6_MACHINE_OPTIONS, \
457 static QEMUMachine pc_i440fx_machine_v1_4 = {
458 PC_I440FX_1_4_MACHINE_OPTIONS,
459 .name = "pc-i440fx-1.4",
460 .init = pc_init_pci_1_4,
461 .compat_props = (GlobalProperty[]) {
463 { /* end of list */ }
467 #define PC_COMPAT_1_3 \
470 .driver = "usb-tablet",\
471 .property = "usb_version",\
472 .value = stringify(1),\
474 .driver = "virtio-net-pci",\
475 .property = "ctrl_mac_addr",\
478 .driver = "virtio-net-pci", \
483 .property = "autonegotiation",\
487 static QEMUMachine pc_machine_v1_3 = {
488 PC_I440FX_1_4_MACHINE_OPTIONS,
490 .init = pc_init_pci_1_3,
491 .compat_props = (GlobalProperty[]) {
493 { /* end of list */ }
497 #define PC_COMPAT_1_2 \
500 .driver = "nec-usb-xhci",\
504 .driver = "nec-usb-xhci",\
508 .driver = "ivshmem",\
509 .property = "use64",\
513 .property = "revision",\
514 .value = stringify(3),\
516 .driver = "qxl-vga",\
517 .property = "revision",\
518 .value = stringify(3),\
525 #define PC_I440FX_1_2_MACHINE_OPTIONS \
526 PC_I440FX_1_4_MACHINE_OPTIONS, \
527 .init = pc_init_pci_1_2
529 static QEMUMachine pc_machine_v1_2 = {
530 PC_I440FX_1_2_MACHINE_OPTIONS,
532 .compat_props = (GlobalProperty[]) {
534 { /* end of list */ }
538 #define PC_COMPAT_1_1 \
541 .driver = "virtio-scsi-pci",\
542 .property = "hotplug",\
545 .driver = "virtio-scsi-pci",\
546 .property = "param_change",\
550 .property = "vgamem_mb",\
551 .value = stringify(8),\
553 .driver = "vmware-svga",\
554 .property = "vgamem_mb",\
555 .value = stringify(8),\
557 .driver = "qxl-vga",\
558 .property = "vgamem_mb",\
559 .value = stringify(8),\
562 .property = "vgamem_mb",\
563 .value = stringify(8),\
565 .driver = "virtio-blk-pci",\
566 .property = "config-wce",\
570 static QEMUMachine pc_machine_v1_1 = {
571 PC_I440FX_1_2_MACHINE_OPTIONS,
573 .compat_props = (GlobalProperty[]) {
575 { /* end of list */ }
579 #define PC_COMPAT_1_0 \
582 .driver = TYPE_ISA_FDC,\
583 .property = "check_media_rate",\
586 .driver = "virtio-balloon-pci",\
587 .property = "class",\
588 .value = stringify(PCI_CLASS_MEMORY_RAM),\
591 .property = "vapic",\
594 .driver = TYPE_USB_DEVICE,\
595 .property = "full-path",\
599 static QEMUMachine pc_machine_v1_0 = {
600 PC_I440FX_1_2_MACHINE_OPTIONS,
602 .compat_props = (GlobalProperty[]) {
604 { /* end of list */ }
609 #define PC_COMPAT_0_15 \
612 static QEMUMachine pc_machine_v0_15 = {
613 PC_I440FX_1_2_MACHINE_OPTIONS,
615 .compat_props = (GlobalProperty[]) {
617 { /* end of list */ }
619 .hw_version = "0.15",
622 #define PC_COMPAT_0_14 \
625 .driver = "virtio-blk-pci",\
626 .property = "event_idx",\
629 .driver = "virtio-serial-pci",\
630 .property = "event_idx",\
633 .driver = "virtio-net-pci",\
634 .property = "event_idx",\
637 .driver = "virtio-balloon-pci",\
638 .property = "event_idx",\
642 static QEMUMachine pc_machine_v0_14 = {
643 PC_I440FX_1_2_MACHINE_OPTIONS,
645 .compat_props = (GlobalProperty[]) {
649 .property = "revision",
650 .value = stringify(2),
653 .property = "revision",
654 .value = stringify(2),
656 { /* end of list */ }
658 .hw_version = "0.14",
661 #define PC_COMPAT_0_13 \
664 .driver = TYPE_PCI_DEVICE,\
665 .property = "command_serr_enable",\
669 .property = "use_broken_id",\
670 .value = stringify(1),\
673 #define PC_I440FX_0_13_MACHINE_OPTIONS \
674 PC_I440FX_1_2_MACHINE_OPTIONS, \
675 .init = pc_init_pci_no_kvmclock
677 static QEMUMachine pc_machine_v0_13 = {
678 PC_I440FX_0_13_MACHINE_OPTIONS,
680 .compat_props = (GlobalProperty[]) {
683 .driver = "virtio-9p-pci",
684 .property = "vectors",
685 .value = stringify(0),
688 .property = "rombar",
689 .value = stringify(0),
691 .driver = "vmware-svga",
692 .property = "rombar",
693 .value = stringify(0),
695 { /* end of list */ }
697 .hw_version = "0.13",
700 #define PC_COMPAT_0_12 \
703 .driver = "virtio-serial-pci",\
704 .property = "max_ports",\
705 .value = stringify(1),\
707 .driver = "virtio-serial-pci",\
708 .property = "vectors",\
709 .value = stringify(0),\
711 .driver = "usb-mouse",\
712 .property = "serial",\
715 .driver = "usb-tablet",\
716 .property = "serial",\
719 .driver = "usb-kbd",\
720 .property = "serial",\
724 static QEMUMachine pc_machine_v0_12 = {
725 PC_I440FX_0_13_MACHINE_OPTIONS,
727 .compat_props = (GlobalProperty[]) {
731 .property = "rombar",
732 .value = stringify(0),
734 .driver = "vmware-svga",
735 .property = "rombar",
736 .value = stringify(0),
738 { /* end of list */ }
740 .hw_version = "0.12",
743 #define PC_COMPAT_0_11 \
746 .driver = "virtio-blk-pci",\
747 .property = "vectors",\
748 .value = stringify(0),\
750 .driver = TYPE_PCI_DEVICE,\
751 .property = "rombar",\
752 .value = stringify(0),\
755 static QEMUMachine pc_machine_v0_11 = {
756 PC_I440FX_0_13_MACHINE_OPTIONS,
758 .compat_props = (GlobalProperty[]) {
761 .driver = "ide-drive",
765 .driver = "scsi-disk",
769 { /* end of list */ }
771 .hw_version = "0.11",
774 static QEMUMachine pc_machine_v0_10 = {
775 PC_I440FX_0_13_MACHINE_OPTIONS,
777 .compat_props = (GlobalProperty[]) {
780 .driver = "virtio-blk-pci",
782 .value = stringify(PCI_CLASS_STORAGE_OTHER),
784 .driver = "virtio-serial-pci",
786 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
788 .driver = "virtio-net-pci",
789 .property = "vectors",
790 .value = stringify(0),
792 .driver = "ide-drive",
796 .driver = "scsi-disk",
800 { /* end of list */ }
802 .hw_version = "0.10",
805 static QEMUMachine isapc_machine = {
806 PC_COMMON_MACHINE_OPTIONS,
808 .desc = "ISA-only PC",
811 .compat_props = (GlobalProperty[]) {
812 { /* end of list */ }
817 static QEMUMachine xenfv_machine = {
818 PC_COMMON_MACHINE_OPTIONS,
820 .desc = "Xen Fully-virtualized PC",
821 .init = pc_xen_hvm_init,
822 .max_cpus = HVM_MAX_VCPUS,
823 .default_machine_opts = "accel=xen",
824 .hot_add_cpu = pc_hot_add_cpu,
825 .compat_props = (GlobalProperty[]) {
826 /* xenfv has no fwcfg and so does not load acpi from QEMU.
827 * as such new acpi features don't work.
830 .driver = "PIIX4_PM",
831 .property = "acpi-pci-hotplug-with-bridge-support",
834 { /* end of list */ }
839 static void pc_machine_init(void)
841 qemu_register_machine(&pc_i440fx_machine_v2_1);
842 qemu_register_machine(&pc_i440fx_machine_v2_0);
843 qemu_register_machine(&pc_i440fx_machine_v1_7);
844 qemu_register_machine(&pc_i440fx_machine_v1_6);
845 qemu_register_machine(&pc_i440fx_machine_v1_5);
846 qemu_register_machine(&pc_i440fx_machine_v1_4);
847 qemu_register_machine(&pc_machine_v1_3);
848 qemu_register_machine(&pc_machine_v1_2);
849 qemu_register_machine(&pc_machine_v1_1);
850 qemu_register_machine(&pc_machine_v1_0);
851 qemu_register_machine(&pc_machine_v0_15);
852 qemu_register_machine(&pc_machine_v0_14);
853 qemu_register_machine(&pc_machine_v0_13);
854 qemu_register_machine(&pc_machine_v0_12);
855 qemu_register_machine(&pc_machine_v0_11);
856 qemu_register_machine(&pc_machine_v0_10);
857 qemu_register_machine(&isapc_machine);
859 qemu_register_machine(&xenfv_machine);
863 machine_init(pc_machine_init);