Merge commit 'ea776abca628d855e03c4929da3864985afd8aae' into upstream-merge
[qemu-kvm.git] / hw / pc_piix.c
blobbc18fdccf7f73477dcbf1b44318d56913bf924b0
1 /*
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
22 * THE SOFTWARE.
25 #include <glib.h>
27 #include "hw.h"
28 #include "pc.h"
29 #include "apic.h"
30 #include "pci.h"
31 #include "pci_ids.h"
32 #include "usb.h"
33 #include "net.h"
34 #include "boards.h"
35 #include "ide.h"
36 #include "kvm.h"
37 #include "kvm/clock.h"
38 #include "sysemu.h"
39 #include "sysbus.h"
40 #include "arch_init.h"
41 #include "blockdev.h"
42 #include "smbus.h"
43 #include "xen.h"
44 #include "memory.h"
45 #include "exec-memory.h"
46 #ifdef CONFIG_XEN
47 # include <xen/hvm/hvm_info_table.h>
48 #endif
50 #define MAX_IDE_BUS 2
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)
60 #ifdef CONFIG_KVM
61 KVMState *s = kvm_state;
62 int i;
64 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
65 for (i = 0; i < 8; ++i) {
66 if (i == 2) {
67 continue;
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);
74 if (pci_enabled) {
75 for (i = 0; i < 24; ++i) {
76 if (i == 0) {
77 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
78 } else if (i != 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)
89 GSIState *s = opaque;
91 if (n < ISA_NUM_IRQS) {
92 /* Kernel will forward to both PIC and IOAPIC */
93 qemu_set_irq(s->i8259_irq[n], level);
94 } else {
95 qemu_set_irq(s->ioapic_irq[n], level);
99 static void ioapic_init(GSIState *gsi_state)
101 DeviceState *dev;
102 SysBusDevice *d;
103 unsigned int i;
105 if (kvm_irqchip_in_kernel()) {
106 dev = qdev_create(NULL, "kvm-ioapic");
107 } else {
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,
125 ram_addr_t ram_size,
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,
131 int pci_enabled,
132 int kvmclock_enabled)
134 int i;
135 ram_addr_t below_4g_mem_size, above_4g_mem_size;
136 PCIBus *pci_bus;
137 ISABus *isa_bus;
138 PCII440FXState *i440fx_state;
139 int piix3_devfn = -1;
140 qemu_irq *cpu_irq;
141 qemu_irq *gsi;
142 qemu_irq *i8259;
143 qemu_irq *smi_irq;
144 GSIState *gsi_state;
145 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
146 BusState *idebus[MAX_IDE_BUS];
147 ISADevice *rtc_state;
148 ISADevice *floppy;
149 MemoryRegion *ram_memory;
150 MemoryRegion *pci_memory;
151 MemoryRegion *rom_memory;
152 void *fw_cfg = NULL;
154 global_cpu_model = cpu_model;
156 pc_cpus_init(cpu_model);
158 if (kvmclock_enabled) {
159 kvmclock_create();
162 if (ram_size >= 0xe0000000 ) {
163 above_4g_mem_size = ram_size - 0xe0000000;
164 below_4g_mem_size = 0xe0000000;
165 } else {
166 above_4g_mem_size = 0;
167 below_4g_mem_size = ram_size;
170 if (pci_enabled) {
171 pci_memory = g_new(MemoryRegion, 1);
172 memory_region_init(pci_memory, "pci", INT64_MAX);
173 rom_memory = pci_memory;
174 } else {
175 pci_memory = NULL;
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,
191 GSI_NUM_PINS);
192 } else {
193 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
196 if (pci_enabled) {
197 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
198 system_memory, system_io, ram_size,
199 below_4g_mem_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);
206 } else {
207 pci_bus = NULL;
208 i440fx_state = NULL;
209 isa_bus = isa_bus_new(NULL, system_io);
210 no_hpet = 1;
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();
218 } else {
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];
226 if (pci_enabled) {
227 ioapic_init(gsi_state);
230 pc_register_ferr_irq(gsi[13]);
232 pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
233 if (xen_enabled()) {
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);
245 else
246 pci_nic_init_nofail(nd, "rtl8139", NULL);
249 ide_drive_get(hd, MAX_IDE_BUS);
250 if (pci_enabled) {
251 PCIDevice *dev;
252 if (xen_enabled()) {
253 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
254 } else {
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");
259 } else {
260 for(i = 0; i < MAX_IDE_BUS; i++) {
261 ISADevice *dev;
262 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
263 ide_irq[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) {
279 i2c_bus *smbus;
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,
284 gsi[9], *smi_irq,
285 kvm_enabled(), fw_cfg);
286 smbus_eeprom_init(smbus, 8, NULL, 0);
289 if (pci_enabled) {
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(),
302 get_system_io(),
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(),
316 get_system_io(),
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)
330 cpu_model = "486";
331 pc_init1(get_system_memory(),
332 get_system_io(),
333 ram_size, boot_device,
334 kernel_filename, kernel_cmdline,
335 initrd_filename, cpu_model, 0, 1);
338 #ifdef CONFIG_XEN
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);
352 xen_vcpu_init();
354 #endif
356 #ifdef CONFIG_KVM_OPTIONS
357 #define KVM_MACHINE_OPTIONS "accel=kvm"
358 #else
359 #define KVM_MACHINE_OPTIONS ""
360 #endif
362 static QEMUMachine pc_machine_v1_2 = {
363 .name = "pc-1.2",
364 .alias = "pc",
365 .desc = "Standard PC",
366 .init = pc_init_pci,
367 .max_cpus = 255,
368 .is_default = 1,
369 .default_machine_opts = KVM_MACHINE_OPTIONS,
372 #define PC_COMPAT_1_1 \
374 .driver = "VGA",\
375 .property = "vgamem_mb",\
376 .value = stringify(16),\
377 },{\
378 .driver = "vmware-svga",\
379 .property = "vgamem_mb",\
380 .value = stringify(16),\
381 },{\
382 .driver = "qxl-vga",\
383 .property = "vgamem_mb",\
384 .value = stringify(16),\
385 },{\
386 .driver = "qxl",\
387 .property = "vgamem_mb",\
388 .value = stringify(16),\
389 },{\
390 .driver = "virtio-blk-pci",\
391 .property = "config-wce",\
392 .value = "off",\
395 static QEMUMachine pc_machine_v1_1 = {
396 .name = "pc-1.1",
397 .desc = "Standard PC",
398 .init = pc_init_pci,
399 .max_cpus = 255,
400 .default_machine_opts = KVM_MACHINE_OPTIONS,
401 .compat_props = (GlobalProperty[]) {
402 PC_COMPAT_1_1,
403 { /* end of list */ }
407 #define PC_COMPAT_1_0 \
408 PC_COMPAT_1_1,\
410 .driver = "pc-sysfw",\
411 .property = "rom_only",\
412 .value = stringify(1),\
413 }, {\
414 .driver = "isa-fdc",\
415 .property = "check_media_rate",\
416 .value = "off",\
417 }, {\
418 .driver = "virtio-balloon-pci",\
419 .property = "class",\
420 .value = stringify(PCI_CLASS_MEMORY_RAM),\
421 },{\
422 .driver = "apic",\
423 .property = "vapic",\
424 .value = "off",\
425 },{\
426 .driver = TYPE_USB_DEVICE,\
427 .property = "full-path",\
428 .value = "no",\
431 static QEMUMachine pc_machine_v1_0 = {
432 .name = "pc-1.0",
433 .desc = "Standard PC",
434 .init = pc_init_pci,
435 .max_cpus = 255,
436 .default_machine_opts = KVM_MACHINE_OPTIONS,
437 .compat_props = (GlobalProperty[]) {
438 PC_COMPAT_1_0,
439 { /* end of list */ }
441 .hw_version = "1.0",
444 #define PC_COMPAT_0_15 \
445 PC_COMPAT_1_0
447 static QEMUMachine pc_machine_v0_15 = {
448 .name = "pc-0.15",
449 .desc = "Standard PC",
450 .init = pc_init_pci,
451 .max_cpus = 255,
452 .default_machine_opts = KVM_MACHINE_OPTIONS,
453 .compat_props = (GlobalProperty[]) {
454 PC_COMPAT_0_15,
455 { /* end of list */ }
457 .hw_version = "0.15",
460 #define PC_COMPAT_0_14 \
461 PC_COMPAT_0_15,\
463 .driver = "virtio-blk-pci",\
464 .property = "event_idx",\
465 .value = "off",\
466 },{\
467 .driver = "virtio-serial-pci",\
468 .property = "event_idx",\
469 .value = "off",\
470 },{\
471 .driver = "virtio-net-pci",\
472 .property = "event_idx",\
473 .value = "off",\
474 },{\
475 .driver = "virtio-balloon-pci",\
476 .property = "event_idx",\
477 .value = "off",\
480 static QEMUMachine pc_machine_v0_14 = {
481 .name = "pc-0.14",
482 .desc = "Standard PC",
483 .init = pc_init_pci,
484 .max_cpus = 255,
485 .default_machine_opts = KVM_MACHINE_OPTIONS,
486 .compat_props = (GlobalProperty[]) {
487 PC_COMPAT_0_14,
489 .driver = "qxl",
490 .property = "revision",
491 .value = stringify(2),
493 .driver = "qxl-vga",
494 .property = "revision",
495 .value = stringify(2),
497 { /* end of list */ }
499 .hw_version = "0.14",
502 #define PC_COMPAT_0_13 \
503 PC_COMPAT_0_14,\
505 .driver = TYPE_PCI_DEVICE,\
506 .property = "command_serr_enable",\
507 .value = "off",\
508 },{\
509 .driver = "AC97",\
510 .property = "use_broken_id",\
511 .value = stringify(1),\
514 static QEMUMachine pc_machine_v0_13 = {
515 .name = "pc-0.13",
516 .desc = "Standard PC",
517 .init = pc_init_pci_no_kvmclock,
518 .max_cpus = 255,
519 .default_machine_opts = KVM_MACHINE_OPTIONS,
520 .compat_props = (GlobalProperty[]) {
521 PC_COMPAT_0_13,
523 .driver = "virtio-9p-pci",
524 .property = "vectors",
525 .value = stringify(0),
527 .driver = "VGA",
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 \
541 PC_COMPAT_0_13,\
543 .driver = "virtio-serial-pci",\
544 .property = "max_ports",\
545 .value = stringify(1),\
546 },{\
547 .driver = "virtio-serial-pci",\
548 .property = "vectors",\
549 .value = stringify(0),\
552 static QEMUMachine pc_machine_v0_12 = {
553 .name = "pc-0.12",
554 .desc = "Standard PC",
555 .init = pc_init_pci_no_kvmclock,
556 .max_cpus = 255,
557 .default_machine_opts = KVM_MACHINE_OPTIONS,
558 .compat_props = (GlobalProperty[]) {
559 PC_COMPAT_0_12,
561 .driver = "VGA",
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 \
575 PC_COMPAT_0_12,\
577 .driver = "virtio-blk-pci",\
578 .property = "vectors",\
579 .value = stringify(0),\
580 },{\
581 .driver = TYPE_PCI_DEVICE,\
582 .property = "rombar",\
583 .value = stringify(0),\
586 static QEMUMachine pc_machine_v0_11 = {
587 .name = "pc-0.11",
588 .desc = "Standard PC, qemu 0.11",
589 .init = pc_init_pci_no_kvmclock,
590 .max_cpus = 255,
591 .default_machine_opts = KVM_MACHINE_OPTIONS,
592 .compat_props = (GlobalProperty[]) {
593 PC_COMPAT_0_11,
595 .driver = "ide-drive",
596 .property = "ver",
597 .value = "0.11",
599 .driver = "scsi-disk",
600 .property = "ver",
601 .value = "0.11",
603 { /* end of list */ }
605 .hw_version = "0.11",
608 static QEMUMachine pc_machine_v0_10 = {
609 .name = "pc-0.10",
610 .desc = "Standard PC, qemu 0.10",
611 .init = pc_init_pci_no_kvmclock,
612 .max_cpus = 255,
613 .default_machine_opts = KVM_MACHINE_OPTIONS,
614 .compat_props = (GlobalProperty[]) {
615 PC_COMPAT_0_11,
617 .driver = "virtio-blk-pci",
618 .property = "class",
619 .value = stringify(PCI_CLASS_STORAGE_OTHER),
621 .driver = "virtio-serial-pci",
622 .property = "class",
623 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
625 .driver = "virtio-net-pci",
626 .property = "vectors",
627 .value = stringify(0),
629 .driver = "ide-drive",
630 .property = "ver",
631 .value = "0.10",
633 .driver = "scsi-disk",
634 .property = "ver",
635 .value = "0.10",
637 { /* end of list */ }
639 .hw_version = "0.10",
642 static QEMUMachine isapc_machine = {
643 .name = "isapc",
644 .desc = "ISA-only PC",
645 .init = pc_init_isa,
646 .max_cpus = 1,
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 */ }
658 #ifdef CONFIG_XEN
659 static QEMUMachine xenfv_machine = {
660 .name = "xenfv",
661 .desc = "Xen Fully-virtualized PC",
662 .init = pc_xen_hvm_init,
663 .max_cpus = HVM_MAX_VCPUS,
664 .default_machine_opts = "accel=xen",
666 #endif
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);
680 #ifdef CONFIG_XEN
681 qemu_register_machine(&xenfv_machine);
682 #endif
685 machine_init(pc_machine_init);