Blame SOURCES/kvm-pci-store-PCI-hole-ranges-in-guestinfo-structure.patch

0a122b
From 43d319c65b1a27349fd329cb2f9c6f5c54f32379 Mon Sep 17 00:00:00 2001
0a122b
Message-Id: <43d319c65b1a27349fd329cb2f9c6f5c54f32379.1387298827.git.minovotn@redhat.com>
0a122b
In-Reply-To: <3ed0fb61a3dc912ef036d7ef450bed192090709e.1387298827.git.minovotn@redhat.com>
0a122b
References: <3ed0fb61a3dc912ef036d7ef450bed192090709e.1387298827.git.minovotn@redhat.com>
0a122b
From: "Michael S. Tsirkin" <mst@redhat.com>
0a122b
Date: Tue, 17 Dec 2013 15:17:22 +0100
0a122b
Subject: [PATCH 13/56] pci: store PCI hole ranges in guestinfo structure
0a122b
0a122b
RH-Author: Michael S. Tsirkin <mst@redhat.com>
0a122b
Message-id: <1387293161-4085-14-git-send-email-mst@redhat.com>
0a122b
Patchwork-id: 56318
0a122b
O-Subject: [PATCH qemu-kvm RHEL7.0 v2 13/57] pci: store PCI hole ranges in guestinfo structure
0a122b
Bugzilla: 1034876
0a122b
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
0a122b
RH-Acked-by: Marcel Apfelbaum <marcel.a@redhat.com>
0a122b
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
0a122b
0a122b
Will be used to pass hole ranges to guests.
0a122b
0a122b
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
0a122b
(cherry picked from commit 3459a625215449b67b9c67d9151ff72892d0a42a)
0a122b
0a122b
Conflicts:
0a122b
	hw/i386/pc_piix.c
0a122b
	hw/i386/pc_q35.c
0a122b
---
0a122b
 include/hw/i386/pc.h      | 19 ++++++++++++++++++-
0a122b
 include/hw/pci-host/q35.h |  2 ++
0a122b
 include/qemu/typedefs.h   |  1 +
0a122b
 hw/i386/pc.c              | 46 +++++++++++++++++++++++++++++++++++++++++++++-
0a122b
 hw/i386/pc_piix.c         | 14 +++++++++++++-
0a122b
 hw/i386/pc_q35.c          |  6 +++++-
0a122b
 hw/pci-host/q35.c         |  8 ++++++++
0a122b
 7 files changed, 92 insertions(+), 4 deletions(-)
0a122b
0a122b
Signed-off-by: Michal Novotny <minovotn@redhat.com>
0a122b
---
0a122b
 hw/i386/pc.c              | 46 +++++++++++++++++++++++++++++++++++++++++++++-
0a122b
 hw/i386/pc_piix.c         | 14 +++++++++++++-
0a122b
 hw/i386/pc_q35.c          |  6 +++++-
0a122b
 hw/pci-host/q35.c         |  8 ++++++++
0a122b
 include/hw/i386/pc.h      | 19 ++++++++++++++++++-
0a122b
 include/hw/pci-host/q35.h |  2 ++
0a122b
 include/qemu/typedefs.h   |  1 +
0a122b
 7 files changed, 92 insertions(+), 4 deletions(-)
0a122b
0a122b
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
0a122b
index 2948781..68a8e1b 100644
0a122b
--- a/hw/i386/pc.c
0a122b
+++ b/hw/i386/pc.c
0a122b
@@ -985,6 +985,48 @@ void pc_cpus_init(const char *cpu_model, DeviceState *icc_bridge)
0a122b
     }
0a122b
 }
0a122b
 
