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>
50 qemu_irq
*ioapic_irq_hack
;
54 static const int ide_iobase
[MAX_IDE_BUS
] = { 0x1f0, 0x170 };
55 static const int ide_iobase2
[MAX_IDE_BUS
] = { 0x3f6, 0x376 };
56 static const int ide_irq
[MAX_IDE_BUS
] = { 14, 15 };
58 const char *global_cpu_model
; /* cpu hotadd */
60 #ifdef UNUSED_UPSTREAM_KVM
61 static void kvm_piix3_setup_irq_routing(bool pci_enabled
)
64 KVMState
*s
= kvm_state
;
67 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
68 for (i
= 0; i
< 8; ++i
) {
72 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
74 for (i
= 8; i
< 16; ++i
) {
75 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
78 for (i
= 0; i
< 24; ++i
) {
80 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_IOAPIC
, 2);
82 kvm_irqchip_add_route(s
, i
, KVM_IRQCHIP_IOAPIC
, i
);
86 ret
= kvm_irqchip_commit_routes(s
);
88 hw_error("KVM IRQ routing setup failed");
91 #endif /* CONFIG_KVM */
95 static void kvm_piix3_gsi_handler(void *opaque
, int n
, int level
)
99 if (n
< ISA_NUM_IRQS
) {
100 /* Kernel will forward to both PIC and IOAPIC */
101 qemu_set_irq(s
->i8259_irq
[n
], level
);
103 qemu_set_irq(s
->ioapic_irq
[n
], level
);
107 static void ioapic_init(GSIState
*gsi_state
)
113 #ifdef UNUSED_UPSTREAM_KVM
114 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
115 dev
= qdev_create(NULL
, "kvm-ioapic");
119 dev
= qdev_create(NULL
, "ioapic");
121 qdev_init_nofail(dev
);
122 d
= sysbus_from_qdev(dev
);
123 sysbus_mmio_map(d
, 0, 0xfec00000);
125 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
126 gsi_state
->ioapic_irq
[i
] = qdev_get_gpio_in(dev
, i
);
130 /* PC hardware initialisation */
131 static void pc_init1(MemoryRegion
*system_memory
,
132 MemoryRegion
*system_io
,
134 const char *boot_device
,
135 const char *kernel_filename
,
136 const char *kernel_cmdline
,
137 const char *initrd_filename
,
138 const char *cpu_model
,
140 int kvmclock_enabled
)
143 ram_addr_t below_4g_mem_size
, above_4g_mem_size
;
146 PCII440FXState
*i440fx_state
;
147 int piix3_devfn
= -1;
154 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
155 BusState
*idebus
[MAX_IDE_BUS
];
156 ISADevice
*rtc_state
;
158 MemoryRegion
*ram_memory
;
159 MemoryRegion
*pci_memory
;
160 MemoryRegion
*rom_memory
;
163 global_cpu_model
= cpu_model
;
165 pc_cpus_init(cpu_model
);
167 if (kvmclock_enabled
) {
171 if (ram_size
>= 0xe0000000 ) {
172 above_4g_mem_size
= ram_size
- 0xe0000000;
173 below_4g_mem_size
= 0xe0000000;
175 above_4g_mem_size
= 0;
176 below_4g_mem_size
= ram_size
;
180 pci_memory
= g_new(MemoryRegion
, 1);
181 memory_region_init(pci_memory
, "pci", INT64_MAX
);
182 rom_memory
= pci_memory
;
185 rom_memory
= system_memory
;
188 /* allocate ram and load rom/bios */
189 if (!xen_enabled()) {
190 pc_memory_init(system_memory
,
191 kernel_filename
, kernel_cmdline
, initrd_filename
,
192 below_4g_mem_size
, above_4g_mem_size
,
193 pci_enabled
? rom_memory
: system_memory
, &ram_memory
);
196 gsi_state
= g_malloc0(sizeof(*gsi_state
));
197 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
198 #ifdef UNUSED_UPSTREAM_KVM
199 kvm_piix3_setup_irq_routing(pci_enabled
);
201 gsi
= qemu_allocate_irqs(kvm_piix3_gsi_handler
, gsi_state
,
204 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
208 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
209 system_memory
, system_io
, ram_size
,
211 0x100000000ULL
- below_4g_mem_size
,
212 0x100000000ULL
+ above_4g_mem_size
,
213 (sizeof(target_phys_addr_t
) == 4
215 : ((uint64_t)1 << 62)),
216 pci_memory
, ram_memory
);
220 isa_bus
= isa_bus_new(NULL
, system_io
);
223 isa_bus_irqs(isa_bus
, gsi
);
225 #ifdef UNUSED_UPSTREAM_KVM
226 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
227 i8259
= kvm_i8259_init(isa_bus
);
231 i8259
= xen_interrupt_controller_init();
233 cpu_irq
= pc_allocate_cpu_irq();
234 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
237 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
238 gsi_state
->i8259_irq
[i
] = i8259
[i
];
241 ioapic_init(gsi_state
);
244 pc_register_ferr_irq(gsi
[13]);
246 dev
= pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
248 qdev_property_add_child(qdev_get_root(), "vga", dev
, NULL
);
252 pci_create_simple(pci_bus
, -1, "xen-platform");
255 /* init basic PC hardware */
256 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
258 for(i
= 0; i
< nb_nics
; i
++) {
259 NICInfo
*nd
= &nd_table
[i
];
261 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
262 pc_init_ne2k_isa(isa_bus
, nd
);
264 pci_nic_init_nofail(nd
, "rtl8139", NULL
);
267 ide_drive_get(hd
, MAX_IDE_BUS
);
271 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
273 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
275 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
276 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
278 /* FIXME there's some major spaghetti here. Somehow we create the
279 * devices on the PIIX before we actually create it. We create the
280 * PIIX3 deep in the recess of the i440fx creation too and then lose
283 * For now, let's "fix" this by making judicious use of paths. This
284 * is not generally the right way to do this.
286 qdev_property_add_child(qdev_resolve_path("/i440fx/piix3", NULL
),
287 "rtc", (DeviceState
*)rtc_state
, NULL
);
289 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
291 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
293 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
294 idebus
[i
] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
298 audio_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
300 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
301 floppy
, idebus
[0], idebus
[1], rtc_state
);
303 if (pci_enabled
&& usb_enabled
) {
304 usb_uhci_piix3_init(pci_bus
, piix3_devfn
+ 2);
307 if (pci_enabled
&& acpi_enabled
) {
310 if (!xen_enabled()) {
311 cmos_s3
= qemu_allocate_irqs(pc_cmos_set_s3_resume
, rtc_state
, 1);
313 cmos_s3
= qemu_allocate_irqs(xen_cmos_set_s3_resume
, rtc_state
, 1);
315 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
, first_cpu
, 1);
316 /* TODO: Populate SPD eeprom data. */
317 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
318 gsi
[9], *cmos_s3
, *smi_irq
,
320 smbus_eeprom_init(smbus
, 8, NULL
, 0);
324 pc_pci_device_init(pci_bus
);
328 static void pc_init_pci(ram_addr_t ram_size
,
329 const char *boot_device
,
330 const char *kernel_filename
,
331 const char *kernel_cmdline
,
332 const char *initrd_filename
,
333 const char *cpu_model
)
335 pc_init1(get_system_memory(),
337 ram_size
, boot_device
,
338 kernel_filename
, kernel_cmdline
,
339 initrd_filename
, cpu_model
, 1, 1);
342 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size
,
343 const char *boot_device
,
344 const char *kernel_filename
,
345 const char *kernel_cmdline
,
346 const char *initrd_filename
,
347 const char *cpu_model
)
349 pc_init1(get_system_memory(),
351 ram_size
, boot_device
,
352 kernel_filename
, kernel_cmdline
,
353 initrd_filename
, cpu_model
, 1, 0);
356 static void pc_init_isa(ram_addr_t ram_size
,
357 const char *boot_device
,
358 const char *kernel_filename
,
359 const char *kernel_cmdline
,
360 const char *initrd_filename
,
361 const char *cpu_model
)
363 if (cpu_model
== NULL
)
365 pc_init1(get_system_memory(),
367 ram_size
, boot_device
,
368 kernel_filename
, kernel_cmdline
,
369 initrd_filename
, cpu_model
, 0, 1);
373 static void pc_xen_hvm_init(ram_addr_t ram_size
,
374 const char *boot_device
,
375 const char *kernel_filename
,
376 const char *kernel_cmdline
,
377 const char *initrd_filename
,
378 const char *cpu_model
)
380 if (xen_hvm_init() != 0) {
381 hw_error("xen hardware virtual machine initialisation failed");
383 pc_init_pci_no_kvmclock(ram_size
, boot_device
,
384 kernel_filename
, kernel_cmdline
,
385 initrd_filename
, cpu_model
);
390 static QEMUMachine pc_machine_v1_0
= {
393 .desc
= "Standard PC",
399 static QEMUMachine pc_machine_v0_15
= {
401 .desc
= "Standard PC",
407 static QEMUMachine pc_machine_v0_14
= {
409 .desc
= "Standard PC",
412 .compat_props
= (GlobalProperty
[]) {
415 .property
= "revision",
416 .value
= stringify(2),
419 .property
= "revision",
420 .value
= stringify(2),
422 .driver
= "virtio-blk-pci",
423 .property
= "event_idx",
426 .driver
= "virtio-serial-pci",
427 .property
= "event_idx",
430 .driver
= "virtio-net-pci",
431 .property
= "event_idx",
434 .driver
= "virtio-balloon-pci",
435 .property
= "event_idx",
438 { /* end of list */ }
442 static QEMUMachine pc_machine_v0_13
= {
444 .desc
= "Standard PC",
445 .init
= pc_init_pci_no_kvmclock
,
447 .compat_props
= (GlobalProperty
[]) {
449 .driver
= "virtio-9p-pci",
450 .property
= "vectors",
451 .value
= stringify(0),
454 .property
= "rombar",
455 .value
= stringify(0),
457 .driver
= "vmware-svga",
458 .property
= "rombar",
459 .value
= stringify(0),
462 .property
= "command_serr_enable",
465 .driver
= "virtio-blk-pci",
466 .property
= "event_idx",
469 .driver
= "virtio-serial-pci",
470 .property
= "event_idx",
473 .driver
= "virtio-net-pci",
474 .property
= "event_idx",
477 .driver
= "virtio-balloon-pci",
478 .property
= "event_idx",
482 .property
= "use_broken_id",
483 .value
= stringify(1),
485 { /* end of list */ }
489 static QEMUMachine pc_machine_v0_12
= {
491 .desc
= "Standard PC",
492 .init
= pc_init_pci_no_kvmclock
,
494 .compat_props
= (GlobalProperty
[]) {
496 .driver
= "virtio-serial-pci",
497 .property
= "max_ports",
498 .value
= stringify(1),
500 .driver
= "virtio-serial-pci",
501 .property
= "vectors",
502 .value
= stringify(0),
505 .property
= "rombar",
506 .value
= stringify(0),
508 .driver
= "vmware-svga",
509 .property
= "rombar",
510 .value
= stringify(0),
513 .property
= "command_serr_enable",
516 .driver
= "virtio-blk-pci",
517 .property
= "event_idx",
520 .driver
= "virtio-serial-pci",
521 .property
= "event_idx",
524 .driver
= "virtio-net-pci",
525 .property
= "event_idx",
528 .driver
= "virtio-balloon-pci",
529 .property
= "event_idx",
533 .property
= "use_broken_id",
534 .value
= stringify(1),
536 { /* end of list */ }
540 static QEMUMachine pc_machine_v0_11
= {
542 .desc
= "Standard PC, qemu 0.11",
543 .init
= pc_init_pci_no_kvmclock
,
545 .compat_props
= (GlobalProperty
[]) {
547 .driver
= "virtio-blk-pci",
548 .property
= "vectors",
549 .value
= stringify(0),
551 .driver
= "virtio-serial-pci",
552 .property
= "max_ports",
553 .value
= stringify(1),
555 .driver
= "virtio-serial-pci",
556 .property
= "vectors",
557 .value
= stringify(0),
559 .driver
= "ide-drive",
563 .driver
= "scsi-disk",
568 .property
= "rombar",
569 .value
= stringify(0),
572 .property
= "command_serr_enable",
575 .driver
= "virtio-blk-pci",
576 .property
= "event_idx",
579 .driver
= "virtio-serial-pci",
580 .property
= "event_idx",
583 .driver
= "virtio-net-pci",
584 .property
= "event_idx",
587 .driver
= "virtio-balloon-pci",
588 .property
= "event_idx",
592 .property
= "use_broken_id",
593 .value
= stringify(1),
595 { /* end of list */ }
599 static QEMUMachine pc_machine_v0_10
= {
601 .desc
= "Standard PC, qemu 0.10",
602 .init
= pc_init_pci_no_kvmclock
,
604 .compat_props
= (GlobalProperty
[]) {
606 .driver
= "virtio-blk-pci",
608 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
610 .driver
= "virtio-serial-pci",
612 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
614 .driver
= "virtio-serial-pci",
615 .property
= "max_ports",
616 .value
= stringify(1),
618 .driver
= "virtio-serial-pci",
619 .property
= "vectors",
620 .value
= stringify(0),
622 .driver
= "virtio-net-pci",
623 .property
= "vectors",
624 .value
= stringify(0),
626 .driver
= "virtio-blk-pci",
627 .property
= "vectors",
628 .value
= stringify(0),
630 .driver
= "ide-drive",
634 .driver
= "scsi-disk",
639 .property
= "rombar",
640 .value
= stringify(0),
643 .property
= "command_serr_enable",
646 .driver
= "virtio-blk-pci",
647 .property
= "event_idx",
650 .driver
= "virtio-serial-pci",
651 .property
= "event_idx",
654 .driver
= "virtio-net-pci",
655 .property
= "event_idx",
658 .driver
= "virtio-balloon-pci",
659 .property
= "event_idx",
663 .property
= "use_broken_id",
664 .value
= stringify(1),
666 { /* end of list */ }
670 static QEMUMachine isapc_machine
= {
672 .desc
= "ISA-only PC",
678 static QEMUMachine xenfv_machine
= {
680 .desc
= "Xen Fully-virtualized PC",
681 .init
= pc_xen_hvm_init
,
682 .max_cpus
= HVM_MAX_VCPUS
,
683 .default_machine_opts
= "accel=xen",
687 static void pc_machine_init(void)
689 qemu_register_machine(&pc_machine_v1_0
);
690 qemu_register_machine(&pc_machine_v0_15
);
691 qemu_register_machine(&pc_machine_v0_14
);
692 qemu_register_machine(&pc_machine_v0_13
);
693 qemu_register_machine(&pc_machine_v0_12
);
694 qemu_register_machine(&pc_machine_v0_11
);
695 qemu_register_machine(&pc_machine_v0_10
);
696 qemu_register_machine(&isapc_machine
);
698 qemu_register_machine(&xenfv_machine
);
702 machine_init(pc_machine_init
);