virtio-pci: change virtio balloon PCI class code
[qemu/opensuse.git] / hw / pc_piix.c
blob5c08245fede26e615cb9277790cca0048e7c94b2
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 "net.h"
33 #include "boards.h"
34 #include "ide.h"
35 #include "kvm.h"
36 #include "kvm/clock.h"
37 #include "sysemu.h"
38 #include "sysbus.h"
39 #include "arch_init.h"
40 #include "blockdev.h"
41 #include "smbus.h"
42 #include "xen.h"
43 #include "memory.h"
44 #include "exec-memory.h"
45 #ifdef CONFIG_XEN
46 # include <xen/hvm/hvm_info_table.h>
47 #endif
49 #define MAX_IDE_BUS 2
51 static const int ide_iobase[MAX_IDE_BUS] = { 0x1f0, 0x170 };
52 static const int ide_iobase2[MAX_IDE_BUS] = { 0x3f6, 0x376 };
53 static const int ide_irq[MAX_IDE_BUS] = { 14, 15 };
55 static void kvm_piix3_setup_irq_routing(bool pci_enabled)
57 #ifdef CONFIG_KVM
58 KVMState *s = kvm_state;
59 int ret, i;
61 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
62 for (i = 0; i < 8; ++i) {
63 if (i == 2) {
64 continue;
66 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
68 for (i = 8; i < 16; ++i) {
69 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
71 if (pci_enabled) {
72 for (i = 0; i < 24; ++i) {
73 if (i == 0) {
74 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
75 } else if (i != 2) {
76 kvm_irqchip_add_route(s, i, KVM_IRQCHIP_IOAPIC, i);
80 ret = kvm_irqchip_commit_routes(s);
81 if (ret < 0) {
82 hw_error("KVM IRQ routing setup failed");
85 #endif /* CONFIG_KVM */
88 static void kvm_piix3_gsi_handler(void *opaque, int n, int level)
90 GSIState *s = opaque;
92 if (n < ISA_NUM_IRQS) {
93 /* Kernel will forward to both PIC and IOAPIC */
94 qemu_set_irq(s->i8259_irq[n], level);
95 } else {
96 qemu_set_irq(s->ioapic_irq[n], level);
100 static void ioapic_init(GSIState *gsi_state)
102 DeviceState *dev;
103 SysBusDevice *d;
104 unsigned int i;
106 if (kvm_irqchip_in_kernel()) {
107 dev = qdev_create(NULL, "kvm-ioapic");
108 } else {
109 dev = qdev_create(NULL, "ioapic");
111 /* FIXME: this should be under the piix3. */
112 object_property_add_child(object_resolve_path("i440fx", NULL),
113 "ioapic", OBJECT(dev), NULL);
114 qdev_init_nofail(dev);
115 d = sysbus_from_qdev(dev);
116 sysbus_mmio_map(d, 0, 0xfec00000);
118 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
119 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
123 /* PC hardware initialisation */
124 static void pc_init1(MemoryRegion *system_memory,
125 MemoryRegion *system_io,
126 ram_addr_t ram_size,
127 const char *boot_device,
128 const char *kernel_filename,
129 const char *kernel_cmdline,
130 const char *initrd_filename,
131 const char *cpu_model,
132 int pci_enabled,
133 int kvmclock_enabled)
135 int i;
136 ram_addr_t below_4g_mem_size, above_4g_mem_size;
137 PCIBus *pci_bus;
138 ISABus *isa_bus;
139 PCII440FXState *i440fx_state;
140 int piix3_devfn = -1;
141 qemu_irq *cpu_irq;
142 qemu_irq *gsi;
143 qemu_irq *i8259;
144 qemu_irq *smi_irq;
145 GSIState *gsi_state;
146 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
147 BusState *idebus[MAX_IDE_BUS];
148 ISADevice *rtc_state;
149 ISADevice *floppy;
150 MemoryRegion *ram_memory;
151 MemoryRegion *pci_memory;
152 MemoryRegion *rom_memory;
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_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_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 pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
231 if (xen_enabled()) {
232 pci_create_simple(pci_bus, -1, "xen-platform");
235 /* init basic PC hardware */
236 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
238 for(i = 0; i < nb_nics; i++) {
239 NICInfo *nd = &nd_table[i];
241 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
242 pc_init_ne2k_isa(isa_bus, nd);
243 else
244 pci_nic_init_nofail(nd, "e1000", NULL);
247 ide_drive_get(hd, MAX_IDE_BUS);
248 if (pci_enabled) {
249 PCIDevice *dev;
250 if (xen_enabled()) {
251 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
252 } else {
253 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
255 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
256 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
257 } else {
258 for(i = 0; i < MAX_IDE_BUS; i++) {
259 ISADevice *dev;
260 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
261 ide_irq[i],
262 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
263 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
267 audio_init(isa_bus, pci_enabled ? pci_bus : NULL);
269 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
270 floppy, idebus[0], idebus[1], rtc_state);
272 if (pci_enabled && usb_enabled) {
273 pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
276 if (pci_enabled && acpi_enabled) {
277 i2c_bus *smbus;
279 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
280 /* TODO: Populate SPD eeprom data. */
281 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
282 gsi[9], *smi_irq,
283 kvm_enabled());
284 smbus_eeprom_init(smbus, 8, NULL, 0);
287 if (pci_enabled) {
288 pc_pci_device_init(pci_bus);
292 static void pc_init_pci(ram_addr_t ram_size,
293 const char *boot_device,
294 const char *kernel_filename,
295 const char *kernel_cmdline,
296 const char *initrd_filename,
297 const char *cpu_model)
299 pc_init1(get_system_memory(),
300 get_system_io(),
301 ram_size, boot_device,
302 kernel_filename, kernel_cmdline,
303 initrd_filename, cpu_model, 1, 1);
306 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size,
307 const char *boot_device,
308 const char *kernel_filename,
309 const char *kernel_cmdline,
310 const char *initrd_filename,
311 const char *cpu_model)
313 pc_init1(get_system_memory(),
314 get_system_io(),
315 ram_size, boot_device,
316 kernel_filename, kernel_cmdline,
317 initrd_filename, cpu_model, 1, 0);
320 static void pc_init_isa(ram_addr_t ram_size,
321 const char *boot_device,
322 const char *kernel_filename,
323 const char *kernel_cmdline,
324 const char *initrd_filename,
325 const char *cpu_model)
327 if (cpu_model == NULL)
328 cpu_model = "486";
329 pc_init1(get_system_memory(),
330 get_system_io(),
331 ram_size, boot_device,
332 kernel_filename, kernel_cmdline,
333 initrd_filename, cpu_model, 0, 1);
336 #ifdef CONFIG_XEN
337 static void pc_xen_hvm_init(ram_addr_t ram_size,
338 const char *boot_device,
339 const char *kernel_filename,
340 const char *kernel_cmdline,
341 const char *initrd_filename,
342 const char *cpu_model)
344 if (xen_hvm_init() != 0) {
345 hw_error("xen hardware virtual machine initialisation failed");
347 pc_init_pci_no_kvmclock(ram_size, boot_device,
348 kernel_filename, kernel_cmdline,
349 initrd_filename, cpu_model);
350 xen_vcpu_init();
352 #endif
354 static QEMUMachine pc_machine_v1_1 = {
355 .name = "pc-1.1",
356 .alias = "pc",
357 .desc = "Standard PC",
358 .init = pc_init_pci,
359 .max_cpus = 255,
360 .is_default = 1,
363 #define PC_COMPAT_1_0 \
365 .driver = "pc-sysfw",\
366 .property = "rom_only",\
367 .value = stringify(1),\
368 }, {\
369 .driver = "isa-fdc",\
370 .property = "check_media_rate",\
371 .value = "off",\
372 }, {\
373 .driver = "virtio-balloon-pci",\
374 .property = "class",\
375 .value = stringify(PCI_CLASS_MEMORY_RAM),\
378 static QEMUMachine pc_machine_v1_0 = {
379 .name = "pc-1.0",
380 .desc = "Standard PC",
381 .init = pc_init_pci,
382 .max_cpus = 255,
383 .compat_props = (GlobalProperty[]) {
384 PC_COMPAT_1_0,
385 { /* end of list */ }
389 #define PC_COMPAT_0_15 \
390 PC_COMPAT_1_0
392 static QEMUMachine pc_machine_v0_15 = {
393 .name = "pc-0.15",
394 .desc = "Standard PC",
395 .init = pc_init_pci,
396 .max_cpus = 255,
397 .compat_props = (GlobalProperty[]) {
398 PC_COMPAT_0_15,
399 { /* end of list */ }
403 #define PC_COMPAT_0_14 \
404 PC_COMPAT_0_15,\
406 .driver = "virtio-blk-pci",\
407 .property = "event_idx",\
408 .value = "off",\
409 },{\
410 .driver = "virtio-serial-pci",\
411 .property = "event_idx",\
412 .value = "off",\
413 },{\
414 .driver = "virtio-net-pci",\
415 .property = "event_idx",\
416 .value = "off",\
417 },{\
418 .driver = "virtio-balloon-pci",\
419 .property = "event_idx",\
420 .value = "off",\
423 static QEMUMachine pc_machine_v0_14 = {
424 .name = "pc-0.14",
425 .desc = "Standard PC",
426 .init = pc_init_pci,
427 .max_cpus = 255,
428 .compat_props = (GlobalProperty[]) {
429 PC_COMPAT_0_14,
431 .driver = "qxl",
432 .property = "revision",
433 .value = stringify(2),
435 .driver = "qxl-vga",
436 .property = "revision",
437 .value = stringify(2),
439 { /* end of list */ }
443 #define PC_COMPAT_0_13 \
444 PC_COMPAT_0_14,\
446 .driver = "PCI",\
447 .property = "command_serr_enable",\
448 .value = "off",\
449 },{\
450 .driver = "AC97",\
451 .property = "use_broken_id",\
452 .value = stringify(1),\
455 static QEMUMachine pc_machine_v0_13 = {
456 .name = "pc-0.13",
457 .desc = "Standard PC",
458 .init = pc_init_pci_no_kvmclock,
459 .max_cpus = 255,
460 .compat_props = (GlobalProperty[]) {
461 PC_COMPAT_0_13,
463 .driver = "virtio-9p-pci",
464 .property = "vectors",
465 .value = stringify(0),
467 .driver = "VGA",
468 .property = "rombar",
469 .value = stringify(0),
471 .driver = "vmware-svga",
472 .property = "rombar",
473 .value = stringify(0),
475 { /* end of list */ }
479 #define PC_COMPAT_0_12 \
480 PC_COMPAT_0_13,\
482 .driver = "virtio-serial-pci",\
483 .property = "max_ports",\
484 .value = stringify(1),\
485 },{\
486 .driver = "virtio-serial-pci",\
487 .property = "vectors",\
488 .value = stringify(0),\
491 static QEMUMachine pc_machine_v0_12 = {
492 .name = "pc-0.12",
493 .desc = "Standard PC",
494 .init = pc_init_pci_no_kvmclock,
495 .max_cpus = 255,
496 .compat_props = (GlobalProperty[]) {
497 PC_COMPAT_0_12,
499 .driver = "VGA",
500 .property = "rombar",
501 .value = stringify(0),
503 .driver = "vmware-svga",
504 .property = "rombar",
505 .value = stringify(0),
507 { /* end of list */ }
511 #define PC_COMPAT_0_11 \
512 PC_COMPAT_0_12,\
514 .driver = "virtio-blk-pci",\
515 .property = "vectors",\
516 .value = stringify(0),\
519 static QEMUMachine pc_machine_v0_11 = {
520 .name = "pc-0.11",
521 .desc = "Standard PC, qemu 0.11",
522 .init = pc_init_pci_no_kvmclock,
523 .max_cpus = 255,
524 .compat_props = (GlobalProperty[]) {
525 PC_COMPAT_0_11,
527 .driver = "ide-drive",
528 .property = "ver",
529 .value = "0.11",
531 .driver = "scsi-disk",
532 .property = "ver",
533 .value = "0.11",
535 { /* end of list */ }
539 static QEMUMachine pc_machine_v0_10 = {
540 .name = "pc-0.10",
541 .desc = "Standard PC, qemu 0.10",
542 .init = pc_init_pci_no_kvmclock,
543 .max_cpus = 255,
544 .compat_props = (GlobalProperty[]) {
545 PC_COMPAT_0_11,
547 .driver = "virtio-blk-pci",
548 .property = "class",
549 .value = stringify(PCI_CLASS_STORAGE_OTHER),
551 .driver = "virtio-serial-pci",
552 .property = "class",
553 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
555 .driver = "virtio-net-pci",
556 .property = "vectors",
557 .value = stringify(0),
559 .driver = "ide-drive",
560 .property = "ver",
561 .value = "0.10",
563 .driver = "scsi-disk",
564 .property = "ver",
565 .value = "0.10",
567 { /* end of list */ }
571 static QEMUMachine isapc_machine = {
572 .name = "isapc",
573 .desc = "ISA-only PC",
574 .init = pc_init_isa,
575 .max_cpus = 1,
576 .compat_props = (GlobalProperty[]) {
578 .driver = "pc-sysfw",
579 .property = "rom_only",
580 .value = stringify(1),
582 { /* end of list */ }
586 #ifdef CONFIG_XEN
587 static QEMUMachine xenfv_machine = {
588 .name = "xenfv",
589 .desc = "Xen Fully-virtualized PC",
590 .init = pc_xen_hvm_init,
591 .max_cpus = HVM_MAX_VCPUS,
592 .default_machine_opts = "accel=xen",
594 #endif
596 static void pc_machine_init(void)
598 qemu_register_machine(&pc_machine_v1_1);
599 qemu_register_machine(&pc_machine_v1_0);
600 qemu_register_machine(&pc_machine_v0_15);
601 qemu_register_machine(&pc_machine_v0_14);
602 qemu_register_machine(&pc_machine_v0_13);
603 qemu_register_machine(&pc_machine_v0_12);
604 qemu_register_machine(&pc_machine_v0_11);
605 qemu_register_machine(&pc_machine_v0_10);
606 qemu_register_machine(&isapc_machine);
607 #ifdef CONFIG_XEN
608 qemu_register_machine(&xenfv_machine);
609 #endif
612 machine_init(pc_machine_init);