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"
47 # include <xen/hvm/hvm_info_table.h>
52 static const int ide_iobase
[MAX_IDE_BUS
] = { 0x1f0, 0x170 };
53 static const int ide_iobase2
[MAX_IDE_BUS
] = { 0x3f6, 0x376 };
54 static const int ide_irq
[MAX_IDE_BUS
] = { 14, 15 };
56 const char *global_cpu_model
; /* cpu hotadd */
58 static void kvm_piix3_setup_irq_routing(bool pci_enabled
)
61 KVMState
*s
= kvm_state
;
64 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
65 for (i
= 0; i
< 8; ++i
) {
69 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
71 for (i
= 8; i
< 16; ++i
) {
72 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
75 for (i
= 0; i
< 24; ++i
) {
77 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_IOAPIC
, 2);
79 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_IOAPIC
, i
);
83 ret
= kvm_irqchip_commit_routes(s
);
85 hw_error("KVM IRQ routing setup failed");
88 #endif /* CONFIG_KVM */
91 static void kvm_piix3_gsi_handler(void *opaque
, int n
, int level
)
95 if (n
< ISA_NUM_IRQS
) {
96 /* Kernel will forward to both PIC and IOAPIC */
97 qemu_set_irq(s
->i8259_irq
[n
], level
);
99 qemu_set_irq(s
->ioapic_irq
[n
], level
);
103 static void ioapic_init(GSIState
*gsi_state
)
109 if (kvm_irqchip_in_kernel()) {
110 dev
= qdev_create(NULL
, "kvm-ioapic");
112 dev
= qdev_create(NULL
, "ioapic");
114 qdev_init_nofail(dev
);
115 d
= sysbus_from_qdev(dev
);
116 sysbus_mmio_map(d
, 0, 0xfec00000);
118 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
119 gsi_state
->ioapic_irq
[i
] = qdev_get_gpio_in(dev
, i
);
123 /* PC hardware initialisation */
124 static void pc_init1(MemoryRegion
*system_memory
,
125 MemoryRegion
*system_io
,
127 const char *boot_device
,
128 const char *kernel_filename
,
129 const char *kernel_cmdline
,
130 const char *initrd_filename
,
131 const char *cpu_model
,
133 int kvmclock_enabled
)
136 ram_addr_t below_4g_mem_size
, above_4g_mem_size
;
139 PCII440FXState
*i440fx_state
;
140 int piix3_devfn
= -1;
146 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
147 BusState
*idebus
[MAX_IDE_BUS
];
148 ISADevice
*rtc_state
;
150 MemoryRegion
*ram_memory
;
151 MemoryRegion
*pci_memory
;
152 MemoryRegion
*rom_memory
;
155 global_cpu_model
= cpu_model
;
157 pc_cpus_init(cpu_model
);
159 if (kvmclock_enabled
) {
163 if (ram_size
>= 0xe0000000 ) {
164 above_4g_mem_size
= ram_size
- 0xe0000000;
165 below_4g_mem_size
= 0xe0000000;
167 above_4g_mem_size
= 0;
168 below_4g_mem_size
= ram_size
;
172 pci_memory
= g_new(MemoryRegion
, 1);
173 memory_region_init(pci_memory
, "pci", INT64_MAX
);
174 rom_memory
= pci_memory
;
177 rom_memory
= system_memory
;
180 /* allocate ram and load rom/bios */
181 if (!xen_enabled()) {
182 pc_memory_init(system_memory
,
183 kernel_filename
, kernel_cmdline
, initrd_filename
,
184 below_4g_mem_size
, above_4g_mem_size
,
185 pci_enabled
? rom_memory
: system_memory
, &ram_memory
);
188 gsi_state
= g_malloc0(sizeof(*gsi_state
));
189 if (kvm_irqchip_in_kernel()) {
190 kvm_piix3_setup_irq_routing(pci_enabled
);
191 gsi
= qemu_allocate_irqs(kvm_piix3_gsi_handler
, gsi_state
,
194 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
198 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
199 system_memory
, system_io
, ram_size
,
201 0x100000000ULL
- below_4g_mem_size
,
202 0x100000000ULL
+ above_4g_mem_size
,
203 (sizeof(target_phys_addr_t
) == 4
205 : ((uint64_t)1 << 62)),
206 pci_memory
, ram_memory
);
210 isa_bus
= isa_bus_new(NULL
, system_io
);
213 isa_bus_irqs(isa_bus
, gsi
);
215 if (kvm_irqchip_in_kernel()) {
216 i8259
= kvm_i8259_init(isa_bus
);
217 } else if (xen_enabled()) {
218 i8259
= xen_interrupt_controller_init();
220 cpu_irq
= pc_allocate_cpu_irq();
221 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
224 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
225 gsi_state
->i8259_irq
[i
] = i8259
[i
];
228 ioapic_init(gsi_state
);
231 pc_register_ferr_irq(gsi
[13]);
233 dev
= pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
235 object_property_add_child(object_get_root(), "vga", OBJECT(dev
), NULL
);
239 pci_create_simple(pci_bus
, -1, "xen-platform");
242 /* init basic PC hardware */
243 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
245 for(i
= 0; i
< nb_nics
; i
++) {
246 NICInfo
*nd
= &nd_table
[i
];
248 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
249 pc_init_ne2k_isa(isa_bus
, nd
);
251 pci_nic_init_nofail(nd
, "rtl8139", NULL
);
254 ide_drive_get(hd
, MAX_IDE_BUS
);
258 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
260 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
262 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
263 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
265 /* FIXME there's some major spaghetti here. Somehow we create the
266 * devices on the PIIX before we actually create it. We create the
267 * PIIX3 deep in the recess of the i440fx creation too and then lose
270 * For now, let's "fix" this by making judicious use of paths. This
271 * is not generally the right way to do this.
273 object_property_add_child(object_resolve_path("/i440fx/piix3", NULL
),
274 "rtc", (Object
*)rtc_state
, NULL
);
276 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
278 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
280 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
281 idebus
[i
] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
285 audio_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
287 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
288 floppy
, idebus
[0], idebus
[1], rtc_state
);
290 if (pci_enabled
&& usb_enabled
) {
291 usb_uhci_piix3_init(pci_bus
, piix3_devfn
+ 2);
294 if (pci_enabled
&& acpi_enabled
) {
297 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
, first_cpu
, 1);
298 /* TODO: Populate SPD eeprom data. */
299 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
302 smbus_eeprom_init(smbus
, 8, NULL
, 0);
306 pc_pci_device_init(pci_bus
);
310 static void pc_init_pci(ram_addr_t ram_size
,
311 const char *boot_device
,
312 const char *kernel_filename
,
313 const char *kernel_cmdline
,
314 const char *initrd_filename
,
315 const char *cpu_model
)
317 pc_init1(get_system_memory(),
319 ram_size
, boot_device
,
320 kernel_filename
, kernel_cmdline
,
321 initrd_filename
, cpu_model
, 1, 1);
324 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size
,
325 const char *boot_device
,
326 const char *kernel_filename
,
327 const char *kernel_cmdline
,
328 const char *initrd_filename
,
329 const char *cpu_model
)
331 pc_init1(get_system_memory(),
333 ram_size
, boot_device
,
334 kernel_filename
, kernel_cmdline
,
335 initrd_filename
, cpu_model
, 1, 0);
338 static void pc_init_isa(ram_addr_t ram_size
,
339 const char *boot_device
,
340 const char *kernel_filename
,
341 const char *kernel_cmdline
,
342 const char *initrd_filename
,
343 const char *cpu_model
)
345 if (cpu_model
== NULL
)
347 pc_init1(get_system_memory(),
349 ram_size
, boot_device
,
350 kernel_filename
, kernel_cmdline
,
351 initrd_filename
, cpu_model
, 0, 1);
355 static void pc_xen_hvm_init(ram_addr_t ram_size
,
356 const char *boot_device
,
357 const char *kernel_filename
,
358 const char *kernel_cmdline
,
359 const char *initrd_filename
,
360 const char *cpu_model
)
362 if (xen_hvm_init() != 0) {
363 hw_error("xen hardware virtual machine initialisation failed");
365 pc_init_pci_no_kvmclock(ram_size
, boot_device
,
366 kernel_filename
, kernel_cmdline
,
367 initrd_filename
, cpu_model
);
372 static QEMUMachine pc_machine_v1_1
= {
375 .desc
= "Standard PC",
379 .default_machine_opts
= "accel=kvm,kernel_irqchip=on",
382 static QEMUMachine pc_machine_v1_0
= {
384 .desc
= "Standard PC",
387 .compat_props
= (GlobalProperty
[]) {
389 .driver
= "pc-sysfw",
390 .property
= "rom_only",
391 .value
= stringify(1),
394 .property
= "check_media_rate",
397 { /* end of list */ }
401 static QEMUMachine pc_machine_v0_15
= {
403 .desc
= "Standard PC",
406 .compat_props
= (GlobalProperty
[]) {
408 .driver
= "pc-sysfw",
409 .property
= "rom_only",
410 .value
= stringify(1),
413 .property
= "check_media_rate",
416 { /* end of list */ }
420 static QEMUMachine pc_machine_v0_14
= {
422 .desc
= "Standard PC",
425 .default_machine_opts
= "accel=kvm,kernel_irqchip=on",
426 .compat_props
= (GlobalProperty
[]) {
429 .property
= "revision",
430 .value
= stringify(2),
433 .property
= "revision",
434 .value
= stringify(2),
436 .driver
= "virtio-blk-pci",
437 .property
= "event_idx",
440 .driver
= "virtio-serial-pci",
441 .property
= "event_idx",
444 .driver
= "virtio-net-pci",
445 .property
= "event_idx",
448 .driver
= "virtio-balloon-pci",
449 .property
= "event_idx",
453 .property
= "check_media_rate",
457 .driver
= "pc-sysfw",
458 .property
= "rom_only",
459 .value
= stringify(1),
461 { /* end of list */ }
465 static QEMUMachine pc_machine_v0_13
= {
467 .desc
= "Standard PC",
468 .init
= pc_init_pci_no_kvmclock
,
470 .default_machine_opts
= "accel=kvm,kernel_irqchip=on",
471 .compat_props
= (GlobalProperty
[]) {
473 .driver
= "virtio-9p-pci",
474 .property
= "vectors",
475 .value
= stringify(0),
478 .property
= "rombar",
479 .value
= stringify(0),
481 .driver
= "vmware-svga",
482 .property
= "rombar",
483 .value
= stringify(0),
486 .property
= "command_serr_enable",
489 .driver
= "virtio-blk-pci",
490 .property
= "event_idx",
493 .driver
= "virtio-serial-pci",
494 .property
= "event_idx",
497 .driver
= "virtio-net-pci",
498 .property
= "event_idx",
501 .driver
= "virtio-balloon-pci",
502 .property
= "event_idx",
506 .property
= "use_broken_id",
507 .value
= stringify(1),
510 .property
= "check_media_rate",
514 .driver
= "pc-sysfw",
515 .property
= "rom_only",
516 .value
= stringify(1),
518 { /* end of list */ }
522 static QEMUMachine pc_machine_v0_12
= {
524 .desc
= "Standard PC",
525 .init
= pc_init_pci_no_kvmclock
,
527 .default_machine_opts
= "accel=kvm,kernel_irqchip=on",
528 .compat_props
= (GlobalProperty
[]) {
530 .driver
= "virtio-serial-pci",
531 .property
= "max_ports",
532 .value
= stringify(1),
534 .driver
= "virtio-serial-pci",
535 .property
= "vectors",
536 .value
= stringify(0),
539 .property
= "rombar",
540 .value
= stringify(0),
542 .driver
= "vmware-svga",
543 .property
= "rombar",
544 .value
= stringify(0),
547 .property
= "command_serr_enable",
550 .driver
= "virtio-blk-pci",
551 .property
= "event_idx",
554 .driver
= "virtio-serial-pci",
555 .property
= "event_idx",
558 .driver
= "virtio-net-pci",
559 .property
= "event_idx",
562 .driver
= "virtio-balloon-pci",
563 .property
= "event_idx",
567 .property
= "use_broken_id",
568 .value
= stringify(1),
571 .property
= "check_media_rate",
575 .driver
= "pc-sysfw",
576 .property
= "rom_only",
577 .value
= stringify(1),
579 { /* end of list */ }
583 static QEMUMachine pc_machine_v0_11
= {
585 .desc
= "Standard PC, qemu 0.11",
586 .init
= pc_init_pci_no_kvmclock
,
588 .default_machine_opts
= "accel=kvm,kernel_irqchip=on",
589 .compat_props
= (GlobalProperty
[]) {
591 .driver
= "virtio-blk-pci",
592 .property
= "vectors",
593 .value
= stringify(0),
595 .driver
= "virtio-serial-pci",
596 .property
= "max_ports",
597 .value
= stringify(1),
599 .driver
= "virtio-serial-pci",
600 .property
= "vectors",
601 .value
= stringify(0),
603 .driver
= "ide-drive",
607 .driver
= "scsi-disk",
612 .property
= "rombar",
613 .value
= stringify(0),
616 .property
= "command_serr_enable",
619 .driver
= "virtio-blk-pci",
620 .property
= "event_idx",
623 .driver
= "virtio-serial-pci",
624 .property
= "event_idx",
627 .driver
= "virtio-net-pci",
628 .property
= "event_idx",
631 .driver
= "virtio-balloon-pci",
632 .property
= "event_idx",
636 .property
= "use_broken_id",
637 .value
= stringify(1),
640 .property
= "check_media_rate",
644 .driver
= "pc-sysfw",
645 .property
= "rom_only",
646 .value
= stringify(1),
648 { /* end of list */ }
652 static QEMUMachine pc_machine_v0_10
= {
654 .desc
= "Standard PC, qemu 0.10",
655 .init
= pc_init_pci_no_kvmclock
,
657 .default_machine_opts
= "accel=kvm,kernel_irqchip=on",
658 .compat_props
= (GlobalProperty
[]) {
660 .driver
= "virtio-blk-pci",
662 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
664 .driver
= "virtio-serial-pci",
666 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
668 .driver
= "virtio-serial-pci",
669 .property
= "max_ports",
670 .value
= stringify(1),
672 .driver
= "virtio-serial-pci",
673 .property
= "vectors",
674 .value
= stringify(0),
676 .driver
= "virtio-net-pci",
677 .property
= "vectors",
678 .value
= stringify(0),
680 .driver
= "virtio-blk-pci",
681 .property
= "vectors",
682 .value
= stringify(0),
684 .driver
= "ide-drive",
688 .driver
= "scsi-disk",
693 .property
= "rombar",
694 .value
= stringify(0),
697 .property
= "command_serr_enable",
700 .driver
= "virtio-blk-pci",
701 .property
= "event_idx",
704 .driver
= "virtio-serial-pci",
705 .property
= "event_idx",
708 .driver
= "virtio-net-pci",
709 .property
= "event_idx",
712 .driver
= "virtio-balloon-pci",
713 .property
= "event_idx",
717 .property
= "use_broken_id",
718 .value
= stringify(1),
721 .property
= "check_media_rate",
725 .driver
= "pc-sysfw",
726 .property
= "rom_only",
727 .value
= stringify(1),
729 { /* end of list */ }
733 static QEMUMachine isapc_machine
= {
735 .desc
= "ISA-only PC",
738 .default_machine_opts
= "accel=kvm,kernel_irqchip=on",
739 .compat_props
= (GlobalProperty
[]) {
741 .driver
= "pc-sysfw",
742 .property
= "rom_only",
743 .value
= stringify(1),
745 { /* end of list */ }
750 static QEMUMachine xenfv_machine
= {
752 .desc
= "Xen Fully-virtualized PC",
753 .init
= pc_xen_hvm_init
,
754 .max_cpus
= HVM_MAX_VCPUS
,
755 .default_machine_opts
= "accel=xen",
759 static void pc_machine_init(void)
761 qemu_register_machine(&pc_machine_v1_1
);
762 qemu_register_machine(&pc_machine_v1_0
);
763 qemu_register_machine(&pc_machine_v0_15
);
764 qemu_register_machine(&pc_machine_v0_14
);
765 qemu_register_machine(&pc_machine_v0_13
);
766 qemu_register_machine(&pc_machine_v0_12
);
767 qemu_register_machine(&pc_machine_v0_11
);
768 qemu_register_machine(&pc_machine_v0_10
);
769 qemu_register_machine(&isapc_machine
);
771 qemu_register_machine(&xenfv_machine
);
775 machine_init(pc_machine_init
);