0a122b
+typedef struct PcGuestInfoState {
0a122b
+    PcGuestInfo info;
0a122b
+    Notifier machine_done;
0a122b
+} PcGuestInfoState;
0a122b
+
0a122b
+static
0a122b
+void pc_guest_info_machine_done(Notifier *notifier, void *data)
0a122b
+{
0a122b
+    PcGuestInfoState *guest_info_state = container_of(notifier,
0a122b
+                                                      PcGuestInfoState,
0a122b
+                                                      machine_done);
0a122b
+}
0a122b
+
0a122b
+PcGuestInfo *pc_guest_info_init(ram_addr_t below_4g_mem_size,
0a122b
+                                ram_addr_t above_4g_mem_size)
0a122b
+{
0a122b
+    PcGuestInfoState *guest_info_state = g_malloc0(sizeof *guest_info_state);
0a122b
+    PcGuestInfo *guest_info = &guest_info_state->info;
0a122b
+
0a122b
+    guest_info->pci_info.w32.end = IO_APIC_DEFAULT_ADDRESS;
0a122b
+    if (sizeof(hwaddr) == 4) {
0a122b
+        guest_info->pci_info.w64.begin = 0;
0a122b
+        guest_info->pci_info.w64.end = 0;
0a122b
+    } else {
0a122b
+        /*
0a122b
+         * BIOS does not set MTRR entries for the 64 bit window, so no need to
0a122b
+         * align address to power of two.  Align address at 1G, this makes sure
0a122b
+         * it can be exactly covered with a PAT entry even when using huge
0a122b
+         * pages.
0a122b
+         */
0a122b
+        guest_info->pci_info.w64.begin =
0a122b
+            ROUND_UP((0x1ULL << 32) + above_4g_mem_size, 0x1ULL << 30);
0a122b
+        guest_info->pci_info.w64.end = guest_info->pci_info.w64.begin +
0a122b
+            (0x1ULL << 62);
0a122b
+        assert(guest_info->pci_info.w64.begin <= guest_info->pci_info.w64.end);
0a122b
+    }
0a122b
+
0a122b
+    guest_info_state->machine_done.notify = pc_guest_info_machine_done;
0a122b
+    qemu_add_machine_init_done_notifier(&guest_info_state->machine_done);
0a122b
+    return guest_info;
0a122b
+}
0a122b
+
0a122b
 void pc_acpi_init(const char *default_dsdt)
