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