Pablo Greco e6a3ae
From e8b5f27c84d9a7e1b1b18b2a472a1500711da828 Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: David Gibson <dgibson@redhat.com>
Pablo Greco e6a3ae
Date: Thu, 30 May 2019 04:37:26 +0100
Pablo Greco e6a3ae
Subject: [PATCH 5/8] vfio/quirks: Add common quirk alloc helper
Pablo Greco e6a3ae
MIME-Version: 1.0
Pablo Greco e6a3ae
Content-Type: text/plain; charset=UTF-8
Pablo Greco e6a3ae
Content-Transfer-Encoding: 8bit
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: David Gibson <dgibson@redhat.com>
Pablo Greco e6a3ae
Message-id: <20190530043728.32575-5-dgibson@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 88422
Pablo Greco e6a3ae
O-Subject: [RHEL-8.1 qemu-kvm PATCH 4/6] vfio/quirks: Add common quirk alloc helper
Pablo Greco e6a3ae
Bugzilla: 1710662
Pablo Greco e6a3ae
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
From: Alex Williamson <alex.williamson@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
This will later be used to include list initialization.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Pablo Greco e6a3ae
Reviewed-by: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Pablo Greco e6a3ae
(cherry picked from commit bcf3c3d029e73d54455e1d7a51177c37d668378c)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1710662
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Signed-off-by: David Gibson <dgibson@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 hw/vfio/pci-quirks.c | 48 +++++++++++++++++++++---------------------------
Pablo Greco e6a3ae
 1 file changed, 21 insertions(+), 27 deletions(-)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/hw/vfio/pci-quirks.c b/hw/vfio/pci-quirks.c
Pablo Greco e6a3ae
index 90859d1..92457ed 100644
Pablo Greco e6a3ae
--- a/hw/vfio/pci-quirks.c
Pablo Greco e6a3ae
+++ b/hw/vfio/pci-quirks.c
Pablo Greco e6a3ae
@@ -275,6 +275,15 @@ static const MemoryRegionOps vfio_ati_3c3_quirk = {
Pablo Greco e6a3ae
     .endianness = DEVICE_LITTLE_ENDIAN,
Pablo Greco e6a3ae
 };
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
+static VFIOQuirk *vfio_quirk_alloc(int nr_mem)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    VFIOQuirk *quirk = g_new0(VFIOQuirk, 1);
Pablo Greco e6a3ae
+    quirk->mem = g_new0(MemoryRegion, nr_mem);
Pablo Greco e6a3ae
+    quirk->nr_mem = nr_mem;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    return quirk;
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
 static void vfio_vga_probe_ati_3c3_quirk(VFIOPCIDevice *vdev)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
     VFIOQuirk *quirk;
Pablo Greco e6a3ae
@@ -288,9 +297,7 @@ static void vfio_vga_probe_ati_3c3_quirk(VFIOPCIDevice *vdev)
Pablo Greco e6a3ae
         return;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
-    quirk->mem = g_new0(MemoryRegion, 1);
Pablo Greco e6a3ae
-    quirk->nr_mem = 1;
Pablo Greco e6a3ae
+    quirk = vfio_quirk_alloc(1);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     memory_region_init_io(quirk->mem, OBJECT(vdev), &vfio_ati_3c3_quirk, vdev,
Pablo Greco e6a3ae
                           "vfio-ati-3c3-quirk", 1);
Pablo Greco e6a3ae
@@ -323,9 +330,7 @@ static void vfio_probe_ati_bar4_quirk(VFIOPCIDevice *vdev, int nr)
Pablo Greco e6a3ae
         return;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
-    quirk->mem = g_new0(MemoryRegion, 2);
Pablo Greco e6a3ae
-    quirk->nr_mem = 2;
Pablo Greco e6a3ae
+    quirk = vfio_quirk_alloc(2);
Pablo Greco e6a3ae
     window = quirk->data = g_malloc0(sizeof(*window) +
Pablo Greco e6a3ae
                                      sizeof(VFIOConfigWindowMatch));
Pablo Greco e6a3ae
     window->vdev = vdev;
Pablo Greco e6a3ae
@@ -371,10 +376,9 @@ static void vfio_probe_ati_bar2_quirk(VFIOPCIDevice *vdev, int nr)
Pablo Greco e6a3ae
         return;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
+    quirk = vfio_quirk_alloc(1);
Pablo Greco e6a3ae
     mirror = quirk->data = g_malloc0(sizeof(*mirror));
Pablo Greco e6a3ae
-    mirror->mem = quirk->mem = g_new0(MemoryRegion, 1);
Pablo Greco e6a3ae
-    quirk->nr_mem = 1;
Pablo Greco e6a3ae
+    mirror->mem = quirk->mem;
Pablo Greco e6a3ae
     mirror->vdev = vdev;
