test: export vm helpers
[qemu-kvm/amd-iommu.git] / hw / pc_piix.c
blob6f477eda0b1d515fb115ffd1c73c4a428d9c53b6
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"
38 qemu_irq *ioapic_irq_hack;
40 #define MAX_IDE_BUS 2
42 static const int ide_iobase[MAX_IDE_BUS] = { 0x1f0, 0x170 };
43 static const int ide_iobase2[MAX_IDE_BUS] = { 0x3f6, 0x376 };
44 static const int ide_irq[MAX_IDE_BUS] = { 14, 15 };
46 const char *global_cpu_model; /* cpu hotadd */
48 static void ioapic_init(IsaIrqState *isa_irq_state)
50 DeviceState *dev;
51 SysBusDevice *d;
52 unsigned int i;
54 dev = qdev_create(NULL, "ioapic");
55 qdev_init_nofail(dev);
56 d = sysbus_from_qdev(dev);
57 sysbus_mmio_map(d, 0, 0xfec00000);
59 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
60 isa_irq_state->ioapic[i] = qdev_get_gpio_in(dev, i);
64 /* PC hardware initialisation */
65 static void pc_init1(ram_addr_t ram_size,
66 const char *boot_device,
67 const char *kernel_filename,
68 const char *kernel_cmdline,
69 const char *initrd_filename,
70 const char *cpu_model,
71 int pci_enabled)
73 int i;
74 ram_addr_t below_4g_mem_size, above_4g_mem_size;
75 PCIBus *pci_bus;
76 PCII440FXState *i440fx_state;
77 int piix3_devfn = -1;
78 qemu_irq *cpu_irq;
79 qemu_irq *isa_irq;
80 qemu_irq *i8259;
81 qemu_irq *cmos_s3;
82 qemu_irq *smi_irq;
83 IsaIrqState *isa_irq_state;
84 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
85 FDCtrl *floppy_controller;
86 ISADevice *rtc_state;
88 global_cpu_model = cpu_model;
90 pc_cpus_init(cpu_model);
92 vmport_init();
94 /* allocate ram and load rom/bios */
95 pc_memory_init(ram_size, kernel_filename, kernel_cmdline, initrd_filename,
96 &below_4g_mem_size, &above_4g_mem_size);
98 cpu_irq = pc_allocate_cpu_irq();
99 #ifdef KVM_CAP_IRQCHIP
100 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
101 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
102 if (pci_enabled) {
103 ioapic_init(isa_irq_state);
105 isa_irq = i8259 = kvm_i8259_init(cpu_irq[0]);
106 ioapic_irq_hack = isa_irq;
107 } else
108 #endif
110 i8259 = i8259_init(cpu_irq[0]);
111 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
112 isa_irq_state->i8259 = i8259;
113 if (pci_enabled) {
114 ioapic_init(isa_irq_state);
116 isa_irq = qemu_allocate_irqs(isa_irq_handler, isa_irq_state, 24);
119 if (pci_enabled) {
120 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, isa_irq, ram_size);
121 } else {
122 pci_bus = NULL;
123 isa_bus_new(NULL);
125 isa_bus_irqs(isa_irq);
127 pc_register_ferr_irq(isa_reserve_irq(13));
128 cpu_irq = pc_allocate_cpu_irq();
130 pc_vga_init(pci_enabled? pci_bus: NULL);
132 /* init basic PC hardware */
133 pc_basic_device_init(isa_irq, &floppy_controller, &rtc_state);
135 for(i = 0; i < nb_nics; i++) {
136 NICInfo *nd = &nd_table[i];
138 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
139 pc_init_ne2k_isa(nd);
140 else
141 pci_nic_init_nofail(nd, "rtl8139", NULL);
144 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
145 fprintf(stderr, "qemu: too many IDE bus\n");
146 exit(1);
149 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
150 hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
153 if (pci_enabled) {
154 pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
155 } else {
156 for(i = 0; i < MAX_IDE_BUS; i++) {
157 isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
158 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
162 pc_audio_init(pci_enabled ? pci_bus : NULL, isa_irq);
164 pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd,
165 floppy_controller, rtc_state);
167 if (pci_enabled && usb_enabled) {
168 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
171 if (pci_enabled && acpi_enabled) {
172 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
173 i2c_bus *smbus;
175 cmos_s3 = qemu_allocate_irqs(pc_cmos_set_s3_resume, rtc_state, 1);
176 smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);
177 /* TODO: Populate SPD eeprom data. */
178 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
179 isa_reserve_irq(9), *cmos_s3, *smi_irq,
180 kvm_enabled());
181 for (i = 0; i < 8; i++) {
182 DeviceState *eeprom;
183 eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
184 qdev_prop_set_uint8(eeprom, "address", 0x50 + i);
185 qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
186 qdev_init_nofail(eeprom);
190 if (i440fx_state) {
191 i440fx_init_memory_mappings(i440fx_state);
194 if (pci_enabled) {
195 pc_pci_device_init(pci_bus);
199 static void pc_init_pci(ram_addr_t ram_size,
200 const char *boot_device,
201 const char *kernel_filename,
202 const char *kernel_cmdline,
203 const char *initrd_filename,
204 const char *cpu_model)
206 pc_init1(ram_size, boot_device,
207 kernel_filename, kernel_cmdline,
208 initrd_filename, cpu_model, 1);
211 static void pc_init_isa(ram_addr_t ram_size,
212 const char *boot_device,
213 const char *kernel_filename,
214 const char *kernel_cmdline,
215 const char *initrd_filename,
216 const char *cpu_model)
218 if (cpu_model == NULL)
219 cpu_model = "486";
220 pc_init1(ram_size, boot_device,
221 kernel_filename, kernel_cmdline,
222 initrd_filename, cpu_model, 0);
225 static QEMUMachine pc_machine = {
226 .name = "pc-0.13",
227 .alias = "pc",
228 .desc = "Standard PC",
229 .init = pc_init_pci,
230 .max_cpus = 255,
231 .is_default = 1,
234 static QEMUMachine pc_machine_v0_12 = {
235 .name = "pc-0.12",
236 .desc = "Standard PC",
237 .init = pc_init_pci,
238 .max_cpus = 255,
239 .compat_props = (GlobalProperty[]) {
241 .driver = "virtio-serial-pci",
242 .property = "max_nr_ports",
243 .value = stringify(1),
245 .driver = "virtio-serial-pci",
246 .property = "vectors",
247 .value = stringify(0),
249 { /* end of list */ }
253 static QEMUMachine pc_machine_v0_11 = {
254 .name = "pc-0.11",
255 .desc = "Standard PC, qemu 0.11",
256 .init = pc_init_pci,
257 .max_cpus = 255,
258 .compat_props = (GlobalProperty[]) {
260 .driver = "virtio-blk-pci",
261 .property = "vectors",
262 .value = stringify(0),
264 .driver = "virtio-serial-pci",
265 .property = "max_nr_ports",
266 .value = stringify(1),
268 .driver = "virtio-serial-pci",
269 .property = "vectors",
270 .value = stringify(0),
272 .driver = "ide-drive",
273 .property = "ver",
274 .value = "0.11",
276 .driver = "scsi-disk",
277 .property = "ver",
278 .value = "0.11",
280 .driver = "PCI",
281 .property = "rombar",
282 .value = stringify(0),
284 { /* end of list */ }
288 static QEMUMachine pc_machine_v0_10 = {
289 .name = "pc-0.10",
290 .desc = "Standard PC, qemu 0.10",
291 .init = pc_init_pci,
292 .max_cpus = 255,
293 .compat_props = (GlobalProperty[]) {
295 .driver = "virtio-blk-pci",
296 .property = "class",
297 .value = stringify(PCI_CLASS_STORAGE_OTHER),
299 .driver = "virtio-serial-pci",
300 .property = "class",
301 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
303 .driver = "virtio-serial-pci",
304 .property = "max_nr_ports",
305 .value = stringify(1),
307 .driver = "virtio-serial-pci",
308 .property = "vectors",
309 .value = stringify(0),
311 .driver = "virtio-net-pci",
312 .property = "vectors",
313 .value = stringify(0),
315 .driver = "virtio-blk-pci",
316 .property = "vectors",
317 .value = stringify(0),
319 .driver = "ide-drive",
320 .property = "ver",
321 .value = "0.10",
323 .driver = "scsi-disk",
324 .property = "ver",
325 .value = "0.10",
327 .driver = "PCI",
328 .property = "rombar",
329 .value = stringify(0),
331 { /* end of list */ }
335 static QEMUMachine isapc_machine = {
336 .name = "isapc",
337 .desc = "ISA-only PC",
338 .init = pc_init_isa,
339 .max_cpus = 1,
342 static void pc_machine_init(void)
344 qemu_register_machine(&pc_machine);
345 qemu_register_machine(&pc_machine_v0_12);
346 qemu_register_machine(&pc_machine_v0_11);
347 qemu_register_machine(&pc_machine_v0_10);
348 qemu_register_machine(&isapc_machine);
351 machine_init(pc_machine_init);