Merge commit 'e32605062cd62c2a958ad28a6ad7de4eeab12027' into upstream-merge
[qemu-kvm.git] / hw / pc_piix.c
blob63dba4ee57d0ea598d3cf6ae41270be518733f5e
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 "usb-uhci.h"
32 #include "usb-ohci.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 ret, 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_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
71 for (i = 8; i < 16; ++i) {
72 kvm_irqchip_add_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_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
78 } else if (i != 2) {
79 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, i);
83 ret = kvm_irqchip_commit_routes(s);
84 if (ret < 0) {
85 hw_error("KVM IRQ routing setup failed");
88 #endif /* CONFIG_KVM */
91 static void kvm_piix3_gsi_handler(void *opaque, int n, int level)
93 GSIState *s = opaque;
95 if (n < ISA_NUM_IRQS) {
96 /* Kernel will forward to both PIC and IOAPIC */
97 qemu_set_irq(s->i8259_irq[n], level);
98 } else {
99 qemu_set_irq(s->ioapic_irq[n], level);
103 static void ioapic_init(GSIState *gsi_state)
105 DeviceState *dev;
106 SysBusDevice *d;
107 unsigned int i;
109 if (kvm_irqchip_in_kernel()) {
110 dev = qdev_create(NULL, "kvm-ioapic");
111 } else {
112 dev = qdev_create(NULL, "ioapic");
114 qdev_init_nofail(dev);
115 d = sysbus_from_qdev(dev);
116 sysbus_mmio_map(d, 0, 0xfec00000);
118 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
119 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
123 /* PC hardware initialisation */
124 static void pc_init1(MemoryRegion *system_memory,
125 MemoryRegion *system_io,
126 ram_addr_t ram_size,
127 const char *boot_device,
128 const char *kernel_filename,
129 const char *kernel_cmdline,
130 const char *initrd_filename,
131 const char *cpu_model,
132 int pci_enabled,
133 int kvmclock_enabled)
135 int i;
136 ram_addr_t below_4g_mem_size, above_4g_mem_size;
137 PCIBus *pci_bus;
138 ISABus *isa_bus;
139 PCII440FXState *i440fx_state;
140 int piix3_devfn = -1;
141 qemu_irq *cpu_irq;
142 qemu_irq *gsi;
143 qemu_irq *i8259;
144 qemu_irq *smi_irq;
145 GSIState *gsi_state;
146 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
147 BusState *idebus[MAX_IDE_BUS];
148 ISADevice *rtc_state;
149 ISADevice *floppy;
150 MemoryRegion *ram_memory;
151 MemoryRegion *pci_memory;
152 MemoryRegion *rom_memory;
153 DeviceState *dev;
155 global_cpu_model = cpu_model;
157 pc_cpus_init(cpu_model);
159 if (kvmclock_enabled) {
160 kvmclock_create();
163 if (ram_size >= 0xe0000000 ) {
164 above_4g_mem_size = ram_size - 0xe0000000;
165 below_4g_mem_size = 0xe0000000;
166 } else {
167 above_4g_mem_size = 0;
168 below_4g_mem_size = ram_size;
171 if (pci_enabled) {
172 pci_memory = g_new(MemoryRegion, 1);
173 memory_region_init(pci_memory, "pci", INT64_MAX);
174 rom_memory = pci_memory;
175 } else {
176 pci_memory = NULL;
177 rom_memory = system_memory;
180 /* allocate ram and load rom/bios */
181 if (!xen_enabled()) {
182 pc_memory_init(system_memory,
183 kernel_filename, kernel_cmdline, initrd_filename,
184 below_4g_mem_size, above_4g_mem_size,
185 pci_enabled ? rom_memory : system_memory, &ram_memory);
188 gsi_state = g_malloc0(sizeof(*gsi_state));
189 if (kvm_irqchip_in_kernel()) {
190 kvm_piix3_setup_irq_routing(pci_enabled);
191 gsi = qemu_allocate_irqs(kvm_piix3_gsi_handler, gsi_state,
192 GSI_NUM_PINS);
193 } else {
194 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
197 if (pci_enabled) {
198 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
199 system_memory, system_io, ram_size,
200 below_4g_mem_size,
201 0x100000000ULL - below_4g_mem_size,
202 0x100000000ULL + above_4g_mem_size,
203 (sizeof(target_phys_addr_t) == 4
205 : ((uint64_t)1 << 62)),
206 pci_memory, ram_memory);
207 } else {
208 pci_bus = NULL;
209 i440fx_state = NULL;
210 isa_bus = isa_bus_new(NULL, system_io);
211 no_hpet = 1;
213 isa_bus_irqs(isa_bus, gsi);
215 if (kvm_irqchip_in_kernel()) {
216 i8259 = kvm_i8259_init(isa_bus);
217 } else if (xen_enabled()) {
218 i8259 = xen_interrupt_controller_init();
219 } else {
220 cpu_irq = pc_allocate_cpu_irq();
221 i8259 = i8259_init(isa_bus, cpu_irq[0]);
224 for (i = 0; i < ISA_NUM_IRQS; i++) {
225 gsi_state->i8259_irq[i] = i8259[i];
227 if (pci_enabled) {
228 ioapic_init(gsi_state);
231 pc_register_ferr_irq(gsi[13]);
233 dev = pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
234 if (dev) {
235 object_property_add_child(object_get_root(), "vga", OBJECT(dev), NULL);
238 if (xen_enabled()) {
239 pci_create_simple(pci_bus, -1, "xen-platform");
242 /* init basic PC hardware */
243 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
245 for(i = 0; i < nb_nics; i++) {
246 NICInfo *nd = &nd_table[i];
248 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
249 pc_init_ne2k_isa(isa_bus, nd);
250 else
251 pci_nic_init_nofail(nd, "rtl8139", NULL);
254 ide_drive_get(hd, MAX_IDE_BUS);
255 if (pci_enabled) {
256 PCIDevice *dev;
257 if (xen_enabled()) {
258 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
259 } else {
260 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
262 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
263 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
265 /* FIXME there's some major spaghetti here. Somehow we create the
266 * devices on the PIIX before we actually create it. We create the
267 * PIIX3 deep in the recess of the i440fx creation too and then lose
268 * the DeviceState.
270 * For now, let's "fix" this by making judicious use of paths. This
271 * is not generally the right way to do this.
273 object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
274 "rtc", (Object *)rtc_state, NULL);
275 } else {
276 for(i = 0; i < MAX_IDE_BUS; i++) {
277 ISADevice *dev;
278 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
279 ide_irq[i],
280 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
281 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
285 audio_init(isa_bus, pci_enabled ? pci_bus : NULL);
287 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
288 floppy, idebus[0], idebus[1], rtc_state);
290 if (pci_enabled && usb_enabled) {
291 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
294 if (pci_enabled && acpi_enabled) {
295 i2c_bus *smbus;
297 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
298 /* TODO: Populate SPD eeprom data. */
299 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
300 gsi[9], *smi_irq,
301 kvm_enabled());
302 smbus_eeprom_init(smbus, 8, NULL, 0);
305 if (pci_enabled) {
306 pc_pci_device_init(pci_bus);
310 static void pc_init_pci(ram_addr_t ram_size,
311 const char *boot_device,
312 const char *kernel_filename,
313 const char *kernel_cmdline,
314 const char *initrd_filename,
315 const char *cpu_model)
317 pc_init1(get_system_memory(),
318 get_system_io(),
319 ram_size, boot_device,
320 kernel_filename, kernel_cmdline,
321 initrd_filename, cpu_model, 1, 1);
324 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size,
325 const char *boot_device,
326 const char *kernel_filename,
327 const char *kernel_cmdline,
328 const char *initrd_filename,
329 const char *cpu_model)
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, 1, 0);
338 static void pc_init_isa(ram_addr_t ram_size,
339 const char *boot_device,
340 const char *kernel_filename,
341 const char *kernel_cmdline,
342 const char *initrd_filename,
343 const char *cpu_model)
345 if (cpu_model == NULL)
346 cpu_model = "486";
347 pc_init1(get_system_memory(),
348 get_system_io(),
349 ram_size, boot_device,
350 kernel_filename, kernel_cmdline,
351 initrd_filename, cpu_model, 0, 1);
354 #ifdef CONFIG_XEN
355 static void pc_xen_hvm_init(ram_addr_t ram_size,
356 const char *boot_device,
357 const char *kernel_filename,
358 const char *kernel_cmdline,
359 const char *initrd_filename,
360 const char *cpu_model)
362 if (xen_hvm_init() != 0) {
363 hw_error("xen hardware virtual machine initialisation failed");
365 pc_init_pci_no_kvmclock(ram_size, boot_device,
366 kernel_filename, kernel_cmdline,
367 initrd_filename, cpu_model);
368 xen_vcpu_init();
370 #endif
372 static QEMUMachine pc_machine_v1_1 = {
373 .name = "pc-1.1",
374 .alias = "pc",
375 .desc = "Standard PC",
376 .init = pc_init_pci,
377 .max_cpus = 255,
378 .is_default = 1,
379 .default_machine_opts = "accel=kvm,kernel_irqchip=on",
382 static QEMUMachine pc_machine_v1_0 = {
383 .name = "pc-1.0",
384 .desc = "Standard PC",
385 .init = pc_init_pci,
386 .max_cpus = 255,
387 .compat_props = (GlobalProperty[]) {
389 .driver = "pc-sysfw",
390 .property = "rom_only",
391 .value = stringify(1),
392 }, {
393 .driver = "isa-fdc",
394 .property = "check_media_rate",
395 .value = "off",
397 { /* end of list */ }
401 static QEMUMachine pc_machine_v0_15 = {
402 .name = "pc-0.15",
403 .desc = "Standard PC",
404 .init = pc_init_pci,
405 .max_cpus = 255,
406 .compat_props = (GlobalProperty[]) {
408 .driver = "pc-sysfw",
409 .property = "rom_only",
410 .value = stringify(1),
411 }, {
412 .driver = "isa-fdc",
413 .property = "check_media_rate",
414 .value = "off",
416 { /* end of list */ }
420 static QEMUMachine pc_machine_v0_14 = {
421 .name = "pc-0.14",
422 .desc = "Standard PC",
423 .init = pc_init_pci,
424 .max_cpus = 255,
425 .default_machine_opts = "accel=kvm,kernel_irqchip=on",
426 .compat_props = (GlobalProperty[]) {
428 .driver = "qxl",
429 .property = "revision",
430 .value = stringify(2),
432 .driver = "qxl-vga",
433 .property = "revision",
434 .value = stringify(2),
436 .driver = "virtio-blk-pci",
437 .property = "event_idx",
438 .value = "off",
440 .driver = "virtio-serial-pci",
441 .property = "event_idx",
442 .value = "off",
444 .driver = "virtio-net-pci",
445 .property = "event_idx",
446 .value = "off",
448 .driver = "virtio-balloon-pci",
449 .property = "event_idx",
450 .value = "off",
452 .driver = "isa-fdc",
453 .property = "check_media_rate",
454 .value = "off",
457 .driver = "pc-sysfw",
458 .property = "rom_only",
459 .value = stringify(1),
461 { /* end of list */ }
465 static QEMUMachine pc_machine_v0_13 = {
466 .name = "pc-0.13",
467 .desc = "Standard PC",
468 .init = pc_init_pci_no_kvmclock,
469 .max_cpus = 255,
470 .default_machine_opts = "accel=kvm,kernel_irqchip=on",
471 .compat_props = (GlobalProperty[]) {
473 .driver = "virtio-9p-pci",
474 .property = "vectors",
475 .value = stringify(0),
477 .driver = "VGA",
478 .property = "rombar",
479 .value = stringify(0),
481 .driver = "vmware-svga",
482 .property = "rombar",
483 .value = stringify(0),
485 .driver = "PCI",
486 .property = "command_serr_enable",
487 .value = "off",
489 .driver = "virtio-blk-pci",
490 .property = "event_idx",
491 .value = "off",
493 .driver = "virtio-serial-pci",
494 .property = "event_idx",
495 .value = "off",
497 .driver = "virtio-net-pci",
498 .property = "event_idx",
499 .value = "off",
501 .driver = "virtio-balloon-pci",
502 .property = "event_idx",
503 .value = "off",
505 .driver = "AC97",
506 .property = "use_broken_id",
507 .value = stringify(1),
509 .driver = "isa-fdc",
510 .property = "check_media_rate",
511 .value = "off",
514 .driver = "pc-sysfw",
515 .property = "rom_only",
516 .value = stringify(1),
518 { /* end of list */ }
522 static QEMUMachine pc_machine_v0_12 = {
523 .name = "pc-0.12",
524 .desc = "Standard PC",
525 .init = pc_init_pci_no_kvmclock,
526 .max_cpus = 255,
527 .default_machine_opts = "accel=kvm,kernel_irqchip=on",
528 .compat_props = (GlobalProperty[]) {
530 .driver = "virtio-serial-pci",
531 .property = "max_ports",
532 .value = stringify(1),
534 .driver = "virtio-serial-pci",
535 .property = "vectors",
536 .value = stringify(0),
538 .driver = "VGA",
539 .property = "rombar",
540 .value = stringify(0),
542 .driver = "vmware-svga",
543 .property = "rombar",
544 .value = stringify(0),
546 .driver = "PCI",
547 .property = "command_serr_enable",
548 .value = "off",
550 .driver = "virtio-blk-pci",
551 .property = "event_idx",
552 .value = "off",
554 .driver = "virtio-serial-pci",
555 .property = "event_idx",
556 .value = "off",
558 .driver = "virtio-net-pci",
559 .property = "event_idx",
560 .value = "off",
562 .driver = "virtio-balloon-pci",
563 .property = "event_idx",
564 .value = "off",
566 .driver = "AC97",
567 .property = "use_broken_id",
568 .value = stringify(1),
570 .driver = "isa-fdc",
571 .property = "check_media_rate",
572 .value = "off",
575 .driver = "pc-sysfw",
576 .property = "rom_only",
577 .value = stringify(1),
579 { /* end of list */ }
583 static QEMUMachine pc_machine_v0_11 = {
584 .name = "pc-0.11",
585 .desc = "Standard PC, qemu 0.11",
586 .init = pc_init_pci_no_kvmclock,
587 .max_cpus = 255,
588 .default_machine_opts = "accel=kvm,kernel_irqchip=on",
589 .compat_props = (GlobalProperty[]) {
591 .driver = "virtio-blk-pci",
592 .property = "vectors",
593 .value = stringify(0),
595 .driver = "virtio-serial-pci",
596 .property = "max_ports",
597 .value = stringify(1),
599 .driver = "virtio-serial-pci",
600 .property = "vectors",
601 .value = stringify(0),
603 .driver = "ide-drive",
604 .property = "ver",
605 .value = "0.11",
607 .driver = "scsi-disk",
608 .property = "ver",
609 .value = "0.11",
611 .driver = "PCI",
612 .property = "rombar",
613 .value = stringify(0),
615 .driver = "PCI",
616 .property = "command_serr_enable",
617 .value = "off",
619 .driver = "virtio-blk-pci",
620 .property = "event_idx",
621 .value = "off",
623 .driver = "virtio-serial-pci",
624 .property = "event_idx",
625 .value = "off",
627 .driver = "virtio-net-pci",
628 .property = "event_idx",
629 .value = "off",
631 .driver = "virtio-balloon-pci",
632 .property = "event_idx",
633 .value = "off",
635 .driver = "AC97",
636 .property = "use_broken_id",
637 .value = stringify(1),
639 .driver = "isa-fdc",
640 .property = "check_media_rate",
641 .value = "off",
644 .driver = "pc-sysfw",
645 .property = "rom_only",
646 .value = stringify(1),
648 { /* end of list */ }
652 static QEMUMachine pc_machine_v0_10 = {
653 .name = "pc-0.10",
654 .desc = "Standard PC, qemu 0.10",
655 .init = pc_init_pci_no_kvmclock,
656 .max_cpus = 255,
657 .default_machine_opts = "accel=kvm,kernel_irqchip=on",
658 .compat_props = (GlobalProperty[]) {
660 .driver = "virtio-blk-pci",
661 .property = "class",
662 .value = stringify(PCI_CLASS_STORAGE_OTHER),
664 .driver = "virtio-serial-pci",
665 .property = "class",
666 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
668 .driver = "virtio-serial-pci",
669 .property = "max_ports",
670 .value = stringify(1),
672 .driver = "virtio-serial-pci",
673 .property = "vectors",
674 .value = stringify(0),
676 .driver = "virtio-net-pci",
677 .property = "vectors",
678 .value = stringify(0),
680 .driver = "virtio-blk-pci",
681 .property = "vectors",
682 .value = stringify(0),
684 .driver = "ide-drive",
685 .property = "ver",
686 .value = "0.10",
688 .driver = "scsi-disk",
689 .property = "ver",
690 .value = "0.10",
692 .driver = "PCI",
693 .property = "rombar",
694 .value = stringify(0),
696 .driver = "PCI",
697 .property = "command_serr_enable",
698 .value = "off",
700 .driver = "virtio-blk-pci",
701 .property = "event_idx",
702 .value = "off",
704 .driver = "virtio-serial-pci",
705 .property = "event_idx",
706 .value = "off",
708 .driver = "virtio-net-pci",
709 .property = "event_idx",
710 .value = "off",
712 .driver = "virtio-balloon-pci",
713 .property = "event_idx",
714 .value = "off",
716 .driver = "AC97",
717 .property = "use_broken_id",
718 .value = stringify(1),
720 .driver = "isa-fdc",
721 .property = "check_media_rate",
722 .value = "off",
725 .driver = "pc-sysfw",
726 .property = "rom_only",
727 .value = stringify(1),
729 { /* end of list */ }
733 static QEMUMachine isapc_machine = {
734 .name = "isapc",
735 .desc = "ISA-only PC",
736 .init = pc_init_isa,
737 .max_cpus = 1,
738 .default_machine_opts = "accel=kvm,kernel_irqchip=on",
739 .compat_props = (GlobalProperty[]) {
741 .driver = "pc-sysfw",
742 .property = "rom_only",
743 .value = stringify(1),
745 { /* end of list */ }
749 #ifdef CONFIG_XEN
750 static QEMUMachine xenfv_machine = {
751 .name = "xenfv",
752 .desc = "Xen Fully-virtualized PC",
753 .init = pc_xen_hvm_init,
754 .max_cpus = HVM_MAX_VCPUS,
755 .default_machine_opts = "accel=xen",
757 #endif
759 static void pc_machine_init(void)
761 qemu_register_machine(&pc_machine_v1_1);
762 qemu_register_machine(&pc_machine_v1_0);
763 qemu_register_machine(&pc_machine_v0_15);
764 qemu_register_machine(&pc_machine_v0_14);
765 qemu_register_machine(&pc_machine_v0_13);
766 qemu_register_machine(&pc_machine_v0_12);
767 qemu_register_machine(&pc_machine_v0_11);
768 qemu_register_machine(&pc_machine_v0_10);
769 qemu_register_machine(&isapc_machine);
770 #ifdef CONFIG_XEN
771 qemu_register_machine(&xenfv_machine);
772 #endif
775 machine_init(pc_machine_init);