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
;
93 PcGuestInfo
*guest_info
;
95 if (xen_enabled() && xen_hvm_init() != 0) {
96 fprintf(stderr
, "xen hardware virtual machine initialisation failed\n");
100 icc_bridge
= qdev_create(NULL
, TYPE_ICC_BRIDGE
);
101 object_property_add_child(qdev_get_machine(), "icc-bridge",
102 OBJECT(icc_bridge
), NULL
);
104 pc_cpus_init(cpu_model
, icc_bridge
);
105 pc_acpi_init("acpi-dsdt.aml");
107 if (kvm_enabled() && kvmclock_enabled
) {
111 if (ram_size
>= 0xe0000000 ) {
112 above_4g_mem_size
= ram_size
- 0xe0000000;
113 below_4g_mem_size
= 0xe0000000;
115 above_4g_mem_size
= 0;
116 below_4g_mem_size
= ram_size
;
120 pci_memory
= g_new(MemoryRegion
, 1);
121 memory_region_init(pci_memory
, "pci", INT64_MAX
);
122 rom_memory
= pci_memory
;
125 rom_memory
= system_memory
;
128 guest_info
= pc_guest_info_init(below_4g_mem_size
, above_4g_mem_size
);
130 /* Set PCI window size the way seabios has always done it. */
131 /* Power of 2 so bios can cover it with a single MTRR */
132 if (ram_size
<= 0x80000000)
133 guest_info
->pci_info
.w32
.begin
= 0x80000000;
134 else if (ram_size
<= 0xc0000000)
135 guest_info
->pci_info
.w32
.begin
= 0xc0000000;
137 guest_info
->pci_info
.w32
.begin
= 0xe0000000;
139 /* allocate ram and load rom/bios */
140 if (!xen_enabled()) {
141 fw_cfg
= pc_memory_init(system_memory
,
142 kernel_filename
, kernel_cmdline
, initrd_filename
,
143 below_4g_mem_size
, above_4g_mem_size
,
144 rom_memory
, &ram_memory
, guest_info
);
147 gsi_state
= g_malloc0(sizeof(*gsi_state
));
148 if (kvm_irqchip_in_kernel()) {
149 kvm_pc_setup_irq_routing(pci_enabled
);
150 gsi
= qemu_allocate_irqs(kvm_pc_gsi_handler
, gsi_state
,
153 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
157 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
158 system_memory
, system_io
, ram_size
,
160 0x100000000ULL
- below_4g_mem_size
,
161 0x100000000ULL
+ above_4g_mem_size
,
164 : ((uint64_t)1 << 62)),
165 pci_memory
, ram_memory
);
169 isa_bus
= isa_bus_new(NULL
, system_io
);
172 isa_bus_irqs(isa_bus
, gsi
);
174 if (kvm_irqchip_in_kernel()) {
175 i8259
= kvm_i8259_init(isa_bus
);
176 } else if (xen_enabled()) {
177 i8259
= xen_interrupt_controller_init();
179 cpu_irq
= pc_allocate_cpu_irq();
180 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
183 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
184 gsi_state
->i8259_irq
[i
] = i8259
[i
];
187 ioapic_init_gsi(gsi_state
, "i440fx");
189 qdev_init_nofail(icc_bridge
);
191 pc_register_ferr_irq(gsi
[13]);
193 pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
195 /* init basic PC hardware */
196 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
198 pc_nic_init(isa_bus
, pci_bus
);
200 ide_drive_get(hd
, MAX_IDE_BUS
);
204 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
206 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
208 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
209 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
211 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
213 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
215 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
216 idebus
[i
] = qdev_get_child_bus(DEVICE(dev
), "ide.0");
220 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
221 floppy
, idebus
[0], idebus
[1], rtc_state
);
223 if (pci_enabled
&& usb_enabled(false)) {
224 pci_create_simple(pci_bus
, piix3_devfn
+ 2, "piix3-usb-uhci");
227 if (pci_enabled
&& acpi_enabled
) {
230 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
,
231 x86_env_get_cpu(first_cpu
), 1);
232 /* TODO: Populate SPD eeprom data. */
233 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
235 kvm_enabled(), fw_cfg
);
236 smbus_eeprom_init(smbus
, 8, NULL
, 0);
240 pc_pci_device_init(pci_bus
);
244 pvpanic_init(isa_bus
);
248 static void pc_init_pci(QEMUMachineInitArgs
*args
)
250 ram_addr_t ram_size
= args
->ram_size
;
251 const char *cpu_model
= args
->cpu_model
;
252 const char *kernel_filename
= args
->kernel_filename
;
253 const char *kernel_cmdline
= args
->kernel_cmdline
;
254 const char *initrd_filename
= args
->initrd_filename
;
255 const char *boot_device
= args
->boot_device
;
256 pc_init1(get_system_memory(),
258 ram_size
, boot_device
,
259 kernel_filename
, kernel_cmdline
,
260 initrd_filename
, cpu_model
, 1, 1);
263 static void pc_init_pci_1_4(QEMUMachineInitArgs
*args
)
266 x86_cpu_compat_set_features("n270", FEAT_1_ECX
, 0, CPUID_EXT_MOVBE
);
270 static void pc_init_pci_1_3(QEMUMachineInitArgs
*args
)
272 enable_compat_apic_id_mode();
277 /* PC machine init function for pc-1.1 to pc-1.2 */
278 static void pc_init_pci_1_2(QEMUMachineInitArgs
*args
)
280 disable_kvm_pv_eoi();
281 enable_compat_apic_id_mode();
286 /* PC machine init function for pc-0.14 to pc-1.0 */
287 static void pc_init_pci_1_0(QEMUMachineInitArgs
*args
)
289 disable_kvm_pv_eoi();
290 enable_compat_apic_id_mode();
295 /* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
296 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs
*args
)
298 ram_addr_t ram_size
= args
->ram_size
;
299 const char *cpu_model
= args
->cpu_model
;
300 const char *kernel_filename
= args
->kernel_filename
;
301 const char *kernel_cmdline
= args
->kernel_cmdline
;
302 const char *initrd_filename
= args
->initrd_filename
;
303 const char *boot_device
= args
->boot_device
;
305 disable_kvm_pv_eoi();
306 enable_compat_apic_id_mode();
307 pc_init1(get_system_memory(),
309 ram_size
, boot_device
,
310 kernel_filename
, kernel_cmdline
,
311 initrd_filename
, cpu_model
, 1, 0);
314 static void pc_init_isa(QEMUMachineInitArgs
*args
)
316 ram_addr_t ram_size
= args
->ram_size
;
317 const char *cpu_model
= args
->cpu_model
;
318 const char *kernel_filename
= args
->kernel_filename
;
319 const char *kernel_cmdline
= args
->kernel_cmdline
;
320 const char *initrd_filename
= args
->initrd_filename
;
321 const char *boot_device
= args
->boot_device
;
323 if (cpu_model
== NULL
)
325 disable_kvm_pv_eoi();
326 enable_compat_apic_id_mode();
327 pc_init1(get_system_memory(),
329 ram_size
, boot_device
,
330 kernel_filename
, kernel_cmdline
,
331 initrd_filename
, cpu_model
, 0, 1);
335 static void pc_xen_hvm_init(QEMUMachineInitArgs
*args
)
341 bus
= pci_find_root_bus(0);
343 pci_create_simple(bus
, -1, "xen-platform");
348 static QEMUMachine pc_i440fx_machine_v1_6
= {
349 .name
= "pc-i440fx-1.6",
351 .desc
= "Standard PC (i440FX + PIIX, 1996)",
353 .hot_add_cpu
= pc_hot_add_cpu
,
356 DEFAULT_MACHINE_OPTIONS
,
359 static QEMUMachine pc_i440fx_machine_v1_5
= {
360 .name
= "pc-i440fx-1.5",
361 .desc
= "Standard PC (i440FX + PIIX, 1996)",
363 .hot_add_cpu
= pc_hot_add_cpu
,
365 .compat_props
= (GlobalProperty
[]) {
367 { /* end of list */ }
369 DEFAULT_MACHINE_OPTIONS
,
372 static QEMUMachine pc_i440fx_machine_v1_4
= {
373 .name
= "pc-i440fx-1.4",
374 .desc
= "Standard PC (i440FX + PIIX, 1996)",
375 .init
= pc_init_pci_1_4
,
377 .compat_props
= (GlobalProperty
[]) {
379 { /* end of list */ }
381 DEFAULT_MACHINE_OPTIONS
,
384 #define PC_COMPAT_1_3 \
387 .driver = "usb-tablet",\
388 .property = "usb_version",\
389 .value = stringify(1),\
391 .driver = "virtio-net-pci",\
392 .property = "ctrl_mac_addr",\
395 .driver = "virtio-net-pci", \
400 .property = "autonegotiation",\
404 static QEMUMachine pc_machine_v1_3
= {
406 .desc
= "Standard PC",
407 .init
= pc_init_pci_1_3
,
409 .compat_props
= (GlobalProperty
[]) {
411 { /* end of list */ }
413 DEFAULT_MACHINE_OPTIONS
,
416 #define PC_COMPAT_1_2 \
419 .driver = "nec-usb-xhci",\
423 .driver = "nec-usb-xhci",\
427 .driver = "ivshmem",\
428 .property = "use64",\
432 .property = "revision",\
433 .value = stringify(3),\
435 .driver = "qxl-vga",\
436 .property = "revision",\
437 .value = stringify(3),\
444 static QEMUMachine pc_machine_v1_2
= {
446 .desc
= "Standard PC",
447 .init
= pc_init_pci_1_2
,
449 .compat_props
= (GlobalProperty
[]) {
451 { /* end of list */ }
453 DEFAULT_MACHINE_OPTIONS
,
456 #define PC_COMPAT_1_1 \
459 .driver = "virtio-scsi-pci",\
460 .property = "hotplug",\
463 .driver = "virtio-scsi-pci",\
464 .property = "param_change",\
468 .property = "vgamem_mb",\
469 .value = stringify(8),\
471 .driver = "vmware-svga",\
472 .property = "vgamem_mb",\
473 .value = stringify(8),\
475 .driver = "qxl-vga",\
476 .property = "vgamem_mb",\
477 .value = stringify(8),\
480 .property = "vgamem_mb",\
481 .value = stringify(8),\
483 .driver = "virtio-blk-pci",\
484 .property = "config-wce",\
488 static QEMUMachine pc_machine_v1_1
= {
490 .desc
= "Standard PC",
491 .init
= pc_init_pci_1_2
,
493 .compat_props
= (GlobalProperty
[]) {
495 { /* end of list */ }
497 DEFAULT_MACHINE_OPTIONS
,
500 #define PC_COMPAT_1_0 \
503 .driver = "pc-sysfw",\
504 .property = "rom_only",\
505 .value = stringify(1),\
507 .driver = TYPE_ISA_FDC,\
508 .property = "check_media_rate",\
511 .driver = "virtio-balloon-pci",\
512 .property = "class",\
513 .value = stringify(PCI_CLASS_MEMORY_RAM),\
516 .property = "vapic",\
519 .driver = TYPE_USB_DEVICE,\
520 .property = "full-path",\
524 static QEMUMachine pc_machine_v1_0
= {
526 .desc
= "Standard PC",
527 .init
= pc_init_pci_1_0
,
529 .compat_props
= (GlobalProperty
[]) {
531 { /* end of list */ }
534 DEFAULT_MACHINE_OPTIONS
,
537 #define PC_COMPAT_0_15 \
540 static QEMUMachine pc_machine_v0_15
= {
542 .desc
= "Standard PC",
543 .init
= pc_init_pci_1_0
,
545 .compat_props
= (GlobalProperty
[]) {
547 { /* end of list */ }
549 .hw_version
= "0.15",
550 DEFAULT_MACHINE_OPTIONS
,
553 #define PC_COMPAT_0_14 \
556 .driver = "virtio-blk-pci",\
557 .property = "event_idx",\
560 .driver = "virtio-serial-pci",\
561 .property = "event_idx",\
564 .driver = "virtio-net-pci",\
565 .property = "event_idx",\
568 .driver = "virtio-balloon-pci",\
569 .property = "event_idx",\
573 static QEMUMachine pc_machine_v0_14
= {
575 .desc
= "Standard PC",
576 .init
= pc_init_pci_1_0
,
578 .compat_props
= (GlobalProperty
[]) {
582 .property
= "revision",
583 .value
= stringify(2),
586 .property
= "revision",
587 .value
= stringify(2),
589 { /* end of list */ }
591 .hw_version
= "0.14",
592 DEFAULT_MACHINE_OPTIONS
,
595 #define PC_COMPAT_0_13 \
598 .driver = TYPE_PCI_DEVICE,\
599 .property = "command_serr_enable",\
603 .property = "use_broken_id",\
604 .value = stringify(1),\
607 static QEMUMachine pc_machine_v0_13
= {
609 .desc
= "Standard PC",
610 .init
= pc_init_pci_no_kvmclock
,
612 .compat_props
= (GlobalProperty
[]) {
615 .driver
= "virtio-9p-pci",
616 .property
= "vectors",
617 .value
= stringify(0),
620 .property
= "rombar",
621 .value
= stringify(0),
623 .driver
= "vmware-svga",
624 .property
= "rombar",
625 .value
= stringify(0),
627 { /* end of list */ }
629 .hw_version
= "0.13",
630 DEFAULT_MACHINE_OPTIONS
,
633 #define PC_COMPAT_0_12 \
636 .driver = "virtio-serial-pci",\
637 .property = "max_ports",\
638 .value = stringify(1),\
640 .driver = "virtio-serial-pci",\
641 .property = "vectors",\
642 .value = stringify(0),\
644 .driver = "usb-mouse",\
645 .property = "serial",\
648 .driver = "usb-tablet",\
649 .property = "serial",\
652 .driver = "usb-kbd",\
653 .property = "serial",\
657 static QEMUMachine pc_machine_v0_12
= {
659 .desc
= "Standard PC",
660 .init
= pc_init_pci_no_kvmclock
,
662 .compat_props
= (GlobalProperty
[]) {
666 .property
= "rombar",
667 .value
= stringify(0),
669 .driver
= "vmware-svga",
670 .property
= "rombar",
671 .value
= stringify(0),
673 { /* end of list */ }
675 .hw_version
= "0.12",
676 DEFAULT_MACHINE_OPTIONS
,
679 #define PC_COMPAT_0_11 \
682 .driver = "virtio-blk-pci",\
683 .property = "vectors",\
684 .value = stringify(0),\
686 .driver = TYPE_PCI_DEVICE,\
687 .property = "rombar",\
688 .value = stringify(0),\
691 static QEMUMachine pc_machine_v0_11
= {
693 .desc
= "Standard PC, qemu 0.11",
694 .init
= pc_init_pci_no_kvmclock
,
696 .compat_props
= (GlobalProperty
[]) {
699 .driver
= "ide-drive",
703 .driver
= "scsi-disk",
707 { /* end of list */ }
709 .hw_version
= "0.11",
710 DEFAULT_MACHINE_OPTIONS
,
713 static QEMUMachine pc_machine_v0_10
= {
715 .desc
= "Standard PC, qemu 0.10",
716 .init
= pc_init_pci_no_kvmclock
,
718 .compat_props
= (GlobalProperty
[]) {
721 .driver
= "virtio-blk-pci",
723 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
725 .driver
= "virtio-serial-pci",
727 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
729 .driver
= "virtio-net-pci",
730 .property
= "vectors",
731 .value
= stringify(0),
733 .driver
= "ide-drive",
737 .driver
= "scsi-disk",
741 { /* end of list */ }
743 .hw_version
= "0.10",
744 DEFAULT_MACHINE_OPTIONS
,
747 static QEMUMachine isapc_machine
= {
749 .desc
= "ISA-only PC",
752 .compat_props
= (GlobalProperty
[]) {
754 .driver
= "pc-sysfw",
755 .property
= "rom_only",
756 .value
= stringify(1),
759 .driver
= "pc-sysfw",
760 .property
= "isapc_ram_fw",
761 .value
= stringify(1),
763 { /* end of list */ }
765 DEFAULT_MACHINE_OPTIONS
,
769 static QEMUMachine xenfv_machine
= {
771 .desc
= "Xen Fully-virtualized PC",
772 .init
= pc_xen_hvm_init
,
773 .max_cpus
= HVM_MAX_VCPUS
,
774 .default_machine_opts
= "accel=xen",
775 DEFAULT_MACHINE_OPTIONS
,
779 static void pc_machine_init(void)
781 qemu_register_machine(&pc_i440fx_machine_v1_6
);
782 qemu_register_machine(&pc_i440fx_machine_v1_5
);
783 qemu_register_machine(&pc_i440fx_machine_v1_4
);
784 qemu_register_machine(&pc_machine_v1_3
);
785 qemu_register_machine(&pc_machine_v1_2
);
786 qemu_register_machine(&pc_machine_v1_1
);
787 qemu_register_machine(&pc_machine_v1_0
);
788 qemu_register_machine(&pc_machine_v0_15
);
789 qemu_register_machine(&pc_machine_v0_14
);
790 qemu_register_machine(&pc_machine_v0_13
);
791 qemu_register_machine(&pc_machine_v0_12
);
792 qemu_register_machine(&pc_machine_v0_11
);
793 qemu_register_machine(&pc_machine_v0_10
);
794 qemu_register_machine(&isapc_machine
);
796 qemu_register_machine(&xenfv_machine
);
800 machine_init(pc_machine_init
);