From d9d646fe71b1c1b32072fd83efd44f6f9cf47b38 Mon Sep 17 00:00:00 2001
From: Igor Mammedov <imammedo@redhat.com>
Date: Mon, 1 Aug 2016 08:11:43 +0200
Subject: [PATCH 43/99] machine: add properties to compat_props incrementaly

RH-Author: Igor Mammedov <imammedo@redhat.com>
Message-id: <1470039143-24450-39-git-send-email-imammedo@redhat.com>
Patchwork-id: 71654
O-Subject: [RHEV-7.3 qemu-kvm-rhev PATCH 38/78] machine: add properties to compat_props incrementaly
Bugzilla: 1087672
RH-Acked-by: Marcel Apfelbaum <marcel@redhat.com>
RH-Acked-by: David Gibson <dgibson@redhat.com>
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>

Switch to adding compat properties incrementaly instead of
completly overwriting compat_props per machine type.
That removes data duplication which we have due to nested
[PC|SPAPR]_COMPAT_* macros.

It also allows to set default device properties from
default foo_machine_options() hook, which will be used
in following patch for putting VMGENID device as
a function if ISA bridge on pc/q35 machines.

Suggested-by: Eduardo Habkost <ehabkost@redhat.com>
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Acked-by: Cornelia Huck <cornelia.huck@de.ibm.com>
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
[ehabkost: Fixed CCW_COMPAT_* and PC_COMPAT_0_* defines]
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>

(cherry picked from commit bacc344c548ce165a0001276ece56ee4b0bddae3)
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>

Conflicts:
    include/hw/boards.h
        due to rhel only commit caaa2b73 that added duplucate macro
        introduced by upstream-2.6 commit
            877f893 "Move SET_MACHINE_COMPAT macro to boards.h"
    hw/arm/virt.c
        rhel specific fixup,
            mimic upstream and remove empty rhel730_compat_props
            in rhel730_virt_instance_init()
    RHEL only:
       hw/i386/pc_piix.c
       include/hw/i386/pc.h
           Make RHEL machine type follow the same logic the commit introduces
          (i.e. drop nesting in PC_RHEL*_COMPAT macroses)

fixup compat machine for rhel7
---
 hw/arm/virt.c              |  8 --------
 hw/core/machine.c          | 10 ++++++++++
 hw/i386/pc_piix.c          | 20 --------------------
 hw/ppc/spapr.c             |  4 ----
 hw/s390x/s390-virtio-ccw.c |  1 -
 include/hw/boards.h        | 21 +++++++++------------
 include/hw/i386/pc.h       | 11 -----------
 vl.c                       |  6 +++++-
 8 files changed, 24 insertions(+), 57 deletions(-)

diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 069d6b4..d00a7a8 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -1418,13 +1418,9 @@ static void virt_2_6_instance_init(Object *obj)
 static void virt_2_6_class_init(ObjectClass *oc, void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
-    static GlobalProperty compat_props[] = {
-        { /* end of list */ }
-    };
 
     mc->desc = "QEMU 2.6 ARM Virtual Machine";
     mc->alias = "virt";
-    mc->compat_props = compat_props;
 }
 
 static const TypeInfo machvirt_info = {
@@ -1495,14 +1491,10 @@ static void rhel730_virt_instance_init(Object *obj)
 static void rhel730_virt_class_init(ObjectClass *oc, void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
-    static GlobalProperty rhel730_compat_props[] = {
-      { /* end of list */ }
-    };
 
     mc->desc = "RHEL 7.3.0 ARM Virtual Machine";
     mc->alias = "virt";
     mc->is_default = 1;
-    mc->compat_props = rhel730_compat_props;
 }
 
 static const TypeInfo rhel730_machvirt_info = {
diff --git a/hw/core/machine.c b/hw/core/machine.c
index 23456f8..1abe0cc 100644
--- a/hw/core/machine.c
+++ b/hw/core/machine.c
@@ -530,6 +530,15 @@ bool machine_mem_merge(MachineState *machine)
     return machine->mem_merge;
 }
 
+static void machine_class_finalize(ObjectClass *klass, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(klass);
+
+    if (mc->compat_props) {
+        g_array_free(mc->compat_props, true);
+    }
+}
+
 static const TypeInfo machine_info = {
     .name = TYPE_MACHINE,
     .parent = TYPE_OBJECT,
@@ -537,6 +546,7 @@ static const TypeInfo machine_info = {
     .class_size = sizeof(MachineClass),
     .class_init    = machine_class_init,
     .class_base_init = machine_class_base_init,
+    .class_finalize = machine_class_finalize,
     .instance_size = sizeof(MachineState),
     .instance_init = machine_initfn,
     .instance_finalize = machine_finalize,
diff --git a/hw/i386/pc_piix.c b/hw/i386/pc_piix.c
index f926fd1..002945b 100644
--- a/hw/i386/pc_piix.c
+++ b/hw/i386/pc_piix.c
@@ -601,7 +601,6 @@ DEFINE_I440FX_MACHINE(v1_4, "pc-i440fx-1.4", pc_compat_1_4,
 
 
 #define PC_COMPAT_1_3 \
-        PC_COMPAT_1_4 \
         {\
             .driver   = "usb-tablet",\
             .property = "usb_version",\
@@ -633,7 +632,6 @@ DEFINE_I440FX_MACHINE(v1_3, "pc-1.3", pc_compat_1_3,
 
 
 #define PC_COMPAT_1_2 \
-        PC_COMPAT_1_3 \
         {\
             .driver   = "nec-usb-xhci",\
             .property = "msi",\
@@ -672,7 +670,6 @@ DEFINE_I440FX_MACHINE(v1_2, "pc-1.2", pc_compat_1_2,
 
 
 #define PC_COMPAT_1_1 \
-        PC_COMPAT_1_2 \
         {\
             .driver   = "virtio-scsi-pci",\
             .property = "hotplug",\
@@ -715,7 +712,6 @@ DEFINE_I440FX_MACHINE(v1_1, "pc-1.1", pc_compat_1_2,
 
 
 #define PC_COMPAT_1_0 \
-        PC_COMPAT_1_1 \
         {\
             .driver   = TYPE_ISA_FDC,\
             .property = "check_media_rate",\
@@ -745,14 +741,10 @@ DEFINE_I440FX_MACHINE(v1_0, "pc-1.0", pc_compat_1_2,
                       pc_i440fx_1_0_machine_options);
 
 
-#define PC_COMPAT_0_15 \
-        PC_COMPAT_1_0
-
 static void pc_i440fx_0_15_machine_options(MachineClass *m)
 {
     pc_i440fx_1_0_machine_options(m);
     m->hw_version = "0.15";
-    SET_MACHINE_COMPAT(m, PC_COMPAT_0_15);
 }
 
 DEFINE_I440FX_MACHINE(v0_15, "pc-0.15", pc_compat_1_2,
@@ -760,7 +752,6 @@ DEFINE_I440FX_MACHINE(v0_15, "pc-0.15", pc_compat_1_2,
 
 
 #define PC_COMPAT_0_14 \
-        PC_COMPAT_0_15 \
         {\
             .driver   = "virtio-blk-pci",\
             .property = "event_idx",\
@@ -799,7 +790,6 @@ DEFINE_I440FX_MACHINE(v0_14, "pc-0.14", pc_compat_1_2,
 
 
 #define PC_COMPAT_0_13 \
-        PC_COMPAT_0_14 \
         {\
             .driver   = TYPE_PCI_DEVICE,\
             .property = "command_serr_enable",\
@@ -836,7 +826,6 @@ DEFINE_I440FX_MACHINE(v0_13, "pc-0.13", pc_compat_0_13,
 
 
 #define PC_COMPAT_0_12 \
-        PC_COMPAT_0_13 \
         {\
             .driver   = "virtio-serial-pci",\
             .property = "max_ports",\
@@ -871,7 +860,6 @@ DEFINE_I440FX_MACHINE(v0_12, "pc-0.12", pc_compat_0_13,
 
 
 #define PC_COMPAT_0_11 \
-        PC_COMPAT_0_12 \
         {\
             .driver   = "virtio-blk-pci",\
             .property = "vectors",\
@@ -902,7 +890,6 @@ DEFINE_I440FX_MACHINE(v0_11, "pc-0.11", pc_compat_0_13,
 
 
 #define PC_COMPAT_0_10 \
-    PC_COMPAT_0_11 \
     {\
         .driver   = "virtio-blk-pci",\
         .property = "class",\
@@ -1218,7 +1205,6 @@ DEFINE_PC_MACHINE(rhel700, "pc-i440fx-rhel7.0.0", pc_init_rhel700,
                   pc_machine_rhel700_options);
 
 #define PC_RHEL6_6_COMPAT \
-        PC_RHEL7_0_COMPAT\
         {\
             .driver   = "scsi-hd",\
             .property = "discard_granularity",\
@@ -1532,7 +1518,6 @@ DEFINE_PC_MACHINE(rhel660, "rhel6.6.0", pc_init_rhel660,
                   pc_machine_rhel660_options);
 
 #define PC_RHEL6_5_COMPAT \
-        PC_RHEL6_6_COMPAT\
         {\
             .driver   = TYPE_USB_DEVICE,\
             .property = "msos-desc",\
@@ -1562,7 +1547,6 @@ DEFINE_PC_MACHINE(rhel650, "rhel6.5.0", pc_init_rhel650,
                   pc_machine_rhel650_options);
 
 #define PC_RHEL6_4_COMPAT \
-        PC_RHEL6_5_COMPAT\
         {\
             .driver   = "virtio-scsi-pci",\
             .property = "vectors",\
@@ -1613,7 +1597,6 @@ DEFINE_PC_MACHINE(rhel640, "rhel6.4.0", pc_init_rhel640,
                   pc_machine_rhel640_options);
 
 #define PC_RHEL6_3_COMPAT \
-        PC_RHEL6_4_COMPAT\
         {\
             .driver   = "Conroe-" TYPE_X86_CPU,\
             .property = "level",\
@@ -1691,7 +1674,6 @@ DEFINE_PC_MACHINE(rhel630, "rhel6.3.0", pc_init_rhel630,
 
 
 #define PC_RHEL6_2_COMPAT \
-        PC_RHEL6_3_COMPAT\
         {\
             .driver = TYPE_X86_CPU,\
             .property = "pmu",\
@@ -1726,7 +1708,6 @@ DEFINE_PC_MACHINE(rhel620, "rhel6.2.0", pc_init_rhel620,
  * it either due to a bug (see RHBZ 1029539 fo more info)
  */
 #define PC_RHEL6_1_COMPAT \
-        PC_RHEL6_2_COMPAT\
         {\
             .driver   = "PIIX4_PM",\
             .property = "disable_s3",\
@@ -1792,7 +1773,6 @@ DEFINE_PC_MACHINE(rhel610, "rhel6.1.0", pc_init_rhel610,
                   pc_machine_rhel610_options);
 
 #define PC_RHEL6_0_COMPAT \
-        PC_RHEL6_1_COMPAT\
         {\
             .driver   = "qxl",\
             .property = "revision",\
diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
index b1ee70d..676a416 100644
--- a/hw/ppc/spapr.c
+++ b/hw/ppc/spapr.c
@@ -2487,7 +2487,6 @@ DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
  * pseries-2.4
  */
 #define SPAPR_COMPAT_2_4 \
-        SPAPR_COMPAT_2_5 \
         HW_COMPAT_2_4
 
 static void spapr_machine_2_4_instance_options(MachineState *machine)
@@ -2510,7 +2509,6 @@ DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
  * pseries-2.3
  */
 #define SPAPR_COMPAT_2_3 \
-        SPAPR_COMPAT_2_4 \
         HW_COMPAT_2_3 \
         {\
             .driver   = "spapr-pci-host-bridge",\
@@ -2538,7 +2536,6 @@ DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
  */
 
 #define SPAPR_COMPAT_2_2 \
-        SPAPR_COMPAT_2_3 \
         HW_COMPAT_2_2 \
         {\
             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
@@ -2563,7 +2560,6 @@ DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
  * pseries-2.1
  */
 #define SPAPR_COMPAT_2_1 \
-        SPAPR_COMPAT_2_2 \
         HW_COMPAT_2_1
 
 
diff --git a/hw/s390x/s390-virtio-ccw.c b/hw/s390x/s390-virtio-ccw.c
index e3df9c7..3e07ec3 100644
--- a/hw/s390x/s390-virtio-ccw.c
+++ b/hw/s390x/s390-virtio-ccw.c
@@ -303,7 +303,6 @@ static const TypeInfo ccw_machine_info = {
         HW_COMPAT_2_5
 
 #define CCW_COMPAT_2_4 \
-        CCW_COMPAT_2_5 \
         HW_COMPAT_2_4 \
         {\
             .driver   = TYPE_S390_SKEYS,\
diff --git a/include/hw/boards.h b/include/hw/boards.h
index f9243ee..ab65753 100644
--- a/include/hw/boards.h
+++ b/include/hw/boards.h
@@ -118,7 +118,7 @@ struct MachineClass {
     const char *default_machine_opts;
     const char *default_boot_order;
     const char *default_display;
-    GlobalProperty *compat_props;
+    GArray *compat_props;
     const char *hw_version;
     ram_addr_t default_ram_size;
     bool option_rom_has_mr;
@@ -190,20 +190,17 @@ struct MachineState {
 
 #define SET_MACHINE_COMPAT(m, COMPAT) \
     do {                              \
+        int i;                        \
         static GlobalProperty props[] = {       \
             COMPAT                              \
             { /* end of list */ }               \
         };                                      \
-        (m)->compat_props = props;              \
+        if (!m->compat_props) { \
+            m->compat_props = g_array_new(false, false, sizeof(void *)); \
+        } \
+        for (i = 0; props[i].driver != NULL; i++) {    \
+            GlobalProperty *prop = &props[i];          \
+            g_array_append_val(m->compat_props, prop); \
+        }                                              \
     } while (0)
-
-#define SET_MACHINE_COMPAT(m, COMPAT) \
-    do {                              \
-        static GlobalProperty props[] = {       \
-            COMPAT                              \
-            { /* end of list */ }               \
-        };                                      \
-        (m)->compat_props = props;              \
-    } while (0)
-
 #endif
diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h
index 51f41a0..68bfa71 100644
--- a/include/hw/i386/pc.h
+++ b/include/hw/i386/pc.h
@@ -366,7 +366,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
     HW_COMPAT_2_5
 
 #define PC_COMPAT_2_4 \
-    PC_COMPAT_2_5 \
     HW_COMPAT_2_4 \
     {\
         .driver   = "Haswell-" TYPE_X86_CPU,\
@@ -437,7 +436,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
 
 
 #define PC_COMPAT_2_3 \
-    PC_COMPAT_2_4 \
     HW_COMPAT_2_3 \
     {\
         .driver   = TYPE_X86_CPU,\
@@ -518,7 +516,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
     },
 
 #define PC_COMPAT_2_2 \
-    PC_COMPAT_2_3 \
     HW_COMPAT_2_2 \
     {\
         .driver = "kvm64" "-" TYPE_X86_CPU,\
@@ -612,7 +609,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
     },
 
 #define PC_COMPAT_2_1 \
-    PC_COMPAT_2_2 \
     HW_COMPAT_2_1 \
     {\
         .driver = "coreduo" "-" TYPE_X86_CPU,\
@@ -626,7 +622,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
     },
 
 #define PC_COMPAT_2_0 \
-    PC_COMPAT_2_1 \
     {\
         .driver   = "virtio-scsi-pci",\
         .property = "any_layout",\
@@ -686,7 +681,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
     },
 
 #define PC_COMPAT_1_7 \
-    PC_COMPAT_2_0 \
     {\
         .driver   = TYPE_USB_DEVICE,\
         .property = "msos-desc",\
@@ -704,7 +698,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
     },
 
 #define PC_COMPAT_1_6 \
-    PC_COMPAT_1_7 \
     {\
         .driver   = "e1000",\
         .property = "mitigation",\
@@ -728,7 +721,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
     },
 
 #define PC_COMPAT_1_5 \
-    PC_COMPAT_1_6 \
     {\
         .driver   = "Conroe-" TYPE_X86_CPU,\
         .property = "model",\
@@ -772,7 +764,6 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
     },
 
 #define PC_COMPAT_1_4 \
-    PC_COMPAT_1_5 \
     {\
         .driver   = "scsi-hd",\
         .property = "discard_granularity",\
@@ -972,7 +963,6 @@ extern void igd_passthrough_isa_bridge_create(PCIBus *bus, uint16_t gpu_dev_id);
 
 
 #define PC_RHEL7_1_COMPAT \
-        PC_RHEL7_2_COMPAT \
         HW_COMPAT_RHEL7_1 \
 	{\
 		.driver = "kvm64" "-" TYPE_X86_CPU,\
@@ -1164,7 +1154,6 @@ extern void igd_passthrough_isa_bridge_create(PCIBus *bus, uint16_t gpu_dev_id);
  * pci-serial-4x) in 7.0.
  */
 #define PC_RHEL7_0_COMPAT \
-        PC_RHEL7_1_COMPAT \
         {\
             .driver   = "virtio-scsi-pci",\
             .property = "any_layout",\
diff --git a/vl.c b/vl.c
index f1f044e..0f1d688 100644
--- a/vl.c
+++ b/vl.c
@@ -4518,7 +4518,11 @@ int main(int argc, char **argv, char **envp)
     }
 
     if (machine_class->compat_props) {
-        qdev_prop_register_global_list(machine_class->compat_props);
+        GlobalProperty *p;
+        for (i = 0; i < machine_class->compat_props->len; i++) {
+            p = g_array_index(machine_class->compat_props, GlobalProperty *, i);
+            qdev_prop_register_global(p);
+        }
     }
     qemu_opts_foreach(qemu_find_opts("global"),
                       global_init_func, NULL, NULL);
-- 
1.8.3.1

