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