]> git.proxmox.com Git - mirror_qemu.git/blob - hw/pc_piix.c
qdev: Move bus properties to a separate global
[mirror_qemu.git] / hw / pc_piix.c
1 /*
2 * QEMU PC System Emulator
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include <glib.h>
26
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
48
49 #define MAX_IDE_BUS 2
50
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 };
54
55 static void kvm_piix3_setup_irq_routing(bool pci_enabled)
56 {
57 #ifdef CONFIG_KVM
58 KVMState *s = kvm_state;
59 int i;
60
61 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
62 for (i = 0; i < 8; ++i) {
63 if (i == 2) {
64 continue;
65 }
66 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
67 }
68 for (i = 8; i < 16; ++i) {
69 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
70 }
71 if (pci_enabled) {
72 for (i = 0; i < 24; ++i) {
73 if (i == 0) {
74 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
75 } else if (i != 2) {
76 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, i);
77 }
78 }
79 }
80 }
81 #endif /* CONFIG_KVM */
82 }
83
84 static void kvm_piix3_gsi_handler(void *opaque, int n, int level)
85 {
86 GSIState *s = opaque;
87
88 if (n < ISA_NUM_IRQS) {
89 /* Kernel will forward to both PIC and IOAPIC */
90 qemu_set_irq(s->i8259_irq[n], level);
91 } else {
92 qemu_set_irq(s->ioapic_irq[n], level);
93 }
94 }
95
96 static void ioapic_init(GSIState *gsi_state)
97 {
98 DeviceState *dev;
99 SysBusDevice *d;
100 unsigned int i;
101
102 if (kvm_irqchip_in_kernel()) {
103 dev = qdev_create(NULL, "kvm-ioapic");
104 } else {
105 dev = qdev_create(NULL, "ioapic");
106 }
107 /* FIXME: this should be under the piix3. */
108 object_property_add_child(object_resolve_path("i440fx", NULL),
109 "ioapic", OBJECT(dev), NULL);
110 qdev_init_nofail(dev);
111 d = sysbus_from_qdev(dev);
112 sysbus_mmio_map(d, 0, 0xfec00000);
113
114 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
115 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
116 }
117 }
118
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)
130 {
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
150 pc_cpus_init(cpu_model);
151
152 if (kvmclock_enabled) {
153 kvmclock_create();
154 }
155
156 if (ram_size >= 0xe0000000 ) {
157 above_4g_mem_size = ram_size - 0xe0000000;
158 below_4g_mem_size = 0xe0000000;
159 } else {
160 above_4g_mem_size = 0;
161 below_4g_mem_size = ram_size;
162 }
163
164 if (pci_enabled) {
165 pci_memory = g_new(MemoryRegion, 1);
166 memory_region_init(pci_memory, "pci", INT64_MAX);
167 rom_memory = pci_memory;
168 } else {
169 pci_memory = NULL;
170 rom_memory = system_memory;
171 }
172
173 /* allocate ram and load rom/bios */
174 if (!xen_enabled()) {
175 pc_memory_init(system_memory,
176 kernel_filename, kernel_cmdline, initrd_filename,
177 below_4g_mem_size, above_4g_mem_size,
178 pci_enabled ? rom_memory : system_memory, &ram_memory);
179 }
180
181 gsi_state = g_malloc0(sizeof(*gsi_state));
182 if (kvm_irqchip_in_kernel()) {
183 kvm_piix3_setup_irq_routing(pci_enabled);
184 gsi = qemu_allocate_irqs(kvm_piix3_gsi_handler, gsi_state,
185 GSI_NUM_PINS);
186 } else {
187 gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
188 }
189
190 if (pci_enabled) {
191 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,
192 system_memory, system_io, ram_size,
193 below_4g_mem_size,
194 0x100000000ULL - below_4g_mem_size,
195 0x100000000ULL + above_4g_mem_size,
196 (sizeof(target_phys_addr_t) == 4
197 ? 0
198 : ((uint64_t)1 << 62)),
199 pci_memory, ram_memory);
200 } else {
201 pci_bus = NULL;
202 i440fx_state = NULL;
203 isa_bus = isa_bus_new(NULL, system_io);
204 no_hpet = 1;
205 }
206 isa_bus_irqs(isa_bus, gsi);
207
208 if (kvm_irqchip_in_kernel()) {
209 i8259 = kvm_i8259_init(isa_bus);
210 } else if (xen_enabled()) {
211 i8259 = xen_interrupt_controller_init();
212 } else {
213 cpu_irq = pc_allocate_cpu_irq();
214 i8259 = i8259_init(isa_bus, cpu_irq[0]);
215 }
216
217 for (i = 0; i < ISA_NUM_IRQS; i++) {
218 gsi_state->i8259_irq[i] = i8259[i];
219 }
220 if (pci_enabled) {
221 ioapic_init(gsi_state);
222 }
223
224 pc_register_ferr_irq(gsi[13]);
225
226 pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
227 if (xen_enabled()) {
228 pci_create_simple(pci_bus, -1, "xen-platform");
229 }
230
231 /* init basic PC hardware */
232 pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
233
234 for(i = 0; i < nb_nics; i++) {
235 NICInfo *nd = &nd_table[i];
236
237 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
238 pc_init_ne2k_isa(isa_bus, nd);
239 else
240 pci_nic_init_nofail(nd, "e1000", NULL);
241 }
242
243 ide_drive_get(hd, MAX_IDE_BUS);
244 if (pci_enabled) {
245 PCIDevice *dev;
246 if (xen_enabled()) {
247 dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
248 } else {
249 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
250 }
251 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
252 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
253 } else {
254 for(i = 0; i < MAX_IDE_BUS; i++) {
255 ISADevice *dev;
256 dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
257 ide_irq[i],
258 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
259 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
260 }
261 }
262
263 audio_init(isa_bus, pci_enabled ? pci_bus : NULL);
264
265 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
266 floppy, idebus[0], idebus[1], rtc_state);
267
268 if (pci_enabled && usb_enabled) {
269 pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
270 }
271
272 if (pci_enabled && acpi_enabled) {
273 i2c_bus *smbus;
274
275 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
276 /* TODO: Populate SPD eeprom data. */
277 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
278 gsi[9], *smi_irq,
279 kvm_enabled());
280 smbus_eeprom_init(smbus, 8, NULL, 0);
281 }
282
283 if (pci_enabled) {
284 pc_pci_device_init(pci_bus);
285 }
286 }
287
288 static void pc_init_pci(ram_addr_t ram_size,
289 const char *boot_device,
290 const char *kernel_filename,
291 const char *kernel_cmdline,
292 const char *initrd_filename,
293 const char *cpu_model)
294 {
295 pc_init1(get_system_memory(),
296 get_system_io(),
297 ram_size, boot_device,
298 kernel_filename, kernel_cmdline,
299 initrd_filename, cpu_model, 1, 1);
300 }
301
302 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size,
303 const char *boot_device,
304 const char *kernel_filename,
305 const char *kernel_cmdline,
306 const char *initrd_filename,
307 const char *cpu_model)
308 {
309 pc_init1(get_system_memory(),
310 get_system_io(),
311 ram_size, boot_device,
312 kernel_filename, kernel_cmdline,
313 initrd_filename, cpu_model, 1, 0);
314 }
315
316 static void pc_init_isa(ram_addr_t ram_size,
317 const char *boot_device,
318 const char *kernel_filename,
319 const char *kernel_cmdline,
320 const char *initrd_filename,
321 const char *cpu_model)
322 {
323 if (cpu_model == NULL)
324 cpu_model = "486";
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, 0, 1);
330 }
331
332 #ifdef CONFIG_XEN
333 static void pc_xen_hvm_init(ram_addr_t ram_size,
334 const char *boot_device,
335 const char *kernel_filename,
336 const char *kernel_cmdline,
337 const char *initrd_filename,
338 const char *cpu_model)
339 {
340 if (xen_hvm_init() != 0) {
341 hw_error("xen hardware virtual machine initialisation failed");
342 }
343 pc_init_pci_no_kvmclock(ram_size, boot_device,
344 kernel_filename, kernel_cmdline,
345 initrd_filename, cpu_model);
346 xen_vcpu_init();
347 }
348 #endif
349
350 static QEMUMachine pc_machine_v1_1 = {
351 .name = "pc-1.1",
352 .alias = "pc",
353 .desc = "Standard PC",
354 .init = pc_init_pci,
355 .max_cpus = 255,
356 .is_default = 1,
357 };
358
359 #define PC_COMPAT_1_0 \
360 {\
361 .driver = "pc-sysfw",\
362 .property = "rom_only",\
363 .value = stringify(1),\
364 }, {\
365 .driver = "isa-fdc",\
366 .property = "check_media_rate",\
367 .value = "off",\
368 }, {\
369 .driver = "virtio-balloon-pci",\
370 .property = "class",\
371 .value = stringify(PCI_CLASS_MEMORY_RAM),\
372 },{\
373 .driver = "apic",\
374 .property = "vapic",\
375 .value = "off",\
376 },{\
377 .driver = "USB",\
378 .property = "full-path",\
379 .value = "no",\
380 }
381
382 static QEMUMachine pc_machine_v1_0 = {
383 .name = "pc-1.0",
384 .desc = "Standard PC",
385 .init = pc_init_pci,
386 .max_cpus = 255,
387 .compat_props = (GlobalProperty[]) {
388 PC_COMPAT_1_0,
389 { /* end of list */ }
390 },
391 };
392
393 #define PC_COMPAT_0_15 \
394 PC_COMPAT_1_0
395
396 static QEMUMachine pc_machine_v0_15 = {
397 .name = "pc-0.15",
398 .desc = "Standard PC",
399 .init = pc_init_pci,
400 .max_cpus = 255,
401 .compat_props = (GlobalProperty[]) {
402 PC_COMPAT_0_15,
403 { /* end of list */ }
404 },
405 };
406
407 #define PC_COMPAT_0_14 \
408 PC_COMPAT_0_15,\
409 {\
410 .driver = "virtio-blk-pci",\
411 .property = "event_idx",\
412 .value = "off",\
413 },{\
414 .driver = "virtio-serial-pci",\
415 .property = "event_idx",\
416 .value = "off",\
417 },{\
418 .driver = "virtio-net-pci",\
419 .property = "event_idx",\
420 .value = "off",\
421 },{\
422 .driver = "virtio-balloon-pci",\
423 .property = "event_idx",\
424 .value = "off",\
425 }
426
427 static QEMUMachine pc_machine_v0_14 = {
428 .name = "pc-0.14",
429 .desc = "Standard PC",
430 .init = pc_init_pci,
431 .max_cpus = 255,
432 .compat_props = (GlobalProperty[]) {
433 PC_COMPAT_0_14,
434 {
435 .driver = "qxl",
436 .property = "revision",
437 .value = stringify(2),
438 },{
439 .driver = "qxl-vga",
440 .property = "revision",
441 .value = stringify(2),
442 },
443 { /* end of list */ }
444 },
445 };
446
447 #define PC_COMPAT_0_13 \
448 PC_COMPAT_0_14,\
449 {\
450 .driver = "PCI",\
451 .property = "command_serr_enable",\
452 .value = "off",\
453 },{\
454 .driver = "AC97",\
455 .property = "use_broken_id",\
456 .value = stringify(1),\
457 }
458
459 static QEMUMachine pc_machine_v0_13 = {
460 .name = "pc-0.13",
461 .desc = "Standard PC",
462 .init = pc_init_pci_no_kvmclock,
463 .max_cpus = 255,
464 .compat_props = (GlobalProperty[]) {
465 PC_COMPAT_0_13,
466 {
467 .driver = "virtio-9p-pci",
468 .property = "vectors",
469 .value = stringify(0),
470 },{
471 .driver = "VGA",
472 .property = "rombar",
473 .value = stringify(0),
474 },{
475 .driver = "vmware-svga",
476 .property = "rombar",
477 .value = stringify(0),
478 },
479 { /* end of list */ }
480 },
481 };
482
483 #define PC_COMPAT_0_12 \
484 PC_COMPAT_0_13,\
485 {\
486 .driver = "virtio-serial-pci",\
487 .property = "max_ports",\
488 .value = stringify(1),\
489 },{\
490 .driver = "virtio-serial-pci",\
491 .property = "vectors",\
492 .value = stringify(0),\
493 }
494
495 static QEMUMachine pc_machine_v0_12 = {
496 .name = "pc-0.12",
497 .desc = "Standard PC",
498 .init = pc_init_pci_no_kvmclock,
499 .max_cpus = 255,
500 .compat_props = (GlobalProperty[]) {
501 PC_COMPAT_0_12,
502 {
503 .driver = "VGA",
504 .property = "rombar",
505 .value = stringify(0),
506 },{
507 .driver = "vmware-svga",
508 .property = "rombar",
509 .value = stringify(0),
510 },
511 { /* end of list */ }
512 }
513 };
514
515 #define PC_COMPAT_0_11 \
516 PC_COMPAT_0_12,\
517 {\
518 .driver = "virtio-blk-pci",\
519 .property = "vectors",\
520 .value = stringify(0),\
521 },{\
522 .driver = "PCI",\
523 .property = "rombar",\
524 .value = stringify(0),\
525 }
526
527 static QEMUMachine pc_machine_v0_11 = {
528 .name = "pc-0.11",
529 .desc = "Standard PC, qemu 0.11",
530 .init = pc_init_pci_no_kvmclock,
531 .max_cpus = 255,
532 .compat_props = (GlobalProperty[]) {
533 PC_COMPAT_0_11,
534 {
535 .driver = "ide-drive",
536 .property = "ver",
537 .value = "0.11",
538 },{
539 .driver = "scsi-disk",
540 .property = "ver",
541 .value = "0.11",
542 },
543 { /* end of list */ }
544 }
545 };
546
547 static QEMUMachine pc_machine_v0_10 = {
548 .name = "pc-0.10",
549 .desc = "Standard PC, qemu 0.10",
550 .init = pc_init_pci_no_kvmclock,
551 .max_cpus = 255,
552 .compat_props = (GlobalProperty[]) {
553 PC_COMPAT_0_11,
554 {
555 .driver = "virtio-blk-pci",
556 .property = "class",
557 .value = stringify(PCI_CLASS_STORAGE_OTHER),
558 },{
559 .driver = "virtio-serial-pci",
560 .property = "class",
561 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
562 },{
563 .driver = "virtio-net-pci",
564 .property = "vectors",
565 .value = stringify(0),
566 },{
567 .driver = "ide-drive",
568 .property = "ver",
569 .value = "0.10",
570 },{
571 .driver = "scsi-disk",
572 .property = "ver",
573 .value = "0.10",
574 },
575 { /* end of list */ }
576 },
577 };
578
579 static QEMUMachine isapc_machine = {
580 .name = "isapc",
581 .desc = "ISA-only PC",
582 .init = pc_init_isa,
583 .max_cpus = 1,
584 .compat_props = (GlobalProperty[]) {
585 {
586 .driver = "pc-sysfw",
587 .property = "rom_only",
588 .value = stringify(1),
589 },
590 { /* end of list */ }
591 },
592 };
593
594 #ifdef CONFIG_XEN
595 static QEMUMachine xenfv_machine = {
596 .name = "xenfv",
597 .desc = "Xen Fully-virtualized PC",
598 .init = pc_xen_hvm_init,
599 .max_cpus = HVM_MAX_VCPUS,
600 .default_machine_opts = "accel=xen",
601 };
602 #endif
603
604 static void pc_machine_init(void)
605 {
606 qemu_register_machine(&pc_machine_v1_1);
607 qemu_register_machine(&pc_machine_v1_0);
608 qemu_register_machine(&pc_machine_v0_15);
609 qemu_register_machine(&pc_machine_v0_14);
610 qemu_register_machine(&pc_machine_v0_13);
611 qemu_register_machine(&pc_machine_v0_12);
612 qemu_register_machine(&pc_machine_v0_11);
613 qemu_register_machine(&pc_machine_v0_10);
614 qemu_register_machine(&isapc_machine);
615 #ifdef CONFIG_XEN
616 qemu_register_machine(&xenfv_machine);
617 #endif
618 }
619
620 machine_init(pc_machine_init);