pci: Do not check if a bus exist in pci_parse_devaddr.
[qemu.git] / hw / pc_piix.c
blob3f99f9a7c2e6baeb2cdbb61324c260696a1f0747
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 "net.h"
32 #include "boards.h"
33 #include "ide.h"
34 #include "kvm.h"
35 #include "kvm/clock.h"
36 #include "sysemu.h"
37 #include "sysbus.h"
38 #include "arch_init.h"
39 #include "blockdev.h"
40 #include "smbus.h"
41 #include "xen.h"
42 #include "memory.h"
43 #include "exec-memory.h"
44 #ifdef CONFIG_XEN
45 # include <xen/hvm/hvm_info_table.h>
46 #endif
48 #define MAX_IDE_BUS 2
50 static const int ide_iobase[MAX_IDE_BUS] = { 0x1f0, 0x170 };
51 static const int ide_iobase2[MAX_IDE_BUS] = { 0x3f6, 0x376 };
52 static const int ide_irq[MAX_IDE_BUS] = { 14, 15 };
54 static void kvm_piix3_setup_irq_routing(bool pci_enabled)
56 #ifdef CONFIG_KVM
57 KVMState *s = kvm_state;
58 int ret, i;
60 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
61 for (i = 0; i < 8; ++i) {
62 if (i == 2) {
63 continue;
65 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
67 for (i = 8; i < 16; ++i) {
68 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
70 if (pci_enabled) {
71 for (i = 0; i < 24; ++i) {
72 if (i == 0) {
73 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
74 } else if (i != 2) {
75 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, i);
79 ret = kvm_irqchip_commit_routes(s);
80 if (ret < 0) {
81 hw_error("KVM IRQ routing setup failed");
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 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,
122 ram_addr_t ram_size,
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,
128 int pci_enabled,
129 int kvmclock_enabled)
131 int i;
132 ram_addr_t below_4g_mem_size, above_4g_mem_size;
133 PCIBus *pci_bus;
134 ISABus *isa_bus;
135 PCII440FXState *i440fx_state;
136 int piix3_devfn = -1;
137 qemu_irq *cpu_irq;
138 qemu_irq *gsi;
139 qemu_irq *i8259;
140 qemu_irq *smi_irq;
141 GSIState *gsi_state;
142 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
143 BusState *idebus[MAX_IDE_BUS];
144 ISADevice *rtc_state;
145 ISADevice *floppy;
146 MemoryRegion *ram_memory;
147 MemoryRegion *pci_memory;
148 MemoryRegion *rom_memory;
149 DeviceState *dev;
151 pc_cpus_init(cpu_model);
153 if (kvmclock_enabled) {
154 kvmclock_create();
157 if (ram_size >= 0xe0000000 ) {
158 above_4g_mem_size = ram_size - 0xe0000000;
159 below_4g_mem_size = 0xe0000000;
160 } else {
161 above_4g_mem_size = 0;
162 below_4g_mem_size = ram_size;
165 if (pci_enabled) {
166 pci_memory = g_new(MemoryRegion, 1);
167 memory_region_init(pci_memory, "pci", INT64_MAX);
168 rom_memory = pci_memory;
169 } else {
170 pci_memory = NULL;
171 rom_memory = system_memory;
174 /* allocate ram and load rom/bios */
175 if (!xen_enabled()) {
176 pc_memory_init(system_memory,
177 kernel_filename, kernel_cmdline, initrd_filename,
178 below_4g_mem_size, above_4g_mem_size,
179 pci_enabled ? rom_memory : system_memory, &ram_memory);
182 gsi_state = g_malloc0(sizeof(*gsi_state));
183 if (kvm_irqchip_in_kernel()) {
184 kvm_piix3_setup_irq_routing(pci_enabled);
185 gsi = qemu_allocate_irqs(kvm_piix3_gsi_handler, gsi_state,
186 GSI_NUM_PINS);
187 } else {
188 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
191 if (pci_enabled) {
192 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
193 system_memory, system_io, ram_size,
194 below_4g_mem_size,
195 0x100000000ULL - below_4g_mem_size,
196 0x100000000ULL + above_4g_mem_size,
197 (sizeof(target_phys_addr_t) == 4
199 : ((uint64_t)1 << 62)),
200 pci_memory, ram_memory);
201 } else {
202 pci_bus = NULL;
203 i440fx_state = NULL;
204 isa_bus = isa_bus_new(NULL, system_io);
205 no_hpet = 1;
207 isa_bus_irqs(isa_bus, gsi);
209 if (kvm_irqchip_in_kernel()) {
210 i8259 = kvm_i8259_init(isa_bus);
211 } else if (xen_enabled()) {
212 i8259 = xen_interrupt_controller_init();
213 } else {
214 cpu_irq = pc_allocate_cpu_irq();
215 i8259 = i8259_init(isa_bus, cpu_irq[0]);
218 for (i = 0; i < ISA_NUM_IRQS; i++) {
219 gsi_state->i8259_irq[i] = i8259[i];
221 if (pci_enabled) {
222 ioapic_init(gsi_state);
225 pc_register_ferr_irq(gsi[13]);
227 dev = pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
228 if (dev) {
229 object_property_add_child(object_get_root(), "vga", OBJECT(dev), NULL);
232 if (xen_enabled()) {
233 pci_create_simple(pci_bus, -1, "xen-platform");
236 /* init basic PC hardware */
237 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
239 for(i = 0; i < nb_nics; i++) {
240 NICInfo *nd = &nd_table[i];
242 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
243 pc_init_ne2k_isa(isa_bus, nd);
244 else
245 pci_nic_init_nofail(nd, "e1000", NULL);
248 ide_drive_get(hd, MAX_IDE_BUS);
249 if (pci_enabled) {
250 PCIDevice *dev;
251 if (xen_enabled()) {
252 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
253 } else {
254 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
256 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
257 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
259 /* FIXME there's some major spaghetti here. Somehow we create the
260 * devices on the PIIX before we actually create it. We create the
261 * PIIX3 deep in the recess of the i440fx creation too and then lose
262 * the DeviceState.
264 * For now, let's "fix" this by making judicious use of paths. This
265 * is not generally the right way to do this.
267 object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
268 "rtc", (Object *)rtc_state, NULL);
269 } else {
270 for(i = 0; i < MAX_IDE_BUS; i++) {
271 ISADevice *dev;
272 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
273 ide_irq[i],
274 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
275 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
279 audio_init(isa_bus, pci_enabled ? pci_bus : NULL);
281 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
282 floppy, idebus[0], idebus[1], rtc_state);
284 if (pci_enabled && usb_enabled) {
285 pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
288 if (pci_enabled && acpi_enabled) {
289 i2c_bus *smbus;
291 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
292 /* TODO: Populate SPD eeprom data. */
293 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
294 gsi[9], *smi_irq,
295 kvm_enabled());
296 smbus_eeprom_init(smbus, 8, NULL, 0);
299 if (pci_enabled) {
300 pc_pci_device_init(pci_bus);
304 static void pc_init_pci(ram_addr_t ram_size,
305 const char *boot_device,
306 const char *kernel_filename,
307 const char *kernel_cmdline,
308 const char *initrd_filename,
309 const char *cpu_model)
311 pc_init1(get_system_memory(),
312 get_system_io(),
313 ram_size, boot_device,
314 kernel_filename, kernel_cmdline,
315 initrd_filename, cpu_model, 1, 1);
318 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size,
319 const char *boot_device,
320 const char *kernel_filename,
321 const char *kernel_cmdline,
322 const char *initrd_filename,
323 const char *cpu_model)
325 pc_init1(get_system_memory(),
326 get_system_io(),
327 ram_size, boot_device,
328 kernel_filename, kernel_cmdline,
329 initrd_filename, cpu_model, 1, 0);
332 static void pc_init_isa(ram_addr_t ram_size,
333 const char *boot_device,
334 const char *kernel_filename,
335 const char *kernel_cmdline,
336 const char *initrd_filename,
337 const char *cpu_model)
339 if (cpu_model == NULL)
340 cpu_model = "486";
341 pc_init1(get_system_memory(),
342 get_system_io(),
343 ram_size, boot_device,
344 kernel_filename, kernel_cmdline,
345 initrd_filename, cpu_model, 0, 1);
348 #ifdef CONFIG_XEN
349 static void pc_xen_hvm_init(ram_addr_t ram_size,
350 const char *boot_device,
351 const char *kernel_filename,
352 const char *kernel_cmdline,
353 const char *initrd_filename,
354 const char *cpu_model)
356 if (xen_hvm_init() != 0) {
357 hw_error("xen hardware virtual machine initialisation failed");
359 pc_init_pci_no_kvmclock(ram_size, boot_device,
360 kernel_filename, kernel_cmdline,
361 initrd_filename, cpu_model);
362 xen_vcpu_init();
364 #endif
366 static QEMUMachine pc_machine_v1_1 = {
367 .name = "pc-1.1",
368 .alias = "pc",
369 .desc = "Standard PC",
370 .init = pc_init_pci,
371 .max_cpus = 255,
372 .is_default = 1,
375 static QEMUMachine pc_machine_v1_0 = {
376 .name = "pc-1.0",
377 .desc = "Standard PC",
378 .init = pc_init_pci,
379 .max_cpus = 255,
380 .compat_props = (GlobalProperty[]) {
382 .driver = "pc-sysfw",
383 .property = "rom_only",
384 .value = stringify(1),
385 }, {
386 .driver = "isa-fdc",
387 .property = "check_media_rate",
388 .value = "off",
390 { /* end of list */ }
394 static QEMUMachine pc_machine_v0_15 = {
395 .name = "pc-0.15",
396 .desc = "Standard PC",
397 .init = pc_init_pci,
398 .max_cpus = 255,
399 .compat_props = (GlobalProperty[]) {
401 .driver = "pc-sysfw",
402 .property = "rom_only",
403 .value = stringify(1),
404 }, {
405 .driver = "isa-fdc",
406 .property = "check_media_rate",
407 .value = "off",
409 { /* end of list */ }
413 static QEMUMachine pc_machine_v0_14 = {
414 .name = "pc-0.14",
415 .desc = "Standard PC",
416 .init = pc_init_pci,
417 .max_cpus = 255,
418 .compat_props = (GlobalProperty[]) {
420 .driver = "qxl",
421 .property = "revision",
422 .value = stringify(2),
424 .driver = "qxl-vga",
425 .property = "revision",
426 .value = stringify(2),
428 .driver = "virtio-blk-pci",
429 .property = "event_idx",
430 .value = "off",
432 .driver = "virtio-serial-pci",
433 .property = "event_idx",
434 .value = "off",
436 .driver = "virtio-net-pci",
437 .property = "event_idx",
438 .value = "off",
440 .driver = "virtio-balloon-pci",
441 .property = "event_idx",
442 .value = "off",
444 .driver = "isa-fdc",
445 .property = "check_media_rate",
446 .value = "off",
449 .driver = "pc-sysfw",
450 .property = "rom_only",
451 .value = stringify(1),
453 { /* end of list */ }
457 static QEMUMachine pc_machine_v0_13 = {
458 .name = "pc-0.13",
459 .desc = "Standard PC",
460 .init = pc_init_pci_no_kvmclock,
461 .max_cpus = 255,
462 .compat_props = (GlobalProperty[]) {
464 .driver = "virtio-9p-pci",
465 .property = "vectors",
466 .value = stringify(0),
468 .driver = "VGA",
469 .property = "rombar",
470 .value = stringify(0),
472 .driver = "vmware-svga",
473 .property = "rombar",
474 .value = stringify(0),
476 .driver = "PCI",
477 .property = "command_serr_enable",
478 .value = "off",
480 .driver = "virtio-blk-pci",
481 .property = "event_idx",
482 .value = "off",
484 .driver = "virtio-serial-pci",
485 .property = "event_idx",
486 .value = "off",
488 .driver = "virtio-net-pci",
489 .property = "event_idx",
490 .value = "off",
492 .driver = "virtio-balloon-pci",
493 .property = "event_idx",
494 .value = "off",
496 .driver = "AC97",
497 .property = "use_broken_id",
498 .value = stringify(1),
500 .driver = "isa-fdc",
501 .property = "check_media_rate",
502 .value = "off",
505 .driver = "pc-sysfw",
506 .property = "rom_only",
507 .value = stringify(1),
509 { /* end of list */ }
513 static QEMUMachine pc_machine_v0_12 = {
514 .name = "pc-0.12",
515 .desc = "Standard PC",
516 .init = pc_init_pci_no_kvmclock,
517 .max_cpus = 255,
518 .compat_props = (GlobalProperty[]) {
520 .driver = "virtio-serial-pci",
521 .property = "max_ports",
522 .value = stringify(1),
524 .driver = "virtio-serial-pci",
525 .property = "vectors",
526 .value = stringify(0),
528 .driver = "VGA",
529 .property = "rombar",
530 .value = stringify(0),
532 .driver = "vmware-svga",
533 .property = "rombar",
534 .value = stringify(0),
536 .driver = "PCI",
537 .property = "command_serr_enable",
538 .value = "off",
540 .driver = "virtio-blk-pci",
541 .property = "event_idx",
542 .value = "off",
544 .driver = "virtio-serial-pci",
545 .property = "event_idx",
546 .value = "off",
548 .driver = "virtio-net-pci",
549 .property = "event_idx",
550 .value = "off",
552 .driver = "virtio-balloon-pci",
553 .property = "event_idx",
554 .value = "off",
556 .driver = "AC97",
557 .property = "use_broken_id",
558 .value = stringify(1),
560 .driver = "isa-fdc",
561 .property = "check_media_rate",
562 .value = "off",
565 .driver = "pc-sysfw",
566 .property = "rom_only",
567 .value = stringify(1),
569 { /* end of list */ }
573 static QEMUMachine pc_machine_v0_11 = {
574 .name = "pc-0.11",
575 .desc = "Standard PC, qemu 0.11",
576 .init = pc_init_pci_no_kvmclock,
577 .max_cpus = 255,
578 .compat_props = (GlobalProperty[]) {
580 .driver = "virtio-blk-pci",
581 .property = "vectors",
582 .value = stringify(0),
584 .driver = "virtio-serial-pci",
585 .property = "max_ports",
586 .value = stringify(1),
588 .driver = "virtio-serial-pci",
589 .property = "vectors",
590 .value = stringify(0),
592 .driver = "ide-drive",
593 .property = "ver",
594 .value = "0.11",
596 .driver = "scsi-disk",
597 .property = "ver",
598 .value = "0.11",
600 .driver = "PCI",
601 .property = "rombar",
602 .value = stringify(0),
604 .driver = "PCI",
605 .property = "command_serr_enable",
606 .value = "off",
608 .driver = "virtio-blk-pci",
609 .property = "event_idx",
610 .value = "off",
612 .driver = "virtio-serial-pci",
613 .property = "event_idx",
614 .value = "off",
616 .driver = "virtio-net-pci",
617 .property = "event_idx",
618 .value = "off",
620 .driver = "virtio-balloon-pci",
621 .property = "event_idx",
622 .value = "off",
624 .driver = "AC97",
625 .property = "use_broken_id",
626 .value = stringify(1),
628 .driver = "isa-fdc",
629 .property = "check_media_rate",
630 .value = "off",
633 .driver = "pc-sysfw",
634 .property = "rom_only",
635 .value = stringify(1),
637 { /* end of list */ }
641 static QEMUMachine pc_machine_v0_10 = {
642 .name = "pc-0.10",
643 .desc = "Standard PC, qemu 0.10",
644 .init = pc_init_pci_no_kvmclock,
645 .max_cpus = 255,
646 .compat_props = (GlobalProperty[]) {
648 .driver = "virtio-blk-pci",
649 .property = "class",
650 .value = stringify(PCI_CLASS_STORAGE_OTHER),
652 .driver = "virtio-serial-pci",
653 .property = "class",
654 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
656 .driver = "virtio-serial-pci",
657 .property = "max_ports",
658 .value = stringify(1),
660 .driver = "virtio-serial-pci",
661 .property = "vectors",
662 .value = stringify(0),
664 .driver = "virtio-net-pci",
665 .property = "vectors",
666 .value = stringify(0),
668 .driver = "virtio-blk-pci",
669 .property = "vectors",
670 .value = stringify(0),
672 .driver = "ide-drive",
673 .property = "ver",
674 .value = "0.10",
676 .driver = "scsi-disk",
677 .property = "ver",
678 .value = "0.10",
680 .driver = "PCI",
681 .property = "rombar",
682 .value = stringify(0),
684 .driver = "PCI",
685 .property = "command_serr_enable",
686 .value = "off",
688 .driver = "virtio-blk-pci",
689 .property = "event_idx",
690 .value = "off",
692 .driver = "virtio-serial-pci",
693 .property = "event_idx",
694 .value = "off",
696 .driver = "virtio-net-pci",
697 .property = "event_idx",
698 .value = "off",
700 .driver = "virtio-balloon-pci",
701 .property = "event_idx",
702 .value = "off",
704 .driver = "AC97",
705 .property = "use_broken_id",
706 .value = stringify(1),
708 .driver = "isa-fdc",
709 .property = "check_media_rate",
710 .value = "off",
713 .driver = "pc-sysfw",
714 .property = "rom_only",
715 .value = stringify(1),
717 { /* end of list */ }
721 static QEMUMachine isapc_machine = {
722 .name = "isapc",
723 .desc = "ISA-only PC",
724 .init = pc_init_isa,
725 .max_cpus = 1,
726 .compat_props = (GlobalProperty[]) {
728 .driver = "pc-sysfw",
729 .property = "rom_only",
730 .value = stringify(1),
732 { /* end of list */ }
736 #ifdef CONFIG_XEN
737 static QEMUMachine xenfv_machine = {
738 .name = "xenfv",
739 .desc = "Xen Fully-virtualized PC",
740 .init = pc_xen_hvm_init,
741 .max_cpus = HVM_MAX_VCPUS,
742 .default_machine_opts = "accel=xen",
744 #endif
746 static void pc_machine_init(void)
748 qemu_register_machine(&pc_machine_v1_1);
749 qemu_register_machine(&pc_machine_v1_0);
750 qemu_register_machine(&pc_machine_v0_15);
751 qemu_register_machine(&pc_machine_v0_14);
752 qemu_register_machine(&pc_machine_v0_13);
753 qemu_register_machine(&pc_machine_v0_12);
754 qemu_register_machine(&pc_machine_v0_11);
755 qemu_register_machine(&pc_machine_v0_10);
756 qemu_register_machine(&isapc_machine);
757 #ifdef CONFIG_XEN
758 qemu_register_machine(&xenfv_machine);
759 #endif
762 machine_init(pc_machine_init);