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;
60 static bool has_pci_info
= true;
62 /* PC hardware initialisation */
63 static void pc_init1(MemoryRegion
*system_memory
,
64 MemoryRegion
*system_io
,
66 const char *boot_device
,
67 const char *kernel_filename
,
68 const char *kernel_cmdline
,
69 const char *initrd_filename
,
70 const char *cpu_model
,
75 ram_addr_t below_4g_mem_size
, above_4g_mem_size
;
78 PCII440FXState
*i440fx_state
;
85 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
86 BusState
*idebus
[MAX_IDE_BUS
];
89 MemoryRegion
*ram_memory
;
90 MemoryRegion
*pci_memory
;
91 MemoryRegion
*rom_memory
;
92 DeviceState
*icc_bridge
;
93 FWCfgState
*fw_cfg
= NULL
;
94 PcGuestInfo
*guest_info
;
96 if (xen_enabled() && xen_hvm_init() != 0) {
97 fprintf(stderr
, "xen hardware virtual machine initialisation failed\n");
101 icc_bridge
= qdev_create(NULL
, TYPE_ICC_BRIDGE
);
102 object_property_add_child(qdev_get_machine(), "icc-bridge",
103 OBJECT(icc_bridge
), NULL
);
105 pc_cpus_init(cpu_model
, icc_bridge
);
106 pc_acpi_init("acpi-dsdt.aml");
108 if (kvm_enabled() && kvmclock_enabled
) {
112 if (ram_size
>= 0xe0000000 ) {
113 above_4g_mem_size
= ram_size
- 0xe0000000;
114 below_4g_mem_size
= 0xe0000000;
116 above_4g_mem_size
= 0;
117 below_4g_mem_size
= ram_size
;
121 pci_memory
= g_new(MemoryRegion
, 1);
122 memory_region_init(pci_memory
, NULL
, "pci", INT64_MAX
);
123 rom_memory
= pci_memory
;
126 rom_memory
= system_memory
;
129 guest_info
= pc_guest_info_init(below_4g_mem_size
, above_4g_mem_size
);
130 guest_info
->has_pci_info
= has_pci_info
;
132 /* Set PCI window size the way seabios has always done it. */
133 /* Power of 2 so bios can cover it with a single MTRR */
134 if (ram_size
<= 0x80000000)
135 guest_info
->pci_info
.w32
.begin
= 0x80000000;
136 else if (ram_size
<= 0xc0000000)
137 guest_info
->pci_info
.w32
.begin
= 0xc0000000;
139 guest_info
->pci_info
.w32
.begin
= 0xe0000000;
141 /* allocate ram and load rom/bios */
142 if (!xen_enabled()) {
143 fw_cfg
= pc_memory_init(system_memory
,
144 kernel_filename
, kernel_cmdline
, initrd_filename
,
145 below_4g_mem_size
, above_4g_mem_size
,
146 rom_memory
, &ram_memory
, guest_info
);
149 gsi_state
= g_malloc0(sizeof(*gsi_state
));
150 if (kvm_irqchip_in_kernel()) {
151 kvm_pc_setup_irq_routing(pci_enabled
);
152 gsi
= qemu_allocate_irqs(kvm_pc_gsi_handler
, gsi_state
,
155 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
159 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
160 system_memory
, system_io
, ram_size
,
162 0x100000000ULL
- below_4g_mem_size
,
163 0x100000000ULL
+ above_4g_mem_size
,
166 : ((uint64_t)1 << 62)),
167 pci_memory
, ram_memory
);
171 isa_bus
= isa_bus_new(NULL
, system_io
);
174 isa_bus_irqs(isa_bus
, gsi
);
176 if (kvm_irqchip_in_kernel()) {
177 i8259
= kvm_i8259_init(isa_bus
);
178 } else if (xen_enabled()) {
179 i8259
= xen_interrupt_controller_init();
181 cpu_irq
= pc_allocate_cpu_irq();
182 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
185 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
186 gsi_state
->i8259_irq
[i
] = i8259
[i
];
189 ioapic_init_gsi(gsi_state
, "i440fx");
191 qdev_init_nofail(icc_bridge
);
193 pc_register_ferr_irq(gsi
[13]);
195 pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
197 /* init basic PC hardware */
198 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
200 pc_nic_init(isa_bus
, pci_bus
);
202 ide_drive_get(hd
, MAX_IDE_BUS
);
206 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
208 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
210 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
211 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
213 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
215 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
217 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
218 idebus
[i
] = qdev_get_child_bus(DEVICE(dev
), "ide.0");
222 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
223 floppy
, idebus
[0], idebus
[1], rtc_state
);
225 if (pci_enabled
&& usb_enabled(false)) {
226 pci_create_simple(pci_bus
, piix3_devfn
+ 2, "piix3-usb-uhci");
229 if (pci_enabled
&& acpi_enabled
) {
232 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
, first_cpu
, 1);
233 /* TODO: Populate SPD eeprom data. */
234 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
236 kvm_enabled(), fw_cfg
);
237 smbus_eeprom_init(smbus
, 8, NULL
, 0);
241 pc_pci_device_init(pci_bus
);
245 pvpanic_init(isa_bus
);
249 static void pc_init_pci(QEMUMachineInitArgs
*args
)
251 ram_addr_t ram_size
= args
->ram_size
;
252 const char *cpu_model
= args
->cpu_model
;
253 const char *kernel_filename
= args
->kernel_filename
;
254 const char *kernel_cmdline
= args
->kernel_cmdline
;
255 const char *initrd_filename
= args
->initrd_filename
;
256 const char *boot_device
= args
->boot_device
;
257 pc_init1(get_system_memory(),
259 ram_size
, boot_device
,
260 kernel_filename
, kernel_cmdline
,
261 initrd_filename
, cpu_model
, 1, 1);
264 static void pc_init_pci_1_5(QEMUMachineInitArgs
*args
)
266 has_pci_info
= false;
270 static void pc_init_pci_1_4(QEMUMachineInitArgs
*args
)
273 x86_cpu_compat_set_features("n270", FEAT_1_ECX
, 0, CPUID_EXT_MOVBE
);
274 pc_init_pci_1_5(args
);
277 static void pc_init_pci_1_3(QEMUMachineInitArgs
*args
)
279 enable_compat_apic_id_mode();
280 pc_init_pci_1_4(args
);
283 /* PC machine init function for pc-1.1 to pc-1.2 */
284 static void pc_init_pci_1_2(QEMUMachineInitArgs
*args
)
286 disable_kvm_pv_eoi();
287 pc_init_pci_1_3(args
);
290 /* PC machine init function for pc-0.14 to pc-1.0 */
291 static void pc_init_pci_1_0(QEMUMachineInitArgs
*args
)
293 pc_init_pci_1_2(args
);
296 /* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
297 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs
*args
)
299 ram_addr_t ram_size
= args
->ram_size
;
300 const char *cpu_model
= args
->cpu_model
;
301 const char *kernel_filename
= args
->kernel_filename
;
302 const char *kernel_cmdline
= args
->kernel_cmdline
;
303 const char *initrd_filename
= args
->initrd_filename
;
304 const char *boot_device
= args
->boot_device
;
306 has_pci_info
= false;
307 disable_kvm_pv_eoi();
308 enable_compat_apic_id_mode();
309 pc_init1(get_system_memory(),
311 ram_size
, boot_device
,
312 kernel_filename
, kernel_cmdline
,
313 initrd_filename
, cpu_model
, 1, 0);
316 static void pc_init_isa(QEMUMachineInitArgs
*args
)
318 ram_addr_t ram_size
= args
->ram_size
;
319 const char *cpu_model
= args
->cpu_model
;
320 const char *kernel_filename
= args
->kernel_filename
;
321 const char *kernel_cmdline
= args
->kernel_cmdline
;
322 const char *initrd_filename
= args
->initrd_filename
;
323 const char *boot_device
= args
->boot_device
;
325 has_pci_info
= false;
326 if (cpu_model
== NULL
)
328 disable_kvm_pv_eoi();
329 enable_compat_apic_id_mode();
330 pc_init1(get_system_memory(),
332 ram_size
, boot_device
,
333 kernel_filename
, kernel_cmdline
,
334 initrd_filename
, cpu_model
, 0, 1);
338 static void pc_xen_hvm_init(QEMUMachineInitArgs
*args
)
344 bus
= pci_find_primary_bus();
346 pci_create_simple(bus
, -1, "xen-platform");
351 static QEMUMachine pc_i440fx_machine_v1_6
= {
352 .name
= "pc-i440fx-1.6",
354 .desc
= "Standard PC (i440FX + PIIX, 1996)",
356 .hot_add_cpu
= pc_hot_add_cpu
,
359 DEFAULT_MACHINE_OPTIONS
,
362 static QEMUMachine pc_i440fx_machine_v1_5
= {
363 .name
= "pc-i440fx-1.5",
364 .desc
= "Standard PC (i440FX + PIIX, 1996)",
365 .init
= pc_init_pci_1_5
,
366 .hot_add_cpu
= pc_hot_add_cpu
,
368 .compat_props
= (GlobalProperty
[]) {
370 { /* end of list */ }
372 DEFAULT_MACHINE_OPTIONS
,
375 static QEMUMachine pc_i440fx_machine_v1_4
= {
376 .name
= "pc-i440fx-1.4",
377 .desc
= "Standard PC (i440FX + PIIX, 1996)",
378 .init
= pc_init_pci_1_4
,
380 .compat_props
= (GlobalProperty
[]) {
382 { /* end of list */ }
384 DEFAULT_MACHINE_OPTIONS
,
387 #define PC_COMPAT_1_3 \
390 .driver = "usb-tablet",\
391 .property = "usb_version",\
392 .value = stringify(1),\
394 .driver = "virtio-net-pci",\
395 .property = "ctrl_mac_addr",\
398 .driver = "virtio-net-pci", \
403 .property = "autonegotiation",\
407 static QEMUMachine pc_machine_v1_3
= {
409 .desc
= "Standard PC",
410 .init
= pc_init_pci_1_3
,
412 .compat_props
= (GlobalProperty
[]) {
414 { /* end of list */ }
416 DEFAULT_MACHINE_OPTIONS
,
419 #define PC_COMPAT_1_2 \
422 .driver = "nec-usb-xhci",\
426 .driver = "nec-usb-xhci",\
430 .driver = "ivshmem",\
431 .property = "use64",\
435 .property = "revision",\
436 .value = stringify(3),\
438 .driver = "qxl-vga",\
439 .property = "revision",\
440 .value = stringify(3),\
447 static QEMUMachine pc_machine_v1_2
= {
449 .desc
= "Standard PC",
450 .init
= pc_init_pci_1_2
,
452 .compat_props
= (GlobalProperty
[]) {
454 { /* end of list */ }
456 DEFAULT_MACHINE_OPTIONS
,
459 #define PC_COMPAT_1_1 \
462 .driver = "virtio-scsi-pci",\
463 .property = "hotplug",\
466 .driver = "virtio-scsi-pci",\
467 .property = "param_change",\
471 .property = "vgamem_mb",\
472 .value = stringify(8),\
474 .driver = "vmware-svga",\
475 .property = "vgamem_mb",\
476 .value = stringify(8),\
478 .driver = "qxl-vga",\
479 .property = "vgamem_mb",\
480 .value = stringify(8),\
483 .property = "vgamem_mb",\
484 .value = stringify(8),\
486 .driver = "virtio-blk-pci",\
487 .property = "config-wce",\
491 static QEMUMachine pc_machine_v1_1
= {
493 .desc
= "Standard PC",
494 .init
= pc_init_pci_1_2
,
496 .compat_props
= (GlobalProperty
[]) {
498 { /* end of list */ }
500 DEFAULT_MACHINE_OPTIONS
,
503 #define PC_COMPAT_1_0 \
506 .driver = "pc-sysfw",\
507 .property = "rom_only",\
508 .value = stringify(1),\
510 .driver = TYPE_ISA_FDC,\
511 .property = "check_media_rate",\
514 .driver = "virtio-balloon-pci",\
515 .property = "class",\
516 .value = stringify(PCI_CLASS_MEMORY_RAM),\
519 .property = "vapic",\
522 .driver = TYPE_USB_DEVICE,\
523 .property = "full-path",\
527 static QEMUMachine pc_machine_v1_0
= {
529 .desc
= "Standard PC",
530 .init
= pc_init_pci_1_0
,
532 .compat_props
= (GlobalProperty
[]) {
534 { /* end of list */ }
537 DEFAULT_MACHINE_OPTIONS
,
540 #define PC_COMPAT_0_15 \
543 static QEMUMachine pc_machine_v0_15
= {
545 .desc
= "Standard PC",
546 .init
= pc_init_pci_1_0
,
548 .compat_props
= (GlobalProperty
[]) {
550 { /* end of list */ }
552 .hw_version
= "0.15",
553 DEFAULT_MACHINE_OPTIONS
,
556 #define PC_COMPAT_0_14 \
559 .driver = "virtio-blk-pci",\
560 .property = "event_idx",\
563 .driver = "virtio-serial-pci",\
564 .property = "event_idx",\
567 .driver = "virtio-net-pci",\
568 .property = "event_idx",\
571 .driver = "virtio-balloon-pci",\
572 .property = "event_idx",\
576 static QEMUMachine pc_machine_v0_14
= {
578 .desc
= "Standard PC",
579 .init
= pc_init_pci_1_0
,
581 .compat_props
= (GlobalProperty
[]) {
585 .property
= "revision",
586 .value
= stringify(2),
589 .property
= "revision",
590 .value
= stringify(2),
592 { /* end of list */ }
594 .hw_version
= "0.14",
595 DEFAULT_MACHINE_OPTIONS
,
598 #define PC_COMPAT_0_13 \
601 .driver = TYPE_PCI_DEVICE,\
602 .property = "command_serr_enable",\
606 .property = "use_broken_id",\
607 .value = stringify(1),\
610 static QEMUMachine pc_machine_v0_13
= {
612 .desc
= "Standard PC",
613 .init
= pc_init_pci_no_kvmclock
,
615 .compat_props
= (GlobalProperty
[]) {
618 .driver
= "virtio-9p-pci",
619 .property
= "vectors",
620 .value
= stringify(0),
623 .property
= "rombar",
624 .value
= stringify(0),
626 .driver
= "vmware-svga",
627 .property
= "rombar",
628 .value
= stringify(0),
630 { /* end of list */ }
632 .hw_version
= "0.13",
633 DEFAULT_MACHINE_OPTIONS
,
636 #define PC_COMPAT_0_12 \
639 .driver = "virtio-serial-pci",\
640 .property = "max_ports",\
641 .value = stringify(1),\
643 .driver = "virtio-serial-pci",\
644 .property = "vectors",\
645 .value = stringify(0),\
647 .driver = "usb-mouse",\
648 .property = "serial",\
651 .driver = "usb-tablet",\
652 .property = "serial",\
655 .driver = "usb-kbd",\
656 .property = "serial",\
660 static QEMUMachine pc_machine_v0_12
= {
662 .desc
= "Standard PC",
663 .init
= pc_init_pci_no_kvmclock
,
665 .compat_props
= (GlobalProperty
[]) {
669 .property
= "rombar",
670 .value
= stringify(0),
672 .driver
= "vmware-svga",
673 .property
= "rombar",
674 .value
= stringify(0),
676 { /* end of list */ }
678 .hw_version
= "0.12",
679 DEFAULT_MACHINE_OPTIONS
,
682 #define PC_COMPAT_0_11 \
685 .driver = "virtio-blk-pci",\
686 .property = "vectors",\
687 .value = stringify(0),\
689 .driver = TYPE_PCI_DEVICE,\
690 .property = "rombar",\
691 .value = stringify(0),\
694 static QEMUMachine pc_machine_v0_11
= {
696 .desc
= "Standard PC, qemu 0.11",
697 .init
= pc_init_pci_no_kvmclock
,
699 .compat_props
= (GlobalProperty
[]) {
702 .driver
= "ide-drive",
706 .driver
= "scsi-disk",
710 { /* end of list */ }
712 .hw_version
= "0.11",
713 DEFAULT_MACHINE_OPTIONS
,
716 static QEMUMachine pc_machine_v0_10
= {
718 .desc
= "Standard PC, qemu 0.10",
719 .init
= pc_init_pci_no_kvmclock
,
721 .compat_props
= (GlobalProperty
[]) {
724 .driver
= "virtio-blk-pci",
726 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
728 .driver
= "virtio-serial-pci",
730 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
732 .driver
= "virtio-net-pci",
733 .property
= "vectors",
734 .value
= stringify(0),
736 .driver
= "ide-drive",
740 .driver
= "scsi-disk",
744 { /* end of list */ }
746 .hw_version
= "0.10",
747 DEFAULT_MACHINE_OPTIONS
,
750 static QEMUMachine isapc_machine
= {
752 .desc
= "ISA-only PC",
755 .compat_props
= (GlobalProperty
[]) {
757 .driver
= "pc-sysfw",
758 .property
= "rom_only",
759 .value
= stringify(1),
762 .driver
= "pc-sysfw",
763 .property
= "isapc_ram_fw",
764 .value
= stringify(1),
766 { /* end of list */ }
768 DEFAULT_MACHINE_OPTIONS
,
772 static QEMUMachine xenfv_machine
= {
774 .desc
= "Xen Fully-virtualized PC",
775 .init
= pc_xen_hvm_init
,
776 .max_cpus
= HVM_MAX_VCPUS
,
777 .default_machine_opts
= "accel=xen",
778 DEFAULT_MACHINE_OPTIONS
,
782 static void pc_machine_init(void)
784 qemu_register_machine(&pc_i440fx_machine_v1_6
);
785 qemu_register_machine(&pc_i440fx_machine_v1_5
);
786 qemu_register_machine(&pc_i440fx_machine_v1_4
);
787 qemu_register_machine(&pc_machine_v1_3
);
788 qemu_register_machine(&pc_machine_v1_2
);
789 qemu_register_machine(&pc_machine_v1_1
);
790 qemu_register_machine(&pc_machine_v1_0
);
791 qemu_register_machine(&pc_machine_v0_15
);
792 qemu_register_machine(&pc_machine_v0_14
);
793 qemu_register_machine(&pc_machine_v0_13
);
794 qemu_register_machine(&pc_machine_v0_12
);
795 qemu_register_machine(&pc_machine_v0_11
);
796 qemu_register_machine(&pc_machine_v0_10
);
797 qemu_register_machine(&isapc_machine
);
799 qemu_register_machine(&xenfv_machine
);
803 machine_init(pc_machine_init
);