Merge branch 'upstream-merge' into next
[qemu-dev-zwu.git] / hw / pc_piix.c
blob7af03fa6e68c0c4d6dacf0ed59a019b026f50ceb
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 "hw.h"
26 #include "pc.h"
27 #include "apic.h"
28 #include "pci.h"
29 #include "usb-uhci.h"
30 #include "usb-ohci.h"
31 #include "net.h"
32 #include "boards.h"
33 #include "ide.h"
34 #include "kvm.h"
35 #include "kvmclock.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 #ifdef CONFIG_XEN
43 # include <xen/hvm/hvm_info_table.h>
44 #endif
46 qemu_irq *ioapic_irq_hack;
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 const char *global_cpu_model; /* cpu hotadd */
56 static void ioapic_init(IsaIrqState *isa_irq_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 isa_irq_state->ioapic[i] = qdev_get_gpio_in(dev, i);
72 /* PC hardware initialisation */
73 static void pc_init1(ram_addr_t ram_size,
74 const char *boot_device,
75 const char *kernel_filename,
76 const char *kernel_cmdline,
77 const char *initrd_filename,
78 const char *cpu_model,
79 int pci_enabled,
80 int kvmclock_enabled)
82 int i;
83 ram_addr_t below_4g_mem_size, above_4g_mem_size;
84 PCIBus *pci_bus;
85 PCII440FXState *i440fx_state;
86 int piix3_devfn = -1;
87 qemu_irq *cpu_irq;
88 qemu_irq *isa_irq;
89 qemu_irq *i8259;
90 qemu_irq *cmos_s3;
91 qemu_irq *smi_irq;
92 IsaIrqState *isa_irq_state;
93 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
94 BusState *idebus[MAX_IDE_BUS];
95 ISADevice *rtc_state;
97 global_cpu_model = cpu_model;
99 pc_cpus_init(cpu_model);
101 if (kvmclock_enabled) {
102 kvmclock_create();
105 if (ram_size >= 0xe0000000 ) {
106 above_4g_mem_size = ram_size - 0xe0000000;
107 below_4g_mem_size = 0xe0000000;
108 } else {
109 above_4g_mem_size = 0;
110 below_4g_mem_size = ram_size;
113 /* allocate ram and load rom/bios */
114 if (!xen_enabled()) {
115 pc_memory_init(kernel_filename, kernel_cmdline, initrd_filename,
116 below_4g_mem_size, above_4g_mem_size);
119 if (!xen_enabled()) {
120 cpu_irq = pc_allocate_cpu_irq();
121 if (!(kvm_enabled() && kvm_irqchip_in_kernel())) {
122 i8259 = i8259_init(cpu_irq[0]);
123 } else {
124 i8259 = kvm_i8259_init(cpu_irq[0]);
126 } else {
127 i8259 = xen_interrupt_controller_init();
129 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
130 isa_irq_state->i8259 = i8259;
131 if (pci_enabled) {
132 ioapic_init(isa_irq_state);
134 if (!(kvm_enabled() && kvm_irqchip_in_kernel())) {
135 isa_irq = qemu_allocate_irqs(isa_irq_handler, isa_irq_state, 24);
136 } else {
137 isa_irq = i8259;
140 if (pci_enabled) {
141 if (!xen_enabled()) {
142 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, isa_irq, ram_size);
143 } else {
144 pci_bus = i440fx_xen_init(&i440fx_state, &piix3_devfn, isa_irq, ram_size);
146 } else {
147 pci_bus = NULL;
148 i440fx_state = NULL;
149 isa_bus_new(NULL);
151 isa_bus_irqs(isa_irq);
153 pc_register_ferr_irq(isa_get_irq(13));
155 pc_vga_init(pci_enabled? pci_bus: NULL);
157 /* init basic PC hardware */
158 pc_basic_device_init(isa_irq, &rtc_state, xen_enabled());
160 for(i = 0; i < nb_nics; i++) {
161 NICInfo *nd = &nd_table[i];
163 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
164 pc_init_ne2k_isa(nd);
165 else
166 pci_nic_init_nofail(nd, "rtl8139", NULL);
169 ide_drive_get(hd, MAX_IDE_BUS);
170 if (pci_enabled) {
171 PCIDevice *dev;
172 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
173 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
174 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
175 } else {
176 for(i = 0; i < MAX_IDE_BUS; i++) {
177 ISADevice *dev;
178 dev = isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
179 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
180 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
184 audio_init(isa_irq, pci_enabled ? pci_bus : NULL);
186 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
187 idebus[0], idebus[1], rtc_state);
189 if (pci_enabled && usb_enabled) {
190 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
193 if (pci_enabled && acpi_enabled) {
194 i2c_bus *smbus;
196 if (!xen_enabled()) {
197 cmos_s3 = qemu_allocate_irqs(pc_cmos_set_s3_resume, rtc_state, 1);
198 } else {
199 cmos_s3 = qemu_allocate_irqs(xen_cmos_set_s3_resume, rtc_state, 1);
201 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
202 /* TODO: Populate SPD eeprom data. */
203 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
204 isa_get_irq(9), *cmos_s3, *smi_irq,
205 kvm_enabled());
206 smbus_eeprom_init(smbus, 8, NULL, 0);
209 if (i440fx_state) {
210 i440fx_init_memory_mappings(i440fx_state);
213 if (pci_enabled) {
214 pc_pci_device_init(pci_bus);
218 static void pc_init_pci(ram_addr_t ram_size,
219 const char *boot_device,
220 const char *kernel_filename,
221 const char *kernel_cmdline,
222 const char *initrd_filename,
223 const char *cpu_model)
225 pc_init1(ram_size, boot_device,
226 kernel_filename, kernel_cmdline,
227 initrd_filename, cpu_model, 1, 1);
230 static void pc_init_pci_no_kvmclock(ram_addr_t ram_size,
231 const char *boot_device,
232 const char *kernel_filename,
233 const char *kernel_cmdline,
234 const char *initrd_filename,
235 const char *cpu_model)
237 pc_init1(ram_size, boot_device,
238 kernel_filename, kernel_cmdline,
239 initrd_filename, cpu_model, 1, 0);
242 static void pc_init_isa(ram_addr_t ram_size,
243 const char *boot_device,
244 const char *kernel_filename,
245 const char *kernel_cmdline,
246 const char *initrd_filename,
247 const char *cpu_model)
249 if (cpu_model == NULL)
250 cpu_model = "486";
251 pc_init1(ram_size, boot_device,
252 kernel_filename, kernel_cmdline,
253 initrd_filename, cpu_model, 0, 1);
256 #ifdef CONFIG_XEN
257 static void pc_xen_hvm_init(ram_addr_t ram_size,
258 const char *boot_device,
259 const char *kernel_filename,
260 const char *kernel_cmdline,
261 const char *initrd_filename,
262 const char *cpu_model)
264 if (xen_hvm_init() != 0) {
265 hw_error("xen hardware virtual machine initialisation failed");
267 pc_init_pci_no_kvmclock(ram_size, boot_device,
268 kernel_filename, kernel_cmdline,
269 initrd_filename, cpu_model);
270 xen_vcpu_init();
272 #endif
274 static QEMUMachine pc_machine = {
275 .name = "pc-0.14",
276 .alias = "pc",
277 .desc = "Standard PC",
278 .init = pc_init_pci,
279 .max_cpus = 255,
280 .is_default = 1,
283 static QEMUMachine pc_machine_v0_13 = {
284 .name = "pc-0.13",
285 .desc = "Standard PC",
286 .init = pc_init_pci_no_kvmclock,
287 .max_cpus = 255,
288 .compat_props = (GlobalProperty[]) {
290 .driver = "virtio-9p-pci",
291 .property = "vectors",
292 .value = stringify(0),
294 .driver = "VGA",
295 .property = "rombar",
296 .value = stringify(0),
298 .driver = "vmware-svga",
299 .property = "rombar",
300 .value = stringify(0),
302 .driver = "PCI",
303 .property = "command_serr_enable",
304 .value = "off",
306 { /* end of list */ }
310 static QEMUMachine pc_machine_v0_12 = {
311 .name = "pc-0.12",
312 .desc = "Standard PC",
313 .init = pc_init_pci_no_kvmclock,
314 .max_cpus = 255,
315 .compat_props = (GlobalProperty[]) {
317 .driver = "virtio-serial-pci",
318 .property = "max_ports",
319 .value = stringify(1),
321 .driver = "virtio-serial-pci",
322 .property = "vectors",
323 .value = stringify(0),
325 .driver = "VGA",
326 .property = "rombar",
327 .value = stringify(0),
329 .driver = "vmware-svga",
330 .property = "rombar",
331 .value = stringify(0),
333 .driver = "PCI",
334 .property = "command_serr_enable",
335 .value = "off",
337 { /* end of list */ }
341 static QEMUMachine pc_machine_v0_11 = {
342 .name = "pc-0.11",
343 .desc = "Standard PC, qemu 0.11",
344 .init = pc_init_pci_no_kvmclock,
345 .max_cpus = 255,
346 .compat_props = (GlobalProperty[]) {
348 .driver = "virtio-blk-pci",
349 .property = "vectors",
350 .value = stringify(0),
352 .driver = "virtio-serial-pci",
353 .property = "max_ports",
354 .value = stringify(1),
356 .driver = "virtio-serial-pci",
357 .property = "vectors",
358 .value = stringify(0),
360 .driver = "ide-drive",
361 .property = "ver",
362 .value = "0.11",
364 .driver = "scsi-disk",
365 .property = "ver",
366 .value = "0.11",
368 .driver = "PCI",
369 .property = "rombar",
370 .value = stringify(0),
372 .driver = "PCI",
373 .property = "command_serr_enable",
374 .value = "off",
376 { /* end of list */ }
380 static QEMUMachine pc_machine_v0_10 = {
381 .name = "pc-0.10",
382 .desc = "Standard PC, qemu 0.10",
383 .init = pc_init_pci_no_kvmclock,
384 .max_cpus = 255,
385 .compat_props = (GlobalProperty[]) {
387 .driver = "virtio-blk-pci",
388 .property = "class",
389 .value = stringify(PCI_CLASS_STORAGE_OTHER),
391 .driver = "virtio-serial-pci",
392 .property = "class",
393 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
395 .driver = "virtio-serial-pci",
396 .property = "max_ports",
397 .value = stringify(1),
399 .driver = "virtio-serial-pci",
400 .property = "vectors",
401 .value = stringify(0),
403 .driver = "virtio-net-pci",
404 .property = "vectors",
405 .value = stringify(0),
407 .driver = "virtio-blk-pci",
408 .property = "vectors",
409 .value = stringify(0),
411 .driver = "ide-drive",
412 .property = "ver",
413 .value = "0.10",
415 .driver = "scsi-disk",
416 .property = "ver",
417 .value = "0.10",
419 .driver = "PCI",
420 .property = "rombar",
421 .value = stringify(0),
423 .driver = "PCI",
424 .property = "command_serr_enable",
425 .value = "off",
427 { /* end of list */ }
431 static QEMUMachine isapc_machine = {
432 .name = "isapc",
433 .desc = "ISA-only PC",
434 .init = pc_init_isa,
435 .max_cpus = 1,
438 #ifdef CONFIG_XEN
439 static QEMUMachine xenfv_machine = {
440 .name = "xenfv",
441 .desc = "Xen Fully-virtualized PC",
442 .init = pc_xen_hvm_init,
443 .max_cpus = HVM_MAX_VCPUS,
444 .default_machine_opts = "accel=xen",
446 #endif
448 static void pc_machine_init(void)
450 qemu_register_machine(&pc_machine);
451 qemu_register_machine(&pc_machine_v0_13);
452 qemu_register_machine(&pc_machine_v0_12);
453 qemu_register_machine(&pc_machine_v0_11);
454 qemu_register_machine(&pc_machine_v0_10);
455 qemu_register_machine(&isapc_machine);
456 #ifdef CONFIG_XEN
457 qemu_register_machine(&xenfv_machine);
458 #endif
461 machine_init(pc_machine_init);