Pablo Greco e6a3ae
     mirror->offset = 0x4000;
Pablo Greco e6a3ae
     mirror->bar = nr;
Pablo Greco e6a3ae
@@ -546,10 +550,8 @@ static void vfio_vga_probe_nvidia_3d0_quirk(VFIOPCIDevice *vdev)
Pablo Greco e6a3ae
         return;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
+    quirk = vfio_quirk_alloc(2);
Pablo Greco e6a3ae
     quirk->data = data = g_malloc0(sizeof(*data));
Pablo Greco e6a3ae
-    quirk->mem = g_new0(MemoryRegion, 2);
Pablo Greco e6a3ae
-    quirk->nr_mem = 2;
Pablo Greco e6a3ae
     data->vdev = vdev;
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_nvidia_3d4_quirk,
Pablo Greco e6a3ae
@@ -665,9 +667,7 @@ static void vfio_probe_nvidia_bar5_quirk(VFIOPCIDevice *vdev, int nr)
Pablo Greco e6a3ae
         return;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
-    quirk->mem = g_new0(MemoryRegion, 4);
Pablo Greco e6a3ae
-    quirk->nr_mem = 4;
Pablo Greco e6a3ae
+    quirk = vfio_quirk_alloc(4);
Pablo Greco e6a3ae
     bar5 = quirk->data = g_malloc0(sizeof(*bar5) +
Pablo Greco e6a3ae
                                    (sizeof(VFIOConfigWindowMatch) * 2));
Pablo Greco e6a3ae
     window = &bar5->window;
Pablo Greco e6a3ae
@@ -760,10 +760,9 @@ static void vfio_probe_nvidia_bar0_quirk(VFIOPCIDevice *vdev, int nr)
Pablo Greco e6a3ae
         return;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
+    quirk = vfio_quirk_alloc(1);
Pablo Greco e6a3ae
     mirror = quirk->data = g_malloc0(sizeof(*mirror));
Pablo Greco e6a3ae
-    mirror->mem = quirk->mem = g_new0(MemoryRegion, 1);
Pablo Greco e6a3ae
-    quirk->nr_mem = 1;
Pablo Greco e6a3ae
+    mirror->mem = quirk->mem;
Pablo Greco e6a3ae
     mirror->vdev = vdev;
Pablo Greco e6a3ae
     mirror->offset = 0x88000;
Pablo Greco e6a3ae
     mirror->bar = nr;
Pablo Greco e6a3ae
@@ -779,10 +778,9 @@ static void vfio_probe_nvidia_bar0_quirk(VFIOPCIDevice *vdev, int nr)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* The 0x1800 offset mirror only seems to get used by legacy VGA */
Pablo Greco e6a3ae
     if (vdev->vga) {
Pablo Greco e6a3ae
-        quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
+        quirk = vfio_quirk_alloc(1);
Pablo Greco e6a3ae
         mirror = quirk->data = g_malloc0(sizeof(*mirror));
Pablo Greco e6a3ae
-        mirror->mem = quirk->mem = g_new0(MemoryRegion, 1);
Pablo Greco e6a3ae
-        quirk->nr_mem = 1;
Pablo Greco e6a3ae
+        mirror->mem = quirk->mem;
Pablo Greco e6a3ae
         mirror->vdev = vdev;
Pablo Greco e6a3ae
         mirror->offset = 0x1800;
Pablo Greco e6a3ae
         mirror->bar = nr;
Pablo Greco e6a3ae
@@ -943,9 +941,7 @@ static void vfio_probe_rtl8168_bar2_quirk(VFIOPCIDevice *vdev, int nr)
Pablo Greco e6a3ae
         return;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
-    quirk->mem = g_new0(MemoryRegion, 2);
Pablo Greco e6a3ae
-    quirk->nr_mem = 2;
Pablo Greco e6a3ae
+    quirk = vfio_quirk_alloc(2);
Pablo Greco e6a3ae
     quirk->data = rtl = g_malloc0(sizeof(*rtl));
Pablo Greco e6a3ae
     rtl->vdev = vdev;
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
@@ -1510,9 +1506,7 @@ static void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* Setup our quirk to munge GTT addresses to the VM allocated buffer */
Pablo Greco e6a3ae
-    quirk = g_malloc0(sizeof(*quirk));
Pablo Greco e6a3ae
-    quirk->mem = g_new0(MemoryRegion, 2);
Pablo Greco e6a3ae
-    quirk->nr_mem = 2;
Pablo Greco e6a3ae
+    quirk = vfio_quirk_alloc(2);
Pablo Greco e6a3ae
     igd = quirk->data = g_malloc0(sizeof(*igd));
Pablo Greco e6a3ae
     igd->vdev = vdev;
Pablo Greco e6a3ae
     igd->index = ~0;
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae