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
36 #include "kvm/clock.h"
39 #include "arch_init.h"
44 #include "exec-memory.h"
46 # include <xen/hvm/hvm_info_table.h>
51 static const int ide_iobase
[MAX_IDE_BUS
] = { 0x1f0, 0x170 };
52 static const int ide_iobase2
[MAX_IDE_BUS
] = { 0x3f6, 0x376 };
53 static const int ide_irq
[MAX_IDE_BUS
] = { 14, 15 };
55 static void kvm_piix3_setup_irq_routing(bool pci_enabled
)
58 KVMState
*s
= kvm_state
;
61 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
62 for (i
= 0; i
< 8; ++i
) {
66 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
68 for (i
= 8; i
< 16; ++i
) {
69 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
72 for (i
= 0; i
< 24; ++i
) {
74 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, 2);
76 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, i
);
81 #endif /* CONFIG_KVM */
84 static void kvm_piix3_gsi_handler(void *opaque
, int n
, int level
)
88 if (n
< ISA_NUM_IRQS
) {
89 /* Kernel will forward to both PIC and IOAPIC */
90 qemu_set_irq(s
->i8259_irq
[n
], level
);
92 qemu_set_irq(s
->ioapic_irq
[n
], level
);
96 static void ioapic_init(GSIState
*gsi_state
)
102 if (kvm_irqchip_in_kernel()) {
103 dev
= qdev_create(NULL
, "kvm-ioapic");
105 dev
= qdev_create(NULL
, "ioapic");
107 /* FIXME: this should be under the piix3. */
108 object_property_add_child(object_resolve_path("i440fx", NULL
),
109 "ioapic", OBJECT(dev
), NULL
);
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
;
150 pc_cpus_init(cpu_model
);
152 if (kvmclock_enabled
) {
156 if (ram_size
>= 0xe0000000 ) {
157 above_4g_mem_size
= ram_size
- 0xe0000000;
158 below_4g_mem_size
= 0xe0000000;
160 above_4g_mem_size
= 0;
161 below_4g_mem_size
= ram_size
;
165 pci_memory
= g_new(MemoryRegion
, 1);
166 memory_region_init(pci_memory
, "pci", INT64_MAX
);
167 rom_memory
= pci_memory
;
170 rom_memory
= system_memory
;
173 /* allocate ram and load rom/bios */
174 if (!xen_enabled()) {
175 pc_memory_init(system_memory
,
176 kernel_filename
, kernel_cmdline
, initrd_filename
,
177 below_4g_mem_size
, above_4g_mem_size
,
178 pci_enabled
? rom_memory
: system_memory
, &ram_memory
);
181 gsi_state
= g_malloc0(sizeof(*gsi_state
));
182 if (kvm_irqchip_in_kernel()) {
183 kvm_piix3_setup_irq_routing(pci_enabled
);
184 gsi
= qemu_allocate_irqs(kvm_piix3_gsi_handler
, gsi_state
,
187 gsi
= qemu_allocate_irqs(gsi_handler
, gsi_state
, GSI_NUM_PINS
);
191 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, &isa_bus
, gsi
,
192 system_memory
, system_io
, ram_size
,
194 0x100000000ULL
- below_4g_mem_size
,
195 0x100000000ULL
+ above_4g_mem_size
,
196 (sizeof(target_phys_addr_t
) == 4
198 : ((uint64_t)1 << 62)),
199 pci_memory
, ram_memory
);
203 isa_bus
= isa_bus_new(NULL
, system_io
);
206 isa_bus_irqs(isa_bus
, gsi
);
208 if (kvm_irqchip_in_kernel()) {
209 i8259
= kvm_i8259_init(isa_bus
);
210 } else if (xen_enabled()) {
211 i8259
= xen_interrupt_controller_init();
213 cpu_irq
= pc_allocate_cpu_irq();
214 i8259
= i8259_init(isa_bus
, cpu_irq
[0]);
217 for (i
= 0; i
< ISA_NUM_IRQS
; i
++) {
218 gsi_state
->i8259_irq
[i
] = i8259
[i
];
221 ioapic_init(gsi_state
);
224 pc_register_ferr_irq(gsi
[13]);
226 pc_vga_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
228 pci_create_simple(pci_bus
, -1, "xen-platform");
231 /* init basic PC hardware */
232 pc_basic_device_init(isa_bus
, gsi
, &rtc_state
, &floppy
, xen_enabled());
234 for(i
= 0; i
< nb_nics
; i
++) {
235 NICInfo
*nd
= &nd_table
[i
];
237 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
238 pc_init_ne2k_isa(isa_bus
, nd
);
240 pci_nic_init_nofail(nd
, "e1000", NULL
);
243 ide_drive_get(hd
, MAX_IDE_BUS
);
247 dev
= pci_piix3_xen_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
249 dev
= pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
251 idebus
[0] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
252 idebus
[1] = qdev_get_child_bus(&dev
->qdev
, "ide.1");
254 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
256 dev
= isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
],
258 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
259 idebus
[i
] = qdev_get_child_bus(&dev
->qdev
, "ide.0");
263 audio_init(isa_bus
, pci_enabled
? pci_bus
: NULL
);
265 pc_cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
,
266 floppy
, idebus
[0], idebus
[1], rtc_state
);
268 if (pci_enabled
&& usb_enabled
) {
269 pci_create_simple(pci_bus
, piix3_devfn
+ 2, "piix3-usb-uhci");
272 if (pci_enabled
&& acpi_enabled
) {
275 smi_irq
= qemu_allocate_irqs(pc_acpi_smi_interrupt
, first_cpu
, 1);
276 /* TODO: Populate SPD eeprom data. */
277 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
280 smbus_eeprom_init(smbus
, 8, NULL
, 0);
284 pc_pci_device_init(pci_bus
);
288 static void pc_init_pci(ram_addr_t ram_size
,
289 const char *boot_device
,
290 const char *kernel_filename
,
291 const char *kernel_cmdline
,
292 const char *initrd_filename
,
293 const char *cpu_model
)
295 pc_init1(get_system_memory(),
297 ram_size
, boot_device
,
298 kernel_filename
, kernel_cmdline
,
299 initrd_filename
, cpu_model
, 1, 1);
302 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size
,
303 const char *boot_device
,
304 const char *kernel_filename
,
305 const char *kernel_cmdline
,
306 const char *initrd_filename
,
307 const char *cpu_model
)
309 pc_init1(get_system_memory(),
311 ram_size
, boot_device
,
312 kernel_filename
, kernel_cmdline
,
313 initrd_filename
, cpu_model
, 1, 0);
316 static void pc_init_isa(ram_addr_t ram_size
,
317 const char *boot_device
,
318 const char *kernel_filename
,
319 const char *kernel_cmdline
,
320 const char *initrd_filename
,
321 const char *cpu_model
)
323 if (cpu_model
== NULL
)
325 pc_init1(get_system_memory(),
327 ram_size
, boot_device
,
328 kernel_filename
, kernel_cmdline
,
329 initrd_filename
, cpu_model
, 0, 1);
333 static void pc_xen_hvm_init(ram_addr_t ram_size
,
334 const char *boot_device
,
335 const char *kernel_filename
,
336 const char *kernel_cmdline
,
337 const char *initrd_filename
,
338 const char *cpu_model
)
340 if (xen_hvm_init() != 0) {
341 hw_error("xen hardware virtual machine initialisation failed");
343 pc_init_pci_no_kvmclock(ram_size
, boot_device
,
344 kernel_filename
, kernel_cmdline
,
345 initrd_filename
, cpu_model
);
350 static QEMUMachine pc_machine_v1_1
= {
353 .desc
= "Standard PC",
359 #define PC_COMPAT_1_0 \
361 .driver = "pc-sysfw",\
362 .property = "rom_only",\
363 .value = stringify(1),\
365 .driver = "isa-fdc",\
366 .property = "check_media_rate",\
369 .driver = "virtio-balloon-pci",\
370 .property = "class",\
371 .value = stringify(PCI_CLASS_MEMORY_RAM),\
374 .property = "vapic",\
378 .property = "full-path",\
382 static QEMUMachine pc_machine_v1_0
= {
384 .desc
= "Standard PC",
387 .compat_props
= (GlobalProperty
[]) {
389 { /* end of list */ }
393 #define PC_COMPAT_0_15 \
396 static QEMUMachine pc_machine_v0_15
= {
398 .desc
= "Standard PC",
401 .compat_props
= (GlobalProperty
[]) {
403 { /* end of list */ }
407 #define PC_COMPAT_0_14 \
410 .driver = "virtio-blk-pci",\
411 .property = "event_idx",\
414 .driver = "virtio-serial-pci",\
415 .property = "event_idx",\
418 .driver = "virtio-net-pci",\
419 .property = "event_idx",\
422 .driver = "virtio-balloon-pci",\
423 .property = "event_idx",\
427 static QEMUMachine pc_machine_v0_14
= {
429 .desc
= "Standard PC",
432 .compat_props
= (GlobalProperty
[]) {
436 .property
= "revision",
437 .value
= stringify(2),
440 .property
= "revision",
441 .value
= stringify(2),
443 { /* end of list */ }
447 #define PC_COMPAT_0_13 \
451 .property = "command_serr_enable",\
455 .property = "use_broken_id",\
456 .value = stringify(1),\
459 static QEMUMachine pc_machine_v0_13
= {
461 .desc
= "Standard PC",
462 .init
= pc_init_pci_no_kvmclock
,
464 .compat_props
= (GlobalProperty
[]) {
467 .driver
= "virtio-9p-pci",
468 .property
= "vectors",
469 .value
= stringify(0),
472 .property
= "rombar",
473 .value
= stringify(0),
475 .driver
= "vmware-svga",
476 .property
= "rombar",
477 .value
= stringify(0),
479 { /* end of list */ }
483 #define PC_COMPAT_0_12 \
486 .driver = "virtio-serial-pci",\
487 .property = "max_ports",\
488 .value = stringify(1),\
490 .driver = "virtio-serial-pci",\
491 .property = "vectors",\
492 .value = stringify(0),\
495 static QEMUMachine pc_machine_v0_12
= {
497 .desc
= "Standard PC",
498 .init
= pc_init_pci_no_kvmclock
,
500 .compat_props
= (GlobalProperty
[]) {
504 .property
= "rombar",
505 .value
= stringify(0),
507 .driver
= "vmware-svga",
508 .property
= "rombar",
509 .value
= stringify(0),
511 { /* end of list */ }
515 #define PC_COMPAT_0_11 \
518 .driver = "virtio-blk-pci",\
519 .property = "vectors",\
520 .value = stringify(0),\
523 .property = "rombar",\
524 .value = stringify(0),\
527 static QEMUMachine pc_machine_v0_11
= {
529 .desc
= "Standard PC, qemu 0.11",
530 .init
= pc_init_pci_no_kvmclock
,
532 .compat_props
= (GlobalProperty
[]) {
535 .driver
= "ide-drive",
539 .driver
= "scsi-disk",
543 { /* end of list */ }
547 static QEMUMachine pc_machine_v0_10
= {
549 .desc
= "Standard PC, qemu 0.10",
550 .init
= pc_init_pci_no_kvmclock
,
552 .compat_props
= (GlobalProperty
[]) {
555 .driver
= "virtio-blk-pci",
557 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
559 .driver
= "virtio-serial-pci",
561 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
563 .driver
= "virtio-net-pci",
564 .property
= "vectors",
565 .value
= stringify(0),
567 .driver
= "ide-drive",
571 .driver
= "scsi-disk",
575 { /* end of list */ }
579 static QEMUMachine isapc_machine
= {
581 .desc
= "ISA-only PC",
584 .compat_props
= (GlobalProperty
[]) {
586 .driver
= "pc-sysfw",
587 .property
= "rom_only",
588 .value
= stringify(1),
590 { /* end of list */ }
595 static QEMUMachine xenfv_machine
= {
597 .desc
= "Xen Fully-virtualized PC",
598 .init
= pc_xen_hvm_init
,
599 .max_cpus
= HVM_MAX_VCPUS
,
600 .default_machine_opts
= "accel=xen",
604 static void pc_machine_init(void)
606 qemu_register_machine(&pc_machine_v1_1
);
607 qemu_register_machine(&pc_machine_v1_0
);
608 qemu_register_machine(&pc_machine_v0_15
);
609 qemu_register_machine(&pc_machine_v0_14
);
610 qemu_register_machine(&pc_machine_v0_13
);
611 qemu_register_machine(&pc_machine_v0_12
);
612 qemu_register_machine(&pc_machine_v0_11
);
613 qemu_register_machine(&pc_machine_v0_10
);
614 qemu_register_machine(&isapc_machine
);
616 qemu_register_machine(&xenfv_machine
);
620 machine_init(pc_machine_init
);