xref: /qemu/hw/i386/pc_piix.c (revision f03868bd5653265e97b253102d77d83ea85efdea)
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 "qemu/osdep.h"
26 
27 #include "hw/hw.h"
28 #include "hw/loader.h"
29 #include "hw/i386/pc.h"
30 #include "hw/i386/apic.h"
31 #include "hw/smbios/smbios.h"
32 #include "hw/pci/pci.h"
33 #include "hw/pci/pci_ids.h"
34 #include "hw/usb.h"
35 #include "net/net.h"
36 #include "hw/boards.h"
37 #include "hw/ide.h"
38 #include "sysemu/kvm.h"
39 #include "hw/kvm/clock.h"
40 #include "sysemu/sysemu.h"
41 #include "hw/sysbus.h"
42 #include "sysemu/arch_init.h"
43 #include "hw/i2c/smbus.h"
44 #include "hw/xen/xen.h"
45 #include "exec/memory.h"
46 #include "exec/address-spaces.h"
47 #include "hw/acpi/acpi.h"
48 #include "cpu.h"
49 #include "qapi/error.h"
50 #include "qemu/error-report.h"
51 #ifdef CONFIG_XEN
52 #include <xen/hvm/hvm_info_table.h>
53 #include "hw/xen/xen_pt.h"
54 #endif
55 #include "migration/global_state.h"
56 #include "migration/misc.h"
57 #include "kvm_i386.h"
58 #include "sysemu/numa.h"
59 
60 #define MAX_IDE_BUS 2
61 
62 static const int ide_iobase[MAX_IDE_BUS] = { 0x1f0, 0x170 };
63 static const int ide_iobase2[MAX_IDE_BUS] = { 0x3f6, 0x376 };
64 static const int ide_irq[MAX_IDE_BUS] = { 14, 15 };
65 
66 /* PC hardware initialisation */
67 static void pc_init1(MachineState *machine,
68                      const char *host_type, const char *pci_type)
69 {
70     PCMachineState *pcms = PC_MACHINE(machine);
71     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
72     MemoryRegion *system_memory = get_system_memory();
73     MemoryRegion *system_io = get_system_io();
74     int i;
75     PCIBus *pci_bus;
76     ISABus *isa_bus;
77     PCII440FXState *i440fx_state;
78     int piix3_devfn = -1;
79     qemu_irq *i8259;
80     qemu_irq smi_irq;
81     GSIState *gsi_state;
82     DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
83     BusState *idebus[MAX_IDE_BUS];
84     ISADevice *rtc_state;
85     MemoryRegion *ram_memory;
86     MemoryRegion *pci_memory;
87     MemoryRegion *rom_memory;
88     ram_addr_t lowmem;
89 
90     /*
91      * Calculate ram split, for memory below and above 4G.  It's a bit
92      * complicated for backward compatibility reasons ...
93      *
94      *  - Traditional split is 3.5G (lowmem = 0xe0000000).  This is the
95      *    default value for max_ram_below_4g now.
96      *
97      *  - Then, to gigabyte align the memory, we move the split to 3G
98      *    (lowmem = 0xc0000000).  But only in case we have to split in
99      *    the first place, i.e. ram_size is larger than (traditional)
100      *    lowmem.  And for new machine types (gigabyte_align = true)
101      *    only, for live migration compatibility reasons.
102      *
103      *  - Next the max-ram-below-4g option was added, which allowed to
104      *    reduce lowmem to a smaller value, to allow a larger PCI I/O
105      *    window below 4G.  qemu doesn't enforce gigabyte alignment here,
106      *    but prints a warning.
107      *
108      *  - Finally max-ram-below-4g got updated to also allow raising lowmem,
109      *    so legacy non-PAE guests can get as much memory as possible in
110      *    the 32bit address space below 4G.
111      *
112      *  - Note that Xen has its own ram setp code in xen_ram_init(),
113      *    called via xen_hvm_init().
114      *
115      * Examples:
116      *    qemu -M pc-1.7 -m 4G    (old default)    -> 3584M low,  512M high
117      *    qemu -M pc -m 4G        (new default)    -> 3072M low, 1024M high
118      *    qemu -M pc,max-ram-below-4g=2G -m 4G     -> 2048M low, 2048M high
119      *    qemu -M pc,max-ram-below-4g=4G -m 3968M  -> 3968M low (=4G-128M)
120      */
121     if (xen_enabled()) {
122         xen_hvm_init(pcms, &ram_memory);
123     } else {
124         if (!pcms->max_ram_below_4g) {
125             pcms->max_ram_below_4g = 0xe0000000; /* default: 3.5G */
126         }
127         lowmem = pcms->max_ram_below_4g;
128         if (machine->ram_size >= pcms->max_ram_below_4g) {
129             if (pcmc->gigabyte_align) {
130                 if (lowmem > 0xc0000000) {
131                     lowmem = 0xc0000000;
132                 }
133                 if (lowmem & ((1ULL << 30) - 1)) {
134                     warn_report("Large machine and max_ram_below_4g "
135                                 "(%" PRIu64 ") not a multiple of 1G; "
136                                 "possible bad performance.",
137                                 pcms->max_ram_below_4g);
138                 }
139             }
140         }
141 
142         if (machine->ram_size >= lowmem) {
143             pcms->above_4g_mem_size = machine->ram_size - lowmem;
144             pcms->below_4g_mem_size = lowmem;
145         } else {
146             pcms->above_4g_mem_size = 0;
147             pcms->below_4g_mem_size = machine->ram_size;
148         }
149     }
150 
151     pc_cpus_init(pcms);
152 
153     if (kvm_enabled() && pcmc->kvmclock_enabled) {
154         kvmclock_create();
155     }
156 
157     if (pcmc->pci_enabled) {
158         pci_memory = g_new(MemoryRegion, 1);
159         memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
160         rom_memory = pci_memory;
161     } else {
162         pci_memory = NULL;
163         rom_memory = system_memory;
164     }
165 
166     pc_guest_info_init(pcms);
167 
168     if (pcmc->smbios_defaults) {
169         MachineClass *mc = MACHINE_GET_CLASS(machine);
170         /* These values are guest ABI, do not change */
171         smbios_set_defaults("QEMU", "Standard PC (i440FX + PIIX, 1996)",
172                             mc->name, pcmc->smbios_legacy_mode,
173                             pcmc->smbios_uuid_encoded,
174                             SMBIOS_ENTRY_POINT_21);
175     }
176 
177     /* allocate ram and load rom/bios */
178     if (!xen_enabled()) {
179         pc_memory_init(pcms, system_memory,
180                        rom_memory, &ram_memory);
181     } else if (machine->kernel_filename != NULL) {
182         /* For xen HVM direct kernel boot, load linux here */
183         xen_load_linux(pcms);
184     }
185 
186     gsi_state = g_malloc0(sizeof(*gsi_state));
187     if (kvm_ioapic_in_kernel()) {
188         kvm_pc_setup_irq_routing(pcmc->pci_enabled);
189         pcms->gsi = qemu_allocate_irqs(kvm_pc_gsi_handler, gsi_state,
190                                        GSI_NUM_PINS);
191     } else {
192         pcms->gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
193     }
194 
195     if (pcmc->pci_enabled) {
196         pci_bus = i440fx_init(host_type,
197                               pci_type,
198                               &i440fx_state, &piix3_devfn, &isa_bus, pcms->gsi,
199                               system_memory, system_io, machine->ram_size,
200                               pcms->below_4g_mem_size,
201                               pcms->above_4g_mem_size,
202                               pci_memory, ram_memory);
203         pcms->bus = pci_bus;
204     } else {
205         pci_bus = NULL;
206         i440fx_state = NULL;
207         isa_bus = isa_bus_new(NULL, get_system_memory(), system_io,
208                               &error_abort);
209         no_hpet = 1;
210     }
211     isa_bus_irqs(isa_bus, pcms->gsi);
212 
213     if (kvm_pic_in_kernel()) {
214         i8259 = kvm_i8259_init(isa_bus);
215     } else if (xen_enabled()) {
216         i8259 = xen_interrupt_controller_init();
217     } else {
218         i8259 = i8259_init(isa_bus, pc_allocate_cpu_irq());
219     }
220 
221     for (i = 0; i < ISA_NUM_IRQS; i++) {
222         gsi_state->i8259_irq[i] = i8259[i];
223     }
224     g_free(i8259);
225     if (pcmc->pci_enabled) {
226         ioapic_init_gsi(gsi_state, "i440fx");
227     }
228 
229     pc_register_ferr_irq(pcms->gsi[13]);
230 
231     pc_vga_init(isa_bus, pcmc->pci_enabled ? pci_bus : NULL);
232 
233     assert(pcms->vmport != ON_OFF_AUTO__MAX);
234     if (pcms->vmport == ON_OFF_AUTO_AUTO) {
235         pcms->vmport = xen_enabled() ? ON_OFF_AUTO_OFF : ON_OFF_AUTO_ON;
236     }
237 
238     /* init basic PC hardware */
239     pc_basic_device_init(isa_bus, pcms->gsi, &rtc_state, true,
240                          (pcms->vmport != ON_OFF_AUTO_ON), pcms->pit, 0x4);
241 
242     pc_nic_init(pcmc, isa_bus, pci_bus);
243 
244     ide_drive_get(hd, ARRAY_SIZE(hd));
245     if (pcmc->pci_enabled) {
246         PCIDevice *dev;
247         if (xen_enabled()) {
248             dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);
249         } else {
250             dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
251         }
252         idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
253         idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
254     } else {
255         for(i = 0; i < MAX_IDE_BUS; i++) {
256             ISADevice *dev;
257             char busname[] = "ide.0";
258             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
259                                ide_irq[i],
260                                hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
261             /*
262              * The ide bus name is ide.0 for the first bus and ide.1 for the
263              * second one.
264              */
265             busname[4] = '0' + i;
266             idebus[i] = qdev_get_child_bus(DEVICE(dev), busname);
267         }
268     }
269 
270     pc_cmos_init(pcms, idebus[0], idebus[1], rtc_state);
271 
272     if (pcmc->pci_enabled && machine_usb(machine)) {
273         pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
274     }
275 
276     if (pcmc->pci_enabled && acpi_enabled) {
277         DeviceState *piix4_pm;
278         I2CBus *smbus;
279 
280         smi_irq = qemu_allocate_irq(pc_acpi_smi_interrupt, first_cpu, 0);
281         /* TODO: Populate SPD eeprom data.  */
282         smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
283                               pcms->gsi[9], smi_irq,
284                               pc_machine_is_smm_enabled(pcms),
285                               &piix4_pm);
286         smbus_eeprom_init(smbus, 8, NULL, 0);
287 
288         object_property_add_link(OBJECT(machine), PC_MACHINE_ACPI_DEVICE_PROP,
289                                  TYPE_HOTPLUG_HANDLER,
290                                  (Object **)&pcms->acpi_dev,
291                                  object_property_allow_set_link,
292                                  OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort);
293         object_property_set_link(OBJECT(machine), OBJECT(piix4_pm),
294                                  PC_MACHINE_ACPI_DEVICE_PROP, &error_abort);
295     }
296 
297     if (pcms->acpi_nvdimm_state.is_enabled) {
298         nvdimm_init_acpi_state(&pcms->acpi_nvdimm_state, system_io,
299                                pcms->fw_cfg, OBJECT(pcms));
300     }
301 }
302 
303 /* Looking for a pc_compat_2_4() function? It doesn't exist.
304  * pc_compat_*() functions that run on machine-init time and
305  * change global QEMU state are deprecated. Please don't create
306  * one, and implement any pc-*-2.4 (and newer) compat code in
307  * HW_COMPAT_*, PC_COMPAT_*, or * pc_*_machine_options().
308  */
309 
310 static void pc_compat_2_3(MachineState *machine)
311 {
312     PCMachineState *pcms = PC_MACHINE(machine);
313     if (kvm_enabled()) {
314         pcms->smm = ON_OFF_AUTO_OFF;
315     }
316 }
317 
318 static void pc_compat_2_2(MachineState *machine)
319 {
320     pc_compat_2_3(machine);
321     machine->suppress_vmdesc = true;
322 }
323 
324 static void pc_compat_2_1(MachineState *machine)
325 {
326     pc_compat_2_2(machine);
327     x86_cpu_change_kvm_default("svm", NULL);
328 }
329 
330 static void pc_compat_2_0(MachineState *machine)
331 {
332     pc_compat_2_1(machine);
333 }
334 
335 static void pc_compat_1_7(MachineState *machine)
336 {
337     pc_compat_2_0(machine);
338     x86_cpu_change_kvm_default("x2apic", NULL);
339 }
340 
341 static void pc_compat_1_6(MachineState *machine)
342 {
343     pc_compat_1_7(machine);
344 }
345 
346 static void pc_compat_1_5(MachineState *machine)
347 {
348     pc_compat_1_6(machine);
349 }
350 
351 static void pc_compat_1_4(MachineState *machine)
352 {
353     pc_compat_1_5(machine);
354 }
355 
356 static void pc_compat_1_3(MachineState *machine)
357 {
358     pc_compat_1_4(machine);
359     enable_compat_apic_id_mode();
360 }
361 
362 /* PC compat function for pc-0.14 to pc-1.2 */
363 static void pc_compat_1_2(MachineState *machine)
364 {
365     pc_compat_1_3(machine);
366     x86_cpu_change_kvm_default("kvm-pv-eoi", NULL);
367 }
368 
369 /* PC compat function for pc-0.10 to pc-0.13 */
370 static void pc_compat_0_13(MachineState *machine)
371 {
372     pc_compat_1_2(machine);
373 }
374 
375 static void pc_init_isa(MachineState *machine)
376 {
377     pc_init1(machine, TYPE_I440FX_PCI_HOST_BRIDGE, TYPE_I440FX_PCI_DEVICE);
378 }
379 
380 #ifdef CONFIG_XEN
381 static void pc_xen_hvm_init_pci(MachineState *machine)
382 {
383     const char *pci_type = has_igd_gfx_passthru ?
384                 TYPE_IGD_PASSTHROUGH_I440FX_PCI_DEVICE : TYPE_I440FX_PCI_DEVICE;
385 
386     pc_init1(machine,
387              TYPE_I440FX_PCI_HOST_BRIDGE,
388              pci_type);
389 }
390 
391 static void pc_xen_hvm_init(MachineState *machine)
392 {
393     PCMachineState *pcms = PC_MACHINE(machine);
394 
395     if (!xen_enabled()) {
396         error_report("xenfv machine requires the xen accelerator");
397         exit(1);
398     }
399 
400     pc_xen_hvm_init_pci(machine);
401     pci_create_simple(pcms->bus, -1, "xen-platform");
402 }
403 #endif
404 
405 #define DEFINE_I440FX_MACHINE(suffix, name, compatfn, optionfn) \
406     static void pc_init_##suffix(MachineState *machine) \
407     { \
408         void (*compat)(MachineState *m) = (compatfn); \
409         if (compat) { \
410             compat(machine); \
411         } \
412         pc_init1(machine, TYPE_I440FX_PCI_HOST_BRIDGE, \
413                  TYPE_I440FX_PCI_DEVICE); \
414     } \
415     DEFINE_PC_MACHINE(suffix, name, pc_init_##suffix, optionfn)
416 
417 static void pc_i440fx_machine_options(MachineClass *m)
418 {
419     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
420     pcmc->default_nic_model = "e1000";
421 
422     m->family = "pc_piix";
423     m->desc = "Standard PC (i440FX + PIIX, 1996)";
424     m->default_machine_opts = "firmware=bios-256k.bin";
425     m->default_display = "std";
426 }
427 
428 static void pc_i440fx_3_0_machine_options(MachineClass *m)
429 {
430     pc_i440fx_machine_options(m);
431     m->alias = "pc";
432     m->is_default = 1;
433     SET_MACHINE_COMPAT(m, PC_COMPAT_2_12);
434 }
435 
436 DEFINE_I440FX_MACHINE(v3_0, "pc-i440fx-3.0", NULL,
437                       pc_i440fx_3_0_machine_options);
438 
439 static void pc_i440fx_2_12_machine_options(MachineClass *m)
440 {
441     pc_i440fx_3_0_machine_options(m);
442     m->is_default = 0;
443     m->alias = NULL;
444     SET_MACHINE_COMPAT(m, PC_COMPAT_2_12);
445 }
446 
447 DEFINE_I440FX_MACHINE(v2_12, "pc-i440fx-2.12", NULL,
448                       pc_i440fx_2_12_machine_options);
449 
450 static void pc_i440fx_2_11_machine_options(MachineClass *m)
451 {
452     pc_i440fx_2_12_machine_options(m);
453     SET_MACHINE_COMPAT(m, PC_COMPAT_2_11);
454 }
455 
456 DEFINE_I440FX_MACHINE(v2_11, "pc-i440fx-2.11", NULL,
457                       pc_i440fx_2_11_machine_options);
458 
459 static void pc_i440fx_2_10_machine_options(MachineClass *m)
460 {
461     pc_i440fx_2_11_machine_options(m);
462     SET_MACHINE_COMPAT(m, PC_COMPAT_2_10);
463     m->auto_enable_numa_with_memhp = false;
464 }
465 
466 DEFINE_I440FX_MACHINE(v2_10, "pc-i440fx-2.10", NULL,
467                       pc_i440fx_2_10_machine_options);
468 
469 static void pc_i440fx_2_9_machine_options(MachineClass *m)
470 {
471     pc_i440fx_2_10_machine_options(m);
472     SET_MACHINE_COMPAT(m, PC_COMPAT_2_9);
473     m->numa_auto_assign_ram = numa_legacy_auto_assign_ram;
474 }
475 
476 DEFINE_I440FX_MACHINE(v2_9, "pc-i440fx-2.9", NULL,
477                       pc_i440fx_2_9_machine_options);
478 
479 static void pc_i440fx_2_8_machine_options(MachineClass *m)
480 {
481     pc_i440fx_2_9_machine_options(m);
482     SET_MACHINE_COMPAT(m, PC_COMPAT_2_8);
483 }
484 
485 DEFINE_I440FX_MACHINE(v2_8, "pc-i440fx-2.8", NULL,
486                       pc_i440fx_2_8_machine_options);
487 
488 
489 static void pc_i440fx_2_7_machine_options(MachineClass *m)
490 {
491     pc_i440fx_2_8_machine_options(m);
492     SET_MACHINE_COMPAT(m, PC_COMPAT_2_7);
493 }
494 
495 DEFINE_I440FX_MACHINE(v2_7, "pc-i440fx-2.7", NULL,
496                       pc_i440fx_2_7_machine_options);
497 
498 
499 static void pc_i440fx_2_6_machine_options(MachineClass *m)
500 {
501     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
502     pc_i440fx_2_7_machine_options(m);
503     pcmc->legacy_cpu_hotplug = true;
504     pcmc->linuxboot_dma_enabled = false;
505     SET_MACHINE_COMPAT(m, PC_COMPAT_2_6);
506 }
507 
508 DEFINE_I440FX_MACHINE(v2_6, "pc-i440fx-2.6", NULL,
509                       pc_i440fx_2_6_machine_options);
510 
511 
512 static void pc_i440fx_2_5_machine_options(MachineClass *m)
513 {
514     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
515     pc_i440fx_2_6_machine_options(m);
516     pcmc->save_tsc_khz = false;
517     m->legacy_fw_cfg_order = 1;
518     SET_MACHINE_COMPAT(m, PC_COMPAT_2_5);
519 }
520 
521 DEFINE_I440FX_MACHINE(v2_5, "pc-i440fx-2.5", NULL,
522                       pc_i440fx_2_5_machine_options);
523 
524 
525 static void pc_i440fx_2_4_machine_options(MachineClass *m)
526 {
527     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
528     pc_i440fx_2_5_machine_options(m);
529     m->hw_version = "2.4.0";
530     pcmc->broken_reserved_end = true;
531     SET_MACHINE_COMPAT(m, PC_COMPAT_2_4);
532 }
533 
534 DEFINE_I440FX_MACHINE(v2_4, "pc-i440fx-2.4", NULL,
535                       pc_i440fx_2_4_machine_options)
536 
537 
538 static void pc_i440fx_2_3_machine_options(MachineClass *m)
539 {
540     pc_i440fx_2_4_machine_options(m);
541     m->hw_version = "2.3.0";
542     SET_MACHINE_COMPAT(m, PC_COMPAT_2_3);
543 }
544 
545 DEFINE_I440FX_MACHINE(v2_3, "pc-i440fx-2.3", pc_compat_2_3,
546                       pc_i440fx_2_3_machine_options);
547 
548 
549 static void pc_i440fx_2_2_machine_options(MachineClass *m)
550 {
551     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
552     pc_i440fx_2_3_machine_options(m);
553     m->hw_version = "2.2.0";
554     SET_MACHINE_COMPAT(m, PC_COMPAT_2_2);
555     pcmc->rsdp_in_ram = false;
556 }
557 
558 DEFINE_I440FX_MACHINE(v2_2, "pc-i440fx-2.2", pc_compat_2_2,
559                       pc_i440fx_2_2_machine_options);
560 
561 
562 static void pc_i440fx_2_1_machine_options(MachineClass *m)
563 {
564     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
565     pc_i440fx_2_2_machine_options(m);
566     m->hw_version = "2.1.0";
567     m->default_display = NULL;
568     SET_MACHINE_COMPAT(m, PC_COMPAT_2_1);
569     pcmc->smbios_uuid_encoded = false;
570     pcmc->enforce_aligned_dimm = false;
571 }
572 
573 DEFINE_I440FX_MACHINE(v2_1, "pc-i440fx-2.1", pc_compat_2_1,
574                       pc_i440fx_2_1_machine_options);
575 
576 
577 
578 static void pc_i440fx_2_0_machine_options(MachineClass *m)
579 {
580     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
581     pc_i440fx_2_1_machine_options(m);
582     m->hw_version = "2.0.0";
583     SET_MACHINE_COMPAT(m, PC_COMPAT_2_0);
584     pcmc->smbios_legacy_mode = true;
585     pcmc->has_reserved_memory = false;
586     /* This value depends on the actual DSDT and SSDT compiled into
587      * the source QEMU; unfortunately it depends on the binary and
588      * not on the machine type, so we cannot make pc-i440fx-1.7 work on
589      * both QEMU 1.7 and QEMU 2.0.
590      *
591      * Large variations cause migration to fail for more than one
592      * consecutive value of the "-smp" maxcpus option.
593      *
594      * For small variations of the kind caused by different iasl versions,
595      * the 4k rounding usually leaves slack.  However, there could be still
596      * one or two values that break.  For QEMU 1.7 and QEMU 2.0 the
597      * slack is only ~10 bytes before one "-smp maxcpus" value breaks!
598      *
599      * 6652 is valid for QEMU 2.0, the right value for pc-i440fx-1.7 on
600      * QEMU 1.7 it is 6414.  For RHEL/CentOS 7.0 it is 6418.
601      */
602     pcmc->legacy_acpi_table_size = 6652;
603     pcmc->acpi_data_size = 0x10000;
604 }
605 
606 DEFINE_I440FX_MACHINE(v2_0, "pc-i440fx-2.0", pc_compat_2_0,
607                       pc_i440fx_2_0_machine_options);
608 
609 
610 static void pc_i440fx_1_7_machine_options(MachineClass *m)
611 {
612     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
613     pc_i440fx_2_0_machine_options(m);
614     m->hw_version = "1.7.0";
615     m->default_machine_opts = NULL;
616     m->option_rom_has_mr = true;
617     SET_MACHINE_COMPAT(m, PC_COMPAT_1_7);
618     pcmc->smbios_defaults = false;
619     pcmc->gigabyte_align = false;
620     pcmc->legacy_acpi_table_size = 6414;
621 }
622 
623 DEFINE_I440FX_MACHINE(v1_7, "pc-i440fx-1.7", pc_compat_1_7,
624                       pc_i440fx_1_7_machine_options);
625 
626 
627 static void pc_i440fx_1_6_machine_options(MachineClass *m)
628 {
629     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
630     pc_i440fx_1_7_machine_options(m);
631     m->hw_version = "1.6.0";
632     m->rom_file_has_mr = false;
633     SET_MACHINE_COMPAT(m, PC_COMPAT_1_6);
634     pcmc->has_acpi_build = false;
635 }
636 
637 DEFINE_I440FX_MACHINE(v1_6, "pc-i440fx-1.6", pc_compat_1_6,
638                       pc_i440fx_1_6_machine_options);
639 
640 
641 static void pc_i440fx_1_5_machine_options(MachineClass *m)
642 {
643     pc_i440fx_1_6_machine_options(m);
644     m->hw_version = "1.5.0";
645     SET_MACHINE_COMPAT(m, PC_COMPAT_1_5);
646 }
647 
648 DEFINE_I440FX_MACHINE(v1_5, "pc-i440fx-1.5", pc_compat_1_5,
649                       pc_i440fx_1_5_machine_options);
650 
651 
652 static void pc_i440fx_1_4_machine_options(MachineClass *m)
653 {
654     pc_i440fx_1_5_machine_options(m);
655     m->hw_version = "1.4.0";
656     m->hot_add_cpu = NULL;
657     SET_MACHINE_COMPAT(m, PC_COMPAT_1_4);
658 }
659 
660 DEFINE_I440FX_MACHINE(v1_4, "pc-i440fx-1.4", pc_compat_1_4,
661                       pc_i440fx_1_4_machine_options);
662 
663 
664 #define PC_COMPAT_1_3 \
665         PC_CPU_MODEL_IDS("1.3.0") \
666         {\
667             .driver   = "usb-tablet",\
668             .property = "usb_version",\
669             .value    = stringify(1),\
670         },{\
671             .driver   = "virtio-net-pci",\
672             .property = "ctrl_mac_addr",\
673             .value    = "off",      \
674         },{ \
675             .driver   = "virtio-net-pci", \
676             .property = "mq", \
677             .value    = "off", \
678         }, {\
679             .driver   = "e1000",\
680             .property = "autonegotiation",\
681             .value    = "off",\
682         },
683 
684 
685 static void pc_i440fx_1_3_machine_options(MachineClass *m)
686 {
687     pc_i440fx_1_4_machine_options(m);
688     m->hw_version = "1.3.0";
689     SET_MACHINE_COMPAT(m, PC_COMPAT_1_3);
690 }
691 
692 DEFINE_I440FX_MACHINE(v1_3, "pc-1.3", pc_compat_1_3,
693                       pc_i440fx_1_3_machine_options);
694 
695 
696 #define PC_COMPAT_1_2 \
697         PC_CPU_MODEL_IDS("1.2.0") \
698         {\
699             .driver   = "nec-usb-xhci",\
700             .property = "msi",\
701             .value    = "off",\
702         },{\
703             .driver   = "nec-usb-xhci",\
704             .property = "msix",\
705             .value    = "off",\
706         },{\
707             .driver   = "ivshmem",\
708             .property = "use64",\
709             .value    = "0",\
710         },{\
711             .driver   = "qxl",\
712             .property = "revision",\
713             .value    = stringify(3),\
714         },{\
715             .driver   = "qxl-vga",\
716             .property = "revision",\
717             .value    = stringify(3),\
718         },{\
719             .driver   = "VGA",\
720             .property = "mmio",\
721             .value    = "off",\
722         },
723 
724 static void pc_i440fx_1_2_machine_options(MachineClass *m)
725 {
726     pc_i440fx_1_3_machine_options(m);
727     m->hw_version = "1.2.0";
728     SET_MACHINE_COMPAT(m, PC_COMPAT_1_2);
729 }
730 
731 DEFINE_I440FX_MACHINE(v1_2, "pc-1.2", pc_compat_1_2,
732                       pc_i440fx_1_2_machine_options);
733 
734 
735 #define PC_COMPAT_1_1 \
736         PC_CPU_MODEL_IDS("1.1.0") \
737         {\
738             .driver   = "virtio-scsi-pci",\
739             .property = "hotplug",\
740             .value    = "off",\
741         },{\
742             .driver   = "virtio-scsi-pci",\
743             .property = "param_change",\
744             .value    = "off",\
745         },{\
746             .driver   = "VGA",\
747             .property = "vgamem_mb",\
748             .value    = stringify(8),\
749         },{\
750             .driver   = "vmware-svga",\
751             .property = "vgamem_mb",\
752             .value    = stringify(8),\
753         },{\
754             .driver   = "qxl-vga",\
755             .property = "vgamem_mb",\
756             .value    = stringify(8),\
757         },{\
758             .driver   = "qxl",\
759             .property = "vgamem_mb",\
760             .value    = stringify(8),\
761         },{\
762             .driver   = "virtio-blk-pci",\
763             .property = "config-wce",\
764             .value    = "off",\
765         },
766 
767 static void pc_i440fx_1_1_machine_options(MachineClass *m)
768 {
769     pc_i440fx_1_2_machine_options(m);
770     m->hw_version = "1.1.0";
771     SET_MACHINE_COMPAT(m, PC_COMPAT_1_1);
772 }
773 
774 DEFINE_I440FX_MACHINE(v1_1, "pc-1.1", pc_compat_1_2,
775                       pc_i440fx_1_1_machine_options);
776 
777 
778 #define PC_COMPAT_1_0 \
779         PC_CPU_MODEL_IDS("1.0") \
780         {\
781             .driver   = TYPE_ISA_FDC,\
782             .property = "check_media_rate",\
783             .value    = "off",\
784         }, {\
785             .driver   = "virtio-balloon-pci",\
786             .property = "class",\
787             .value    = stringify(PCI_CLASS_MEMORY_RAM),\
788         },{\
789             .driver   = "apic-common",\
790             .property = "vapic",\
791             .value    = "off",\
792         },{\
793             .driver   = TYPE_USB_DEVICE,\
794             .property = "full-path",\
795             .value    = "no",\
796         },
797 
798 static void pc_i440fx_1_0_machine_options(MachineClass *m)
799 {
800     pc_i440fx_1_1_machine_options(m);
801     m->hw_version = "1.0";
802     SET_MACHINE_COMPAT(m, PC_COMPAT_1_0);
803 }
804 
805 DEFINE_I440FX_MACHINE(v1_0, "pc-1.0", pc_compat_1_2,
806                       pc_i440fx_1_0_machine_options);
807 
808 
809 #define PC_COMPAT_0_15 \
810         PC_CPU_MODEL_IDS("0.15")
811 
812 static void pc_i440fx_0_15_machine_options(MachineClass *m)
813 {
814     pc_i440fx_1_0_machine_options(m);
815     m->hw_version = "0.15";
816     SET_MACHINE_COMPAT(m, PC_COMPAT_0_15);
817 }
818 
819 DEFINE_I440FX_MACHINE(v0_15, "pc-0.15", pc_compat_1_2,
820                       pc_i440fx_0_15_machine_options);
821 
822 
823 #define PC_COMPAT_0_14 \
824         PC_CPU_MODEL_IDS("0.14") \
825         {\
826             .driver   = "virtio-blk-pci",\
827             .property = "event_idx",\
828             .value    = "off",\
829         },{\
830             .driver   = "virtio-serial-pci",\
831             .property = "event_idx",\
832             .value    = "off",\
833         },{\
834             .driver   = "virtio-net-pci",\
835             .property = "event_idx",\
836             .value    = "off",\
837         },{\
838             .driver   = "virtio-balloon-pci",\
839             .property = "event_idx",\
840             .value    = "off",\
841         },{\
842             .driver   = "qxl",\
843             .property = "revision",\
844             .value    = stringify(2),\
845         },{\
846             .driver   = "qxl-vga",\
847             .property = "revision",\
848             .value    = stringify(2),\
849         },
850 
851 static void pc_i440fx_0_14_machine_options(MachineClass *m)
852 {
853     pc_i440fx_0_15_machine_options(m);
854     m->hw_version = "0.14";
855     SET_MACHINE_COMPAT(m, PC_COMPAT_0_14);
856 }
857 
858 DEFINE_I440FX_MACHINE(v0_14, "pc-0.14", pc_compat_1_2,
859                       pc_i440fx_0_14_machine_options);
860 
861 
862 #define PC_COMPAT_0_13 \
863         PC_CPU_MODEL_IDS("0.13") \
864         {\
865             .driver   = TYPE_PCI_DEVICE,\
866             .property = "command_serr_enable",\
867             .value    = "off",\
868         },{\
869             .driver   = "AC97",\
870             .property = "use_broken_id",\
871             .value    = stringify(1),\
872         },{\
873             .driver   = "virtio-9p-pci",\
874             .property = "vectors",\
875             .value    = stringify(0),\
876         },{\
877             .driver   = "VGA",\
878             .property = "rombar",\
879             .value    = stringify(0),\
880         },{\
881             .driver   = "vmware-svga",\
882             .property = "rombar",\
883             .value    = stringify(0),\
884         },
885 
886 static void pc_i440fx_0_13_machine_options(MachineClass *m)
887 {
888     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
889     pc_i440fx_0_14_machine_options(m);
890     m->hw_version = "0.13";
891     SET_MACHINE_COMPAT(m, PC_COMPAT_0_13);
892     pcmc->kvmclock_enabled = false;
893 }
894 
895 DEFINE_I440FX_MACHINE(v0_13, "pc-0.13", pc_compat_0_13,
896                       pc_i440fx_0_13_machine_options);
897 
898 
899 #define PC_COMPAT_0_12 \
900         PC_CPU_MODEL_IDS("0.12") \
901         {\
902             .driver   = "virtio-serial-pci",\
903             .property = "max_ports",\
904             .value    = stringify(1),\
905         },{\
906             .driver   = "virtio-serial-pci",\
907             .property = "vectors",\
908             .value    = stringify(0),\
909         },{\
910             .driver   = "usb-mouse",\
911             .property = "serial",\
912             .value    = "1",\
913         },{\
914             .driver   = "usb-tablet",\
915             .property = "serial",\
916             .value    = "1",\
917         },{\
918             .driver   = "usb-kbd",\
919             .property = "serial",\
920             .value    = "1",\
921         },
922 
923 static void pc_i440fx_0_12_machine_options(MachineClass *m)
924 {
925     pc_i440fx_0_13_machine_options(m);
926     m->hw_version = "0.12";
927     SET_MACHINE_COMPAT(m, PC_COMPAT_0_12);
928 }
929 
930 DEFINE_I440FX_MACHINE(v0_12, "pc-0.12", pc_compat_0_13,
931                       pc_i440fx_0_12_machine_options);
932 
933 
934 #define PC_COMPAT_0_11 \
935         PC_CPU_MODEL_IDS("0.11") \
936         {\
937             .driver   = "virtio-blk-pci",\
938             .property = "vectors",\
939             .value    = stringify(0),\
940         },{\
941             .driver   = TYPE_PCI_DEVICE,\
942             .property = "rombar",\
943             .value    = stringify(0),\
944         },{\
945             .driver   = "ide-drive",\
946             .property = "ver",\
947             .value    = "0.11",\
948         },{\
949             .driver   = "scsi-disk",\
950             .property = "ver",\
951             .value    = "0.11",\
952         },
953 
954 static void pc_i440fx_0_11_machine_options(MachineClass *m)
955 {
956     pc_i440fx_0_12_machine_options(m);
957     m->hw_version = "0.11";
958     SET_MACHINE_COMPAT(m, PC_COMPAT_0_11);
959 }
960 
961 DEFINE_I440FX_MACHINE(v0_11, "pc-0.11", pc_compat_0_13,
962                       pc_i440fx_0_11_machine_options);
963 
964 
965 #define PC_COMPAT_0_10 \
966     PC_CPU_MODEL_IDS("0.10") \
967     {\
968         .driver   = "virtio-blk-pci",\
969         .property = "class",\
970         .value    = stringify(PCI_CLASS_STORAGE_OTHER),\
971     },{\
972         .driver   = "virtio-serial-pci",\
973         .property = "class",\
974         .value    = stringify(PCI_CLASS_DISPLAY_OTHER),\
975     },{\
976         .driver   = "virtio-net-pci",\
977         .property = "vectors",\
978         .value    = stringify(0),\
979     },{\
980         .driver   = "ide-drive",\
981         .property = "ver",\
982         .value    = "0.10",\
983     },{\
984         .driver   = "scsi-disk",\
985         .property = "ver",\
986         .value    = "0.10",\
987     },
988 
989 static void pc_i440fx_0_10_machine_options(MachineClass *m)
990 {
991     pc_i440fx_0_11_machine_options(m);
992     m->hw_version = "0.10";
993     SET_MACHINE_COMPAT(m, PC_COMPAT_0_10);
994 }
995 
996 DEFINE_I440FX_MACHINE(v0_10, "pc-0.10", pc_compat_0_13,
997                       pc_i440fx_0_10_machine_options);
998 
999 typedef struct {
1000     uint16_t gpu_device_id;
1001     uint16_t pch_device_id;
1002     uint8_t pch_revision_id;
1003 } IGDDeviceIDInfo;
1004 
1005 /* In real world different GPU should have different PCH. But actually
1006  * the different PCH DIDs likely map to different PCH SKUs. We do the
1007  * same thing for the GPU. For PCH, the different SKUs are going to be
1008  * all the same silicon design and implementation, just different
1009  * features turn on and off with fuses. The SW interfaces should be
1010  * consistent across all SKUs in a given family (eg LPT). But just same
1011  * features may not be supported.
1012  *
1013  * Most of these different PCH features probably don't matter to the
1014  * Gfx driver, but obviously any difference in display port connections
1015  * will so it should be fine with any PCH in case of passthrough.
1016  *
1017  * So currently use one PCH version, 0x8c4e, to cover all HSW(Haswell)
1018  * scenarios, 0x9cc3 for BDW(Broadwell).
1019  */
1020 static const IGDDeviceIDInfo igd_combo_id_infos[] = {
1021     /* HSW Classic */
1022     {0x0402, 0x8c4e, 0x04}, /* HSWGT1D, HSWD_w7 */
1023     {0x0406, 0x8c4e, 0x04}, /* HSWGT1M, HSWM_w7 */
1024     {0x0412, 0x8c4e, 0x04}, /* HSWGT2D, HSWD_w7 */
1025     {0x0416, 0x8c4e, 0x04}, /* HSWGT2M, HSWM_w7 */
1026     {0x041E, 0x8c4e, 0x04}, /* HSWGT15D, HSWD_w7 */
1027     /* HSW ULT */
1028     {0x0A06, 0x8c4e, 0x04}, /* HSWGT1UT, HSWM_w7 */
1029     {0x0A16, 0x8c4e, 0x04}, /* HSWGT2UT, HSWM_w7 */
1030     {0x0A26, 0x8c4e, 0x06}, /* HSWGT3UT, HSWM_w7 */
1031     {0x0A2E, 0x8c4e, 0x04}, /* HSWGT3UT28W, HSWM_w7 */
1032     {0x0A1E, 0x8c4e, 0x04}, /* HSWGT2UX, HSWM_w7 */
1033     {0x0A0E, 0x8c4e, 0x04}, /* HSWGT1ULX, HSWM_w7 */
1034     /* HSW CRW */
1035     {0x0D26, 0x8c4e, 0x04}, /* HSWGT3CW, HSWM_w7 */
1036     {0x0D22, 0x8c4e, 0x04}, /* HSWGT3CWDT, HSWD_w7 */
1037     /* HSW Server */
1038     {0x041A, 0x8c4e, 0x04}, /* HSWSVGT2, HSWD_w7 */
1039     /* HSW SRVR */
1040     {0x040A, 0x8c4e, 0x04}, /* HSWSVGT1, HSWD_w7 */
1041     /* BSW */
1042     {0x1606, 0x9cc3, 0x03}, /* BDWULTGT1, BDWM_w7 */
1043     {0x1616, 0x9cc3, 0x03}, /* BDWULTGT2, BDWM_w7 */
1044     {0x1626, 0x9cc3, 0x03}, /* BDWULTGT3, BDWM_w7 */
1045     {0x160E, 0x9cc3, 0x03}, /* BDWULXGT1, BDWM_w7 */
1046     {0x161E, 0x9cc3, 0x03}, /* BDWULXGT2, BDWM_w7 */
1047     {0x1602, 0x9cc3, 0x03}, /* BDWHALOGT1, BDWM_w7 */
1048     {0x1612, 0x9cc3, 0x03}, /* BDWHALOGT2, BDWM_w7 */
1049     {0x1622, 0x9cc3, 0x03}, /* BDWHALOGT3, BDWM_w7 */
1050     {0x162B, 0x9cc3, 0x03}, /* BDWHALO28W, BDWM_w7 */
1051     {0x162A, 0x9cc3, 0x03}, /* BDWGT3WRKS, BDWM_w7 */
1052     {0x162D, 0x9cc3, 0x03}, /* BDWGT3SRVR, BDWM_w7 */
1053 };
1054 
1055 static void isa_bridge_class_init(ObjectClass *klass, void *data)
1056 {
1057     DeviceClass *dc = DEVICE_CLASS(klass);
1058     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1059 
1060     dc->desc        = "ISA bridge faked to support IGD PT";
1061     k->vendor_id    = PCI_VENDOR_ID_INTEL;
1062     k->class_id     = PCI_CLASS_BRIDGE_ISA;
1063 };
1064 
1065 static TypeInfo isa_bridge_info = {
1066     .name          = "igd-passthrough-isa-bridge",
1067     .parent        = TYPE_PCI_DEVICE,
1068     .instance_size = sizeof(PCIDevice),
1069     .class_init = isa_bridge_class_init,
1070     .interfaces = (InterfaceInfo[]) {
1071         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1072         { },
1073     },
1074 };
1075 
1076 static void pt_graphics_register_types(void)
1077 {
1078     type_register_static(&isa_bridge_info);
1079 }
1080 type_init(pt_graphics_register_types)
1081 
1082 void igd_passthrough_isa_bridge_create(PCIBus *bus, uint16_t gpu_dev_id)
1083 {
1084     struct PCIDevice *bridge_dev;
1085     int i, num;
1086     uint16_t pch_dev_id = 0xffff;
1087     uint8_t pch_rev_id;
1088 
1089     num = ARRAY_SIZE(igd_combo_id_infos);
1090     for (i = 0; i < num; i++) {
1091         if (gpu_dev_id == igd_combo_id_infos[i].gpu_device_id) {
1092             pch_dev_id = igd_combo_id_infos[i].pch_device_id;
1093             pch_rev_id = igd_combo_id_infos[i].pch_revision_id;
1094         }
1095     }
1096 
1097     if (pch_dev_id == 0xffff) {
1098         return;
1099     }
1100 
1101     /* Currently IGD drivers always need to access PCH by 1f.0. */
1102     bridge_dev = pci_create_simple(bus, PCI_DEVFN(0x1f, 0),
1103                                    "igd-passthrough-isa-bridge");
1104 
1105     /*
1106      * Note that vendor id is always PCI_VENDOR_ID_INTEL.
1107      */
1108     if (!bridge_dev) {
1109         fprintf(stderr, "set igd-passthrough-isa-bridge failed!\n");
1110         return;
1111     }
1112     pci_config_set_device_id(bridge_dev->config, pch_dev_id);
1113     pci_config_set_revision(bridge_dev->config, pch_rev_id);
1114 }
1115 
1116 static void isapc_machine_options(MachineClass *m)
1117 {
1118     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
1119     m->desc = "ISA-only PC";
1120     m->max_cpus = 1;
1121     m->option_rom_has_mr = true;
1122     m->rom_file_has_mr = false;
1123     pcmc->pci_enabled = false;
1124     pcmc->has_acpi_build = false;
1125     pcmc->smbios_defaults = false;
1126     pcmc->gigabyte_align = false;
1127     pcmc->smbios_legacy_mode = true;
1128     pcmc->has_reserved_memory = false;
1129     pcmc->default_nic_model = "ne2k_isa";
1130     m->default_cpu_type = X86_CPU_TYPE_NAME("486");
1131 }
1132 
1133 DEFINE_PC_MACHINE(isapc, "isapc", pc_init_isa,
1134                   isapc_machine_options);
1135 
1136 
1137 #ifdef CONFIG_XEN
1138 static void xenfv_machine_options(MachineClass *m)
1139 {
1140     m->desc = "Xen Fully-virtualized PC";
1141     m->max_cpus = HVM_MAX_VCPUS;
1142     m->default_machine_opts = "accel=xen";
1143 }
1144 
1145 DEFINE_PC_MACHINE(xenfv, "xenfv", pc_xen_hvm_init,
1146                   xenfv_machine_options);
1147 #endif
1148