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
37 #include "kvm/clock.h"
40 #include "arch_init.h"
45 #include "exec-memory.h"
48 # include <xen/hvm/hvm_info_table.h>
53 static const int ide_iobase
[MAX_IDE_BUS
] = { 0x1f0, 0x170 };
54 static const int ide_iobase2
[MAX_IDE_BUS
] = { 0x3f6, 0x376 };
55 static const int ide_irq
[MAX_IDE_BUS
] = { 14, 15 };
57 static void kvm_piix3_setup_irq_routing(bool pci_enabled
)
60 KVMState
*s
= kvm_state
;
63 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
64 for (i
= 0; i
< 8; ++i
) {
68 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
70 for (i
= 8; i
< 16; ++i
) {
71 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
74 for (i
= 0; i
< 24; ++i
) {
76 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, 2);
78 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, i
);
83 #endif /* CONFIG_KVM */
86 static void kvm_piix3_gsi_handler(void *opaque
, int n
, int level
)
90 if (n
< ISA_NUM_IRQS
) {
91 /* Kernel will forward to both PIC and IOAPIC */
92 qemu_set_irq(s
->i8259_irq
[n
], level
);
94 qemu_set_irq(s
->ioapic_irq
[n
], level
);
98 static void ioapic_init(GSIState
*gsi_state
)
104 if (kvm_irqchip_in_kernel()) {
105 dev
= qdev_create(NULL
, "kvm-ioapic");
107 dev
= qdev_create(NULL
, "ioapic");
109 /* FIXME: this should be under the piix3. */
110 object_property_add_child(object_resolve_path("i440fx", NULL
),
111 "ioapic", OBJECT(dev
), NULL
);
112 qdev_init_nofail(dev
);
113 d
= sysbus_from_qdev(dev
);
114 sysbus_mmio_map(d
, 0, 0xfec00000);
116 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
117 gsi_state
->ioapic_irq
[i
] = qdev_get_gpio_in(dev
, i
);
121 /* PC hardware initialisation */
122 static void pc_init1(MemoryRegion
*system_memory
,
123 MemoryRegion
*system_io
,
125 const char *boot_device
,
126 const char *kernel_filename
,
127 const char *kernel_cmdline
,
128 const char *initrd_filename
,
129 const char *cpu_model
,
131 int kvmclock_enabled
)
134 ram_addr_t below_4g_mem_size
, above_4g_mem_size
;
137 PCII440FXState
*i440fx_state
;
138 int piix3_devfn
= -1;
144 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
145 BusState
*idebus
[MAX_IDE_BUS
];
146 ISADevice
*rtc_state
;
148 MemoryRegion
*ram_memory
;
149 MemoryRegion
*pci_memory
;
150 MemoryRegion
*rom_memory
;
153 pc_cpus_init(cpu_model
);
155 if (kvmclock_enabled
) {
159 if (ram_size
>= 0xe0000000 ) {
160 above_4g_mem_size
= ram_size
- 0xe0000000;
161 below_4g_mem_size
= 0xe0000000;
163 above_4g_mem_size
= 0;
164 below_4g_mem_size
= ram_size
;
168 pci_memory
= g_new(MemoryRegion
, 1);
169 memory_region_init(pci_memory
, "pci", INT64_MAX
);
170 rom_memory
= pci_memory
;
173 rom_memory
= system_memory
;
176 /* allocate ram and load rom/bios */
177 if (!xen_enabled()) {
178 fw_cfg
= pc_memory_init(system_memory
,
179 kernel_filename
, kernel_cmdline
, initrd_filename
,
180 below_4g_mem_size
, above_4g_mem_size
,
181 pci_enabled
? rom_memory
: system_memory
, &ram_memory
);
184 gsi_state
= g_malloc0(sizeof(*gsi_state
));
185 if (kvm_irqchip_in_kernel()) {
186 kvm_piix3_setup_irq_routing(pci_enabled
);
187 gsi
= qemu_allocate_irqs(kvm_piix3_gsi_handler
, gsi_state
,
190 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
194 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
195 system_memory
, system_io
, ram_size
,
197 0x100000000ULL
- below_4g_mem_size
,
198 0x100000000ULL
+ above_4g_mem_size
,
201 : ((uint64_t)1 << 62)),
202 pci_memory
, ram_memory
);
206 isa_bus
= isa_bus_new(NULL
, system_io
);
209 isa_bus_irqs(isa_bus
, gsi
);
211 if (kvm_irqchip_in_kernel()) {
212 i8259
= kvm_i8259_init(isa_bus
);
213 } else if (xen_enabled()) {
214 i8259
= xen_interrupt_controller_init();
216 cpu_irq
= pc_allocate_cpu_irq();
217 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
220 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
221 gsi_state
->i8259_irq
[i
] = i8259
[i
];
224 ioapic_init(gsi_state
);
227 pc_register_ferr_irq(gsi
[13]);
229 pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
231 pci_create_simple(pci_bus
, -1, "xen-platform");
234 /* init basic PC hardware */
235 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
237 for(i
= 0; i
< nb_nics
; i
++) {
238 NICInfo
*nd
= &nd_table
[i
];
240 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
241 pc_init_ne2k_isa(isa_bus
, nd
);
243 pci_nic_init_nofail(nd
, "e1000", NULL
);
246 ide_drive_get(hd
, MAX_IDE_BUS
);
250 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
252 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
254 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
255 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
257 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
259 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
261 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
262 idebus
[i
] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
266 audio_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
268 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
269 floppy
, idebus
[0], idebus
[1], rtc_state
);
271 if (pci_enabled
&& usb_enabled(false)) {
272 pci_create_simple(pci_bus
, piix3_devfn
+ 2, "piix3-usb-uhci");
275 if (pci_enabled
&& acpi_enabled
) {
278 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
, first_cpu
, 1);
279 /* TODO: Populate SPD eeprom data. */
280 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
282 kvm_enabled(), fw_cfg
);
283 smbus_eeprom_init(smbus
, 8, NULL
, 0);
287 pc_pci_device_init(pci_bus
);
291 static void pc_init_pci(QEMUMachineInitArgs
*args
)
293 ram_addr_t ram_size
= args
->ram_size
;
294 const char *cpu_model
= args
->cpu_model
;
295 const char *kernel_filename
= args
->kernel_filename
;
296 const char *kernel_cmdline
= args
->kernel_cmdline
;
297 const char *initrd_filename
= args
->initrd_filename
;
298 const char *boot_device
= args
->boot_device
;
299 pc_init1(get_system_memory(),
301 ram_size
, boot_device
,
302 kernel_filename
, kernel_cmdline
,
303 initrd_filename
, cpu_model
, 1, 1);
306 static void pc_init_pci_1_3(QEMUMachineInitArgs
*args
)
312 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs
*args
)
314 ram_addr_t ram_size
= args
->ram_size
;
315 const char *cpu_model
= args
->cpu_model
;
316 const char *kernel_filename
= args
->kernel_filename
;
317 const char *kernel_cmdline
= args
->kernel_cmdline
;
318 const char *initrd_filename
= args
->initrd_filename
;
319 const char *boot_device
= args
->boot_device
;
320 pc_init1(get_system_memory(),
322 ram_size
, boot_device
,
323 kernel_filename
, kernel_cmdline
,
324 initrd_filename
, cpu_model
, 1, 0);
327 static void pc_init_isa(QEMUMachineInitArgs
*args
)
329 ram_addr_t ram_size
= args
->ram_size
;
330 const char *cpu_model
= args
->cpu_model
;
331 const char *kernel_filename
= args
->kernel_filename
;
332 const char *kernel_cmdline
= args
->kernel_cmdline
;
333 const char *initrd_filename
= args
->initrd_filename
;
334 const char *boot_device
= args
->boot_device
;
335 if (cpu_model
== NULL
)
337 pc_init1(get_system_memory(),
339 ram_size
, boot_device
,
340 kernel_filename
, kernel_cmdline
,
341 initrd_filename
, cpu_model
, 0, 1);
345 static void pc_xen_hvm_init(QEMUMachineInitArgs
*args
)
347 if (xen_hvm_init() != 0) {
348 hw_error("xen hardware virtual machine initialisation failed");
350 pc_init_pci_no_kvmclock(args
);
355 static QEMUMachine pc_machine_v1_3
= {
358 .desc
= "Standard PC",
359 .init
= pc_init_pci_1_3
,
364 #define PC_COMPAT_1_2 \
366 .driver = "nec-usb-xhci",\
370 .driver = "nec-usb-xhci",\
374 .driver = "ivshmem",\
375 .property = "use64",\
379 .property = "revision",\
380 .value = stringify(3),\
382 .driver = "qxl-vga",\
383 .property = "revision",\
384 .value = stringify(3),\
391 static QEMUMachine pc_machine_v1_2
= {
393 .desc
= "Standard PC",
396 .compat_props
= (GlobalProperty
[]) {
398 { /* end of list */ }
402 #define PC_COMPAT_1_1 \
405 .driver = "virtio-scsi-pci",\
406 .property = "hotplug",\
409 .driver = "virtio-scsi-pci",\
410 .property = "param_change",\
414 .property = "vgamem_mb",\
415 .value = stringify(8),\
417 .driver = "vmware-svga",\
418 .property = "vgamem_mb",\
419 .value = stringify(8),\
421 .driver = "qxl-vga",\
422 .property = "vgamem_mb",\
423 .value = stringify(8),\
426 .property = "vgamem_mb",\
427 .value = stringify(8),\
429 .driver = "virtio-blk-pci",\
430 .property = "config-wce",\
434 static QEMUMachine pc_machine_v1_1
= {
436 .desc
= "Standard PC",
439 .compat_props
= (GlobalProperty
[]) {
441 { /* end of list */ }
445 #define PC_COMPAT_1_0 \
448 .driver = "pc-sysfw",\
449 .property = "rom_only",\
450 .value = stringify(1),\
452 .driver = "isa-fdc",\
453 .property = "check_media_rate",\
456 .driver = "virtio-balloon-pci",\
457 .property = "class",\
458 .value = stringify(PCI_CLASS_MEMORY_RAM),\
461 .property = "vapic",\
464 .driver = TYPE_USB_DEVICE,\
465 .property = "full-path",\
469 static QEMUMachine pc_machine_v1_0
= {
471 .desc
= "Standard PC",
474 .compat_props
= (GlobalProperty
[]) {
476 { /* end of list */ }
481 #define PC_COMPAT_0_15 \
484 static QEMUMachine pc_machine_v0_15
= {
486 .desc
= "Standard PC",
489 .compat_props
= (GlobalProperty
[]) {
491 { /* end of list */ }
493 .hw_version
= "0.15",
496 #define PC_COMPAT_0_14 \
499 .driver = "virtio-blk-pci",\
500 .property = "event_idx",\
503 .driver = "virtio-serial-pci",\
504 .property = "event_idx",\
507 .driver = "virtio-net-pci",\
508 .property = "event_idx",\
511 .driver = "virtio-balloon-pci",\
512 .property = "event_idx",\
516 static QEMUMachine pc_machine_v0_14
= {
518 .desc
= "Standard PC",
521 .compat_props
= (GlobalProperty
[]) {
525 .property
= "revision",
526 .value
= stringify(2),
529 .property
= "revision",
530 .value
= stringify(2),
532 { /* end of list */ }
534 .hw_version
= "0.14",
537 #define PC_COMPAT_0_13 \
540 .driver = TYPE_PCI_DEVICE,\
541 .property = "command_serr_enable",\
545 .property = "use_broken_id",\
546 .value = stringify(1),\
549 static QEMUMachine pc_machine_v0_13
= {
551 .desc
= "Standard PC",
552 .init
= pc_init_pci_no_kvmclock
,
554 .compat_props
= (GlobalProperty
[]) {
557 .driver
= "virtio-9p-pci",
558 .property
= "vectors",
559 .value
= stringify(0),
562 .property
= "rombar",
563 .value
= stringify(0),
565 .driver
= "vmware-svga",
566 .property
= "rombar",
567 .value
= stringify(0),
569 { /* end of list */ }
571 .hw_version
= "0.13",
574 #define PC_COMPAT_0_12 \
577 .driver = "virtio-serial-pci",\
578 .property = "max_ports",\
579 .value = stringify(1),\
581 .driver = "virtio-serial-pci",\
582 .property = "vectors",\
583 .value = stringify(0),\
586 static QEMUMachine pc_machine_v0_12
= {
588 .desc
= "Standard PC",
589 .init
= pc_init_pci_no_kvmclock
,
591 .compat_props
= (GlobalProperty
[]) {
595 .property
= "rombar",
596 .value
= stringify(0),
598 .driver
= "vmware-svga",
599 .property
= "rombar",
600 .value
= stringify(0),
602 { /* end of list */ }
604 .hw_version
= "0.12",
607 #define PC_COMPAT_0_11 \
610 .driver = "virtio-blk-pci",\
611 .property = "vectors",\
612 .value = stringify(0),\
614 .driver = TYPE_PCI_DEVICE,\
615 .property = "rombar",\
616 .value = stringify(0),\
619 static QEMUMachine pc_machine_v0_11
= {
621 .desc
= "Standard PC, qemu 0.11",
622 .init
= pc_init_pci_no_kvmclock
,
624 .compat_props
= (GlobalProperty
[]) {
627 .driver
= "ide-drive",
631 .driver
= "scsi-disk",
635 { /* end of list */ }
637 .hw_version
= "0.11",
640 static QEMUMachine pc_machine_v0_10
= {
642 .desc
= "Standard PC, qemu 0.10",
643 .init
= pc_init_pci_no_kvmclock
,
645 .compat_props
= (GlobalProperty
[]) {
648 .driver
= "virtio-blk-pci",
650 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
652 .driver
= "virtio-serial-pci",
654 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
656 .driver
= "virtio-net-pci",
657 .property
= "vectors",
658 .value
= stringify(0),
660 .driver
= "ide-drive",
664 .driver
= "scsi-disk",
668 { /* end of list */ }
670 .hw_version
= "0.10",
673 static QEMUMachine isapc_machine
= {
675 .desc
= "ISA-only PC",
678 .compat_props
= (GlobalProperty
[]) {
680 .driver
= "pc-sysfw",
681 .property
= "rom_only",
682 .value
= stringify(1),
684 { /* end of list */ }
689 static QEMUMachine xenfv_machine
= {
691 .desc
= "Xen Fully-virtualized PC",
692 .init
= pc_xen_hvm_init
,
693 .max_cpus
= HVM_MAX_VCPUS
,
694 .default_machine_opts
= "accel=xen",
698 static void pc_machine_init(void)
700 qemu_register_machine(&pc_machine_v1_3
);
701 qemu_register_machine(&pc_machine_v1_2
);
702 qemu_register_machine(&pc_machine_v1_1
);
703 qemu_register_machine(&pc_machine_v1_0
);
704 qemu_register_machine(&pc_machine_v0_15
);
705 qemu_register_machine(&pc_machine_v0_14
);
706 qemu_register_machine(&pc_machine_v0_13
);
707 qemu_register_machine(&pc_machine_v0_12
);
708 qemu_register_machine(&pc_machine_v0_11
);
709 qemu_register_machine(&pc_machine_v0_10
);
710 qemu_register_machine(&isapc_machine
);
712 qemu_register_machine(&xenfv_machine
);
716 machine_init(pc_machine_init
);