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
35 #include "kvm/clock.h"
38 #include "arch_init.h"
43 #include "exec-memory.h"
45 # include <xen/hvm/hvm_info_table.h>
50 static const int ide_iobase
[MAX_IDE_BUS
] = { 0x1f0, 0x170 };
51 static const int ide_iobase2
[MAX_IDE_BUS
] = { 0x3f6, 0x376 };
52 static const int ide_irq
[MAX_IDE_BUS
] = { 14, 15 };
54 static void kvm_piix3_setup_irq_routing(bool pci_enabled
)
57 KVMState
*s
= kvm_state
;
60 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
61 for (i
= 0; i
< 8; ++i
) {
65 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
67 for (i
= 8; i
< 16; ++i
) {
68 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
71 for (i
= 0; i
< 24; ++i
) {
73 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_IOAPIC
, 2);
75 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_IOAPIC
, i
);
79 ret
= kvm_irqchip_commit_routes(s
);
81 hw_error("KVM IRQ routing setup failed");
84 #endif /* CONFIG_KVM */
87 static void kvm_piix3_gsi_handler(void *opaque
, int n
, int level
)
91 if (n
< ISA_NUM_IRQS
) {
92 /* Kernel will forward to both PIC and IOAPIC */
93 qemu_set_irq(s
->i8259_irq
[n
], level
);
95 qemu_set_irq(s
->ioapic_irq
[n
], level
);
99 static void ioapic_init(GSIState
*gsi_state
)
105 if (kvm_irqchip_in_kernel()) {
106 dev
= qdev_create(NULL
, "kvm-ioapic");
108 dev
= qdev_create(NULL
, "ioapic");
110 qdev_init_nofail(dev
);
111 d
= sysbus_from_qdev(dev
);
112 sysbus_mmio_map(d
, 0, 0xfec00000);
114 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
115 gsi_state
->ioapic_irq
[i
] = qdev_get_gpio_in(dev
, i
);
119 /* PC hardware initialisation */
120 static void pc_init1(MemoryRegion
*system_memory
,
121 MemoryRegion
*system_io
,
123 const char *boot_device
,
124 const char *kernel_filename
,
125 const char *kernel_cmdline
,
126 const char *initrd_filename
,
127 const char *cpu_model
,
129 int kvmclock_enabled
)
132 ram_addr_t below_4g_mem_size
, above_4g_mem_size
;
135 PCII440FXState
*i440fx_state
;
136 int piix3_devfn
= -1;
142 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
143 BusState
*idebus
[MAX_IDE_BUS
];
144 ISADevice
*rtc_state
;
146 MemoryRegion
*ram_memory
;
147 MemoryRegion
*pci_memory
;
148 MemoryRegion
*rom_memory
;
151 pc_cpus_init(cpu_model
);
153 if (kvmclock_enabled
) {
157 if (ram_size
>= 0xe0000000 ) {
158 above_4g_mem_size
= ram_size
- 0xe0000000;
159 below_4g_mem_size
= 0xe0000000;
161 above_4g_mem_size
= 0;
162 below_4g_mem_size
= ram_size
;
166 pci_memory
= g_new(MemoryRegion
, 1);
167 memory_region_init(pci_memory
, "pci", INT64_MAX
);
168 rom_memory
= pci_memory
;
171 rom_memory
= system_memory
;
174 /* allocate ram and load rom/bios */
175 if (!xen_enabled()) {
176 pc_memory_init(system_memory
,
177 kernel_filename
, kernel_cmdline
, initrd_filename
,
178 below_4g_mem_size
, above_4g_mem_size
,
179 pci_enabled
? rom_memory
: system_memory
, &ram_memory
);
182 gsi_state
= g_malloc0(sizeof(*gsi_state
));
183 if (kvm_irqchip_in_kernel()) {
184 kvm_piix3_setup_irq_routing(pci_enabled
);
185 gsi
= qemu_allocate_irqs(kvm_piix3_gsi_handler
, gsi_state
,
188 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
192 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
193 system_memory
, system_io
, ram_size
,
195 0x100000000ULL
- below_4g_mem_size
,
196 0x100000000ULL
+ above_4g_mem_size
,
197 (sizeof(target_phys_addr_t
) == 4
199 : ((uint64_t)1 << 62)),
200 pci_memory
, ram_memory
);
204 isa_bus
= isa_bus_new(NULL
, system_io
);
207 isa_bus_irqs(isa_bus
, gsi
);
209 if (kvm_irqchip_in_kernel()) {
210 i8259
= kvm_i8259_init(isa_bus
);
211 } else if (xen_enabled()) {
212 i8259
= xen_interrupt_controller_init();
214 cpu_irq
= pc_allocate_cpu_irq();
215 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
218 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
219 gsi_state
->i8259_irq
[i
] = i8259
[i
];
222 ioapic_init(gsi_state
);
225 pc_register_ferr_irq(gsi
[13]);
227 dev
= pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
229 object_property_add_child(object_get_root(), "vga", OBJECT(dev
), NULL
);
233 pci_create_simple(pci_bus
, -1, "xen-platform");
236 /* init basic PC hardware */
237 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
239 for(i
= 0; i
< nb_nics
; i
++) {
240 NICInfo
*nd
= &nd_table
[i
];
242 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
243 pc_init_ne2k_isa(isa_bus
, nd
);
245 pci_nic_init_nofail(nd
, "e1000", NULL
);
248 ide_drive_get(hd
, MAX_IDE_BUS
);
252 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
254 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
256 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
257 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
259 /* FIXME there's some major spaghetti here. Somehow we create the
260 * devices on the PIIX before we actually create it. We create the
261 * PIIX3 deep in the recess of the i440fx creation too and then lose
264 * For now, let's "fix" this by making judicious use of paths. This
265 * is not generally the right way to do this.
267 object_property_add_child(object_resolve_path("/i440fx/piix3", NULL
),
268 "rtc", (Object
*)rtc_state
, NULL
);
270 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
272 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
274 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
275 idebus
[i
] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
279 audio_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
281 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
282 floppy
, idebus
[0], idebus
[1], rtc_state
);
284 if (pci_enabled
&& usb_enabled
) {
285 pci_create_simple(pci_bus
, piix3_devfn
+ 2, "piix3-usb-uhci");
288 if (pci_enabled
&& acpi_enabled
) {
291 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
, first_cpu
, 1);
292 /* TODO: Populate SPD eeprom data. */
293 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
296 smbus_eeprom_init(smbus
, 8, NULL
, 0);
300 pc_pci_device_init(pci_bus
);
304 static void pc_init_pci(ram_addr_t ram_size
,
305 const char *boot_device
,
306 const char *kernel_filename
,
307 const char *kernel_cmdline
,
308 const char *initrd_filename
,
309 const char *cpu_model
)
311 pc_init1(get_system_memory(),
313 ram_size
, boot_device
,
314 kernel_filename
, kernel_cmdline
,
315 initrd_filename
, cpu_model
, 1, 1);
318 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size
,
319 const char *boot_device
,
320 const char *kernel_filename
,
321 const char *kernel_cmdline
,
322 const char *initrd_filename
,
323 const char *cpu_model
)
325 pc_init1(get_system_memory(),
327 ram_size
, boot_device
,
328 kernel_filename
, kernel_cmdline
,
329 initrd_filename
, cpu_model
, 1, 0);
332 static void pc_init_isa(ram_addr_t ram_size
,
333 const char *boot_device
,
334 const char *kernel_filename
,
335 const char *kernel_cmdline
,
336 const char *initrd_filename
,
337 const char *cpu_model
)
339 if (cpu_model
== NULL
)
341 pc_init1(get_system_memory(),
343 ram_size
, boot_device
,
344 kernel_filename
, kernel_cmdline
,
345 initrd_filename
, cpu_model
, 0, 1);
349 static void pc_xen_hvm_init(ram_addr_t ram_size
,
350 const char *boot_device
,
351 const char *kernel_filename
,
352 const char *kernel_cmdline
,
353 const char *initrd_filename
,
354 const char *cpu_model
)
356 if (xen_hvm_init() != 0) {
357 hw_error("xen hardware virtual machine initialisation failed");
359 pc_init_pci_no_kvmclock(ram_size
, boot_device
,
360 kernel_filename
, kernel_cmdline
,
361 initrd_filename
, cpu_model
);
366 static QEMUMachine pc_machine_v1_1
= {
369 .desc
= "Standard PC",
375 static QEMUMachine pc_machine_v1_0
= {
377 .desc
= "Standard PC",
380 .compat_props
= (GlobalProperty
[]) {
382 .driver
= "pc-sysfw",
383 .property
= "rom_only",
384 .value
= stringify(1),
387 .property
= "check_media_rate",
390 { /* end of list */ }
394 static QEMUMachine pc_machine_v0_15
= {
396 .desc
= "Standard PC",
399 .compat_props
= (GlobalProperty
[]) {
401 .driver
= "pc-sysfw",
402 .property
= "rom_only",
403 .value
= stringify(1),
406 .property
= "check_media_rate",
409 { /* end of list */ }
413 static QEMUMachine pc_machine_v0_14
= {
415 .desc
= "Standard PC",
418 .compat_props
= (GlobalProperty
[]) {
421 .property
= "revision",
422 .value
= stringify(2),
425 .property
= "revision",
426 .value
= stringify(2),
428 .driver
= "virtio-blk-pci",
429 .property
= "event_idx",
432 .driver
= "virtio-serial-pci",
433 .property
= "event_idx",
436 .driver
= "virtio-net-pci",
437 .property
= "event_idx",
440 .driver
= "virtio-balloon-pci",
441 .property
= "event_idx",
445 .property
= "check_media_rate",
449 .driver
= "pc-sysfw",
450 .property
= "rom_only",
451 .value
= stringify(1),
453 { /* end of list */ }
457 static QEMUMachine pc_machine_v0_13
= {
459 .desc
= "Standard PC",
460 .init
= pc_init_pci_no_kvmclock
,
462 .compat_props
= (GlobalProperty
[]) {
464 .driver
= "virtio-9p-pci",
465 .property
= "vectors",
466 .value
= stringify(0),
469 .property
= "rombar",
470 .value
= stringify(0),
472 .driver
= "vmware-svga",
473 .property
= "rombar",
474 .value
= stringify(0),
477 .property
= "command_serr_enable",
480 .driver
= "virtio-blk-pci",
481 .property
= "event_idx",
484 .driver
= "virtio-serial-pci",
485 .property
= "event_idx",
488 .driver
= "virtio-net-pci",
489 .property
= "event_idx",
492 .driver
= "virtio-balloon-pci",
493 .property
= "event_idx",
497 .property
= "use_broken_id",
498 .value
= stringify(1),
501 .property
= "check_media_rate",
505 .driver
= "pc-sysfw",
506 .property
= "rom_only",
507 .value
= stringify(1),
509 { /* end of list */ }
513 static QEMUMachine pc_machine_v0_12
= {
515 .desc
= "Standard PC",
516 .init
= pc_init_pci_no_kvmclock
,
518 .compat_props
= (GlobalProperty
[]) {
520 .driver
= "virtio-serial-pci",
521 .property
= "max_ports",
522 .value
= stringify(1),
524 .driver
= "virtio-serial-pci",
525 .property
= "vectors",
526 .value
= stringify(0),
529 .property
= "rombar",
530 .value
= stringify(0),
532 .driver
= "vmware-svga",
533 .property
= "rombar",
534 .value
= stringify(0),
537 .property
= "command_serr_enable",
540 .driver
= "virtio-blk-pci",
541 .property
= "event_idx",
544 .driver
= "virtio-serial-pci",
545 .property
= "event_idx",
548 .driver
= "virtio-net-pci",
549 .property
= "event_idx",
552 .driver
= "virtio-balloon-pci",
553 .property
= "event_idx",
557 .property
= "use_broken_id",
558 .value
= stringify(1),
561 .property
= "check_media_rate",
565 .driver
= "pc-sysfw",
566 .property
= "rom_only",
567 .value
= stringify(1),
569 { /* end of list */ }
573 static QEMUMachine pc_machine_v0_11
= {
575 .desc
= "Standard PC, qemu 0.11",
576 .init
= pc_init_pci_no_kvmclock
,
578 .compat_props
= (GlobalProperty
[]) {
580 .driver
= "virtio-blk-pci",
581 .property
= "vectors",
582 .value
= stringify(0),
584 .driver
= "virtio-serial-pci",
585 .property
= "max_ports",
586 .value
= stringify(1),
588 .driver
= "virtio-serial-pci",
589 .property
= "vectors",
590 .value
= stringify(0),
592 .driver
= "ide-drive",
596 .driver
= "scsi-disk",
601 .property
= "rombar",
602 .value
= stringify(0),
605 .property
= "command_serr_enable",
608 .driver
= "virtio-blk-pci",
609 .property
= "event_idx",
612 .driver
= "virtio-serial-pci",
613 .property
= "event_idx",
616 .driver
= "virtio-net-pci",
617 .property
= "event_idx",
620 .driver
= "virtio-balloon-pci",
621 .property
= "event_idx",
625 .property
= "use_broken_id",
626 .value
= stringify(1),
629 .property
= "check_media_rate",
633 .driver
= "pc-sysfw",
634 .property
= "rom_only",
635 .value
= stringify(1),
637 { /* end of list */ }
641 static QEMUMachine pc_machine_v0_10
= {
643 .desc
= "Standard PC, qemu 0.10",
644 .init
= pc_init_pci_no_kvmclock
,
646 .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-serial-pci",
657 .property
= "max_ports",
658 .value
= stringify(1),
660 .driver
= "virtio-serial-pci",
661 .property
= "vectors",
662 .value
= stringify(0),
664 .driver
= "virtio-net-pci",
665 .property
= "vectors",
666 .value
= stringify(0),
668 .driver
= "virtio-blk-pci",
669 .property
= "vectors",
670 .value
= stringify(0),
672 .driver
= "ide-drive",
676 .driver
= "scsi-disk",
681 .property
= "rombar",
682 .value
= stringify(0),
685 .property
= "command_serr_enable",
688 .driver
= "virtio-blk-pci",
689 .property
= "event_idx",
692 .driver
= "virtio-serial-pci",
693 .property
= "event_idx",
696 .driver
= "virtio-net-pci",
697 .property
= "event_idx",
700 .driver
= "virtio-balloon-pci",
701 .property
= "event_idx",
705 .property
= "use_broken_id",
706 .value
= stringify(1),
709 .property
= "check_media_rate",
713 .driver
= "pc-sysfw",
714 .property
= "rom_only",
715 .value
= stringify(1),
717 { /* end of list */ }
721 static QEMUMachine isapc_machine
= {
723 .desc
= "ISA-only PC",
726 .compat_props
= (GlobalProperty
[]) {
728 .driver
= "pc-sysfw",
729 .property
= "rom_only",
730 .value
= stringify(1),
732 { /* end of list */ }
737 static QEMUMachine xenfv_machine
= {
739 .desc
= "Xen Fully-virtualized PC",
740 .init
= pc_xen_hvm_init
,
741 .max_cpus
= HVM_MAX_VCPUS
,
742 .default_machine_opts
= "accel=xen",
746 static void pc_machine_init(void)
748 qemu_register_machine(&pc_machine_v1_1
);
749 qemu_register_machine(&pc_machine_v1_0
);
750 qemu_register_machine(&pc_machine_v0_15
);
751 qemu_register_machine(&pc_machine_v0_14
);
752 qemu_register_machine(&pc_machine_v0_13
);
753 qemu_register_machine(&pc_machine_v0_12
);
754 qemu_register_machine(&pc_machine_v0_11
);
755 qemu_register_machine(&pc_machine_v0_10
);
756 qemu_register_machine(&isapc_machine
);
758 qemu_register_machine(&xenfv_machine
);
762 machine_init(pc_machine_init
);