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
;
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
;
131 guest_info
->isapc_ram_fw
= !pci_enabled
;
133 /* allocate ram and load rom/bios */
134 if (!xen_enabled()) {
135 fw_cfg
= pc_memory_init(system_memory
,
136 kernel_filename
, kernel_cmdline
, initrd_filename
,
137 below_4g_mem_size
, above_4g_mem_size
,
138 rom_memory
, &ram_memory
, guest_info
);
141 gsi_state
= g_malloc0(sizeof(*gsi_state
));
142 if (kvm_irqchip_in_kernel()) {
143 kvm_pc_setup_irq_routing(pci_enabled
);
144 gsi
= qemu_allocate_irqs(kvm_pc_gsi_handler
, gsi_state
,
147 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
151 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
152 system_memory
, system_io
, ram_size
,
154 0x100000000ULL
- below_4g_mem_size
,
156 pci_memory
, ram_memory
);
160 isa_bus
= isa_bus_new(NULL
, system_io
);
163 isa_bus_irqs(isa_bus
, gsi
);
165 if (kvm_irqchip_in_kernel()) {
166 i8259
= kvm_i8259_init(isa_bus
);
167 } else if (xen_enabled()) {
168 i8259
= xen_interrupt_controller_init();
170 cpu_irq
= pc_allocate_cpu_irq();
171 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
174 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
175 gsi_state
->i8259_irq
[i
] = i8259
[i
];
178 ioapic_init_gsi(gsi_state
, "i440fx");
180 qdev_init_nofail(icc_bridge
);
182 pc_register_ferr_irq(gsi
[13]);
184 pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
186 /* init basic PC hardware */
187 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
189 pc_nic_init(isa_bus
, pci_bus
);
191 ide_drive_get(hd
, MAX_IDE_BUS
);
195 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
197 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
199 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
200 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
202 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
204 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
206 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
207 idebus
[i
] = qdev_get_child_bus(DEVICE(dev
), "ide.0");
211 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
212 floppy
, idebus
[0], idebus
[1], rtc_state
);
214 if (pci_enabled
&& usb_enabled(false)) {
215 pci_create_simple(pci_bus
, piix3_devfn
+ 2, "piix3-usb-uhci");
218 if (pci_enabled
&& acpi_enabled
) {
221 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
, first_cpu
, 1);
222 /* TODO: Populate SPD eeprom data. */
223 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
225 kvm_enabled(), fw_cfg
);
226 smbus_eeprom_init(smbus
, 8, NULL
, 0);
230 pc_pci_device_init(pci_bus
);
234 pvpanic_init(isa_bus
);
238 static void pc_init_pci(QEMUMachineInitArgs
*args
)
240 ram_addr_t ram_size
= args
->ram_size
;
241 const char *cpu_model
= args
->cpu_model
;
242 const char *kernel_filename
= args
->kernel_filename
;
243 const char *kernel_cmdline
= args
->kernel_cmdline
;
244 const char *initrd_filename
= args
->initrd_filename
;
245 const char *boot_device
= args
->boot_device
;
246 pc_init1(get_system_memory(),
248 ram_size
, boot_device
,
249 kernel_filename
, kernel_cmdline
,
250 initrd_filename
, cpu_model
, 1, 1);
253 static void pc_init_pci_1_6(QEMUMachineInitArgs
*args
)
255 has_pci_info
= false;
260 static void pc_init_pci_1_5(QEMUMachineInitArgs
*args
)
262 pc_init_pci_1_6(args
);
265 static void pc_init_pci_1_4(QEMUMachineInitArgs
*args
)
267 x86_cpu_compat_set_features("n270", FEAT_1_ECX
, 0, CPUID_EXT_MOVBE
);
268 has_pci_info
= false;
272 static void pc_init_pci_1_3(QEMUMachineInitArgs
*args
)
274 enable_compat_apic_id_mode();
275 pc_init_pci_1_4(args
);
278 /* PC machine init function for pc-1.1 to pc-1.2 */
279 static void pc_init_pci_1_2(QEMUMachineInitArgs
*args
)
281 disable_kvm_pv_eoi();
282 pc_init_pci_1_3(args
);
285 /* PC machine init function for pc-0.14 to pc-1.0 */
286 static void pc_init_pci_1_0(QEMUMachineInitArgs
*args
)
288 pc_init_pci_1_2(args
);
291 /* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
292 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs
*args
)
294 ram_addr_t ram_size
= args
->ram_size
;
295 const char *cpu_model
= args
->cpu_model
;
296 const char *kernel_filename
= args
->kernel_filename
;
297 const char *kernel_cmdline
= args
->kernel_cmdline
;
298 const char *initrd_filename
= args
->initrd_filename
;
299 const char *boot_device
= args
->boot_device
;
300 has_pci_info
= false;
301 disable_kvm_pv_eoi();
302 enable_compat_apic_id_mode();
303 pc_init1(get_system_memory(),
305 ram_size
, boot_device
,
306 kernel_filename
, kernel_cmdline
,
307 initrd_filename
, cpu_model
, 1, 0);
310 static void pc_init_isa(QEMUMachineInitArgs
*args
)
312 ram_addr_t ram_size
= args
->ram_size
;
313 const char *cpu_model
= args
->cpu_model
;
314 const char *kernel_filename
= args
->kernel_filename
;
315 const char *kernel_cmdline
= args
->kernel_cmdline
;
316 const char *initrd_filename
= args
->initrd_filename
;
317 const char *boot_device
= args
->boot_device
;
318 has_pci_info
= false;
319 if (cpu_model
== NULL
)
321 disable_kvm_pv_eoi();
322 enable_compat_apic_id_mode();
323 pc_init1(get_system_memory(),
325 ram_size
, boot_device
,
326 kernel_filename
, kernel_cmdline
,
327 initrd_filename
, cpu_model
, 0, 1);
331 static void pc_xen_hvm_init(QEMUMachineInitArgs
*args
)
337 bus
= pci_find_primary_bus();
339 pci_create_simple(bus
, -1, "xen-platform");
344 static QEMUMachine pc_i440fx_machine_v1_6
= {
345 .name
= "pc-i440fx-1.6",
347 .desc
= "Standard PC (i440FX + PIIX, 1996)",
348 .init
= pc_init_pci_1_6
,
349 .hot_add_cpu
= pc_hot_add_cpu
,
352 DEFAULT_MACHINE_OPTIONS
,
355 static QEMUMachine pc_i440fx_machine_v1_5
= {
356 .name
= "pc-i440fx-1.5",
357 .desc
= "Standard PC (i440FX + PIIX, 1996)",
358 .init
= pc_init_pci_1_5
,
359 .hot_add_cpu
= pc_hot_add_cpu
,
361 .compat_props
= (GlobalProperty
[]) {
363 { /* end of list */ }
365 DEFAULT_MACHINE_OPTIONS
,
368 static QEMUMachine pc_i440fx_machine_v1_4
= {
369 .name
= "pc-i440fx-1.4",
370 .desc
= "Standard PC (i440FX + PIIX, 1996)",
371 .init
= pc_init_pci_1_4
,
373 .compat_props
= (GlobalProperty
[]) {
375 { /* end of list */ }
377 DEFAULT_MACHINE_OPTIONS
,
380 #define PC_COMPAT_1_3 \
383 .driver = "usb-tablet",\
384 .property = "usb_version",\
385 .value = stringify(1),\
387 .driver = "virtio-net-pci",\
388 .property = "ctrl_mac_addr",\
391 .driver = "virtio-net-pci", \
396 .property = "autonegotiation",\
400 static QEMUMachine pc_machine_v1_3
= {
402 .desc
= "Standard PC",
403 .init
= pc_init_pci_1_3
,
405 .compat_props
= (GlobalProperty
[]) {
407 { /* end of list */ }
409 DEFAULT_MACHINE_OPTIONS
,
412 #define PC_COMPAT_1_2 \
415 .driver = "nec-usb-xhci",\
419 .driver = "nec-usb-xhci",\
423 .driver = "ivshmem",\
424 .property = "use64",\
428 .property = "revision",\
429 .value = stringify(3),\
431 .driver = "qxl-vga",\
432 .property = "revision",\
433 .value = stringify(3),\
440 static QEMUMachine pc_machine_v1_2
= {
442 .desc
= "Standard PC",
443 .init
= pc_init_pci_1_2
,
445 .compat_props
= (GlobalProperty
[]) {
447 { /* end of list */ }
449 DEFAULT_MACHINE_OPTIONS
,
452 #define PC_COMPAT_1_1 \
455 .driver = "virtio-scsi-pci",\
456 .property = "hotplug",\
459 .driver = "virtio-scsi-pci",\
460 .property = "param_change",\
464 .property = "vgamem_mb",\
465 .value = stringify(8),\
467 .driver = "vmware-svga",\
468 .property = "vgamem_mb",\
469 .value = stringify(8),\
471 .driver = "qxl-vga",\
472 .property = "vgamem_mb",\
473 .value = stringify(8),\
476 .property = "vgamem_mb",\
477 .value = stringify(8),\
479 .driver = "virtio-blk-pci",\
480 .property = "config-wce",\
484 static QEMUMachine pc_machine_v1_1
= {
486 .desc
= "Standard PC",
487 .init
= pc_init_pci_1_2
,
489 .compat_props
= (GlobalProperty
[]) {
491 { /* end of list */ }
493 DEFAULT_MACHINE_OPTIONS
,
496 #define PC_COMPAT_1_0 \
499 .driver = TYPE_ISA_FDC,\
500 .property = "check_media_rate",\
503 .driver = "virtio-balloon-pci",\
504 .property = "class",\
505 .value = stringify(PCI_CLASS_MEMORY_RAM),\
508 .property = "vapic",\
511 .driver = TYPE_USB_DEVICE,\
512 .property = "full-path",\
516 static QEMUMachine pc_machine_v1_0
= {
518 .desc
= "Standard PC",
519 .init
= pc_init_pci_1_0
,
521 .compat_props
= (GlobalProperty
[]) {
523 { /* end of list */ }
526 DEFAULT_MACHINE_OPTIONS
,
529 #define PC_COMPAT_0_15 \
532 static QEMUMachine pc_machine_v0_15
= {
534 .desc
= "Standard PC",
535 .init
= pc_init_pci_1_0
,
537 .compat_props
= (GlobalProperty
[]) {
539 { /* end of list */ }
541 .hw_version
= "0.15",
542 DEFAULT_MACHINE_OPTIONS
,
545 #define PC_COMPAT_0_14 \
548 .driver = "virtio-blk-pci",\
549 .property = "event_idx",\
552 .driver = "virtio-serial-pci",\
553 .property = "event_idx",\
556 .driver = "virtio-net-pci",\
557 .property = "event_idx",\
560 .driver = "virtio-balloon-pci",\
561 .property = "event_idx",\
565 static QEMUMachine pc_machine_v0_14
= {
567 .desc
= "Standard PC",
568 .init
= pc_init_pci_1_0
,
570 .compat_props
= (GlobalProperty
[]) {
574 .property
= "revision",
575 .value
= stringify(2),
578 .property
= "revision",
579 .value
= stringify(2),
581 { /* end of list */ }
583 .hw_version
= "0.14",
584 DEFAULT_MACHINE_OPTIONS
,
587 #define PC_COMPAT_0_13 \
590 .driver = TYPE_PCI_DEVICE,\
591 .property = "command_serr_enable",\
595 .property = "use_broken_id",\
596 .value = stringify(1),\
599 static QEMUMachine pc_machine_v0_13
= {
601 .desc
= "Standard PC",
602 .init
= pc_init_pci_no_kvmclock
,
604 .compat_props
= (GlobalProperty
[]) {
607 .driver
= "virtio-9p-pci",
608 .property
= "vectors",
609 .value
= stringify(0),
612 .property
= "rombar",
613 .value
= stringify(0),
615 .driver
= "vmware-svga",
616 .property
= "rombar",
617 .value
= stringify(0),
619 { /* end of list */ }
621 .hw_version
= "0.13",
622 DEFAULT_MACHINE_OPTIONS
,
625 #define PC_COMPAT_0_12 \
628 .driver = "virtio-serial-pci",\
629 .property = "max_ports",\
630 .value = stringify(1),\
632 .driver = "virtio-serial-pci",\
633 .property = "vectors",\
634 .value = stringify(0),\
636 .driver = "usb-mouse",\
637 .property = "serial",\
640 .driver = "usb-tablet",\
641 .property = "serial",\
644 .driver = "usb-kbd",\
645 .property = "serial",\
649 static QEMUMachine pc_machine_v0_12
= {
651 .desc
= "Standard PC",
652 .init
= pc_init_pci_no_kvmclock
,
654 .compat_props
= (GlobalProperty
[]) {
658 .property
= "rombar",
659 .value
= stringify(0),
661 .driver
= "vmware-svga",
662 .property
= "rombar",
663 .value
= stringify(0),
665 { /* end of list */ }
667 .hw_version
= "0.12",
668 DEFAULT_MACHINE_OPTIONS
,
671 #define PC_COMPAT_0_11 \
674 .driver = "virtio-blk-pci",\
675 .property = "vectors",\
676 .value = stringify(0),\
678 .driver = TYPE_PCI_DEVICE,\
679 .property = "rombar",\
680 .value = stringify(0),\
683 static QEMUMachine pc_machine_v0_11
= {
685 .desc
= "Standard PC, qemu 0.11",
686 .init
= pc_init_pci_no_kvmclock
,
688 .compat_props
= (GlobalProperty
[]) {
691 .driver
= "ide-drive",
695 .driver
= "scsi-disk",
699 { /* end of list */ }
701 .hw_version
= "0.11",
702 DEFAULT_MACHINE_OPTIONS
,
705 static QEMUMachine pc_machine_v0_10
= {
707 .desc
= "Standard PC, qemu 0.10",
708 .init
= pc_init_pci_no_kvmclock
,
710 .compat_props
= (GlobalProperty
[]) {
713 .driver
= "virtio-blk-pci",
715 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
717 .driver
= "virtio-serial-pci",
719 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
721 .driver
= "virtio-net-pci",
722 .property
= "vectors",
723 .value
= stringify(0),
725 .driver
= "ide-drive",
729 .driver
= "scsi-disk",
733 { /* end of list */ }
735 .hw_version
= "0.10",
736 DEFAULT_MACHINE_OPTIONS
,
739 static QEMUMachine isapc_machine
= {
741 .desc
= "ISA-only PC",
744 .compat_props
= (GlobalProperty
[]) {
745 { /* end of list */ }
747 DEFAULT_MACHINE_OPTIONS
,
751 static QEMUMachine xenfv_machine
= {
753 .desc
= "Xen Fully-virtualized PC",
754 .init
= pc_xen_hvm_init
,
755 .max_cpus
= HVM_MAX_VCPUS
,
756 .default_machine_opts
= "accel=xen",
757 DEFAULT_MACHINE_OPTIONS
,
761 static void pc_machine_init(void)
763 qemu_register_machine(&pc_i440fx_machine_v1_6
);
764 qemu_register_machine(&pc_i440fx_machine_v1_5
);
765 qemu_register_machine(&pc_i440fx_machine_v1_4
);
766 qemu_register_machine(&pc_machine_v1_3
);
767 qemu_register_machine(&pc_machine_v1_2
);
768 qemu_register_machine(&pc_machine_v1_1
);
769 qemu_register_machine(&pc_machine_v1_0
);
770 qemu_register_machine(&pc_machine_v0_15
);
771 qemu_register_machine(&pc_machine_v0_14
);
772 qemu_register_machine(&pc_machine_v0_13
);
773 qemu_register_machine(&pc_machine_v0_12
);
774 qemu_register_machine(&pc_machine_v0_11
);
775 qemu_register_machine(&pc_machine_v0_10
);
776 qemu_register_machine(&isapc_machine
);
778 qemu_register_machine(&xenfv_machine
);
782 machine_init(pc_machine_init
);