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 static void kvm_piix3_setup_irq_routing(bool pci_enabled)
59 KVMState *s = kvm_state;
62 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
63 for (i = 0; i < 8; ++i) {
67 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
69 for (i = 8; i < 16; ++i) {
70 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
73 for (i = 0; i < 24; ++i) {
75 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
77 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, i);
81 ret = kvm_irqchip_commit_routes(s);
83 hw_error("KVM IRQ routing setup failed");
86 #endif /* CONFIG_KVM */
89 static void kvm_piix3_gsi_handler(void *opaque, int n, int level)
93 if (n < ISA_NUM_IRQS) {
94 /* Kernel will forward to both PIC and IOAPIC */
95 qemu_set_irq(s->i8259_irq[n], level);
97 qemu_set_irq(s->ioapic_irq[n], level);
101 static void ioapic_init(GSIState *gsi_state)
107 if (kvm_irqchip_in_kernel()) {
108 dev = qdev_create(NULL, "kvm-ioapic");
110 dev = qdev_create(NULL, "ioapic");
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 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,
199 (sizeof(target_phys_addr_t) == 4
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 dev = pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
231 object_property_add_child(object_get_root(), "vga", OBJECT(dev), NULL);
235 pci_create_simple(pci_bus, -1, "xen-platform");
238 /* init basic PC hardware */
239 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
241 for(i = 0; i < nb_nics; i++) {
242 NICInfo *nd = &nd_table[i];
244 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
245 pc_init_ne2k_isa(isa_bus, nd);
247 pci_nic_init_nofail(nd, "e1000", NULL);
250 ide_drive_get(hd, MAX_IDE_BUS);
254 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
256 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
258 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
259 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
261 /* FIXME there's some major spaghetti here. Somehow we create the
262 * devices on the PIIX before we actually create it. We create the
263 * PIIX3 deep in the recess of the i440fx creation too and then lose
266 * For now, let's "fix" this by making judicious use of paths. This
267 * is not generally the right way to do this.
269 object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
270 "rtc", (Object *)rtc_state, NULL);
272 for(i = 0; i < MAX_IDE_BUS; i++) {
274 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
276 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
277 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
281 audio_init(isa_bus, pci_enabled ? pci_bus : NULL);
283 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
284 floppy, idebus[0], idebus[1], rtc_state);
286 if (pci_enabled && usb_enabled) {
287 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
290 if (pci_enabled && acpi_enabled) {
293 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
294 /* TODO: Populate SPD eeprom data. */
295 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
298 smbus_eeprom_init(smbus, 8, NULL, 0);
302 pc_pci_device_init(pci_bus);
306 static void pc_init_pci(ram_addr_t ram_size,
307 const char *boot_device,
308 const char *kernel_filename,
309 const char *kernel_cmdline,
310 const char *initrd_filename,
311 const char *cpu_model)
313 pc_init1(get_system_memory(),
315 ram_size, boot_device,
316 kernel_filename, kernel_cmdline,
317 initrd_filename, cpu_model, 1, 1);
320 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size,
321 const char *boot_device,
322 const char *kernel_filename,
323 const char *kernel_cmdline,
324 const char *initrd_filename,
325 const char *cpu_model)
327 pc_init1(get_system_memory(),
329 ram_size, boot_device,
330 kernel_filename, kernel_cmdline,
331 initrd_filename, cpu_model, 1, 0);
334 static void pc_init_isa(ram_addr_t ram_size,
335 const char *boot_device,
336 const char *kernel_filename,
337 const char *kernel_cmdline,
338 const char *initrd_filename,
339 const char *cpu_model)
341 if (cpu_model == NULL)
343 pc_init1(get_system_memory(),
345 ram_size, boot_device,
346 kernel_filename, kernel_cmdline,
347 initrd_filename, cpu_model, 0, 1);
351 static void pc_xen_hvm_init(ram_addr_t ram_size,
352 const char *boot_device,
353 const char *kernel_filename,
354 const char *kernel_cmdline,
355 const char *initrd_filename,
356 const char *cpu_model)
358 if (xen_hvm_init() != 0) {
359 hw_error("xen hardware virtual machine initialisation failed");
361 pc_init_pci_no_kvmclock(ram_size, boot_device,
362 kernel_filename, kernel_cmdline,
363 initrd_filename, cpu_model);
368 static QEMUMachine pc_machine_v1_1 = {
371 .desc = "Standard PC",
377 static QEMUMachine pc_machine_v1_0 = {
379 .desc = "Standard PC",
382 .compat_props = (GlobalProperty[]) {
384 .driver = "pc-sysfw",
385 .property = "rom_only",
386 .value = stringify(1),
389 .property = "check_media_rate",
392 { /* end of list */ }
396 static QEMUMachine pc_machine_v0_15 = {
398 .desc = "Standard PC",
401 .compat_props = (GlobalProperty[]) {
403 .driver = "pc-sysfw",
404 .property = "rom_only",
405 .value = stringify(1),
408 .property = "check_media_rate",
411 { /* end of list */ }
415 static QEMUMachine pc_machine_v0_14 = {
417 .desc = "Standard PC",
420 .compat_props = (GlobalProperty[]) {
423 .property = "revision",
424 .value = stringify(2),
427 .property = "revision",
428 .value = stringify(2),
430 .driver = "virtio-blk-pci",
431 .property = "event_idx",
434 .driver = "virtio-serial-pci",
435 .property = "event_idx",
438 .driver = "virtio-net-pci",
439 .property = "event_idx",
442 .driver = "virtio-balloon-pci",
443 .property = "event_idx",
447 .property = "check_media_rate",
451 .driver = "pc-sysfw",
452 .property = "rom_only",
453 .value = stringify(1),
455 { /* end of list */ }
459 static QEMUMachine pc_machine_v0_13 = {
461 .desc = "Standard PC",
462 .init = pc_init_pci_no_kvmclock,
464 .compat_props = (GlobalProperty[]) {
466 .driver = "virtio-9p-pci",
467 .property = "vectors",
468 .value = stringify(0),
471 .property = "rombar",
472 .value = stringify(0),
474 .driver = "vmware-svga",
475 .property = "rombar",
476 .value = stringify(0),
479 .property = "command_serr_enable",
482 .driver = "virtio-blk-pci",
483 .property = "event_idx",
486 .driver = "virtio-serial-pci",
487 .property = "event_idx",
490 .driver = "virtio-net-pci",
491 .property = "event_idx",
494 .driver = "virtio-balloon-pci",
495 .property = "event_idx",
499 .property = "use_broken_id",
500 .value = stringify(1),
503 .property = "check_media_rate",
507 .driver = "pc-sysfw",
508 .property = "rom_only",
509 .value = stringify(1),
511 { /* end of list */ }
515 static QEMUMachine pc_machine_v0_12 = {
517 .desc = "Standard PC",
518 .init = pc_init_pci_no_kvmclock,
520 .compat_props = (GlobalProperty[]) {
522 .driver = "virtio-serial-pci",
523 .property = "max_ports",
524 .value = stringify(1),
526 .driver = "virtio-serial-pci",
527 .property = "vectors",
528 .value = stringify(0),
531 .property = "rombar",
532 .value = stringify(0),
534 .driver = "vmware-svga",
535 .property = "rombar",
536 .value = stringify(0),
539 .property = "command_serr_enable",
542 .driver = "virtio-blk-pci",
543 .property = "event_idx",
546 .driver = "virtio-serial-pci",
547 .property = "event_idx",
550 .driver = "virtio-net-pci",
551 .property = "event_idx",
554 .driver = "virtio-balloon-pci",
555 .property = "event_idx",
559 .property = "use_broken_id",
560 .value = stringify(1),
563 .property = "check_media_rate",
567 .driver = "pc-sysfw",
568 .property = "rom_only",
569 .value = stringify(1),
571 { /* end of list */ }
575 static QEMUMachine pc_machine_v0_11 = {
577 .desc = "Standard PC, qemu 0.11",
578 .init = pc_init_pci_no_kvmclock,
580 .compat_props = (GlobalProperty[]) {
582 .driver = "virtio-blk-pci",
583 .property = "vectors",
584 .value = stringify(0),
586 .driver = "virtio-serial-pci",
587 .property = "max_ports",
588 .value = stringify(1),
590 .driver = "virtio-serial-pci",
591 .property = "vectors",
592 .value = stringify(0),
594 .driver = "ide-drive",
598 .driver = "scsi-disk",
603 .property = "rombar",
604 .value = stringify(0),
607 .property = "command_serr_enable",
610 .driver = "virtio-blk-pci",
611 .property = "event_idx",
614 .driver = "virtio-serial-pci",
615 .property = "event_idx",
618 .driver = "virtio-net-pci",
619 .property = "event_idx",
622 .driver = "virtio-balloon-pci",
623 .property = "event_idx",
627 .property = "use_broken_id",
628 .value = stringify(1),
631 .property = "check_media_rate",
635 .driver = "pc-sysfw",
636 .property = "rom_only",
637 .value = stringify(1),
639 { /* end of list */ }
643 static QEMUMachine pc_machine_v0_10 = {
645 .desc = "Standard PC, qemu 0.10",
646 .init = pc_init_pci_no_kvmclock,
648 .compat_props = (GlobalProperty[]) {
650 .driver = "virtio-blk-pci",
652 .value = stringify(PCI_CLASS_STORAGE_OTHER),
654 .driver = "virtio-serial-pci",
656 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
658 .driver = "virtio-serial-pci",
659 .property = "max_ports",
660 .value = stringify(1),
662 .driver = "virtio-serial-pci",
663 .property = "vectors",
664 .value = stringify(0),
666 .driver = "virtio-net-pci",
667 .property = "vectors",
668 .value = stringify(0),
670 .driver = "virtio-blk-pci",
671 .property = "vectors",
672 .value = stringify(0),
674 .driver = "ide-drive",
678 .driver = "scsi-disk",
683 .property = "rombar",
684 .value = stringify(0),
687 .property = "command_serr_enable",
690 .driver = "virtio-blk-pci",
691 .property = "event_idx",
694 .driver = "virtio-serial-pci",
695 .property = "event_idx",
698 .driver = "virtio-net-pci",
699 .property = "event_idx",
702 .driver = "virtio-balloon-pci",
703 .property = "event_idx",
707 .property = "use_broken_id",
708 .value = stringify(1),
711 .property = "check_media_rate",
715 .driver = "pc-sysfw",
716 .property = "rom_only",
717 .value = stringify(1),
719 { /* end of list */ }
723 static QEMUMachine isapc_machine = {
725 .desc = "ISA-only PC",
728 .compat_props = (GlobalProperty[]) {
730 .driver = "pc-sysfw",
731 .property = "rom_only",
732 .value = stringify(1),
734 { /* end of list */ }
739 static QEMUMachine xenfv_machine = {
741 .desc = "Xen Fully-virtualized PC",
742 .init = pc_xen_hvm_init,
743 .max_cpus = HVM_MAX_VCPUS,
744 .default_machine_opts = "accel=xen",
748 static void pc_machine_init(void)
750 qemu_register_machine(&pc_machine_v1_1);
751 qemu_register_machine(&pc_machine_v1_0);
752 qemu_register_machine(&pc_machine_v0_15);
753 qemu_register_machine(&pc_machine_v0_14);
754 qemu_register_machine(&pc_machine_v0_13);
755 qemu_register_machine(&pc_machine_v0_12);
756 qemu_register_machine(&pc_machine_v0_11);
757 qemu_register_machine(&pc_machine_v0_10);
758 qemu_register_machine(&isapc_machine);
760 qemu_register_machine(&xenfv_machine);
764 machine_init(pc_machine_init);