Merge branch 'master' of git://git.sv.gnu.org/qemu into next
[qemu/qemu-dev-zwu.git] / hw / pc_piix.c
blob3d07ce51b7019c7b05dcedbb27db76fa3eff9070
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));
131 cpu_irq = pc_allocate_cpu_irq();
133 pc_vga_init(pci_enabled? pci_bus: NULL);
135 /* init basic PC hardware */
136 pc_basic_device_init(isa_irq, &floppy_controller, &rtc_state);
138 for(i = 0; i < nb_nics; i++) {
139 NICInfo *nd = &nd_table[i];
141 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
142 pc_init_ne2k_isa(nd);
143 else
144 pci_nic_init_nofail(nd, "rtl8139", NULL);
147 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
148 fprintf(stderr, "qemu: too many IDE bus\n");
149 exit(1);
152 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
153 hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
156 if (pci_enabled) {
157 PCIDevice *dev;
158 dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
159 idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
160 idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
161 } else {
162 for(i = 0; i < MAX_IDE_BUS; i++) {
163 ISADevice *dev;
164 dev = isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
165 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
166 idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
170 pc_audio_init(pci_enabled ? pci_bus : NULL, isa_irq);
172 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
173 idebus[0], idebus[1], floppy_controller, rtc_state);
175 if (pci_enabled && usb_enabled) {
176 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
179 if (pci_enabled && acpi_enabled) {
180 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
181 i2c_bus *smbus;
183 cmos_s3 = qemu_allocate_irqs(pc_cmos_set_s3_resume, rtc_state, 1);
184 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
185 /* TODO: Populate SPD eeprom data. */
186 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
187 isa_reserve_irq(9), *cmos_s3, *smi_irq,
188 kvm_enabled());
189 for (i = 0; i < 8; i++) {
190 DeviceState *eeprom;
191 eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
192 qdev_prop_set_uint8(eeprom, "address", 0x50 + i);
193 qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
194 qdev_init_nofail(eeprom);
198 if (i440fx_state) {
199 i440fx_init_memory_mappings(i440fx_state);
202 if (pci_enabled) {
203 pc_pci_device_init(pci_bus);
207 static void pc_init_pci(ram_addr_t ram_size,
208 const char *boot_device,
209 const char *kernel_filename,
210 const char *kernel_cmdline,
211 const char *initrd_filename,
212 const char *cpu_model)
214 pc_init1(ram_size, boot_device,
215 kernel_filename, kernel_cmdline,
216 initrd_filename, cpu_model, 1);
219 static void pc_init_isa(ram_addr_t ram_size,
220 const char *boot_device,
221 const char *kernel_filename,
222 const char *kernel_cmdline,
223 const char *initrd_filename,
224 const char *cpu_model)
226 if (cpu_model == NULL)
227 cpu_model = "486";
228 pc_init1(ram_size, boot_device,
229 kernel_filename, kernel_cmdline,
230 initrd_filename, cpu_model, 0);
233 static QEMUMachine pc_machine = {
234 .name = "pc-0.13",
235 .alias = "pc",
236 .desc = "Standard PC",
237 .init = pc_init_pci,
238 .max_cpus = 255,
239 .is_default = 1,
242 static QEMUMachine pc_machine_v0_12 = {
243 .name = "pc-0.12",
244 .desc = "Standard PC",
245 .init = pc_init_pci,
246 .max_cpus = 255,
247 .compat_props = (GlobalProperty[]) {
249 .driver = "virtio-serial-pci",
250 .property = "max_ports",
251 .value = stringify(1),
253 .driver = "virtio-serial-pci",
254 .property = "vectors",
255 .value = stringify(0),
257 { /* end of list */ }
261 static QEMUMachine pc_machine_v0_11 = {
262 .name = "pc-0.11",
263 .desc = "Standard PC, qemu 0.11",
264 .init = pc_init_pci,
265 .max_cpus = 255,
266 .compat_props = (GlobalProperty[]) {
268 .driver = "virtio-blk-pci",
269 .property = "vectors",
270 .value = stringify(0),
272 .driver = "virtio-serial-pci",
273 .property = "max_ports",
274 .value = stringify(1),
276 .driver = "virtio-serial-pci",
277 .property = "vectors",
278 .value = stringify(0),
280 .driver = "ide-drive",
281 .property = "ver",
282 .value = "0.11",
284 .driver = "scsi-disk",
285 .property = "ver",
286 .value = "0.11",
288 .driver = "PCI",
289 .property = "rombar",
290 .value = stringify(0),
292 { /* end of list */ }
296 static QEMUMachine pc_machine_v0_10 = {
297 .name = "pc-0.10",
298 .desc = "Standard PC, qemu 0.10",
299 .init = pc_init_pci,
300 .max_cpus = 255,
301 .compat_props = (GlobalProperty[]) {
303 .driver = "virtio-blk-pci",
304 .property = "class",
305 .value = stringify(PCI_CLASS_STORAGE_OTHER),
307 .driver = "virtio-serial-pci",
308 .property = "class",
309 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
311 .driver = "virtio-serial-pci",
312 .property = "max_ports",
313 .value = stringify(1),
315 .driver = "virtio-serial-pci",
316 .property = "vectors",
317 .value = stringify(0),
319 .driver = "virtio-net-pci",
320 .property = "vectors",
321 .value = stringify(0),
323 .driver = "virtio-blk-pci",
324 .property = "vectors",
325 .value = stringify(0),
327 .driver = "ide-drive",
328 .property = "ver",
329 .value = "0.10",
331 .driver = "scsi-disk",
332 .property = "ver",
333 .value = "0.10",
335 .driver = "PCI",
336 .property = "rombar",
337 .value = stringify(0),
339 { /* end of list */ }
343 static QEMUMachine isapc_machine = {
344 .name = "isapc",
345 .desc = "ISA-only PC",
346 .init = pc_init_isa,
347 .max_cpus = 1,
350 static void pc_machine_init(void)
352 qemu_register_machine(&pc_machine);
353 qemu_register_machine(&pc_machine_v0_12);
354 qemu_register_machine(&pc_machine_v0_11);
355 qemu_register_machine(&pc_machine_v0_10);
356 qemu_register_machine(&isapc_machine);
359 machine_init(pc_machine_init);