0a122b
 {
0a122b
     char *filename;
0a122b
@@ -1026,7 +1068,8 @@ FWCfgState *pc_memory_init(MemoryRegion *system_memory,
0a122b
                            ram_addr_t below_4g_mem_size,
0a122b
                            ram_addr_t above_4g_mem_size,
0a122b
                            MemoryRegion *rom_memory,
0a122b
-                           MemoryRegion **ram_memory)
0a122b
+                           MemoryRegion **ram_memory,
0a122b
+                           PcGuestInfo *guest_info)
0a122b
 {
0a122b
     int linux_boot, i;
0a122b
     MemoryRegion *ram, *option_rom_mr;
0a122b
@@ -1078,6 +1121,7 @@ FWCfgState *pc_memory_init(MemoryRegion *system_memory,
0a122b
     for (i = 0; i < nb_option_roms; i++) {
0a122b
         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
0a122b
     }
0a122b
+    guest_info->fw_cfg = fw_cfg;
0a122b
     return fw_cfg;
0a122b
 }
0a122b
 
0a122b
diff --git a/hw/i386/pc_piix.c b/hw/i386/pc_piix.c
0a122b
index 07848c1..3a77998 100644
0a122b
--- a/hw/i386/pc_piix.c
0a122b
+++ b/hw/i386/pc_piix.c
0a122b
@@ -87,6 +87,7 @@ static void pc_init1(QEMUMachineInitArgs *args,
0a122b
     MemoryRegion *rom_memory;
0a122b
     DeviceState *icc_bridge;
0a122b
     FWCfgState *fw_cfg = NULL;
0a122b
+    PcGuestInfo *guest_info;
0a122b
 
0a122b
     icc_bridge = qdev_create(NULL, TYPE_ICC_BRIDGE);
0a122b
     object_property_add_child(qdev_get_machine(), "icc-bridge",
0a122b
@@ -120,13 +121,24 @@ static void pc_init1(QEMUMachineInitArgs *args,
0a122b
         smbios_set_type1_defaults("Red Hat", "KVM", args->machine->desc);
0a122b
     }
0a122b
 
0a122b
+    guest_info = pc_guest_info_init(below_4g_mem_size, above_4g_mem_size);
0a122b
+
0a122b
+    /* Set PCI window size the way seabios has always done it. */
0a122b
+    /* Power of 2 so bios can cover it with a single MTRR */
0a122b
+    if (ram_size <= 0x80000000)
0a122b
+        guest_info->pci_info.w32.begin = 0x80000000;
0a122b
+    else if (ram_size <= 0xc0000000)
0a122b
+        guest_info->pci_info.w32.begin = 0xc0000000;
0a122b
+    else
0a122b
+        guest_info->pci_info.w32.begin = 0xe0000000;
0a122b
+
0a122b
     /* allocate ram and load rom/bios */
0a122b
     if (!xen_enabled()) {
0a122b
         fw_cfg = pc_memory_init(system_memory,
0a122b
                        args->kernel_filename, args->kernel_cmdline,
0a122b
                        args->initrd_filename,
0a122b
                        below_4g_mem_size, above_4g_mem_size,
0a122b
-                       rom_memory, &ram_memory);
0a122b
+                       rom_memory, &ram_memory, guest_info);
0a122b
     }
0a122b
 
0a122b
     gsi_state = g_malloc0(sizeof(*gsi_state));
0a122b
diff --git a/hw/i386/pc_q35.c b/hw/i386/pc_q35.c
0a122b
index 8fa6793..9fab93c 100644
0a122b
--- a/hw/i386/pc_q35.c
0a122b
+++ b/hw/i386/pc_q35.c
0a122b
@@ -73,6 +73,7 @@ static void pc_q35_init(QEMUMachineInitArgs *args)
0a122b
     ICH9LPCState *ich9_lpc;
0a122b
     PCIDevice *ahci;
0a122b
     DeviceState *icc_bridge;
0a122b
+    PcGuestInfo *guest_info;
0a122b
 
0a122b
     icc_bridge = qdev_create(NULL, TYPE_ICC_BRIDGE);
0a122b
     object_property_add_child(qdev_get_machine(), "icc-bridge",
0a122b
@@ -106,13 +107,15 @@ static void pc_q35_init(QEMUMachineInitArgs *args)
0a122b
         smbios_set_type1_defaults("Red Hat", "KVM", args->machine->desc);
0a122b
     }
0a122b
 
0a122b
+    guest_info = pc_guest_info_init(below_4g_mem_size, above_4g_mem_size);
0a122b
+
0a122b
     /* allocate ram and load rom/bios */
0a122b
     if (!xen_enabled()) {
0a122b
         pc_memory_init(get_system_memory(),
0a122b
                        args->kernel_filename, args->kernel_cmdline,
0a122b
                        args->initrd_filename,
0a122b
                        below_4g_mem_size, above_4g_mem_size,
0a122b
-                       rom_memory, &ram_memory);
0a122b
+                       rom_memory, &ram_memory, guest_info);
0a122b
     }
0a122b
 
0a122b
     /* irq lines */
0a122b
@@ -134,6 +137,7 @@ static void pc_q35_init(QEMUMachineInitArgs *args)
0a122b
     q35_host->mch.address_space_io = get_system_io();
0a122b
     q35_host->mch.below_4g_mem_size = below_4g_mem_size;
0a122b
     q35_host->mch.above_4g_mem_size = above_4g_mem_size;
0a122b
+    q35_host->mch.guest_info = guest_info;
0a122b
     /* pci */
0a122b
     qdev_init_nofail(DEVICE(q35_host));
0a122b
     host_bus = q35_host->host.pci.bus;
0a122b
diff --git a/hw/pci-host/q35.c b/hw/pci-host/q35.c
0a122b
index ed934c3..0989fc5 100644
0a122b
--- a/hw/pci-host/q35.c
0a122b
+++ b/hw/pci-host/q35.c
0a122b
@@ -245,6 +245,14 @@ static int mch_init(PCIDevice *d)
0a122b
     hwaddr pci_hole64_size;
0a122b
     MCHPCIState *mch = MCH_PCI_DEVICE(d);
0a122b
 
0a122b
+    /* Leave enough space for the biggest MCFG BAR */
0a122b
+    /* TODO: this matches current bios behaviour, but
0a122b
+     * it's not a power of two, which means an MTRR
0a122b
+     * can't cover it exactly.
0a122b
+     */
0a122b
+    mch->guest_info->pci_info.w32.begin = MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT +
0a122b
+        MCH_HOST_BRIDGE_PCIEXBAR_MAX;
0a122b
+
0a122b
     /* setup pci memory regions */
0a122b
     memory_region_init_alias(&mch->pci_hole, "pci-hole",
0a122b
                              mch->pci_address_space,
0a122b
diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h
0a122b
index 2cf7baa..2518db6 100644
0a122b
--- a/include/hw/i386/pc.h
0a122b
+++ b/include/hw/i386/pc.h
0a122b
@@ -10,8 +10,20 @@
0a122b
 #include "exec/memory.h"
0a122b
 #include "hw/i386/ioapic.h"
0a122b
 
0a122b
+#include "qemu/range.h"
0a122b
+
0a122b
 /* PC-style peripherals (also used by other machines).  */
0a122b
 
0a122b
+typedef struct PcPciInfo {
0a122b
+    Range w32;
0a122b
+    Range w64;
0a122b
+} PcPciInfo;
0a122b
+
0a122b
+struct PcGuestInfo {
0a122b
+    PcPciInfo pci_info;
0a122b
+    FWCfgState *fw_cfg;
0a122b
+};
0a122b
+
0a122b
 /* parallel.c */
0a122b
 static inline bool parallel_init(ISABus *bus, int index, CharDriverState *chr)
0a122b
 {
0a122b
@@ -84,6 +96,10 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level);
0a122b
 void pc_cpus_init(const char *cpu_model, DeviceState *icc_bridge);
0a122b
 void pc_hot_add_cpu(const int64_t id, Error **errp);
0a122b
 void pc_acpi_init(const char *default_dsdt);
0a122b
+
0a122b
+PcGuestInfo *pc_guest_info_init(ram_addr_t below_4g_mem_size,
0a122b
+                                ram_addr_t above_4g_mem_size);
0a122b
+
0a122b
 FWCfgState *pc_memory_init(MemoryRegion *system_memory,
0a122b
                            const char *kernel_filename,
0a122b
                            const char *kernel_cmdline,
0a122b
@@ -91,7 +107,8 @@ FWCfgState *pc_memory_init(MemoryRegion *system_memory,
0a122b
                            ram_addr_t below_4g_mem_size,
0a122b
                            ram_addr_t above_4g_mem_size,
0a122b
                            MemoryRegion *rom_memory,
0a122b
-                           MemoryRegion **ram_memory);
0a122b
+                           MemoryRegion **ram_memory,
0a122b
+                           PcGuestInfo *guest_info);
0a122b
 qemu_irq *pc_allocate_cpu_irq(void);
0a122b
 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus);
0a122b
 void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
0a122b
diff --git a/include/hw/pci-host/q35.h b/include/hw/pci-host/q35.h
0a122b
index e182c82..b083831 100644
0a122b
--- a/include/hw/pci-host/q35.h
0a122b
+++ b/include/hw/pci-host/q35.h
0a122b
@@ -55,6 +55,7 @@ typedef struct MCHPCIState {
0a122b
     uint8_t smm_enabled;
0a122b
     ram_addr_t below_4g_mem_size;
0a122b
     ram_addr_t above_4g_mem_size;
0a122b
+    PcGuestInfo *guest_info;
0a122b
 } MCHPCIState;
0a122b
 
0a122b
 typedef struct Q35PCIHost {
0a122b
@@ -81,6 +82,7 @@ typedef struct Q35PCIHost {
0a122b
 #define MCH_HOST_BRIDGE_PCIEXBAR               0x60    /* 64bit register */
0a122b
 #define MCH_HOST_BRIDGE_PCIEXBAR_SIZE          8       /* 64bit register */
0a122b
 #define MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT       0xb0000000
0a122b
+#define MCH_HOST_BRIDGE_PCIEXBAR_MAX           (0x10000000) /* 256M */
0a122b
 #define MCH_HOST_BRIDGE_PCIEXBAR_ADMSK         Q35_MASK(64, 35, 28)
0a122b
 #define MCH_HOST_BRIDGE_PCIEXBAR_128ADMSK      ((uint64_t)(1 << 26))
0a122b
 #define MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK       ((uint64_t)(1 << 25))
0a122b
diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
0a122b
index a332d88..70d250f 100644
0a122b
--- a/include/qemu/typedefs.h
0a122b
+++ b/include/qemu/typedefs.h
0a122b
@@ -64,6 +64,7 @@ typedef struct VirtIODevice VirtIODevice;
0a122b
 typedef struct QEMUSGList QEMUSGList;
0a122b
 typedef struct SHPCDevice SHPCDevice;
0a122b
 typedef struct FWCfgState FWCfgState;
0a122b
+typedef struct PcGuestInfo PcGuestInfo;
0a122b
 typedef struct Range Range;
0a122b
 
0a122b
 #endif /* QEMU_TYPEDEFS_H */
0a122b
-- 
0a122b
1.7.11.7
0a122b