qom: move properties from qdev to object
[qemu-kvm.git] / hw / pc_piix.c
blobc06f1b544ec769295c34366b42fe7cd9c694290a
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 static void kvm_piix3_setup_irq_routing(bool pci_enabled)
58 #ifdef CONFIG_KVM
59 KVMState *s = kvm_state;
60 int ret, i;
62 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
63 for (i = 0; i < 8; ++i) {
64 if (i == 2) {
65 continue;
67 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
69 for (i = 8; i < 16; ++i) {
70 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
72 if (pci_enabled) {
73 for (i = 0; i < 24; ++i) {
74 if (i == 0) {
75 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
76 } else if (i != 2) {
77 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, i);
81 ret = kvm_irqchip_commit_routes(s);
82 if (ret < 0) {
83 hw_error("KVM IRQ routing setup failed");
86 #endif /* CONFIG_KVM */
89 static void kvm_piix3_gsi_handler(void *opaque, int n, int level)
91 GSIState *s = opaque;
93 if (n < ISA_NUM_IRQS) {
94 /* Kernel will forward to both PIC and IOAPIC */
95 qemu_set_irq(s->i8259_irq[n], level);
96 } else {
97 qemu_set_irq(s->ioapic_irq[n], level);
101 static void ioapic_init(GSIState *gsi_state)
103 DeviceState *dev;
104 SysBusDevice *d;
105 unsigned int i;
107 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
108 dev = qdev_create(NULL, "kvm-ioapic");
109 } else {
110 dev = qdev_create(NULL, "ioapic");
112 qdev_init_nofail(dev);
113 d = sysbus_from_qdev(dev);
114 sysbus_mmio_map(d, 0, 0xfec00000);
116 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
117 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
121 /* PC hardware initialisation */
122 static void pc_init1(MemoryRegion *system_memory,
123 MemoryRegion *system_io,
124 ram_addr_t ram_size,
125 const char *boot_device,
126 const char *kernel_filename,
127 const char *kernel_cmdline,
128 const char *initrd_filename,
129 const char *cpu_model,
130 int pci_enabled,
131 int kvmclock_enabled)
133 int i;
134 ram_addr_t below_4g_mem_size, above_4g_mem_size;
135 PCIBus *pci_bus;
136 ISABus *isa_bus;
137 PCII440FXState *i440fx_state;
138 int piix3_devfn = -1;
139 qemu_irq *cpu_irq;
140 qemu_irq *gsi;
141 qemu_irq *i8259;
142 qemu_irq *cmos_s3;
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 DeviceState *dev;
154 pc_cpus_init(cpu_model);
156 if (kvmclock_enabled) {
157 kvmclock_create();
160 if (ram_size >= 0xe0000000 ) {
161 above_4g_mem_size = ram_size - 0xe0000000;
162 below_4g_mem_size = 0xe0000000;
163 } else {
164 above_4g_mem_size = 0;
165 below_4g_mem_size = ram_size;
168 if (pci_enabled) {
169 pci_memory = g_new(MemoryRegion, 1);
170 memory_region_init(pci_memory, "pci", INT64_MAX);
171 rom_memory = pci_memory;
172 } else {
173 pci_memory = NULL;
174 rom_memory = system_memory;
177 /* allocate ram and load rom/bios */
178 if (!xen_enabled()) {
179 pc_memory_init(system_memory,
180 kernel_filename, kernel_cmdline, initrd_filename,
181 below_4g_mem_size, above_4g_mem_size,
182 pci_enabled ? rom_memory : system_memory, &ram_memory);
185 gsi_state = g_malloc0(sizeof(*gsi_state));
186 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
187 kvm_piix3_setup_irq_routing(pci_enabled);
188 gsi = qemu_allocate_irqs(kvm_piix3_gsi_handler, gsi_state,
189 GSI_NUM_PINS);
190 } else {
191 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
194 if (pci_enabled) {
195 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
196 system_memory, system_io, ram_size,
197 below_4g_mem_size,
198 0x100000000ULL - below_4g_mem_size,
199 0x100000000ULL + above_4g_mem_size,
200 (sizeof(target_phys_addr_t) == 4
202 : ((uint64_t)1 << 62)),
203 pci_memory, ram_memory);
204 } else {
205 pci_bus = NULL;
206 i440fx_state = NULL;
207 isa_bus = isa_bus_new(NULL, system_io);
208 no_hpet = 1;
210 isa_bus_irqs(isa_bus, gsi);
212 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
213 i8259 = kvm_i8259_init(isa_bus);
214 } else if (xen_enabled()) {
215 i8259 = xen_interrupt_controller_init();
216 } else {
217 cpu_irq = pc_allocate_cpu_irq();
218 i8259 = i8259_init(isa_bus, cpu_irq[0]);
221 for (i = 0; i < ISA_NUM_IRQS; i++) {
222 gsi_state->i8259_irq[i] = i8259[i];
224 if (pci_enabled) {
225 ioapic_init(gsi_state);
228 pc_register_ferr_irq(gsi[13]);
230 dev = pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
231 if (dev) {
232 object_property_add_child(object_get_root(), "vga", OBJECT(dev), NULL);
235 if (xen_enabled()) {
236 pci_create_simple(pci_bus, -1, "xen-platform");
239 /* init basic PC hardware */
240 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
242 for(i = 0; i < nb_nics; i++) {
243 NICInfo *nd = &nd_table[i];
245 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
246 pc_init_ne2k_isa(isa_bus, nd);
247 else
248 pci_nic_init_nofail(nd, "e1000", NULL);
251 ide_drive_get(hd, MAX_IDE_BUS);
252 if (pci_enabled) {
253 PCIDevice *dev;
254 if (xen_enabled()) {
255 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
256 } else {
257 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
259 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
260 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
262 /* FIXME there's some major spaghetti here. Somehow we create the
263 * devices on the PIIX before we actually create it. We create the
264 * PIIX3 deep in the recess of the i440fx creation too and then lose
265 * the DeviceState.
267 * For now, let's "fix" this by making judicious use of paths. This
268 * is not generally the right way to do this.
270 object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
271 "rtc", (Object *)rtc_state, NULL);
272 } else {
273 for(i = 0; i < MAX_IDE_BUS; i++) {
274 ISADevice *dev;
275 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
276 ide_irq[i],
277 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
278 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
282 audio_init(isa_bus, pci_enabled ? pci_bus : NULL);
284 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
285 floppy, idebus[0], idebus[1], rtc_state);
287 if (pci_enabled && usb_enabled) {
288 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
291 if (pci_enabled && acpi_enabled) {
292 i2c_bus *smbus;
294 if (!xen_enabled()) {
295 cmos_s3 = qemu_allocate_irqs(pc_cmos_set_s3_resume, rtc_state, 1);
296 } else {
297 cmos_s3 = qemu_allocate_irqs(xen_cmos_set_s3_resume, rtc_state, 1);
299 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
300 /* TODO: Populate SPD eeprom data. */
301 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
302 gsi[9], *cmos_s3, *smi_irq,
303 kvm_enabled());
304 smbus_eeprom_init(smbus, 8, NULL, 0);
307 if (pci_enabled) {
308 pc_pci_device_init(pci_bus);
312 static void pc_init_pci(ram_addr_t ram_size,
313 const char *boot_device,
314 const char *kernel_filename,
315 const char *kernel_cmdline,
316 const char *initrd_filename,
317 const char *cpu_model)
319 pc_init1(get_system_memory(),
320 get_system_io(),
321 ram_size, boot_device,
322 kernel_filename, kernel_cmdline,
323 initrd_filename, cpu_model, 1, 1);
326 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size,
327 const char *boot_device,
328 const char *kernel_filename,
329 const char *kernel_cmdline,
330 const char *initrd_filename,
331 const char *cpu_model)
333 pc_init1(get_system_memory(),
334 get_system_io(),
335 ram_size, boot_device,
336 kernel_filename, kernel_cmdline,
337 initrd_filename, cpu_model, 1, 0);
340 static void pc_init_isa(ram_addr_t ram_size,
341 const char *boot_device,
342 const char *kernel_filename,
343 const char *kernel_cmdline,
344 const char *initrd_filename,
345 const char *cpu_model)
347 if (cpu_model == NULL)
348 cpu_model = "486";
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, 0, 1);
356 #ifdef CONFIG_XEN
357 static void pc_xen_hvm_init(ram_addr_t ram_size,
358 const char *boot_device,
359 const char *kernel_filename,
360 const char *kernel_cmdline,
361 const char *initrd_filename,
362 const char *cpu_model)
364 if (xen_hvm_init() != 0) {
365 hw_error("xen hardware virtual machine initialisation failed");
367 pc_init_pci_no_kvmclock(ram_size, boot_device,
368 kernel_filename, kernel_cmdline,
369 initrd_filename, cpu_model);
370 xen_vcpu_init();
372 #endif
374 static QEMUMachine pc_machine_v1_0 = {
375 .name = "pc-1.0",
376 .alias = "pc",
377 .desc = "Standard PC",
378 .init = pc_init_pci,
379 .max_cpus = 255,
380 .is_default = 1,
383 static QEMUMachine pc_machine_v0_15 = {
384 .name = "pc-0.15",
385 .desc = "Standard PC",
386 .init = pc_init_pci,
387 .max_cpus = 255,
388 .is_default = 1,
391 static QEMUMachine pc_machine_v0_14 = {
392 .name = "pc-0.14",
393 .desc = "Standard PC",
394 .init = pc_init_pci,
395 .max_cpus = 255,
396 .compat_props = (GlobalProperty[]) {
398 .driver = "qxl",
399 .property = "revision",
400 .value = stringify(2),
402 .driver = "qxl-vga",
403 .property = "revision",
404 .value = stringify(2),
406 .driver = "virtio-blk-pci",
407 .property = "event_idx",
408 .value = "off",
410 .driver = "virtio-serial-pci",
411 .property = "event_idx",
412 .value = "off",
414 .driver = "virtio-net-pci",
415 .property = "event_idx",
416 .value = "off",
418 .driver = "virtio-balloon-pci",
419 .property = "event_idx",
420 .value = "off",
422 { /* end of list */ }
426 static QEMUMachine pc_machine_v0_13 = {
427 .name = "pc-0.13",
428 .desc = "Standard PC",
429 .init = pc_init_pci_no_kvmclock,
430 .max_cpus = 255,
431 .compat_props = (GlobalProperty[]) {
433 .driver = "virtio-9p-pci",
434 .property = "vectors",
435 .value = stringify(0),
437 .driver = "VGA",
438 .property = "rombar",
439 .value = stringify(0),
441 .driver = "vmware-svga",
442 .property = "rombar",
443 .value = stringify(0),
445 .driver = "PCI",
446 .property = "command_serr_enable",
447 .value = "off",
449 .driver = "virtio-blk-pci",
450 .property = "event_idx",
451 .value = "off",
453 .driver = "virtio-serial-pci",
454 .property = "event_idx",
455 .value = "off",
457 .driver = "virtio-net-pci",
458 .property = "event_idx",
459 .value = "off",
461 .driver = "virtio-balloon-pci",
462 .property = "event_idx",
463 .value = "off",
465 .driver = "AC97",
466 .property = "use_broken_id",
467 .value = stringify(1),
469 { /* end of list */ }
473 static QEMUMachine pc_machine_v0_12 = {
474 .name = "pc-0.12",
475 .desc = "Standard PC",
476 .init = pc_init_pci_no_kvmclock,
477 .max_cpus = 255,
478 .compat_props = (GlobalProperty[]) {
480 .driver = "virtio-serial-pci",
481 .property = "max_ports",
482 .value = stringify(1),
484 .driver = "virtio-serial-pci",
485 .property = "vectors",
486 .value = stringify(0),
488 .driver = "VGA",
489 .property = "rombar",
490 .value = stringify(0),
492 .driver = "vmware-svga",
493 .property = "rombar",
494 .value = stringify(0),
496 .driver = "PCI",
497 .property = "command_serr_enable",
498 .value = "off",
500 .driver = "virtio-blk-pci",
501 .property = "event_idx",
502 .value = "off",
504 .driver = "virtio-serial-pci",
505 .property = "event_idx",
506 .value = "off",
508 .driver = "virtio-net-pci",
509 .property = "event_idx",
510 .value = "off",
512 .driver = "virtio-balloon-pci",
513 .property = "event_idx",
514 .value = "off",
516 .driver = "AC97",
517 .property = "use_broken_id",
518 .value = stringify(1),
520 { /* end of list */ }
524 static QEMUMachine pc_machine_v0_11 = {
525 .name = "pc-0.11",
526 .desc = "Standard PC, qemu 0.11",
527 .init = pc_init_pci_no_kvmclock,
528 .max_cpus = 255,
529 .compat_props = (GlobalProperty[]) {
531 .driver = "virtio-blk-pci",
532 .property = "vectors",
533 .value = stringify(0),
535 .driver = "virtio-serial-pci",
536 .property = "max_ports",
537 .value = stringify(1),
539 .driver = "virtio-serial-pci",
540 .property = "vectors",
541 .value = stringify(0),
543 .driver = "ide-drive",
544 .property = "ver",
545 .value = "0.11",
547 .driver = "scsi-disk",
548 .property = "ver",
549 .value = "0.11",
551 .driver = "PCI",
552 .property = "rombar",
553 .value = stringify(0),
555 .driver = "PCI",
556 .property = "command_serr_enable",
557 .value = "off",
559 .driver = "virtio-blk-pci",
560 .property = "event_idx",
561 .value = "off",
563 .driver = "virtio-serial-pci",
564 .property = "event_idx",
565 .value = "off",
567 .driver = "virtio-net-pci",
568 .property = "event_idx",
569 .value = "off",
571 .driver = "virtio-balloon-pci",
572 .property = "event_idx",
573 .value = "off",
575 .driver = "AC97",
576 .property = "use_broken_id",
577 .value = stringify(1),
579 { /* end of list */ }
583 static QEMUMachine pc_machine_v0_10 = {
584 .name = "pc-0.10",
585 .desc = "Standard PC, qemu 0.10",
586 .init = pc_init_pci_no_kvmclock,
587 .max_cpus = 255,
588 .compat_props = (GlobalProperty[]) {
590 .driver = "virtio-blk-pci",
591 .property = "class",
592 .value = stringify(PCI_CLASS_STORAGE_OTHER),
594 .driver = "virtio-serial-pci",
595 .property = "class",
596 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
598 .driver = "virtio-serial-pci",
599 .property = "max_ports",
600 .value = stringify(1),
602 .driver = "virtio-serial-pci",
603 .property = "vectors",
604 .value = stringify(0),
606 .driver = "virtio-net-pci",
607 .property = "vectors",
608 .value = stringify(0),
610 .driver = "virtio-blk-pci",
611 .property = "vectors",
612 .value = stringify(0),
614 .driver = "ide-drive",
615 .property = "ver",
616 .value = "0.10",
618 .driver = "scsi-disk",
619 .property = "ver",
620 .value = "0.10",
622 .driver = "PCI",
623 .property = "rombar",
624 .value = stringify(0),
626 .driver = "PCI",
627 .property = "command_serr_enable",
628 .value = "off",
630 .driver = "virtio-blk-pci",
631 .property = "event_idx",
632 .value = "off",
634 .driver = "virtio-serial-pci",
635 .property = "event_idx",
636 .value = "off",
638 .driver = "virtio-net-pci",
639 .property = "event_idx",
640 .value = "off",
642 .driver = "virtio-balloon-pci",
643 .property = "event_idx",
644 .value = "off",
646 .driver = "AC97",
647 .property = "use_broken_id",
648 .value = stringify(1),
650 { /* end of list */ }
654 static QEMUMachine isapc_machine = {
655 .name = "isapc",
656 .desc = "ISA-only PC",
657 .init = pc_init_isa,
658 .max_cpus = 1,
661 #ifdef CONFIG_XEN
662 static QEMUMachine xenfv_machine = {
663 .name = "xenfv",
664 .desc = "Xen Fully-virtualized PC",
665 .init = pc_xen_hvm_init,
666 .max_cpus = HVM_MAX_VCPUS,
667 .default_machine_opts = "accel=xen",
669 #endif
671 static void pc_machine_init(void)
673 qemu_register_machine(&pc_machine_v1_0);
674 qemu_register_machine(&pc_machine_v0_15);
675 qemu_register_machine(&pc_machine_v0_14);
676 qemu_register_machine(&pc_machine_v0_13);
677 qemu_register_machine(&pc_machine_v0_12);
678 qemu_register_machine(&pc_machine_v0_11);
679 qemu_register_machine(&pc_machine_v0_10);
680 qemu_register_machine(&isapc_machine);
681 #ifdef CONFIG_XEN
682 qemu_register_machine(&xenfv_machine);
683 #endif
686 machine_init(pc_machine_init);