PPC: e500: dt: create mpic node dynamically
[qemu/agraf.git] / hw / pc_piix.c
blobeae258cefd7609536cb8a3101db0cbe47d7f54d1
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 "pci_ids.h"
32 #include "usb.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 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_irq_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
69 for (i = 8; i < 16; ++i) {
70 kvm_irqchip_add_irq_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_irq_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
76 } else if (i != 2) {
77 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, i);
82 #endif /* CONFIG_KVM */
85 static void kvm_piix3_gsi_handler(void *opaque, int n, int level)
87 GSIState *s = opaque;
89 if (n < ISA_NUM_IRQS) {
90 /* Kernel will forward to both PIC and IOAPIC */
91 qemu_set_irq(s->i8259_irq[n], level);
92 } else {
93 qemu_set_irq(s->ioapic_irq[n], level);
97 static void ioapic_init(GSIState *gsi_state)
99 DeviceState *dev;
100 SysBusDevice *d;
101 unsigned int i;
103 if (kvm_irqchip_in_kernel()) {
104 dev = qdev_create(NULL, "kvm-ioapic");
105 } else {
106 dev = qdev_create(NULL, "ioapic");
108 /* FIXME: this should be under the piix3. */
109 object_property_add_child(object_resolve_path("i440fx", NULL),
110 "ioapic", OBJECT(dev), NULL);
111 qdev_init_nofail(dev);
112 d = sysbus_from_qdev(dev);
113 sysbus_mmio_map(d, 0, 0xfec00000);
115 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
116 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
120 /* PC hardware initialisation */
121 static void pc_init1(MemoryRegion *system_memory,
122 MemoryRegion *system_io,
123 ram_addr_t ram_size,
124 const char *boot_device,
125 const char *kernel_filename,
126 const char *kernel_cmdline,
127 const char *initrd_filename,
128 const char *cpu_model,
129 int pci_enabled,
130 int kvmclock_enabled)
132 int i;
133 ram_addr_t below_4g_mem_size, above_4g_mem_size;
134 PCIBus *pci_bus;
135 ISABus *isa_bus;
136 PCII440FXState *i440fx_state;
137 int piix3_devfn = -1;
138 qemu_irq *cpu_irq;
139 qemu_irq *gsi;
140 qemu_irq *i8259;
141 qemu_irq *smi_irq;
142 GSIState *gsi_state;
143 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
144 BusState *idebus[MAX_IDE_BUS];
145 ISADevice *rtc_state;
146 ISADevice *floppy;
147 MemoryRegion *ram_memory;
148 MemoryRegion *pci_memory;
149 MemoryRegion *rom_memory;
150 void *fw_cfg = NULL;
152 pc_cpus_init(cpu_model);
154 if (kvmclock_enabled) {
155 kvmclock_create();
158 if (ram_size >= 0xe0000000 ) {
159 above_4g_mem_size = ram_size - 0xe0000000;
160 below_4g_mem_size = 0xe0000000;
161 } else {
162 above_4g_mem_size = 0;
163 below_4g_mem_size = ram_size;
166 if (pci_enabled) {
167 pci_memory = g_new(MemoryRegion, 1);
168 memory_region_init(pci_memory, "pci", INT64_MAX);
169 rom_memory = pci_memory;
170 } else {
171 pci_memory = NULL;
172 rom_memory = system_memory;
175 /* allocate ram and load rom/bios */
176 if (!xen_enabled()) {
177 fw_cfg = pc_memory_init(system_memory,
178 kernel_filename, kernel_cmdline, initrd_filename,
179 below_4g_mem_size, above_4g_mem_size,
180 pci_enabled ? rom_memory : system_memory, &ram_memory);
183 gsi_state = g_malloc0(sizeof(*gsi_state));
184 if (kvm_irqchip_in_kernel()) {
185 kvm_piix3_setup_irq_routing(pci_enabled);
186 gsi = qemu_allocate_irqs(kvm_piix3_gsi_handler, gsi_state,
187 GSI_NUM_PINS);
188 } else {
189 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
192 if (pci_enabled) {
193 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
194 system_memory, system_io, ram_size,
195 below_4g_mem_size,
196 0x100000000ULL - below_4g_mem_size,
197 0x100000000ULL + above_4g_mem_size,
198 (sizeof(target_phys_addr_t) == 4
200 : ((uint64_t)1 << 62)),
201 pci_memory, ram_memory);
202 } else {
203 pci_bus = NULL;
204 i440fx_state = NULL;
205 isa_bus = isa_bus_new(NULL, system_io);
206 no_hpet = 1;
208 isa_bus_irqs(isa_bus, gsi);
210 if (kvm_irqchip_in_kernel()) {
211 i8259 = kvm_i8259_init(isa_bus);
212 } else if (xen_enabled()) {
213 i8259 = xen_interrupt_controller_init();
214 } else {
215 cpu_irq = pc_allocate_cpu_irq();
216 i8259 = i8259_init(isa_bus, cpu_irq[0]);
219 for (i = 0; i < ISA_NUM_IRQS; i++) {
220 gsi_state->i8259_irq[i] = i8259[i];
222 if (pci_enabled) {
223 ioapic_init(gsi_state);
226 pc_register_ferr_irq(gsi[13]);
228 pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
229 if (xen_enabled()) {
230 pci_create_simple(pci_bus, -1, "xen-platform");
233 /* init basic PC hardware */
234 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
236 for(i = 0; i < nb_nics; i++) {
237 NICInfo *nd = &nd_table[i];
239 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
240 pc_init_ne2k_isa(isa_bus, nd);
241 else
242 pci_nic_init_nofail(nd, "e1000", NULL);
245 ide_drive_get(hd, MAX_IDE_BUS);
246 if (pci_enabled) {
247 PCIDevice *dev;
248 if (xen_enabled()) {
249 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
250 } else {
251 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
253 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
254 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
255 } else {
256 for(i = 0; i < MAX_IDE_BUS; i++) {
257 ISADevice *dev;
258 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
259 ide_irq[i],
260 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
261 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
265 audio_init(isa_bus, pci_enabled ? pci_bus : NULL);
267 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
268 floppy, idebus[0], idebus[1], rtc_state);
270 if (pci_enabled && usb_enabled) {
271 pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
274 if (pci_enabled && acpi_enabled) {
275 i2c_bus *smbus;
277 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
278 /* TODO: Populate SPD eeprom data. */
279 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
280 gsi[9], *smi_irq,
281 kvm_enabled(), fw_cfg);
282 smbus_eeprom_init(smbus, 8, NULL, 0);
285 if (pci_enabled) {
286 pc_pci_device_init(pci_bus);
290 static void pc_init_pci(ram_addr_t ram_size,
291 const char *boot_device,
292 const char *kernel_filename,
293 const char *kernel_cmdline,
294 const char *initrd_filename,
295 const char *cpu_model)
297 pc_init1(get_system_memory(),
298 get_system_io(),
299 ram_size, boot_device,
300 kernel_filename, kernel_cmdline,
301 initrd_filename, cpu_model, 1, 1);
304 static void pc_init_pci_no_kvmclock(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, 0);
318 static void pc_init_isa(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 if (cpu_model == NULL)
326 cpu_model = "486";
327 pc_init1(get_system_memory(),
328 get_system_io(),
329 ram_size, boot_device,
330 kernel_filename, kernel_cmdline,
331 initrd_filename, cpu_model, 0, 1);
334 #ifdef CONFIG_XEN
335 static void pc_xen_hvm_init(ram_addr_t ram_size,
336 const char *boot_device,
337 const char *kernel_filename,
338 const char *kernel_cmdline,
339 const char *initrd_filename,
340 const char *cpu_model)
342 if (xen_hvm_init() != 0) {
343 hw_error("xen hardware virtual machine initialisation failed");
345 pc_init_pci_no_kvmclock(ram_size, boot_device,
346 kernel_filename, kernel_cmdline,
347 initrd_filename, cpu_model);
348 xen_vcpu_init();
350 #endif
352 static QEMUMachine pc_machine_v1_1 = {
353 .name = "pc-1.1",
354 .alias = "pc",
355 .desc = "Standard PC",
356 .init = pc_init_pci,
357 .max_cpus = 255,
358 .is_default = 1,
361 #define PC_COMPAT_1_0 \
363 .driver = "pc-sysfw",\
364 .property = "rom_only",\
365 .value = stringify(1),\
366 }, {\
367 .driver = "isa-fdc",\
368 .property = "check_media_rate",\
369 .value = "off",\
370 }, {\
371 .driver = "virtio-balloon-pci",\
372 .property = "class",\
373 .value = stringify(PCI_CLASS_MEMORY_RAM),\
374 },{\
375 .driver = "apic",\
376 .property = "vapic",\
377 .value = "off",\
378 },{\
379 .driver = TYPE_USB_DEVICE,\
380 .property = "full-path",\
381 .value = "no",\
384 static QEMUMachine pc_machine_v1_0 = {
385 .name = "pc-1.0",
386 .desc = "Standard PC",
387 .init = pc_init_pci,
388 .max_cpus = 255,
389 .compat_props = (GlobalProperty[]) {
390 PC_COMPAT_1_0,
391 { /* end of list */ }
393 .hw_version = "1.0",
396 #define PC_COMPAT_0_15 \
397 PC_COMPAT_1_0
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 .compat_props = (GlobalProperty[]) {
405 PC_COMPAT_0_15,
406 { /* end of list */ }
408 .hw_version = "0.15",
411 #define PC_COMPAT_0_14 \
412 PC_COMPAT_0_15,\
414 .driver = "virtio-blk-pci",\
415 .property = "event_idx",\
416 .value = "off",\
417 },{\
418 .driver = "virtio-serial-pci",\
419 .property = "event_idx",\
420 .value = "off",\
421 },{\
422 .driver = "virtio-net-pci",\
423 .property = "event_idx",\
424 .value = "off",\
425 },{\
426 .driver = "virtio-balloon-pci",\
427 .property = "event_idx",\
428 .value = "off",\
431 static QEMUMachine pc_machine_v0_14 = {
432 .name = "pc-0.14",
433 .desc = "Standard PC",
434 .init = pc_init_pci,
435 .max_cpus = 255,
436 .compat_props = (GlobalProperty[]) {
437 PC_COMPAT_0_14,
439 .driver = "qxl",
440 .property = "revision",
441 .value = stringify(2),
443 .driver = "qxl-vga",
444 .property = "revision",
445 .value = stringify(2),
447 { /* end of list */ }
449 .hw_version = "0.14",
452 #define PC_COMPAT_0_13 \
453 PC_COMPAT_0_14,\
455 .driver = TYPE_PCI_DEVICE,\
456 .property = "command_serr_enable",\
457 .value = "off",\
458 },{\
459 .driver = "AC97",\
460 .property = "use_broken_id",\
461 .value = stringify(1),\
464 static QEMUMachine pc_machine_v0_13 = {
465 .name = "pc-0.13",
466 .desc = "Standard PC",
467 .init = pc_init_pci_no_kvmclock,
468 .max_cpus = 255,
469 .compat_props = (GlobalProperty[]) {
470 PC_COMPAT_0_13,
472 .driver = "virtio-9p-pci",
473 .property = "vectors",
474 .value = stringify(0),
476 .driver = "VGA",
477 .property = "rombar",
478 .value = stringify(0),
480 .driver = "vmware-svga",
481 .property = "rombar",
482 .value = stringify(0),
484 { /* end of list */ }
486 .hw_version = "0.13",
489 #define PC_COMPAT_0_12 \
490 PC_COMPAT_0_13,\
492 .driver = "virtio-serial-pci",\
493 .property = "max_ports",\
494 .value = stringify(1),\
495 },{\
496 .driver = "virtio-serial-pci",\
497 .property = "vectors",\
498 .value = stringify(0),\
501 static QEMUMachine pc_machine_v0_12 = {
502 .name = "pc-0.12",
503 .desc = "Standard PC",
504 .init = pc_init_pci_no_kvmclock,
505 .max_cpus = 255,
506 .compat_props = (GlobalProperty[]) {
507 PC_COMPAT_0_12,
509 .driver = "VGA",
510 .property = "rombar",
511 .value = stringify(0),
513 .driver = "vmware-svga",
514 .property = "rombar",
515 .value = stringify(0),
517 { /* end of list */ }
519 .hw_version = "0.12",
522 #define PC_COMPAT_0_11 \
523 PC_COMPAT_0_12,\
525 .driver = "virtio-blk-pci",\
526 .property = "vectors",\
527 .value = stringify(0),\
528 },{\
529 .driver = TYPE_PCI_DEVICE,\
530 .property = "rombar",\
531 .value = stringify(0),\
534 static QEMUMachine pc_machine_v0_11 = {
535 .name = "pc-0.11",
536 .desc = "Standard PC, qemu 0.11",
537 .init = pc_init_pci_no_kvmclock,
538 .max_cpus = 255,
539 .compat_props = (GlobalProperty[]) {
540 PC_COMPAT_0_11,
542 .driver = "ide-drive",
543 .property = "ver",
544 .value = "0.11",
546 .driver = "scsi-disk",
547 .property = "ver",
548 .value = "0.11",
550 { /* end of list */ }
552 .hw_version = "0.11",
555 static QEMUMachine pc_machine_v0_10 = {
556 .name = "pc-0.10",
557 .desc = "Standard PC, qemu 0.10",
558 .init = pc_init_pci_no_kvmclock,
559 .max_cpus = 255,
560 .compat_props = (GlobalProperty[]) {
561 PC_COMPAT_0_11,
563 .driver = "virtio-blk-pci",
564 .property = "class",
565 .value = stringify(PCI_CLASS_STORAGE_OTHER),
567 .driver = "virtio-serial-pci",
568 .property = "class",
569 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
571 .driver = "virtio-net-pci",
572 .property = "vectors",
573 .value = stringify(0),
575 .driver = "ide-drive",
576 .property = "ver",
577 .value = "0.10",
579 .driver = "scsi-disk",
580 .property = "ver",
581 .value = "0.10",
583 { /* end of list */ }
585 .hw_version = "0.10",
588 static QEMUMachine isapc_machine = {
589 .name = "isapc",
590 .desc = "ISA-only PC",
591 .init = pc_init_isa,
592 .max_cpus = 1,
593 .compat_props = (GlobalProperty[]) {
595 .driver = "pc-sysfw",
596 .property = "rom_only",
597 .value = stringify(1),
599 { /* end of list */ }
603 #ifdef CONFIG_XEN
604 static QEMUMachine xenfv_machine = {
605 .name = "xenfv",
606 .desc = "Xen Fully-virtualized PC",
607 .init = pc_xen_hvm_init,
608 .max_cpus = HVM_MAX_VCPUS,
609 .default_machine_opts = "accel=xen",
611 #endif
613 static void pc_machine_init(void)
615 qemu_register_machine(&pc_machine_v1_1);
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);