Merge commit 'f711df67d611e4762966a249742a5f7499e19f99' into upstream-merge
[qemu-kvm/stefanha.git] / hw / pc_piix.c
blobfbe276638c33b2a87b9a81a13c82447f28361a05
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 "sysemu.h"
36 #include "sysbus.h"
37 #include "blockdev.h"
39 qemu_irq *ioapic_irq_hack;
41 #define MAX_IDE_BUS 2
43 static const int ide_iobase[MAX_IDE_BUS] = { 0x1f0, 0x170 };
44 static const int ide_iobase2[MAX_IDE_BUS] = { 0x3f6, 0x376 };
45 static const int ide_irq[MAX_IDE_BUS] = { 14, 15 };
47 const char *global_cpu_model; /* cpu hotadd */
49 static void ioapic_init(IsaIrqState *isa_irq_state)
51 DeviceState *dev;
52 SysBusDevice *d;
53 unsigned int i;
55 dev = qdev_create(NULL, "ioapic");
56 qdev_init_nofail(dev);
57 d = sysbus_from_qdev(dev);
58 sysbus_mmio_map(d, 0, 0xfec00000);
60 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
61 isa_irq_state->ioapic[i] = qdev_get_gpio_in(dev, i);
65 /* PC hardware initialisation */
66 static void pc_init1(ram_addr_t ram_size,
67 const char *boot_device,
68 const char *kernel_filename,
69 const char *kernel_cmdline,
70 const char *initrd_filename,
71 const char *cpu_model,
72 int pci_enabled)
74 int i;
75 ram_addr_t below_4g_mem_size, above_4g_mem_size;
76 PCIBus *pci_bus;
77 PCII440FXState *i440fx_state;
78 int piix3_devfn = -1;
79 qemu_irq *cpu_irq;
80 qemu_irq *isa_irq;
81 qemu_irq *i8259;
82 qemu_irq *cmos_s3;
83 qemu_irq *smi_irq;
84 IsaIrqState *isa_irq_state;
85 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
86 FDCtrl *floppy_controller;
87 BusState *idebus[MAX_IDE_BUS];
88 ISADevice *rtc_state;
90 global_cpu_model = cpu_model;
92 pc_cpus_init(cpu_model);
94 vmport_init();
96 /* allocate ram and load rom/bios */
97 pc_memory_init(ram_size, kernel_filename, kernel_cmdline, initrd_filename,
98 &below_4g_mem_size, &above_4g_mem_size);
100 cpu_irq = pc_allocate_cpu_irq();
101 #ifdef KVM_CAP_IRQCHIP
102 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
103 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
104 if (pci_enabled) {
105 ioapic_init(isa_irq_state);
107 isa_irq = i8259 = kvm_i8259_init(cpu_irq[0]);
108 ioapic_irq_hack = isa_irq;
109 } else
110 #endif
112 i8259 = i8259_init(cpu_irq[0]);
113 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
114 isa_irq_state->i8259 = i8259;
115 if (pci_enabled) {
116 ioapic_init(isa_irq_state);
118 isa_irq = qemu_allocate_irqs(isa_irq_handler, isa_irq_state, 24);
121 if (pci_enabled) {
122 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, isa_irq, ram_size);
123 } else {
124 pci_bus = NULL;
125 i440fx_state = NULL;
126 isa_bus_new(NULL);
128 isa_bus_irqs(isa_irq);
130 pc_register_ferr_irq(isa_reserve_irq(13));
132 pc_vga_init(pci_enabled? pci_bus: NULL);
134 /* init basic PC hardware */
135 pc_basic_device_init(isa_irq, &floppy_controller, &rtc_state);
137 for(i = 0; i < nb_nics; i++) {
138 NICInfo *nd = &nd_table[i];
140 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
141 pc_init_ne2k_isa(nd);
142 else
143 pci_nic_init_nofail(nd, "rtl8139", NULL);
146 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
147 fprintf(stderr, "qemu: too many IDE bus\n");
148 exit(1);
151 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
152 hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
155 if (pci_enabled) {
156 PCIDevice *dev;
157 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
158 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
159 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
160 } else {
161 for(i = 0; i < MAX_IDE_BUS; i++) {
162 ISADevice *dev;
163 dev = isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
164 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
165 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
169 pc_audio_init(pci_enabled ? pci_bus : NULL, isa_irq);
171 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
172 idebus[0], idebus[1], floppy_controller, rtc_state);
174 if (pci_enabled && usb_enabled) {
175 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
178 if (pci_enabled && acpi_enabled) {
179 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
180 i2c_bus *smbus;
182 cmos_s3 = qemu_allocate_irqs(pc_cmos_set_s3_resume, rtc_state, 1);
183 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
184 /* TODO: Populate SPD eeprom data. */
185 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
186 isa_reserve_irq(9), *cmos_s3, *smi_irq,
187 kvm_enabled());
188 for (i = 0; i < 8; i++) {
189 DeviceState *eeprom;
190 eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
191 qdev_prop_set_uint8(eeprom, "address", 0x50 + i);
192 qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
193 qdev_init_nofail(eeprom);
197 if (i440fx_state) {
198 i440fx_init_memory_mappings(i440fx_state);
201 if (pci_enabled) {
202 pc_pci_device_init(pci_bus);
206 static void pc_init_pci(ram_addr_t ram_size,
207 const char *boot_device,
208 const char *kernel_filename,
209 const char *kernel_cmdline,
210 const char *initrd_filename,
211 const char *cpu_model)
213 pc_init1(ram_size, boot_device,
214 kernel_filename, kernel_cmdline,
215 initrd_filename, cpu_model, 1);
218 static void pc_init_isa(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 if (cpu_model == NULL)
226 cpu_model = "486";
227 pc_init1(ram_size, boot_device,
228 kernel_filename, kernel_cmdline,
229 initrd_filename, cpu_model, 0);
232 static QEMUMachine pc_machine = {
233 .name = "pc-0.14",
234 .alias = "pc",
235 .desc = "Standard PC",
236 .init = pc_init_pci,
237 .max_cpus = 255,
238 .is_default = 1,
241 static QEMUMachine pc_machine_v0_13 = {
242 .name = "pc-0.13",
243 .desc = "Standard PC",
244 .init = pc_init_pci,
245 .max_cpus = 255,
246 .compat_props = (GlobalProperty[]) {
248 .driver = "virtio-9p-pci",
249 .property = "vectors",
250 .value = stringify(0),
252 .driver = "VGA",
253 .property = "rombar",
254 .value = stringify(0),
256 .driver = "vmware-svga",
257 .property = "rombar",
258 .value = stringify(0),
260 { /* end of list */ }
264 static QEMUMachine pc_machine_v0_12 = {
265 .name = "pc-0.12",
266 .desc = "Standard PC",
267 .init = pc_init_pci,
268 .max_cpus = 255,
269 .compat_props = (GlobalProperty[]) {
271 .driver = "virtio-serial-pci",
272 .property = "max_ports",
273 .value = stringify(1),
275 .driver = "virtio-serial-pci",
276 .property = "vectors",
277 .value = stringify(0),
279 .driver = "VGA",
280 .property = "rombar",
281 .value = stringify(0),
283 .driver = "vmware-svga",
284 .property = "rombar",
285 .value = stringify(0),
287 { /* end of list */ }
291 static QEMUMachine pc_machine_v0_11 = {
292 .name = "pc-0.11",
293 .desc = "Standard PC, qemu 0.11",
294 .init = pc_init_pci,
295 .max_cpus = 255,
296 .compat_props = (GlobalProperty[]) {
298 .driver = "virtio-blk-pci",
299 .property = "vectors",
300 .value = stringify(0),
302 .driver = "virtio-serial-pci",
303 .property = "max_ports",
304 .value = stringify(1),
306 .driver = "virtio-serial-pci",
307 .property = "vectors",
308 .value = stringify(0),
310 .driver = "ide-drive",
311 .property = "ver",
312 .value = "0.11",
314 .driver = "scsi-disk",
315 .property = "ver",
316 .value = "0.11",
318 .driver = "PCI",
319 .property = "rombar",
320 .value = stringify(0),
322 { /* end of list */ }
326 static QEMUMachine pc_machine_v0_10 = {
327 .name = "pc-0.10",
328 .desc = "Standard PC, qemu 0.10",
329 .init = pc_init_pci,
330 .max_cpus = 255,
331 .compat_props = (GlobalProperty[]) {
333 .driver = "virtio-blk-pci",
334 .property = "class",
335 .value = stringify(PCI_CLASS_STORAGE_OTHER),
337 .driver = "virtio-serial-pci",
338 .property = "class",
339 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
341 .driver = "virtio-serial-pci",
342 .property = "max_ports",
343 .value = stringify(1),
345 .driver = "virtio-serial-pci",
346 .property = "vectors",
347 .value = stringify(0),
349 .driver = "virtio-net-pci",
350 .property = "vectors",
351 .value = stringify(0),
353 .driver = "virtio-blk-pci",
354 .property = "vectors",
355 .value = stringify(0),
357 .driver = "ide-drive",
358 .property = "ver",
359 .value = "0.10",
361 .driver = "scsi-disk",
362 .property = "ver",
363 .value = "0.10",
365 .driver = "PCI",
366 .property = "rombar",
367 .value = stringify(0),
369 { /* end of list */ }
373 static QEMUMachine isapc_machine = {
374 .name = "isapc",
375 .desc = "ISA-only PC",
376 .init = pc_init_isa,
377 .max_cpus = 1,
380 static void pc_machine_init(void)
382 qemu_register_machine(&pc_machine);
383 qemu_register_machine(&pc_machine_v0_13);
384 qemu_register_machine(&pc_machine_v0_12);
385 qemu_register_machine(&pc_machine_v0_11);
386 qemu_register_machine(&pc_machine_v0_10);
387 qemu_register_machine(&isapc_machine);
390 machine_init(pc_machine_init);