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_irq_route(s
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
71 for (i
= 8; i
< 16; ++i
) {
72 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
75 for (i
= 0; i
< 24; ++i
) {
77 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, 2);
79 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, i
);
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 /* FIXME: this should be under the piix3. */
111 object_property_add_child(object_resolve_path("i440fx", NULL
),
112 "ioapic", OBJECT(dev
), NULL
);
113 qdev_init_nofail(dev
);
114 d
= sysbus_from_qdev(dev
);
115 sysbus_mmio_map(d
, 0, 0xfec00000);
117 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
118 gsi_state
->ioapic_irq
[i
] = qdev_get_gpio_in(dev
, i
);
122 /* PC hardware initialisation */
123 static void pc_init1(MemoryRegion
*system_memory
,
124 MemoryRegion
*system_io
,
126 const char *boot_device
,
127 const char *kernel_filename
,
128 const char *kernel_cmdline
,
129 const char *initrd_filename
,
130 const char *cpu_model
,
132 int kvmclock_enabled
)
135 ram_addr_t below_4g_mem_size
, above_4g_mem_size
;
138 PCII440FXState
*i440fx_state
;
139 int piix3_devfn
= -1;
145 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
146 BusState
*idebus
[MAX_IDE_BUS
];
147 ISADevice
*rtc_state
;
149 MemoryRegion
*ram_memory
;
150 MemoryRegion
*pci_memory
;
151 MemoryRegion
*rom_memory
;
154 global_cpu_model
= cpu_model
;
156 pc_cpus_init(cpu_model
);
158 if (kvmclock_enabled
) {
162 if (ram_size
>= 0xe0000000 ) {
163 above_4g_mem_size
= ram_size
- 0xe0000000;
164 below_4g_mem_size
= 0xe0000000;
166 above_4g_mem_size
= 0;
167 below_4g_mem_size
= ram_size
;
171 pci_memory
= g_new(MemoryRegion
, 1);
172 memory_region_init(pci_memory
, "pci", INT64_MAX
);
173 rom_memory
= pci_memory
;
176 rom_memory
= system_memory
;
179 /* allocate ram and load rom/bios */
180 if (!xen_enabled()) {
181 fw_cfg
= pc_memory_init(system_memory
,
182 kernel_filename
, kernel_cmdline
, initrd_filename
,
183 below_4g_mem_size
, above_4g_mem_size
,
184 pci_enabled
? rom_memory
: system_memory
, &ram_memory
);
187 gsi_state
= g_malloc0(sizeof(*gsi_state
));
188 if (kvm_irqchip_in_kernel()) {
189 kvm_piix3_setup_irq_routing(pci_enabled
);
190 gsi
= qemu_allocate_irqs(kvm_piix3_gsi_handler
, gsi_state
,
193 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
197 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
198 system_memory
, system_io
, ram_size
,
200 0x100000000ULL
- below_4g_mem_size
,
201 0x100000000ULL
+ above_4g_mem_size
,
202 (sizeof(target_phys_addr_t
) == 4
204 : ((uint64_t)1 << 62)),
205 pci_memory
, ram_memory
);
209 isa_bus
= isa_bus_new(NULL
, system_io
);
212 isa_bus_irqs(isa_bus
, gsi
);
214 if (kvm_irqchip_in_kernel()) {
215 i8259
= kvm_i8259_init(isa_bus
);
216 } else if (xen_enabled()) {
217 i8259
= xen_interrupt_controller_init();
219 cpu_irq
= pc_allocate_cpu_irq();
220 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
223 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
224 gsi_state
->i8259_irq
[i
] = i8259
[i
];
227 ioapic_init(gsi_state
);
230 pc_register_ferr_irq(gsi
[13]);
232 pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
234 pci_create_simple(pci_bus
, -1, "xen-platform");
237 /* init basic PC hardware */
238 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
240 for(i
= 0; i
< nb_nics
; i
++) {
241 NICInfo
*nd
= &nd_table
[i
];
243 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
244 pc_init_ne2k_isa(isa_bus
, nd
);
246 pci_nic_init_nofail(nd
, "rtl8139", NULL
);
249 ide_drive_get(hd
, MAX_IDE_BUS
);
253 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
255 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
257 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
258 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
260 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
262 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
264 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
265 idebus
[i
] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
269 audio_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
271 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
272 floppy
, idebus
[0], idebus
[1], rtc_state
);
274 if (pci_enabled
&& usb_enabled
) {
275 pci_create_simple(pci_bus
, piix3_devfn
+ 2, "piix3-usb-uhci");
278 if (pci_enabled
&& acpi_enabled
) {
281 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
, first_cpu
, 1);
282 /* TODO: Populate SPD eeprom data. */
283 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
285 kvm_enabled(), fw_cfg
);
286 smbus_eeprom_init(smbus
, 8, NULL
, 0);
290 pc_pci_device_init(pci_bus
);
294 static void pc_init_pci(ram_addr_t ram_size
,
295 const char *boot_device
,
296 const char *kernel_filename
,
297 const char *kernel_cmdline
,
298 const char *initrd_filename
,
299 const char *cpu_model
)
301 pc_init1(get_system_memory(),
303 ram_size
, boot_device
,
304 kernel_filename
, kernel_cmdline
,
305 initrd_filename
, cpu_model
, 1, 1);
308 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size
,
309 const char *boot_device
,
310 const char *kernel_filename
,
311 const char *kernel_cmdline
,
312 const char *initrd_filename
,
313 const char *cpu_model
)
315 pc_init1(get_system_memory(),
317 ram_size
, boot_device
,
318 kernel_filename
, kernel_cmdline
,
319 initrd_filename
, cpu_model
, 1, 0);
322 static void pc_init_isa(ram_addr_t ram_size
,
323 const char *boot_device
,
324 const char *kernel_filename
,
325 const char *kernel_cmdline
,
326 const char *initrd_filename
,
327 const char *cpu_model
)
329 if (cpu_model
== NULL
)
331 pc_init1(get_system_memory(),
333 ram_size
, boot_device
,
334 kernel_filename
, kernel_cmdline
,
335 initrd_filename
, cpu_model
, 0, 1);
339 static void pc_xen_hvm_init(ram_addr_t ram_size
,
340 const char *boot_device
,
341 const char *kernel_filename
,
342 const char *kernel_cmdline
,
343 const char *initrd_filename
,
344 const char *cpu_model
)
346 if (xen_hvm_init() != 0) {
347 hw_error("xen hardware virtual machine initialisation failed");
349 pc_init_pci_no_kvmclock(ram_size
, boot_device
,
350 kernel_filename
, kernel_cmdline
,
351 initrd_filename
, cpu_model
);
356 #ifdef CONFIG_KVM_OPTIONS
357 #define KVM_MACHINE_OPTIONS "accel=kvm"
359 #define KVM_MACHINE_OPTIONS ""
362 static QEMUMachine pc_machine_v1_2
= {
365 .desc
= "Standard PC",
369 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
372 #define PC_COMPAT_1_1 \
375 .property = "vgamem_mb",\
376 .value = stringify(16),\
378 .driver = "vmware-svga",\
379 .property = "vgamem_mb",\
380 .value = stringify(16),\
382 .driver = "qxl-vga",\
383 .property = "vgamem_mb",\
384 .value = stringify(16),\
387 .property = "vgamem_mb",\
388 .value = stringify(16),\
390 .driver = "virtio-blk-pci",\
391 .property = "config-wce",\
395 static QEMUMachine pc_machine_v1_1
= {
397 .desc
= "Standard PC",
400 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
401 .compat_props
= (GlobalProperty
[]) {
403 { /* end of list */ }
407 #define PC_COMPAT_1_0 \
410 .driver = "pc-sysfw",\
411 .property = "rom_only",\
412 .value = stringify(1),\
414 .driver = "isa-fdc",\
415 .property = "check_media_rate",\
418 .driver = "virtio-balloon-pci",\
419 .property = "class",\
420 .value = stringify(PCI_CLASS_MEMORY_RAM),\
423 .property = "vapic",\
426 .driver = TYPE_USB_DEVICE,\
427 .property = "full-path",\
431 static QEMUMachine pc_machine_v1_0
= {
433 .desc
= "Standard PC",
436 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
437 .compat_props
= (GlobalProperty
[]) {
439 { /* end of list */ }
444 #define PC_COMPAT_0_15 \
447 static QEMUMachine pc_machine_v0_15
= {
449 .desc
= "Standard PC",
452 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
453 .compat_props
= (GlobalProperty
[]) {
455 { /* end of list */ }
457 .hw_version
= "0.15",
460 #define PC_COMPAT_0_14 \
463 .driver = "virtio-blk-pci",\
464 .property = "event_idx",\
467 .driver = "virtio-serial-pci",\
468 .property = "event_idx",\
471 .driver = "virtio-net-pci",\
472 .property = "event_idx",\
475 .driver = "virtio-balloon-pci",\
476 .property = "event_idx",\
480 static QEMUMachine pc_machine_v0_14
= {
482 .desc
= "Standard PC",
485 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
486 .compat_props
= (GlobalProperty
[]) {
490 .property
= "revision",
491 .value
= stringify(2),
494 .property
= "revision",
495 .value
= stringify(2),
497 { /* end of list */ }
499 .hw_version
= "0.14",
502 #define PC_COMPAT_0_13 \
505 .driver = TYPE_PCI_DEVICE,\
506 .property = "command_serr_enable",\
510 .property = "use_broken_id",\
511 .value = stringify(1),\
514 static QEMUMachine pc_machine_v0_13
= {
516 .desc
= "Standard PC",
517 .init
= pc_init_pci_no_kvmclock
,
519 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
520 .compat_props
= (GlobalProperty
[]) {
523 .driver
= "virtio-9p-pci",
524 .property
= "vectors",
525 .value
= stringify(0),
528 .property
= "rombar",
529 .value
= stringify(0),
531 .driver
= "vmware-svga",
532 .property
= "rombar",
533 .value
= stringify(0),
535 { /* end of list */ }
537 .hw_version
= "0.13",
540 #define PC_COMPAT_0_12 \
543 .driver = "virtio-serial-pci",\
544 .property = "max_ports",\
545 .value = stringify(1),\
547 .driver = "virtio-serial-pci",\
548 .property = "vectors",\
549 .value = stringify(0),\
552 static QEMUMachine pc_machine_v0_12
= {
554 .desc
= "Standard PC",
555 .init
= pc_init_pci_no_kvmclock
,
557 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
558 .compat_props
= (GlobalProperty
[]) {
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.12",
574 #define PC_COMPAT_0_11 \
577 .driver = "virtio-blk-pci",\
578 .property = "vectors",\
579 .value = stringify(0),\
581 .driver = TYPE_PCI_DEVICE,\
582 .property = "rombar",\
583 .value = stringify(0),\
586 static QEMUMachine pc_machine_v0_11
= {
588 .desc
= "Standard PC, qemu 0.11",
589 .init
= pc_init_pci_no_kvmclock
,
591 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
592 .compat_props
= (GlobalProperty
[]) {
595 .driver
= "ide-drive",
599 .driver
= "scsi-disk",
603 { /* end of list */ }
605 .hw_version
= "0.11",
608 static QEMUMachine pc_machine_v0_10
= {
610 .desc
= "Standard PC, qemu 0.10",
611 .init
= pc_init_pci_no_kvmclock
,
613 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
614 .compat_props
= (GlobalProperty
[]) {
617 .driver
= "virtio-blk-pci",
619 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
621 .driver
= "virtio-serial-pci",
623 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
625 .driver
= "virtio-net-pci",
626 .property
= "vectors",
627 .value
= stringify(0),
629 .driver
= "ide-drive",
633 .driver
= "scsi-disk",
637 { /* end of list */ }
639 .hw_version
= "0.10",
642 static QEMUMachine isapc_machine
= {
644 .desc
= "ISA-only PC",
647 .default_machine_opts
= KVM_MACHINE_OPTIONS
,
648 .compat_props
= (GlobalProperty
[]) {
650 .driver
= "pc-sysfw",
651 .property
= "rom_only",
652 .value
= stringify(1),
654 { /* end of list */ }
659 static QEMUMachine xenfv_machine
= {
661 .desc
= "Xen Fully-virtualized PC",
662 .init
= pc_xen_hvm_init
,
663 .max_cpus
= HVM_MAX_VCPUS
,
664 .default_machine_opts
= "accel=xen",
668 static void pc_machine_init(void)
670 qemu_register_machine(&pc_machine_v1_2
);
671 qemu_register_machine(&pc_machine_v1_1
);
672 qemu_register_machine(&pc_machine_v1_0
);
673 qemu_register_machine(&pc_machine_v0_15
);
674 qemu_register_machine(&pc_machine_v0_14
);
675 qemu_register_machine(&pc_machine_v0_13
);
676 qemu_register_machine(&pc_machine_v0_12
);
677 qemu_register_machine(&pc_machine_v0_11
);
678 qemu_register_machine(&pc_machine_v0_10
);
679 qemu_register_machine(&isapc_machine
);
681 qemu_register_machine(&xenfv_machine
);
685 machine_init(pc_machine_init
);