prep: qdev'ify Raven host bridge (PCIDevice)
[qemu/kevin.git] / hw / pc_piix.c
blob3aea3cc9de9b193d9c14e0a9c521b00a6385006e
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 "kvmclock.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 ioapic_init(GSIState *gsi_state)
58 DeviceState *dev;
59 SysBusDevice *d;
60 unsigned int i;
62 dev = qdev_create(NULL, "ioapic");
63 qdev_init_nofail(dev);
64 d = sysbus_from_qdev(dev);
65 sysbus_mmio_map(d, 0, 0xfec00000);
67 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
68 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
72 /* PC hardware initialisation */
73 static void pc_init1(MemoryRegion *system_memory,
74 MemoryRegion *system_io,
75 ram_addr_t ram_size,
76 const char *boot_device,
77 const char *kernel_filename,
78 const char *kernel_cmdline,
79 const char *initrd_filename,
80 const char *cpu_model,
81 int pci_enabled,
82 int kvmclock_enabled)
84 int i;
85 ram_addr_t below_4g_mem_size, above_4g_mem_size;
86 PCIBus *pci_bus;
87 ISABus *isa_bus;
88 PCII440FXState *i440fx_state;
89 int piix3_devfn = -1;
90 qemu_irq *cpu_irq;
91 qemu_irq *gsi;
92 qemu_irq *i8259;
93 qemu_irq *cmos_s3;
94 qemu_irq *smi_irq;
95 GSIState *gsi_state;
96 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
97 BusState *idebus[MAX_IDE_BUS];
98 ISADevice *rtc_state;
99 ISADevice *floppy;
100 MemoryRegion *ram_memory;
101 MemoryRegion *pci_memory;
102 MemoryRegion *rom_memory;
103 DeviceState *dev;
105 pc_cpus_init(cpu_model);
107 if (kvmclock_enabled) {
108 kvmclock_create();
111 if (ram_size >= 0xe0000000 ) {
112 above_4g_mem_size = ram_size - 0xe0000000;
113 below_4g_mem_size = 0xe0000000;
114 } else {
115 above_4g_mem_size = 0;
116 below_4g_mem_size = ram_size;
119 if (pci_enabled) {
120 pci_memory = g_new(MemoryRegion, 1);
121 memory_region_init(pci_memory, "pci", INT64_MAX);
122 rom_memory = pci_memory;
123 } else {
124 pci_memory = NULL;
125 rom_memory = system_memory;
128 /* allocate ram and load rom/bios */
129 if (!xen_enabled()) {
130 pc_memory_init(system_memory,
131 kernel_filename, kernel_cmdline, initrd_filename,
132 below_4g_mem_size, above_4g_mem_size,
133 pci_enabled ? rom_memory : system_memory, &ram_memory);
136 gsi_state = g_malloc0(sizeof(*gsi_state));
137 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
139 if (pci_enabled) {
140 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
141 system_memory, system_io, ram_size,
142 below_4g_mem_size,
143 0x100000000ULL - below_4g_mem_size,
144 0x100000000ULL + above_4g_mem_size,
145 (sizeof(target_phys_addr_t) == 4
147 : ((uint64_t)1 << 62)),
148 pci_memory, ram_memory);
149 } else {
150 pci_bus = NULL;
151 i440fx_state = NULL;
152 isa_bus = isa_bus_new(NULL, system_io);
153 no_hpet = 1;
155 isa_bus_irqs(isa_bus, gsi);
157 if (!xen_enabled()) {
158 cpu_irq = pc_allocate_cpu_irq();
159 i8259 = i8259_init(isa_bus, cpu_irq[0]);
160 } else {
161 i8259 = xen_interrupt_controller_init();
164 for (i = 0; i < ISA_NUM_IRQS; i++) {
165 gsi_state->i8259_irq[i] = i8259[i];
167 if (pci_enabled) {
168 ioapic_init(gsi_state);
171 pc_register_ferr_irq(gsi[13]);
173 dev = pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
174 if (dev) {
175 qdev_property_add_child(qdev_get_root(), "vga", dev, NULL);
178 if (xen_enabled()) {
179 pci_create_simple(pci_bus, -1, "xen-platform");
182 /* init basic PC hardware */
183 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
185 for(i = 0; i < nb_nics; i++) {
186 NICInfo *nd = &nd_table[i];
188 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
189 pc_init_ne2k_isa(isa_bus, nd);
190 else
191 pci_nic_init_nofail(nd, "e1000", NULL);
194 ide_drive_get(hd, MAX_IDE_BUS);
195 if (pci_enabled) {
196 PCIDevice *dev;
197 if (xen_enabled()) {
198 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
199 } else {
200 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
202 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
203 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
205 /* FIXME there's some major spaghetti here. Somehow we create the
206 * devices on the PIIX before we actually create it. We create the
207 * PIIX3 deep in the recess of the i440fx creation too and then lose
208 * the DeviceState.
210 * For now, let's "fix" this by making judicious use of paths. This
211 * is not generally the right way to do this.
213 qdev_property_add_child(qdev_resolve_path("/i440fx/piix3", NULL),
214 "rtc", (DeviceState *)rtc_state, NULL);
215 } else {
216 for(i = 0; i < MAX_IDE_BUS; i++) {
217 ISADevice *dev;
218 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
219 ide_irq[i],
220 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
221 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
225 audio_init(isa_bus, pci_enabled ? pci_bus : NULL);
227 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
228 floppy, idebus[0], idebus[1], rtc_state);
230 if (pci_enabled && usb_enabled) {
231 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
234 if (pci_enabled && acpi_enabled) {
235 i2c_bus *smbus;
237 if (!xen_enabled()) {
238 cmos_s3 = qemu_allocate_irqs(pc_cmos_set_s3_resume, rtc_state, 1);
239 } else {
240 cmos_s3 = qemu_allocate_irqs(xen_cmos_set_s3_resume, rtc_state, 1);
242 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
243 /* TODO: Populate SPD eeprom data. */
244 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
245 gsi[9], *cmos_s3, *smi_irq,
246 kvm_enabled());
247 smbus_eeprom_init(smbus, 8, NULL, 0);
250 if (pci_enabled) {
251 pc_pci_device_init(pci_bus);
255 static void pc_init_pci(ram_addr_t ram_size,
256 const char *boot_device,
257 const char *kernel_filename,
258 const char *kernel_cmdline,
259 const char *initrd_filename,
260 const char *cpu_model)
262 pc_init1(get_system_memory(),
263 get_system_io(),
264 ram_size, boot_device,
265 kernel_filename, kernel_cmdline,
266 initrd_filename, cpu_model, 1, 1);
269 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size,
270 const char *boot_device,
271 const char *kernel_filename,
272 const char *kernel_cmdline,
273 const char *initrd_filename,
274 const char *cpu_model)
276 pc_init1(get_system_memory(),
277 get_system_io(),
278 ram_size, boot_device,
279 kernel_filename, kernel_cmdline,
280 initrd_filename, cpu_model, 1, 0);
283 static void pc_init_isa(ram_addr_t ram_size,
284 const char *boot_device,
285 const char *kernel_filename,
286 const char *kernel_cmdline,
287 const char *initrd_filename,
288 const char *cpu_model)
290 if (cpu_model == NULL)
291 cpu_model = "486";
292 pc_init1(get_system_memory(),
293 get_system_io(),
294 ram_size, boot_device,
295 kernel_filename, kernel_cmdline,
296 initrd_filename, cpu_model, 0, 1);
299 #ifdef CONFIG_XEN
300 static void pc_xen_hvm_init(ram_addr_t ram_size,
301 const char *boot_device,
302 const char *kernel_filename,
303 const char *kernel_cmdline,
304 const char *initrd_filename,
305 const char *cpu_model)
307 if (xen_hvm_init() != 0) {
308 hw_error("xen hardware virtual machine initialisation failed");
310 pc_init_pci_no_kvmclock(ram_size, boot_device,
311 kernel_filename, kernel_cmdline,
312 initrd_filename, cpu_model);
313 xen_vcpu_init();
315 #endif
317 static QEMUMachine pc_machine_v1_0 = {
318 .name = "pc-1.0",
319 .alias = "pc",
320 .desc = "Standard PC",
321 .init = pc_init_pci,
322 .max_cpus = 255,
323 .is_default = 1,
326 static QEMUMachine pc_machine_v0_15 = {
327 .name = "pc-0.15",
328 .desc = "Standard PC",
329 .init = pc_init_pci,
330 .max_cpus = 255,
331 .is_default = 1,
334 static QEMUMachine pc_machine_v0_14 = {
335 .name = "pc-0.14",
336 .desc = "Standard PC",
337 .init = pc_init_pci,
338 .max_cpus = 255,
339 .compat_props = (GlobalProperty[]) {
341 .driver = "qxl",
342 .property = "revision",
343 .value = stringify(2),
345 .driver = "qxl-vga",
346 .property = "revision",
347 .value = stringify(2),
349 .driver = "virtio-blk-pci",
350 .property = "event_idx",
351 .value = "off",
353 .driver = "virtio-serial-pci",
354 .property = "event_idx",
355 .value = "off",
357 .driver = "virtio-net-pci",
358 .property = "event_idx",
359 .value = "off",
361 .driver = "virtio-balloon-pci",
362 .property = "event_idx",
363 .value = "off",
365 { /* end of list */ }
369 static QEMUMachine pc_machine_v0_13 = {
370 .name = "pc-0.13",
371 .desc = "Standard PC",
372 .init = pc_init_pci_no_kvmclock,
373 .max_cpus = 255,
374 .compat_props = (GlobalProperty[]) {
376 .driver = "virtio-9p-pci",
377 .property = "vectors",
378 .value = stringify(0),
380 .driver = "VGA",
381 .property = "rombar",
382 .value = stringify(0),
384 .driver = "vmware-svga",
385 .property = "rombar",
386 .value = stringify(0),
388 .driver = "PCI",
389 .property = "command_serr_enable",
390 .value = "off",
392 .driver = "virtio-blk-pci",
393 .property = "event_idx",
394 .value = "off",
396 .driver = "virtio-serial-pci",
397 .property = "event_idx",
398 .value = "off",
400 .driver = "virtio-net-pci",
401 .property = "event_idx",
402 .value = "off",
404 .driver = "virtio-balloon-pci",
405 .property = "event_idx",
406 .value = "off",
408 .driver = "AC97",
409 .property = "use_broken_id",
410 .value = stringify(1),
412 { /* end of list */ }
416 static QEMUMachine pc_machine_v0_12 = {
417 .name = "pc-0.12",
418 .desc = "Standard PC",
419 .init = pc_init_pci_no_kvmclock,
420 .max_cpus = 255,
421 .compat_props = (GlobalProperty[]) {
423 .driver = "virtio-serial-pci",
424 .property = "max_ports",
425 .value = stringify(1),
427 .driver = "virtio-serial-pci",
428 .property = "vectors",
429 .value = stringify(0),
431 .driver = "VGA",
432 .property = "rombar",
433 .value = stringify(0),
435 .driver = "vmware-svga",
436 .property = "rombar",
437 .value = stringify(0),
439 .driver = "PCI",
440 .property = "command_serr_enable",
441 .value = "off",
443 .driver = "virtio-blk-pci",
444 .property = "event_idx",
445 .value = "off",
447 .driver = "virtio-serial-pci",
448 .property = "event_idx",
449 .value = "off",
451 .driver = "virtio-net-pci",
452 .property = "event_idx",
453 .value = "off",
455 .driver = "virtio-balloon-pci",
456 .property = "event_idx",
457 .value = "off",
459 .driver = "AC97",
460 .property = "use_broken_id",
461 .value = stringify(1),
463 { /* end of list */ }
467 static QEMUMachine pc_machine_v0_11 = {
468 .name = "pc-0.11",
469 .desc = "Standard PC, qemu 0.11",
470 .init = pc_init_pci_no_kvmclock,
471 .max_cpus = 255,
472 .compat_props = (GlobalProperty[]) {
474 .driver = "virtio-blk-pci",
475 .property = "vectors",
476 .value = stringify(0),
478 .driver = "virtio-serial-pci",
479 .property = "max_ports",
480 .value = stringify(1),
482 .driver = "virtio-serial-pci",
483 .property = "vectors",
484 .value = stringify(0),
486 .driver = "ide-drive",
487 .property = "ver",
488 .value = "0.11",
490 .driver = "scsi-disk",
491 .property = "ver",
492 .value = "0.11",
494 .driver = "PCI",
495 .property = "rombar",
496 .value = stringify(0),
498 .driver = "PCI",
499 .property = "command_serr_enable",
500 .value = "off",
502 .driver = "virtio-blk-pci",
503 .property = "event_idx",
504 .value = "off",
506 .driver = "virtio-serial-pci",
507 .property = "event_idx",
508 .value = "off",
510 .driver = "virtio-net-pci",
511 .property = "event_idx",
512 .value = "off",
514 .driver = "virtio-balloon-pci",
515 .property = "event_idx",
516 .value = "off",
518 .driver = "AC97",
519 .property = "use_broken_id",
520 .value = stringify(1),
522 { /* end of list */ }
526 static QEMUMachine pc_machine_v0_10 = {
527 .name = "pc-0.10",
528 .desc = "Standard PC, qemu 0.10",
529 .init = pc_init_pci_no_kvmclock,
530 .max_cpus = 255,
531 .compat_props = (GlobalProperty[]) {
533 .driver = "virtio-blk-pci",
534 .property = "class",
535 .value = stringify(PCI_CLASS_STORAGE_OTHER),
537 .driver = "virtio-serial-pci",
538 .property = "class",
539 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
541 .driver = "virtio-serial-pci",
542 .property = "max_ports",
543 .value = stringify(1),
545 .driver = "virtio-serial-pci",
546 .property = "vectors",
547 .value = stringify(0),
549 .driver = "virtio-net-pci",
550 .property = "vectors",
551 .value = stringify(0),
553 .driver = "virtio-blk-pci",
554 .property = "vectors",
555 .value = stringify(0),
557 .driver = "ide-drive",
558 .property = "ver",
559 .value = "0.10",
561 .driver = "scsi-disk",
562 .property = "ver",
563 .value = "0.10",
565 .driver = "PCI",
566 .property = "rombar",
567 .value = stringify(0),
569 .driver = "PCI",
570 .property = "command_serr_enable",
571 .value = "off",
573 .driver = "virtio-blk-pci",
574 .property = "event_idx",
575 .value = "off",
577 .driver = "virtio-serial-pci",
578 .property = "event_idx",
579 .value = "off",
581 .driver = "virtio-net-pci",
582 .property = "event_idx",
583 .value = "off",
585 .driver = "virtio-balloon-pci",
586 .property = "event_idx",
587 .value = "off",
589 .driver = "AC97",
590 .property = "use_broken_id",
591 .value = stringify(1),
593 { /* end of list */ }
597 static QEMUMachine isapc_machine = {
598 .name = "isapc",
599 .desc = "ISA-only PC",
600 .init = pc_init_isa,
601 .max_cpus = 1,
604 #ifdef CONFIG_XEN
605 static QEMUMachine xenfv_machine = {
606 .name = "xenfv",
607 .desc = "Xen Fully-virtualized PC",
608 .init = pc_xen_hvm_init,
609 .max_cpus = HVM_MAX_VCPUS,
610 .default_machine_opts = "accel=xen",
612 #endif
614 static void pc_machine_init(void)
616 qemu_register_machine(&pc_machine_v1_0);
617 qemu_register_machine(&pc_machine_v0_15);
618 qemu_register_machine(&pc_machine_v0_14);
619 qemu_register_machine(&pc_machine_v0_13);
620 qemu_register_machine(&pc_machine_v0_12);
621 qemu_register_machine(&pc_machine_v0_11);
622 qemu_register_machine(&pc_machine_v0_10);
623 qemu_register_machine(&isapc_machine);
624 #ifdef CONFIG_XEN
625 qemu_register_machine(&xenfv_machine);
626 #endif
629 machine_init(pc_machine_init);