Merge commit 'd34e8f6e9d3a396c3327aa9807c83f9e1f4a7bd7' into upstream-merge
[qemu-kvm.git] / hw / pc_piix.c
blob76f1fc7cb148f9c874b786e054096ceadbd850ba
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 qemu_irq *ioapic_irq_hack;
52 #define MAX_IDE_BUS 2
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)
63 #ifdef CONFIG_KVM
64 KVMState *s = kvm_state;
65 int ret, i;
67 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
68 for (i = 0; i < 8; ++i) {
69 if (i == 2) {
70 continue;
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);
77 if (pci_enabled) {
78 for (i = 0; i < 24; ++i) {
79 if (i == 0) {
80 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
81 } else if (i != 2) {
82 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, i);
86 ret = kvm_irqchip_commit_routes(s);
87 if (ret < 0) {
88 hw_error("KVM IRQ routing setup failed");
91 #endif /* CONFIG_KVM */
93 #endif
95 static void kvm_piix3_gsi_handler(void *opaque, int n, int level)
97 GSIState *s = opaque;
99 if (n < ISA_NUM_IRQS) {
100 /* Kernel will forward to both PIC and IOAPIC */
101 qemu_set_irq(s->i8259_irq[n], level);
102 } else {
103 qemu_set_irq(s->ioapic_irq[n], level);
107 static void ioapic_init(GSIState *gsi_state)
109 DeviceState *dev;
110 SysBusDevice *d;
111 unsigned int i;
113 #ifdef UNUSED_UPSTREAM_KVM
114 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
115 dev = qdev_create(NULL, "kvm-ioapic");
116 } else
117 #endif
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,
133 ram_addr_t ram_size,
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,
139 int pci_enabled,
140 int kvmclock_enabled)
142 int i;
143 ram_addr_t below_4g_mem_size, above_4g_mem_size;
144 PCIBus *pci_bus;
145 ISABus *isa_bus;
146 PCII440FXState *i440fx_state;
147 int piix3_devfn = -1;
148 qemu_irq *cpu_irq;
149 qemu_irq *gsi;
150 qemu_irq *i8259;
151 qemu_irq *cmos_s3;
152 qemu_irq *smi_irq;
153 GSIState *gsi_state;
154 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
155 BusState *idebus[MAX_IDE_BUS];
156 ISADevice *rtc_state;
157 ISADevice *floppy;
158 MemoryRegion *ram_memory;
159 MemoryRegion *pci_memory;
160 MemoryRegion *rom_memory;
161 DeviceState *dev;
163 global_cpu_model = cpu_model;
165 pc_cpus_init(cpu_model);
167 if (kvmclock_enabled) {
168 kvmclock_create();
171 if (ram_size >= 0xe0000000 ) {
172 above_4g_mem_size = ram_size - 0xe0000000;
173 below_4g_mem_size = 0xe0000000;
174 } else {
175 above_4g_mem_size = 0;
176 below_4g_mem_size = ram_size;
179 if (pci_enabled) {
180 pci_memory = g_new(MemoryRegion, 1);
181 memory_region_init(pci_memory, "pci", INT64_MAX);
182 rom_memory = pci_memory;
183 } else {
184 pci_memory = NULL;
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);
200 #endif
201 gsi = qemu_allocate_irqs(kvm_piix3_gsi_handler, gsi_state,
202 GSI_NUM_PINS);
203 } else {
204 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
207 if (pci_enabled) {
208 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
209 system_memory, system_io, ram_size,
210 below_4g_mem_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);
217 } else {
218 pci_bus = NULL;
219 i440fx_state = NULL;
220 isa_bus = isa_bus_new(NULL, system_io);
221 no_hpet = 1;
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);
228 } else
229 #endif
230 if (xen_enabled()) {
231 i8259 = xen_interrupt_controller_init();
232 } else {
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];
240 if (pci_enabled) {
241 ioapic_init(gsi_state);
244 pc_register_ferr_irq(gsi[13]);
246 dev = pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
247 if (dev) {
248 qdev_property_add_child(qdev_get_root(), "vga", dev, NULL);
251 if (xen_enabled()) {
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);
263 else
264 pci_nic_init_nofail(nd, "rtl8139", NULL);
267 ide_drive_get(hd, MAX_IDE_BUS);
268 if (pci_enabled) {
269 PCIDevice *dev;
270 if (xen_enabled()) {
271 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
272 } else {
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
281 * the DeviceState.
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);
288 } else {
289 for(i = 0; i < MAX_IDE_BUS; i++) {
290 ISADevice *dev;
291 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
292 ide_irq[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) {
308 i2c_bus *smbus;
310 if (!xen_enabled()) {
311 cmos_s3 = qemu_allocate_irqs(pc_cmos_set_s3_resume, rtc_state, 1);
312 } else {
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,
319 kvm_enabled());
320 smbus_eeprom_init(smbus, 8, NULL, 0);
323 if (pci_enabled) {
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(),
336 get_system_io(),
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(),
350 get_system_io(),
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)
364 cpu_model = "486";
365 pc_init1(get_system_memory(),
366 get_system_io(),
367 ram_size, boot_device,
368 kernel_filename, kernel_cmdline,
369 initrd_filename, cpu_model, 0, 1);
372 #ifdef CONFIG_XEN
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);
386 xen_vcpu_init();
388 #endif
390 static QEMUMachine pc_machine_v1_0 = {
391 .name = "pc-1.0",
392 .alias = "pc",
393 .desc = "Standard PC",
394 .init = pc_init_pci,
395 .max_cpus = 255,
396 .is_default = 1,
399 static QEMUMachine pc_machine_v0_15 = {
400 .name = "pc-0.15",
401 .desc = "Standard PC",
402 .init = pc_init_pci,
403 .max_cpus = 255,
404 .is_default = 1,
407 static QEMUMachine pc_machine_v0_14 = {
408 .name = "pc-0.14",
409 .desc = "Standard PC",
410 .init = pc_init_pci,
411 .max_cpus = 255,
412 .compat_props = (GlobalProperty[]) {
414 .driver = "qxl",
415 .property = "revision",
416 .value = stringify(2),
418 .driver = "qxl-vga",
419 .property = "revision",
420 .value = stringify(2),
422 .driver = "virtio-blk-pci",
423 .property = "event_idx",
424 .value = "off",
426 .driver = "virtio-serial-pci",
427 .property = "event_idx",
428 .value = "off",
430 .driver = "virtio-net-pci",
431 .property = "event_idx",
432 .value = "off",
434 .driver = "virtio-balloon-pci",
435 .property = "event_idx",
436 .value = "off",
438 { /* end of list */ }
442 static QEMUMachine pc_machine_v0_13 = {
443 .name = "pc-0.13",
444 .desc = "Standard PC",
445 .init = pc_init_pci_no_kvmclock,
446 .max_cpus = 255,
447 .compat_props = (GlobalProperty[]) {
449 .driver = "virtio-9p-pci",
450 .property = "vectors",
451 .value = stringify(0),
453 .driver = "VGA",
454 .property = "rombar",
455 .value = stringify(0),
457 .driver = "vmware-svga",
458 .property = "rombar",
459 .value = stringify(0),
461 .driver = "PCI",
462 .property = "command_serr_enable",
463 .value = "off",
465 .driver = "virtio-blk-pci",
466 .property = "event_idx",
467 .value = "off",
469 .driver = "virtio-serial-pci",
470 .property = "event_idx",
471 .value = "off",
473 .driver = "virtio-net-pci",
474 .property = "event_idx",
475 .value = "off",
477 .driver = "virtio-balloon-pci",
478 .property = "event_idx",
479 .value = "off",
481 .driver = "AC97",
482 .property = "use_broken_id",
483 .value = stringify(1),
485 { /* end of list */ }
489 static QEMUMachine pc_machine_v0_12 = {
490 .name = "pc-0.12",
491 .desc = "Standard PC",
492 .init = pc_init_pci_no_kvmclock,
493 .max_cpus = 255,
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),
504 .driver = "VGA",
505 .property = "rombar",
506 .value = stringify(0),
508 .driver = "vmware-svga",
509 .property = "rombar",
510 .value = stringify(0),
512 .driver = "PCI",
513 .property = "command_serr_enable",
514 .value = "off",
516 .driver = "virtio-blk-pci",
517 .property = "event_idx",
518 .value = "off",
520 .driver = "virtio-serial-pci",
521 .property = "event_idx",
522 .value = "off",
524 .driver = "virtio-net-pci",
525 .property = "event_idx",
526 .value = "off",
528 .driver = "virtio-balloon-pci",
529 .property = "event_idx",
530 .value = "off",
532 .driver = "AC97",
533 .property = "use_broken_id",
534 .value = stringify(1),
536 { /* end of list */ }
540 static QEMUMachine pc_machine_v0_11 = {
541 .name = "pc-0.11",
542 .desc = "Standard PC, qemu 0.11",
543 .init = pc_init_pci_no_kvmclock,
544 .max_cpus = 255,
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",
560 .property = "ver",
561 .value = "0.11",
563 .driver = "scsi-disk",
564 .property = "ver",
565 .value = "0.11",
567 .driver = "PCI",
568 .property = "rombar",
569 .value = stringify(0),
571 .driver = "PCI",
572 .property = "command_serr_enable",
573 .value = "off",
575 .driver = "virtio-blk-pci",
576 .property = "event_idx",
577 .value = "off",
579 .driver = "virtio-serial-pci",
580 .property = "event_idx",
581 .value = "off",
583 .driver = "virtio-net-pci",
584 .property = "event_idx",
585 .value = "off",
587 .driver = "virtio-balloon-pci",
588 .property = "event_idx",
589 .value = "off",
591 .driver = "AC97",
592 .property = "use_broken_id",
593 .value = stringify(1),
595 { /* end of list */ }
599 static QEMUMachine pc_machine_v0_10 = {
600 .name = "pc-0.10",
601 .desc = "Standard PC, qemu 0.10",
602 .init = pc_init_pci_no_kvmclock,
603 .max_cpus = 255,
604 .compat_props = (GlobalProperty[]) {
606 .driver = "virtio-blk-pci",
607 .property = "class",
608 .value = stringify(PCI_CLASS_STORAGE_OTHER),
610 .driver = "virtio-serial-pci",
611 .property = "class",
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",
631 .property = "ver",
632 .value = "0.10",
634 .driver = "scsi-disk",
635 .property = "ver",
636 .value = "0.10",
638 .driver = "PCI",
639 .property = "rombar",
640 .value = stringify(0),
642 .driver = "PCI",
643 .property = "command_serr_enable",
644 .value = "off",
646 .driver = "virtio-blk-pci",
647 .property = "event_idx",
648 .value = "off",
650 .driver = "virtio-serial-pci",
651 .property = "event_idx",
652 .value = "off",
654 .driver = "virtio-net-pci",
655 .property = "event_idx",
656 .value = "off",
658 .driver = "virtio-balloon-pci",
659 .property = "event_idx",
660 .value = "off",
662 .driver = "AC97",
663 .property = "use_broken_id",
664 .value = stringify(1),
666 { /* end of list */ }
670 static QEMUMachine isapc_machine = {
671 .name = "isapc",
672 .desc = "ISA-only PC",
673 .init = pc_init_isa,
674 .max_cpus = 1,
677 #ifdef CONFIG_XEN
678 static QEMUMachine xenfv_machine = {
679 .name = "xenfv",
680 .desc = "Xen Fully-virtualized PC",
681 .init = pc_xen_hvm_init,
682 .max_cpus = HVM_MAX_VCPUS,
683 .default_machine_opts = "accel=xen",
685 #endif
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);
697 #ifdef CONFIG_XEN
698 qemu_register_machine(&xenfv_machine);
699 #endif
702 machine_init(pc_machine_init);