Skip to contentSkip to conversation

hw/pc: implement multiple primary busses for pc machines

RFC PATCH Superseded Marcel Apfelbaum 4 participants 27 messages Feb 16, 2015 – Feb 20, 2015

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v2 cover hw/pc: implement multiple primary busses for pc machines

Series status:
The series is fully functional. The reason is still RFC is because depends on Igor's ACPI series that is still in the mailing list. Once it gets accepted I'll resend this series rebased on it.
- Limitations:
- Pxb's bus does not support hotplug. It will be addressed on top of this series because is already getting to big.

You are more than welcome to try using:
-device pxb-device,id=pxb,bus_nr=4,numa_node=1 -device e1000,bus=pxb,addr=0x1 -bios <patched with the above series>

v1->v2:
- Add support for multiple pxb devices.
- Attach pxb's bus to specific NUMA node.
- Got rid of the hacks from prev version.
- Tested also for Win7 and Fedora 20, and for virtio blk devices.
- Several bug-fixes resulting in a stable version ready for submission.

This series depends on:
- [SeaBIOS] [PATCH V2 0/2] fw/pci: better support for multiple host bridges
- [Qemu-devel] [PATCH v4 0/3] pc: acpi-build: make linker & RSDP tables dynamic
- [PATCH v3 00/52] ACPI refactoring: replace template patching with C AML API

Reasoning:
We need multiple primary busess for a few reasons, the most important one is to be able to associate a pass-trough device with a guest NUMA node. The OS-es are able to associate a NUMA node only to a primary bus, not to a specific PCI device or a pci-2-pci bridge.
PC machines support multiple NUMA nodes for CPUs and memory, however the IO was not yet supported.

patch 1 adds the necessary acpi constructs based on Igor's series
patch 2-5 implements acpi code needed to expose the pxb's primary bus to guests patch 6 separates the pci_bus code into a designated file
patch 7-11 handles the implicit assumptions in code that only one primary bus can exist patch 12 handles the actual implementation of the PXB devices
patch 13-14 enables the device
patch 15 implements PXB map_irq function, (can be squashed into the actual PXB) patch 16-17 adds NUMA support

Marcel Apfelbaum (17):
acpi: added needed acpi constructs
hw/acpi: add support for multiple root busses
hw/apci: add _PRT method for extra root busses
hw/acpi: add _CRS method for extra root busses
hw/acpi: remove from root bus 0 the crs resources used by other busses. hw/pci: move pci bus related code to separate files
hw/pci: made pci_bus_is_root a PCIBusClass method
hw/pci: made pci_bus_num a PCIBusClass method
hw/pci: introduce TYPE_PCI_MAIN_HOST_BRIDGE interface hw/pci: removed 'rootbus nr is 0' assumption from qmp_pci_query hw/pci: implement iteration over multiple host bridges hw/pci: introduce PCI Expander Bridge (PXB)
hw/pci: inform bios if the system has more than one pci bridge hw/pci: piix - suport multiple host bridges
hw/pxb: add map_irq func
hw/pci_bus: add support for NUMA nodes
hw/pxb: add numa_node parameter

 arch_init.c                         |   1 +
 hw/acpi/acpi-build-utils.c          | 107 +++++++-
 hw/alpha/typhoon.c                  |   1 +
 hw/i386/acpi-build.c                | 357 ++++++++++++++++++++++++-
 hw/i386/kvm/pci-assign.c            |   1 +
 hw/i386/pc.c                        |  13 +
 hw/mips/gt64xxx_pci.c               |   1 +
 hw/pci-bridge/Makefile.objs         |   1 +
 hw/pci-bridge/pci_expander_bridge.c | 208 +++++++++++++++
 hw/pci-host/bonito.c                |   1 +
 hw/pci-host/grackle.c               |   1 +
 hw/pci-host/piix.c                  |  63 ++++-
 hw/pci-host/ppce500.c               |   1 +
 hw/pci-host/q35.c                   |   5 +
 hw/pci-host/uninorth.c              |   1 +
 hw/pci/Makefile.objs                |   2 +-
 hw/pci/pci-hotplug-old.c            |   1 +
 hw/pci/pci.c                        | 501 +---------------------------------
 hw/pci/pci_bus.c                    | 517 ++++++++++++++++++++++++++++++++++++
 hw/pci/pci_host.c                   |   6 +
 hw/ppc/ppc4xx_pci.c                 |   1 +
 hw/scsi/megasas.c                   |   1 +
 hw/sh4/r2d.c                        |   1 +
 hw/sh4/sh_pci.c                     |   1 +
 hw/vfio/pci.c                       |   1 +
 hw/xen/xen_pt.c                     |   1 +
 include/hw/acpi/acpi-build-utils.h  |  12 +
 include/hw/pci/pci.h                |   6 +-
 include/hw/pci/pci_bus.h            |  35 +++
 include/hw/pci/pci_host.h           |  11 +
 include/sysemu/sysemu.h             |   1 +
 31 files changed, 1348 insertions(+), 512 deletions(-)
 create mode 100644 hw/pci-bridge/pci_expander_bridge.c
 create mode 100644 hw/pci/pci_bus.c
Signature · 1 line
2.1.0
#1 lore raw

RFC PATCH v2 1/17 acpi: added needed acpi constructs

---
 hw/acpi/acpi-build-utils.c         | 107 +++++++++++++++++++++++++++++++++++--
 include/hw/acpi/acpi-build-utils.h |  12 +++++
 2 files changed, 116 insertions(+), 3 deletions(-)
hw/acpi/acpi-build-utils.c +104 -3
index 58f88cd..19cd146 100644
@@ -439,6 +439,26 @@ AcpiAml acpi_and(AcpiAml arg1, AcpiAml arg2)
     return var;
 }
 
+AcpiAml acpi_or(AcpiAml arg1, AcpiAml arg2)
+{
+    AcpiAml var = aml_allocate_internal(0, NON_BLOCK);
+    build_append_byte(var.buf, 0x7D); /* AndOp */
+    aml_append(&var, arg1);
+    aml_append(&var, arg2);
+    build_append_int(var.buf, 0x00); /* NullNameOp */
+    return var;
+}
+
+AcpiAml acpi_add(AcpiAml arg1, AcpiAml arg2)
+{
+    AcpiAml var = aml_allocate_internal(0, NON_BLOCK);
+    build_append_byte(var.buf, 0x72); /* AddOp */
+    aml_append(&var, arg1);
+    aml_append(&var, arg2);
+    build_append_int(var.buf, 0x00); /* NullNameOp */
+    return var;
+}
+
 /* ACPI 5.0: 20.2.5.3 Type 1 Opcodes Encoding: DefNotify */
 AcpiAml acpi_notify(AcpiAml arg1, AcpiAml arg2)
 {
@@ -518,6 +538,53 @@ AcpiAml acpi_equal(AcpiAml arg1, AcpiAml arg2)
     build_append_byte(var.buf, 0x93); /* LequalOp */
     aml_append(&var, arg1);
     aml_append(&var, arg2);
+    return var;
+}
+
+AcpiAml acpi_increment(AcpiAml arg1)
+{
+    AcpiAml var = aml_allocate_internal(0, NON_BLOCK);
+    build_append_byte(var.buf, 0x75); /* LequalOp */
+    aml_append(&var, arg1);
+    return var;
+}
+
+AcpiAml acpi_lless(AcpiAml arg1, AcpiAml arg2)
+{
+    AcpiAml var = aml_allocate_internal(0, NON_BLOCK);
+    build_append_byte(var.buf, 0x95); /* LequalOp */
+    aml_append(&var, arg1);
+    aml_append(&var, arg2);
+//    build_append_int(var.buf, 0x00); /* NullNameOp */
+    return var;
+}
+
+AcpiAml acpi_shiftright(AcpiAml arg1, int count)
+{
+    AcpiAml var = aml_allocate_internal(0, NON_BLOCK);
+    build_append_byte(var.buf, 0x7A); /* RightShiftOp */
+    aml_append(&var, arg1);
+    aml_append(&var, acpi_int(count));
+    build_append_int(var.buf, 0x00); /* NullNameOp */
+    return var;
+}
+
+AcpiAml acpi_shiftleft(AcpiAml arg1, int count)
+{
+    AcpiAml var = aml_allocate_internal(0, NON_BLOCK);
+    build_append_byte(var.buf, 0x79); /* RightLeftOp */
+    aml_append(&var, arg1);
+    aml_append(&var, acpi_int(count));
+    build_append_int(var.buf, 0x00); /* NullNameOp */
+    return var;
+}
+
+AcpiAml acpi_index(AcpiAml arg1, AcpiAml idx)
+{
+    AcpiAml var = aml_allocate_internal(0, NON_BLOCK);
+    build_append_byte(var.buf, 0x88); /* IndexOp */
+    aml_append(&var, arg1);
+    aml_append(&var, idx);
     build_append_int(var.buf, 0x00); /* NullNameOp */
     return var;
 }
@@ -530,6 +597,13 @@ AcpiAml acpi_if(AcpiAml predicate)
     return var;
 }
 
+AcpiAml acpi_while(AcpiAml predicate)
+{
+    AcpiAml var = aml_allocate_internal(0xA2 /* WhileOp */, PACKAGE);
+    aml_append(&var, predicate);
+    return var;
+}
+
 /* ACPI 5.0: 20.2.5.2 Named Objects Encoding: DefMethod */
 AcpiAml acpi_method(const char *name, int arg_count)
 {
@@ -652,14 +726,41 @@ AcpiAml GCC_FMT_ATTR(1, 2) acpi_string(const char *name_format, ...)
     return var;
 }
 
-/* ACPI 5.0: 20.2.6.2 Local Objects Encoding: Local0Op */
-AcpiAml acpi_local0(void)
+/* ACPI 5.0: 20.2.6.2 Local Objects Encoding: LocalXOp */
+static AcpiAml acpi_local(uint8_t index)
 {
     AcpiAml var = aml_allocate_internal(0, NON_BLOCK);
-    build_append_byte(var.buf, 0x60); /* Local0Op */
+
+    assert(index < 8);
+    build_append_byte(var.buf, 0x60 + index); /* Local0Op */
     return var;
 }
 
+AcpiAml acpi_local0(void)
+{
+    return acpi_local(0);
+}
+
+AcpiAml acpi_local1(void)
+{
+    return acpi_local(1);
+}
+
+AcpiAml acpi_local2(void)
+{
+    return acpi_local(2);
+}
+
+AcpiAml acpi_local3(void)
+{
+    return acpi_local(3);
+}
+
+AcpiAml acpi_local4(void)
+{
+    return acpi_local(4);
+}
+
 /* ACPI 5.0: 20.2.5.4 Type 2 Opcodes Encoding: DefVarPackage */
 AcpiAml acpi_varpackage(uint32_t num_elements)
 {
include/hw/acpi/acpi-build-utils.h +12 -0
index 868d439..e39a82d 100644
@@ -108,6 +108,8 @@ AcpiAml acpi_arg2(void);
 AcpiAml acpi_arg3(void);
 AcpiAml acpi_store(AcpiAml val, AcpiAml target);
 AcpiAml acpi_and(AcpiAml arg1, AcpiAml arg2);
+AcpiAml acpi_or(AcpiAml arg1, AcpiAml arg2);
+AcpiAml acpi_add(AcpiAml arg1, AcpiAml arg2);
 AcpiAml acpi_notify(AcpiAml arg1, AcpiAml arg2);
 AcpiAml acpi_call1(const char *method, AcpiAml arg1);
 AcpiAml acpi_call2(const char *method, AcpiAml arg1, AcpiAml arg2);
@@ -123,7 +125,16 @@ AcpiAml acpi_named_field(const char *name, unsigned length);
 AcpiAml acpi_reserved_field(unsigned length);
 AcpiAml GCC_FMT_ATTR(1, 2) acpi_string(const char *name_format, ...);
 AcpiAml acpi_local0(void);
+AcpiAml acpi_local1(void);
+AcpiAml acpi_local2(void);
+AcpiAml acpi_local3(void);
+AcpiAml acpi_local4(void);
+AcpiAml acpi_increment(AcpiAml arg1);
 AcpiAml acpi_equal(AcpiAml arg1, AcpiAml arg2);
+AcpiAml acpi_lless(AcpiAml arg1, AcpiAml arg2);
+AcpiAml acpi_index(AcpiAml arg1, AcpiAml idx);
+AcpiAml acpi_shiftright(AcpiAml arg1, int count);
+AcpiAml acpi_shiftleft(AcpiAml arg1, int count);
 AcpiAml GCC_FMT_ATTR(4, 5)
 acpi_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
                const char *name_format, ...);
@@ -155,6 +166,7 @@ AcpiAml acpi_def_block(const char *signature, uint8_t revision,
                        const char *oem_id, const char *oem_table_id,
                        uint32_t oem_revision);
 AcpiAml acpi_if(AcpiAml predicate);
+AcpiAml acpi_while(AcpiAml predicate);
 AcpiAml acpi_method(const char *name, int arg_count);
 AcpiAml GCC_FMT_ATTR(1, 2) acpi_scope(const char *name_format, ...);
 AcpiAml GCC_FMT_ATTR(1, 2) acpi_device(const char *name_format, ...);
Signature · 1 line
2.1.0
#2 lore raw

RFC PATCH v2 2/17 hw/acpi: add support for multiple root busses

In reply to Marcel Apfelbaum

If the machine has several root busses, we need to add them to acpi in order to be properly detected by guests.

---
 hw/i386/acpi-build.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
hw/i386/acpi-build.c +32 -0
index e7db85d..fedcb2e 100644
@@ -60,6 +60,8 @@
 #include "qom/qom-qobject.h"
 #include "exec/ram_addr.h"
 
+#include "qmp-commands.h"
+
 /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
  * -M pc-i440fx-2.0.  Even if the actual amount of AML generated grows
  * a little bit, there should be plenty of free space since the DSDT
@@ -661,6 +663,36 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
     ssdt =
         acpi_def_block("SSDT", 1, ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, 1);
 
+    {
+        PciInfoList *info_list, *info;
+        Error *err = NULL;
+
+        info_list = qmp_query_pci(&err);
+        if (err) {
+            error_free(err);
+            return;
+        }
+
+        for (info = info_list; info; info = info->next) {
+            PciInfo *bus_info = info->value;
+
+            if (bus_info->bus == 0) {
+                continue;
+            }
+
+            scope = acpi_scope("\\_SB");
+            dev = acpi_device("PC%.02X", (uint8_t)bus_info->bus);
+            aml_append(&dev, acpi_name_decl("_UID",
+                acpi_string("PC%.02X", (uint8_t)bus_info->bus)));
+            aml_append(&dev, acpi_name_decl("_HID", acpi_string("PNP0A03")));
+            aml_append(&dev,
+                acpi_name_decl("_BBN", acpi_int((uint8_t)bus_info->bus)));
+            aml_append(&scope, dev);
+            aml_append(&ssdt, scope);
+        }
+        qapi_free_PciInfoList(info_list);
+    }
+
     scope = acpi_scope("\\_SB.PCI0");
     /* build PCI0._CRS */
     crs = acpi_resource_template();
Signature · 1 line
2.1.0
#3 lore raw

RFC PATCH v2 3/17 hw/apci: add _PRT method for extra root busses

In reply to Marcel Apfelbaum
---
 hw/i386/acpi-build.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
hw/i386/acpi-build.c +77 -0
index fedcb2e..ee1a50a 100644
@@ -643,6 +643,82 @@ static void build_append_pci_bus_devices(AcpiAml *parent_scope, PCIBus *bus,
     aml_append(parent_scope, method);
 }
 
+static AcpiAml build_prt(void)
+{
+    AcpiAml method, pkg, if_ctx, while_ctx;
+
+    method = acpi_method("_PRT", 0);
+
+    aml_append(&method, acpi_store(acpi_package(128), acpi_local0()));
+    aml_append(&method, acpi_store(acpi_int(0), acpi_local1()));
+    while_ctx = acpi_while(acpi_lless(acpi_local1(), acpi_int(128)));
+    {
+        aml_append(&while_ctx,
+                acpi_store(acpi_shiftright(acpi_local1(), 2),
+                    acpi_local2()));
+        aml_append(&while_ctx, acpi_store(
+                    acpi_and(acpi_add(acpi_local1(), acpi_local2()), acpi_int(3)), acpi_local3()));
+
+        if_ctx = acpi_if(acpi_equal(acpi_local3(), acpi_int(0)));
+        {
+            pkg = acpi_package(4);
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&pkg, acpi_name("LNKD"));
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&if_ctx, acpi_store(pkg, acpi_local4()));
+        }
+        aml_append(&while_ctx, if_ctx);
+
+        if_ctx = acpi_if(acpi_equal(acpi_local3(), acpi_int(1)));
+        {
+            pkg = acpi_package(4);
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&pkg, acpi_name("LNKA"));
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&if_ctx, acpi_store(pkg, acpi_local4()));
+        }
+        aml_append(&while_ctx, if_ctx);
+
+        if_ctx = acpi_if(acpi_equal(acpi_local3(), acpi_int(2)));
+        {
+            pkg = acpi_package(4);
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&pkg, acpi_name("LNKB"));
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&if_ctx, acpi_store(pkg, acpi_local4()));
+        }
+        aml_append(&while_ctx, if_ctx);
+
+        if_ctx = acpi_if(acpi_equal(acpi_local3(), acpi_int(3)));
+        {
+            pkg = acpi_package(4);
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&pkg, acpi_name("LNKC"));
+            aml_append(&pkg, acpi_int(0));
+            aml_append(&if_ctx, acpi_store(pkg, acpi_local4()));
+        }
+        aml_append(&while_ctx, if_ctx);
+
+        aml_append(&while_ctx, acpi_store(
+                    acpi_or(acpi_shiftleft(acpi_local2(), 16), acpi_int(0xFFFF)),
+                    acpi_index(acpi_local4(), acpi_int(0))));
+        aml_append(&while_ctx, acpi_store(
+                    acpi_and(acpi_local1(), acpi_int(3)),
+                    acpi_index(acpi_local4(), acpi_int(1))));
+        aml_append(&while_ctx, acpi_store(
+                    acpi_local4(), acpi_index(acpi_local0(), acpi_local1())));
+        aml_append(&while_ctx, acpi_increment(acpi_local1()));
+    }
+    aml_append(&method, while_ctx);
+    aml_append(&method, acpi_return(acpi_local0()));
+
+    return method;
+ }
+
 static void
 build_ssdt(AcpiAml *table_aml, GArray *linker,
            AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
@@ -687,6 +763,7 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
             aml_append(&dev, acpi_name_decl("_HID", acpi_string("PNP0A03")));
             aml_append(&dev,
                 acpi_name_decl("_BBN", acpi_int((uint8_t)bus_info->bus)));
+            aml_append(&dev, build_prt());
             aml_append(&scope, dev);
             aml_append(&ssdt, scope);
         }
Signature · 1 line
2.1.0
#4 lore raw

RFC PATCH v2 4/17 hw/acpi: add _CRS method for extra root busses

In reply to Marcel Apfelbaum

Save the IO/mem/bus numbers ranges assigned to the extra root busses to be removed from the root bus 0 range.

---
 hw/i386/acpi-build.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 146 insertions(+)
hw/i386/acpi-build.c +146 -0
index ee1a50a..0822a20 100644
@@ -719,6 +719,145 @@ static AcpiAml build_prt(void)
     return method;
  }
 
+typedef struct PciRangeEntry {
+    QLIST_ENTRY(PciRangeEntry) entry;
+    int64_t base;
+    int64_t limit;
+} PciRangeEntry;
+
+typedef QLIST_HEAD(PciRangeQ, PciRangeEntry) PciRangeQ;
+
+static void pci_range_insert(PciRangeQ *list, int64_t base, int64_t limit) {
+    PciRangeEntry *entry, *next, *e;
+
+    if (!base) {
+        return;
+    }
+
+   if (limit - base + 1 < 0x1000)
+       limit = base + 0x1000 - 1;
+
+    e = g_malloc(sizeof(*entry));
+    e->base = base;
+    e->limit = limit;
+
+    if (QLIST_EMPTY(list)) {
+        QLIST_INSERT_HEAD(list, e, entry);
+    } else {
+        QLIST_FOREACH_SAFE(entry, list, entry, next) {
+            if (base < entry->base) {
+                QLIST_INSERT_BEFORE(entry, e, entry);
+                break;
+            } else if (!next) {
+                QLIST_INSERT_AFTER(entry, e, entry);
+                break;
+            }
+        }
+    }
+}
+
+static void pci_range_list_free(PciRangeQ *list)
+{
+    PciRangeEntry *entry, *next;
+
+    QLIST_FOREACH_SAFE(entry, list, entry, next) {
+        QLIST_REMOVE(entry, entry);
+        g_free(entry);
+    }
+}
+
+static AcpiAml build_crs(PcPciInfo *pci, PciInfo *bus_info,
+                         PciRangeQ *io_ranges, PciRangeQ *mem_ranges)
+{
+    PciDeviceInfoList *dev_list;
+    PciMemoryRange range;
+    AcpiAml crs;
+    uint8_t max_bus;
+
+    crs = acpi_resource_template();
+    max_bus = bus_info->bus;
+
+    for (dev_list = bus_info->devices; dev_list; dev_list = dev_list->next) {
+        PciMemoryRegionList *region;
+
+        for (region = dev_list->value->regions; region; region = region->next) {
+            range.base = region->value->address;
+            range.limit = region->value->address + region->value->size - 1;
+
+            if (!strcmp(region->value->type, "io")) {
+                aml_append(&crs,
+                        acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                            acpi_pos_decode, acpi_entire_range,
+                            0x0000,
+                            range.base,
+                            range.limit,
+                            0x0000,
+                            range.limit - range.base + 1));
+                pci_range_insert(io_ranges, range.base, range.limit);
+            } else { /* "memory" */
+                aml_append(&crs,
+                        acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                            acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                            0,
+                            range.base,
+                            range.limit,
+                            0,
+                            range.limit - range.base + 1));
+                pci_range_insert(mem_ranges, range.base, range.limit);
+            }
+        }
+
+        if (dev_list->value->has_pci_bridge) {
+            PciBridgeInfo *bridge_info = dev_list->value->pci_bridge;
+
+            if (bridge_info->bus.subordinate > max_bus) {
+                max_bus = bridge_info->bus.subordinate;
+            }
+
+            range = *bridge_info->bus.io_range;
+            aml_append(&crs,
+                    acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                        acpi_pos_decode, acpi_entire_range,
+                        0x0000,
+                        range.base,
+                        range.limit,
+                        0x0000,
+                        range.limit - range.base + 1));
+            pci_range_insert(io_ranges, range.base, range.limit);
+
+            range = *bridge_info->bus.memory_range;
+            aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                        acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                        0,
+                        range.base,
+                        range.limit,
+                        0,
+                        range.limit - range.base + 1));
+            pci_range_insert(mem_ranges, range.base, range.limit);
+
+            range = *bridge_info->bus.prefetchable_range;
+            aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                        acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                        0,
+                        range.base,
+                        range.limit,
+                        0,
+                        range.limit - range.base + 1));
+            pci_range_insert(mem_ranges, range.base, range.limit);
+        }
+    }
+
+    aml_append(&crs,
+        acpi_word_bus_number(acpi_min_fixed, acpi_max_fixed,
+                             acpi_pos_decode,
+                             0x0000, bus_info->bus, max_bus,
+                             0x0000, max_bus - bus_info->bus + 1));
+
+    return crs;
+}
+
 static void
 build_ssdt(AcpiAml *table_aml, GArray *linker,
            AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
@@ -729,6 +868,8 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
     unsigned acpi_cpus = guest_info->apic_id_limit;
     AcpiAml pkg, scope, dev, method, crs, field, ifctx, ssdt;
     int i;
+    PciRangeQ io_ranges = QLIST_HEAD_INITIALIZER(io_ranges);
+    PciRangeQ mem_ranges = QLIST_HEAD_INITIALIZER(mem_ranges);
 
     /* The current AML generator can cover the APIC ID range [0..255],
      * inclusive, for VCPU hotplug. */
@@ -764,9 +905,14 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
             aml_append(&dev,
                 acpi_name_decl("_BBN", acpi_int((uint8_t)bus_info->bus)));
             aml_append(&dev, build_prt());
+            crs = build_crs(pci, bus_info, &io_ranges, &mem_ranges);
+            aml_append(&dev, acpi_name_decl("_CRS", crs));
             aml_append(&scope, dev);
             aml_append(&ssdt, scope);
         }
+
+        pci_range_list_free(&io_ranges);
+        pci_range_list_free(&mem_ranges);
         qapi_free_PciInfoList(info_list);
     }
 
Signature · 1 line
2.1.0
#5 lore raw

Re: [Qemu-devel] [PATCH RFC V2 04/17] hw/acpi: add _CRS method for extra root busses

Quoted 2 lines · On Mon, 16 Feb 2015 11:54:04 +0200 Marcel Apfelbaum <[email protected]> wrote:

Save the IO/mem/bus numbers ranges assigned to the extra root busses to be removed from the root bus 0 range.

Is it possible to make BIOS program extra buses and root bus 0 in sane way so their resources won't intersect?
That way QEMU will be able to just read resources from root buses and generate AML in the same manner for every root bus instead of collect->exclude hack and special casing root bus 0.

Quoted 180 lines
---
 hw/i386/acpi-build.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 146 insertions(+)
hw/i386/acpi-build.c +146 -0
index ee1a50a..0822a20 100644
@@ -719,6 +719,145 @@ static AcpiAml build_prt(void)
     return method;
  }
 
+typedef struct PciRangeEntry {
+    QLIST_ENTRY(PciRangeEntry) entry;
+    int64_t base;
+    int64_t limit;
+} PciRangeEntry;
+
+typedef QLIST_HEAD(PciRangeQ, PciRangeEntry) PciRangeQ;
+
+static void pci_range_insert(PciRangeQ *list, int64_t base, int64_t limit) {
+    PciRangeEntry *entry, *next, *e;
+
+    if (!base) {
+        return;
+    }
+
+   if (limit - base + 1 < 0x1000)
+       limit = base + 0x1000 - 1;
+
+    e = g_malloc(sizeof(*entry));
+    e->base = base;
+    e->limit = limit;
+
+    if (QLIST_EMPTY(list)) {
+        QLIST_INSERT_HEAD(list, e, entry);
+    } else {
+        QLIST_FOREACH_SAFE(entry, list, entry, next) {
+            if (base < entry->base) {
+                QLIST_INSERT_BEFORE(entry, e, entry);
+                break;
+            } else if (!next) {
+                QLIST_INSERT_AFTER(entry, e, entry);
+                break;
+            }
+        }
+    }
+}
+
+static void pci_range_list_free(PciRangeQ *list)
+{
+    PciRangeEntry *entry, *next;
+
+    QLIST_FOREACH_SAFE(entry, list, entry, next) {
+        QLIST_REMOVE(entry, entry);
+        g_free(entry);
+    }
+}
+
+static AcpiAml build_crs(PcPciInfo *pci, PciInfo *bus_info,
+                         PciRangeQ *io_ranges, PciRangeQ *mem_ranges)
+{
+    PciDeviceInfoList *dev_list;
+    PciMemoryRange range;
+    AcpiAml crs;
+    uint8_t max_bus;
+
+    crs = acpi_resource_template();
+    max_bus = bus_info->bus;
+
+    for (dev_list = bus_info->devices; dev_list; dev_list = dev_list->next) {
+        PciMemoryRegionList *region;
+
+        for (region = dev_list->value->regions; region; region = region->next) {
+            range.base = region->value->address;
+            range.limit = region->value->address + region->value->size - 1;
+
+            if (!strcmp(region->value->type, "io")) {
+                aml_append(&crs,
+                        acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                            acpi_pos_decode, acpi_entire_range,
+                            0x0000,
+                            range.base,
+                            range.limit,
+                            0x0000,
+                            range.limit - range.base + 1));
+                pci_range_insert(io_ranges, range.base, range.limit);
+            } else { /* "memory" */
+                aml_append(&crs,
+                        acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                            acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                            0,
+                            range.base,
+                            range.limit,
+                            0,
+                            range.limit - range.base + 1));
+                pci_range_insert(mem_ranges, range.base, range.limit);
+            }
+        }
+
+        if (dev_list->value->has_pci_bridge) {
+            PciBridgeInfo *bridge_info = dev_list->value->pci_bridge;
+
+            if (bridge_info->bus.subordinate > max_bus) {
+                max_bus = bridge_info->bus.subordinate;
+            }
+
+            range = *bridge_info->bus.io_range;
+            aml_append(&crs,
+                    acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                        acpi_pos_decode, acpi_entire_range,
+                        0x0000,
+                        range.base,
+                        range.limit,
+                        0x0000,
+                        range.limit - range.base + 1));
+            pci_range_insert(io_ranges, range.base, range.limit);
+
+            range = *bridge_info->bus.memory_range;
+            aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                        acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                        0,
+                        range.base,
+                        range.limit,
+                        0,
+                        range.limit - range.base + 1));
+            pci_range_insert(mem_ranges, range.base, range.limit);
+
+            range = *bridge_info->bus.prefetchable_range;
+            aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                        acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                        0,
+                        range.base,
+                        range.limit,
+                        0,
+                        range.limit - range.base + 1));
+            pci_range_insert(mem_ranges, range.base, range.limit);
+        }
+    }
+
+    aml_append(&crs,
+        acpi_word_bus_number(acpi_min_fixed, acpi_max_fixed,
+                             acpi_pos_decode,
+                             0x0000, bus_info->bus, max_bus,
+                             0x0000, max_bus - bus_info->bus + 1));
+
+    return crs;
+}
+
 static void
 build_ssdt(AcpiAml *table_aml, GArray *linker,
            AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
@@ -729,6 +868,8 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
     unsigned acpi_cpus = guest_info->apic_id_limit;
     AcpiAml pkg, scope, dev, method, crs, field, ifctx, ssdt;
     int i;
+    PciRangeQ io_ranges = QLIST_HEAD_INITIALIZER(io_ranges);
+    PciRangeQ mem_ranges = QLIST_HEAD_INITIALIZER(mem_ranges);
 
     /* The current AML generator can cover the APIC ID range [0..255],
      * inclusive, for VCPU hotplug. */
@@ -764,9 +905,14 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
             aml_append(&dev,
                 acpi_name_decl("_BBN", acpi_int((uint8_t)bus_info->bus)));
             aml_append(&dev, build_prt());
+            crs = build_crs(pci, bus_info, &io_ranges, &mem_ranges);
+            aml_append(&dev, acpi_name_decl("_CRS", crs));
             aml_append(&scope, dev);
             aml_append(&ssdt, scope);
         }
+
+        pci_range_list_free(&io_ranges);
+        pci_range_list_free(&mem_ranges);
         qapi_free_PciInfoList(info_list);
     }
 
#6 lore raw

Re: [Qemu-devel] [PATCH RFC V2 04/17] hw/acpi: add _CRS method for extra root busses

Quoted 5 lines · Igor Mammedov
Quoted 2 lines · On Mon, 16 Feb 2015 11:54:04 +0200 Marcel Apfelbaum <[email protected]> wrote:

Save the IO/mem/bus numbers ranges assigned to the extra root busses to be removed from the root bus 0 range.

Hi Igor,
Thanks for the review.

Quoted 2 lines

Is it possible to make BIOS program extra buses and root bus 0 in sane way so their resources won't intersect?

It is possible and actually the first version was coded like that, however the version was rejected by the BIOS review because the resources allocation would not be optimal.
This is the best way to allocate the resources so no space will be wasted.

Quoted 3 lines

That way QEMU will be able to just read resources from root buses and generate AML in the same manner for every root bus instead of collect->exclude hack and special casing root bus 0.

This implementation supports actually the possibility to read resources also for bus 0, I chose this approach to have Bus 0 (and all busses behind it) the only owner of the unused/hot-pluggable memory ranges. The extra roots are hot-pluggable because they have a pci-2-pci bridge behind.

Thanks,
Marcel

Quoted 182 lines
Quoted 181 lines
---
  hw/i386/acpi-build.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 146 insertions(+)
hw/i386/acpi-build.c +146 -0
index ee1a50a..0822a20 100644
@@ -719,6 +719,145 @@ static AcpiAml build_prt(void)
      return method;
   }
 
+typedef struct PciRangeEntry {
+    QLIST_ENTRY(PciRangeEntry) entry;
+    int64_t base;
+    int64_t limit;
+} PciRangeEntry;
+
+typedef QLIST_HEAD(PciRangeQ, PciRangeEntry) PciRangeQ;
+
+static void pci_range_insert(PciRangeQ *list, int64_t base, int64_t limit) {
+    PciRangeEntry *entry, *next, *e;
+
+    if (!base) {
+        return;
+    }
+
+   if (limit - base + 1 < 0x1000)
+       limit = base + 0x1000 - 1;
+
+    e = g_malloc(sizeof(*entry));
+    e->base = base;
+    e->limit = limit;
+
+    if (QLIST_EMPTY(list)) {
+        QLIST_INSERT_HEAD(list, e, entry);
+    } else {
+        QLIST_FOREACH_SAFE(entry, list, entry, next) {
+            if (base < entry->base) {
+                QLIST_INSERT_BEFORE(entry, e, entry);
+                break;
+            } else if (!next) {
+                QLIST_INSERT_AFTER(entry, e, entry);
+                break;
+            }
+        }
+    }
+}
+
+static void pci_range_list_free(PciRangeQ *list)
+{
+    PciRangeEntry *entry, *next;
+
+    QLIST_FOREACH_SAFE(entry, list, entry, next) {
+        QLIST_REMOVE(entry, entry);
+        g_free(entry);
+    }
+}
+
+static AcpiAml build_crs(PcPciInfo *pci, PciInfo *bus_info,
+                         PciRangeQ *io_ranges, PciRangeQ *mem_ranges)
+{
+    PciDeviceInfoList *dev_list;
+    PciMemoryRange range;
+    AcpiAml crs;
+    uint8_t max_bus;
+
+    crs = acpi_resource_template();
+    max_bus = bus_info->bus;
+
+    for (dev_list = bus_info->devices; dev_list; dev_list = dev_list->next) {
+        PciMemoryRegionList *region;
+
+        for (region = dev_list->value->regions; region; region = region->next) {
+            range.base = region->value->address;
+            range.limit = region->value->address + region->value->size - 1;
+
+            if (!strcmp(region->value->type, "io")) {
+                aml_append(&crs,
+                        acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                            acpi_pos_decode, acpi_entire_range,
+                            0x0000,
+                            range.base,
+                            range.limit,
+                            0x0000,
+                            range.limit - range.base + 1));
+                pci_range_insert(io_ranges, range.base, range.limit);
+            } else { /* "memory" */
+                aml_append(&crs,
+                        acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                            acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                            0,
+                            range.base,
+                            range.limit,
+                            0,
+                            range.limit - range.base + 1));
+                pci_range_insert(mem_ranges, range.base, range.limit);
+            }
+        }
+
+        if (dev_list->value->has_pci_bridge) {
+            PciBridgeInfo *bridge_info = dev_list->value->pci_bridge;
+
+            if (bridge_info->bus.subordinate > max_bus) {
+                max_bus = bridge_info->bus.subordinate;
+            }
+
+            range = *bridge_info->bus.io_range;
+            aml_append(&crs,
+                    acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                        acpi_pos_decode, acpi_entire_range,
+                        0x0000,
+                        range.base,
+                        range.limit,
+                        0x0000,
+                        range.limit - range.base + 1));
+            pci_range_insert(io_ranges, range.base, range.limit);
+
+            range = *bridge_info->bus.memory_range;
+            aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                        acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                        0,
+                        range.base,
+                        range.limit,
+                        0,
+                        range.limit - range.base + 1));
+            pci_range_insert(mem_ranges, range.base, range.limit);
+
+            range = *bridge_info->bus.prefetchable_range;
+            aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                        acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                        0,
+                        range.base,
+                        range.limit,
+                        0,
+                        range.limit - range.base + 1));
+            pci_range_insert(mem_ranges, range.base, range.limit);
+        }
+    }
+
+    aml_append(&crs,
+        acpi_word_bus_number(acpi_min_fixed, acpi_max_fixed,
+                             acpi_pos_decode,
+                             0x0000, bus_info->bus, max_bus,
+                             0x0000, max_bus - bus_info->bus + 1));
+
+    return crs;
+}
+
  static void
  build_ssdt(AcpiAml *table_aml, GArray *linker,
             AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
@@ -729,6 +868,8 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
      unsigned acpi_cpus = guest_info->apic_id_limit;
      AcpiAml pkg, scope, dev, method, crs, field, ifctx, ssdt;
      int i;
+    PciRangeQ io_ranges = QLIST_HEAD_INITIALIZER(io_ranges);
+    PciRangeQ mem_ranges = QLIST_HEAD_INITIALIZER(mem_ranges);
 
      /* The current AML generator can cover the APIC ID range [0..255],
       * inclusive, for VCPU hotplug. */
@@ -764,9 +905,14 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
              aml_append(&dev,
                  acpi_name_decl("_BBN", acpi_int((uint8_t)bus_info->bus)));
              aml_append(&dev, build_prt());
+            crs = build_crs(pci, bus_info, &io_ranges, &mem_ranges);
+            aml_append(&dev, acpi_name_decl("_CRS", crs));
              aml_append(&scope, dev);
              aml_append(&ssdt, scope);
          }
+
+        pci_range_list_free(&io_ranges);
+        pci_range_list_free(&mem_ranges);
          qapi_free_PciInfoList(info_list);
      }
 
#7 lore raw

Re: [Qemu-devel] [PATCH RFC V2 04/17] hw/acpi: add _CRS method for extra root busses

In reply to Igor Mammedov
Quoted 10 lines · Igor Mammedov
Quoted 2 lines · On Mon, 16 Feb 2015 11:54:04 +0200 Marcel Apfelbaum <[email protected]> wrote:

Save the IO/mem/bus numbers ranges assigned to the extra root busses to be removed from the root bus 0 range.

Is it possible to make BIOS program extra buses and root bus 0 in sane way so their resources won't intersect?
That way QEMU will be able to just read resources from root buses and generate AML in the same manner for every root bus instead of collect->exclude hack and special casing root bus 0.

Even if it is, I think it's cleaner not to assume specific guest behaviour.
Since it's not a lot of code to handle arbitrary configurations, it's easier to just do it generically.

Quoted 180 lines
Quoted 180 lines
---
 hw/i386/acpi-build.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 146 insertions(+)
hw/i386/acpi-build.c +146 -0
index ee1a50a..0822a20 100644
@@ -719,6 +719,145 @@ static AcpiAml build_prt(void)
     return method;
  }
 
+typedef struct PciRangeEntry {
+    QLIST_ENTRY(PciRangeEntry) entry;
+    int64_t base;
+    int64_t limit;
+} PciRangeEntry;
+
+typedef QLIST_HEAD(PciRangeQ, PciRangeEntry) PciRangeQ;
+
+static void pci_range_insert(PciRangeQ *list, int64_t base, int64_t limit) {
+    PciRangeEntry *entry, *next, *e;
+
+    if (!base) {
+        return;
+    }
+
+   if (limit - base + 1 < 0x1000)
+       limit = base + 0x1000 - 1;
+
+    e = g_malloc(sizeof(*entry));
+    e->base = base;
+    e->limit = limit;
+
+    if (QLIST_EMPTY(list)) {
+        QLIST_INSERT_HEAD(list, e, entry);
+    } else {
+        QLIST_FOREACH_SAFE(entry, list, entry, next) {
+            if (base < entry->base) {
+                QLIST_INSERT_BEFORE(entry, e, entry);
+                break;
+            } else if (!next) {
+                QLIST_INSERT_AFTER(entry, e, entry);
+                break;
+            }
+        }
+    }
+}
+
+static void pci_range_list_free(PciRangeQ *list)
+{
+    PciRangeEntry *entry, *next;
+
+    QLIST_FOREACH_SAFE(entry, list, entry, next) {
+        QLIST_REMOVE(entry, entry);
+        g_free(entry);
+    }
+}
+
+static AcpiAml build_crs(PcPciInfo *pci, PciInfo *bus_info,
+                         PciRangeQ *io_ranges, PciRangeQ *mem_ranges)
+{
+    PciDeviceInfoList *dev_list;
+    PciMemoryRange range;
+    AcpiAml crs;
+    uint8_t max_bus;
+
+    crs = acpi_resource_template();
+    max_bus = bus_info->bus;
+
+    for (dev_list = bus_info->devices; dev_list; dev_list = dev_list->next) {
+        PciMemoryRegionList *region;
+
+        for (region = dev_list->value->regions; region; region = region->next) {
+            range.base = region->value->address;
+            range.limit = region->value->address + region->value->size - 1;
+
+            if (!strcmp(region->value->type, "io")) {
+                aml_append(&crs,
+                        acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                            acpi_pos_decode, acpi_entire_range,
+                            0x0000,
+                            range.base,
+                            range.limit,
+                            0x0000,
+                            range.limit - range.base + 1));
+                pci_range_insert(io_ranges, range.base, range.limit);
+            } else { /* "memory" */
+                aml_append(&crs,
+                        acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                            acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                            0,
+                            range.base,
+                            range.limit,
+                            0,
+                            range.limit - range.base + 1));
+                pci_range_insert(mem_ranges, range.base, range.limit);
+            }
+        }
+
+        if (dev_list->value->has_pci_bridge) {
+            PciBridgeInfo *bridge_info = dev_list->value->pci_bridge;
+
+            if (bridge_info->bus.subordinate > max_bus) {
+                max_bus = bridge_info->bus.subordinate;
+            }
+
+            range = *bridge_info->bus.io_range;
+            aml_append(&crs,
+                    acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                        acpi_pos_decode, acpi_entire_range,
+                        0x0000,
+                        range.base,
+                        range.limit,
+                        0x0000,
+                        range.limit - range.base + 1));
+            pci_range_insert(io_ranges, range.base, range.limit);
+
+            range = *bridge_info->bus.memory_range;
+            aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                        acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                        0,
+                        range.base,
+                        range.limit,
+                        0,
+                        range.limit - range.base + 1));
+            pci_range_insert(mem_ranges, range.base, range.limit);
+
+            range = *bridge_info->bus.prefetchable_range;
+            aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed,
+                        acpi_max_fixed, acpi_non_cacheable, acpi_ReadWrite,
+                        0,
+                        range.base,
+                        range.limit,
+                        0,
+                        range.limit - range.base + 1));
+            pci_range_insert(mem_ranges, range.base, range.limit);
+        }
+    }
+
+    aml_append(&crs,
+        acpi_word_bus_number(acpi_min_fixed, acpi_max_fixed,
+                             acpi_pos_decode,
+                             0x0000, bus_info->bus, max_bus,
+                             0x0000, max_bus - bus_info->bus + 1));
+
+    return crs;
+}
+
 static void
 build_ssdt(AcpiAml *table_aml, GArray *linker,
            AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
@@ -729,6 +868,8 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
     unsigned acpi_cpus = guest_info->apic_id_limit;
     AcpiAml pkg, scope, dev, method, crs, field, ifctx, ssdt;
     int i;
+    PciRangeQ io_ranges = QLIST_HEAD_INITIALIZER(io_ranges);
+    PciRangeQ mem_ranges = QLIST_HEAD_INITIALIZER(mem_ranges);
 
     /* The current AML generator can cover the APIC ID range [0..255],
      * inclusive, for VCPU hotplug. */
@@ -764,9 +905,14 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
             aml_append(&dev,
                 acpi_name_decl("_BBN", acpi_int((uint8_t)bus_info->bus)));
             aml_append(&dev, build_prt());
+            crs = build_crs(pci, bus_info, &io_ranges, &mem_ranges);
+            aml_append(&dev, acpi_name_decl("_CRS", crs));
             aml_append(&scope, dev);
             aml_append(&ssdt, scope);
         }
+
+        pci_range_list_free(&io_ranges);
+        pci_range_list_free(&mem_ranges);
         qapi_free_PciInfoList(info_list);
     }
 
#8 lore raw

Re: [Qemu-devel] [PATCH RFC V2 04/17] hw/acpi: add _CRS method for extra root busses

In reply to Marcel Apfelbaum
Quoted 23 lines · Marcel Apfelbaum

Save the IO/mem/bus numbers ranges assigned to the extra root busses to be removed from the root bus 0 range.

---
  hw/i386/acpi-build.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 146 insertions(+)
hw/i386/acpi-build.c +7 -0
index ee1a50a..0822a20 100644
@@ -719,6 +719,145 @@ static AcpiAml build_prt(void)
      return method;
   }
 
+typedef struct PciRangeEntry {
+    QLIST_ENTRY(PciRangeEntry) entry;
+    int64_t base;
+    int64_t limit;
+} PciRangeEntry;
+
+typedef QLIST_HEAD(PciRangeQ, PciRangeEntry) PciRangeQ;

hw/i386/acpi-build.c static AcpiAml build_prt(void) · line 738

Quoted 10 lines
hw/i386/acpi-build.c +10 -0
+
+static void pci_range_insert(PciRangeQ *list, int64_t base, int64_t limit) {
+    PciRangeEntry *entry, *next, *e;
+
+    if (!base) {
+        return;
+    }
+
+   if (limit - base + 1 < 0x1000)
+       limit = base + 0x1000 - 1;

The above hack is not needed anymore, it was needed before:
- [PATCH v4 0/3] pc: acpi-build: make linker & RSDP tables dynamic
as a temp solution.

I will remove it from the next version.
Thanks,
Marcel

[...]

#9 lore raw

RFC PATCH v2 5/17 hw/acpi: remove from root bus 0 the crs resources used by other busses.

In reply to Marcel Apfelbaum

If multiple root busses are used, root bus 0 cannot use all the pci holes ranges. Remove the IO/mem ranges used by the other primary busses.

---
 hw/i386/acpi-build.c | 85 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 73 insertions(+), 12 deletions(-)
hw/i386/acpi-build.c +73 -12
index 0822a20..b2b0036 100644
@@ -870,6 +870,9 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
     int i;
     PciRangeQ io_ranges = QLIST_HEAD_INITIALIZER(io_ranges);
     PciRangeQ mem_ranges = QLIST_HEAD_INITIALIZER(mem_ranges);
+    PciMemoryRange range;
+    PciRangeEntry *entry;
+    int root_bus_limit = 0xFF;
 
     /* The current AML generator can cover the APIC ID range [0..255],
      * inclusive, for VCPU hotplug. */
@@ -897,6 +900,10 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
                 continue;
             }
 
+            if (bus_info->bus < root_bus_limit) {
+                root_bus_limit = bus_info->bus - 1;
+            }
+
             scope = acpi_scope("\\_SB");
             dev = acpi_device("PC%.02X", (uint8_t)bus_info->bus);
             aml_append(&dev, acpi_name_decl("_UID",
@@ -911,8 +918,6 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
             aml_append(&ssdt, scope);
         }
 
-        pci_range_list_free(&io_ranges);
-        pci_range_list_free(&mem_ranges);
         qapi_free_PciInfoList(info_list);
     }
 
@@ -921,26 +926,79 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
     crs = acpi_resource_template();
     aml_append(&crs,
         acpi_word_bus_number(acpi_min_fixed, acpi_max_fixed, acpi_pos_decode,
-                             0x0000, 0x0000, 0x00FF, 0x0000, 0x0100));
+                             0x0000, 0x0, root_bus_limit,
+                             0x0000, root_bus_limit + 1));
+
     aml_append(&crs, acpi_io(acpi_decode16, 0x0CF8, 0x0CF8, 0x01, 0x08));
 
     aml_append(&crs,
         acpi_word_io(acpi_min_fixed, acpi_max_fixed,
                      acpi_pos_decode, acpi_entire_range,
                      0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
-    aml_append(&crs,
-        acpi_word_io(acpi_min_fixed, acpi_max_fixed,
-                     acpi_pos_decode, acpi_entire_range,
-                     0x0000, 0x0D00, 0xFFFF, 0x0000, 0xF300));
+
+    /* prepare PCI IO ranges */
+    range.base = 0x0D00;
+    range.limit = 0xFFFF;
+    if (QLIST_EMPTY(&io_ranges)) {
+        aml_append(&crs,
+            acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                         acpi_pos_decode, acpi_entire_range,
+                         0x0000, range.base, range.limit,
+                         0x0000, range.limit - range.base + 1));
+    } else {
+        QLIST_FOREACH(entry, &io_ranges, entry) {
+            if (range.base < entry->base) {
+                aml_append(&crs,
+                    acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                                 acpi_pos_decode, acpi_entire_range,
+                                 0x0000, range.base, entry->base - 1,
+                                 0x0000, entry->base - range.base));
+            }
+            range.base = entry->limit + 1;
+            if (!QLIST_NEXT(entry, entry)) {
+                aml_append(&crs,
+                    acpi_word_io(acpi_min_fixed, acpi_max_fixed,
+                                 acpi_pos_decode, acpi_entire_range,
+                                 0x0000, range.base, range.limit,
+                                 0x0000, range.limit - range.base + 1));
+            }
+        }
+    }
+
     aml_append(&crs,
         acpi_dword_memory(acpi_pos_decode, acpi_min_fixed, acpi_max_fixed,
                           acpi_cacheable, acpi_ReadWrite,
                           0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
-    aml_append(&crs,
-        acpi_dword_memory(acpi_pos_decode, acpi_min_fixed, acpi_max_fixed,
-                          acpi_non_cacheable, acpi_ReadWrite,
-                          0, pci->w32.begin, pci->w32.end - 1, 0,
-                          pci->w32.end - pci->w32.begin));
+
+    /* prepare PCI memory ranges */
+    range.base = pci->w32.begin;
+    range.limit = pci->w32.end - 1;
+    if (QLIST_EMPTY(&mem_ranges)) {
+        aml_append(&crs,
+            acpi_dword_memory(acpi_pos_decode, acpi_min_fixed, acpi_max_fixed,
+                              acpi_non_cacheable, acpi_ReadWrite,
+                              0, range.base, range.limit,
+                              0, range.limit - range.base + 1));
+    } else {
+        QLIST_FOREACH(entry, &mem_ranges, entry) {
+            if (range.base < entry->base) {
+                aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed, acpi_max_fixed,
+                                      acpi_non_cacheable, acpi_ReadWrite,
+                                      0, range.base, entry->base - 1,
+                                      0, entry->base - range.base));
+            }
+            range.base = entry->limit + 1;
+            if (!QLIST_NEXT(entry, entry)) {
+                aml_append(&crs,
+                    acpi_dword_memory(acpi_pos_decode, acpi_min_fixed, acpi_max_fixed,
+                                      acpi_non_cacheable, acpi_ReadWrite,
+                                      0, range.base, range.limit,
+                                      0, range.base - range.limit + 1));
+            }
+        }
+    }
+
     if (pci->w64.begin) {
         aml_append(&crs,
             acpi_qword_memory(acpi_pos_decode, acpi_min_fixed, acpi_max_fixed,
@@ -950,6 +1008,9 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
     }
     aml_append(&scope, acpi_name_decl("_CRS", crs));
 
+    pci_range_list_free(&io_ranges);
+    pci_range_list_free(&mem_ranges);
+
     /* reserve PCIHP resources */
     if (pm->pcihp_io_len) {
         dev = acpi_device("PHPR");
Signature · 1 line
2.1.0
#10 lore raw

RFC PATCH v2 6/17 hw/pci: move pci bus related code to separate files

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

This refactoring moves all the code needed (recursively) to register TYPE_PCI_BUS type to a new file hw/pci/pci_bus.c . This allows to properly add new functionality to the pci bus class.

---
 arch_init.c              |   1 +
 hw/alpha/typhoon.c       |   1 +
 hw/mips/gt64xxx_pci.c    |   1 +
 hw/pci-host/bonito.c     |   1 +
 hw/pci-host/grackle.c    |   1 +
 hw/pci-host/piix.c       |   1 +
 hw/pci-host/ppce500.c    |   1 +
 hw/pci-host/q35.c        |   1 +
 hw/pci-host/uninorth.c   |   1 +
 hw/pci/Makefile.objs     |   2 +-
 hw/pci/pci.c             | 472 +--------------------------------------------
 hw/pci/pci_bus.c         | 491 +++++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/ppc4xx_pci.c      |   1 +
 hw/sh4/r2d.c             |   1 +
 hw/sh4/sh_pci.c          |   1 +
 include/hw/pci/pci.h     |   3 +-
 include/hw/pci/pci_bus.h |   8 +
 17 files changed, 514 insertions(+), 474 deletions(-)
 create mode 100644 hw/pci/pci_bus.c
arch_init.c +1 -0
index 89c8fa4..f2f7452 100644
@@ -37,6 +37,7 @@
 #include "audio/audio.h"
 #include "hw/i386/pc.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/audio/audio.h"
 #include "sysemu/kvm.h"
 #include "migration/migration.h"
hw/alpha/typhoon.c +1 -0
index 5310006..c8be8a6 100644
@@ -8,6 +8,7 @@
 
 #include "cpu.h"
 #include "hw/hw.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/devices.h"
 #include "sysemu/sysemu.h"
 #include "alpha_sys.h"
hw/mips/gt64xxx_pci.c +1 -0
index 1f2fe5f..6856bb0 100644
@@ -25,6 +25,7 @@
 #include "hw/hw.h"
 #include "hw/mips/mips.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_host.h"
 #include "hw/i386/pc.h"
 #include "exec/address-spaces.h"
hw/pci-host/bonito.c +1 -0
index 56292ad..72d6e12 100644
@@ -41,6 +41,7 @@
 
 #include "hw/hw.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/i386/pc.h"
 #include "hw/mips/mips.h"
 #include "hw/pci/pci_host.h"
hw/pci-host/grackle.c +1 -0
index 6c7cfdb..7ee6417 100644
@@ -26,6 +26,7 @@
 #include "hw/pci/pci_host.h"
 #include "hw/ppc/mac.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 
 /* debug Grackle */
 //#define DEBUG_GRACKLE
hw/pci-host/piix.c +1 -0
index 1530038..f7cfbc8 100644
@@ -25,6 +25,7 @@
 #include "hw/hw.h"
 #include "hw/i386/pc.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_host.h"
 #include "hw/isa/isa.h"
 #include "hw/sysbus.h"
hw/pci-host/ppce500.c +1 -0
index 574f8b2..a6aef6a 100644
@@ -17,6 +17,7 @@
 #include "hw/hw.h"
 #include "hw/ppc/e500-ccsr.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_host.h"
 #include "qemu/bswap.h"
 #include "hw/pci-host/ppce500.h"
hw/pci-host/q35.c +1 -0
index b20bad8..c36fefa 100644
@@ -28,6 +28,7 @@
  * THE SOFTWARE.
  */
 #include "hw/hw.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/pci-host/q35.h"
 #include "qapi/visitor.h"
 
hw/pci-host/uninorth.c +1 -0
index 21f805f..c9cc570 100644
@@ -24,6 +24,7 @@
 #include "hw/hw.h"
 #include "hw/ppc/mac.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_host.h"
 
 /* debug UniNorth */
hw/pci/Makefile.objs +1 -1
index 80f8aa6..63aa4c5 100644
@@ -1,4 +1,4 @@
-common-obj-$(CONFIG_PCI) += pci.o pci_bridge.o
+common-obj-$(CONFIG_PCI) += pci.o pci_bridge.o pci_bus.o
 common-obj-$(CONFIG_PCI) += msix.o msi.o
 common-obj-$(CONFIG_PCI) += shpc.o
 common-obj-$(CONFIG_PCI) += slotid_cap.o
hw/pci/pci.c +1 -471

531 lines not on this page · raw message

hw/pci/pci_bus.c new file +491 -0

491 lines not on this page · raw message

hw/ppc/ppc4xx_pci.c +1 -0
index 0bb3cdb..f5847bc 100644
@@ -23,6 +23,7 @@
 #include "hw/ppc/ppc.h"
 #include "hw/ppc/ppc4xx.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_host.h"
 #include "exec/address-spaces.h"
 
hw/sh4/r2d.c +1 -0
index 12f44d2..80d3c43 100644
@@ -30,6 +30,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/boards.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "net/net.h"
 #include "sh7750_regs.h"
 #include "hw/ide.h"
hw/sh4/sh_pci.c +1 -0
index a2f6d9e..f02e998 100644
@@ -24,6 +24,7 @@
 #include "hw/sysbus.h"
 #include "hw/sh4/sh.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_host.h"
 #include "qemu/bswap.h"
 #include "exec/address-spaces.h"
include/hw/pci/pci.h +1 -2
index 97a83d3..966e9cd 100644
@@ -335,8 +335,6 @@ typedef void (*pci_set_irq_fn)(void *opaque, int irq_num, int level);
 typedef int (*pci_map_irq_fn)(PCIDevice *pci_dev, int irq_num);
 typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);
 
-#define TYPE_PCI_BUS "PCI"
-#define PCI_BUS(obj) OBJECT_CHECK(PCIBus, (obj), TYPE_PCI_BUS)
 #define TYPE_PCIE_BUS "PCIE"
 
 bool pci_bus_is_express(PCIBus *bus);
@@ -368,6 +366,7 @@ void pci_bus_fire_intx_routing_notifier(PCIBus *bus);
 void pci_device_set_intx_routing_notifier(PCIDevice *dev,
                                           PCIINTxRoutingNotifier notifier);
 void pci_device_reset(PCIDevice *dev);
+void pci_do_device_reset(PCIDevice *dev);
 
 PCIDevice *pci_nic_init(NICInfo *nd, PCIBus *rootbus,
                         const char *default_model,
include/hw/pci/pci_bus.h +8 -0
index fabaeee..ea427a3 100644
@@ -1,6 +1,8 @@
 #ifndef QEMU_PCI_BUS_H
 #define QEMU_PCI_BUS_H
 
+#include "hw/pci/pci.h"
+
 /*
  * PCI Bus and Bridge datastructures.
  *
@@ -8,6 +10,12 @@
  * use accessor functions in pci.h, pci_bridge.h
  */
 
+PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
+PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
+
+#define TYPE_PCI_BUS "PCI"
+#define PCI_BUS(obj) OBJECT_CHECK(PCIBus, (obj), TYPE_PCI_BUS)
+
 struct PCIBus {
     BusState qbus;
     PCIIOMMUFunc iommu_fn;
Signature · 1 line
2.1.0
#11 lore raw

RFC PATCH v2 7/17 hw/pci: made pci_bus_is_root a PCIBusClass method

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

Refactoring it as a method of PCIBusClass will allow different implementations for subclasses.

Removed the assumption that the root bus does not have a parent device because is specific only to the default class implementation.

---
 hw/pci/pci.c             | 11 ++++-------
 hw/pci/pci_bus.c         |  9 +++++++++
 hw/vfio/pci.c            |  1 +
 include/hw/pci/pci.h     |  1 -
 include/hw/pci/pci_bus.h | 15 +++++++++++++++
 5 files changed, 29 insertions(+), 8 deletions(-)
hw/pci/pci.c +4 -7
index 933ef65..0b4e138 100644
@@ -204,7 +204,10 @@ PCIBus *pci_device_root_bus(const PCIDevice *d)
 {
     PCIBus *bus = d->bus;
 
-    while ((d = bus->parent_dev) != NULL) {
+    while (!pci_bus_is_root(bus)) {
+        d = bus->parent_dev;
+        assert(d != NULL);
+
         bus = d->bus;
     }
 
@@ -217,7 +220,6 @@ const char *pci_root_bus_path(PCIDevice *dev)
     PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
     PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge);
 
-    assert(!rootbus->parent_dev);
     assert(host_bridge->bus == rootbus);
 
     if (hc->root_bus_path) {
@@ -249,11 +251,6 @@ bool pci_bus_is_express(PCIBus *bus)
     return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
 }
 
-bool pci_bus_is_root(PCIBus *bus)
-{
-    return !bus->parent_dev;
-}
-
 void pci_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent,
                          const char *name,
                          MemoryRegion *address_space_mem,
hw/pci/pci_bus.c +9 -0
index d156194..0922a75 100644
@@ -464,9 +464,15 @@ static void pcibus_reset(BusState *qbus)
     }
 }
 
+static bool pcibus_is_root(PCIBus *bus)
+{
+    return !bus->parent_dev;
+}
+
 static void pci_bus_class_init(ObjectClass *klass, void *data)
 {
     BusClass *k = BUS_CLASS(klass);
+    PCIBusClass *pbc = PCI_BUS_CLASS(klass);
 
     k->print_dev = pcibus_dev_print;
     k->get_dev_path = pcibus_get_dev_path;
@@ -474,11 +480,14 @@ static void pci_bus_class_init(ObjectClass *klass, void *data)
     k->realize = pci_bus_realize;
     k->unrealize = pci_bus_unrealize;
     k->reset = pcibus_reset;
+
+    pbc->is_root = pcibus_is_root;
 }
 
 static const TypeInfo pci_bus_info = {
     .name = TYPE_PCI_BUS,
     .parent = TYPE_BUS,
+    .class_size = sizeof(PCIBusClass),
     .instance_size = sizeof(PCIBus),
     .class_init = pci_bus_class_init,
 };
hw/vfio/pci.c +1 -0
index 014a92c..a6f3179 100644
@@ -32,6 +32,7 @@
 #include "hw/pci/msi.h"
 #include "hw/pci/msix.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "qemu-common.h"
 #include "qemu/error-report.h"
 #include "qemu/event_notifier.h"
include/hw/pci/pci.h +0 -1
index 966e9cd..7b2567a 100644
@@ -338,7 +338,6 @@ typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);
 #define TYPE_PCIE_BUS "PCIE"
 
 bool pci_bus_is_express(PCIBus *bus);
-bool pci_bus_is_root(PCIBus *bus);
 void pci_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent,
                          const char *name,
                          MemoryRegion *address_space_mem,
include/hw/pci/pci_bus.h +15 -0
index ea427a3..306ef10 100644
@@ -15,6 +15,16 @@ PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
 
 #define TYPE_PCI_BUS "PCI"
 #define PCI_BUS(obj) OBJECT_CHECK(PCIBus, (obj), TYPE_PCI_BUS)
+#define PCI_BUS_CLASS(klass) OBJECT_CLASS_CHECK(PCIBusClass, (klass), TYPE_PCI_BUS)
+#define PCI_BUS_GET_CLASS(obj) OBJECT_GET_CLASS(PCIBusClass, (obj), TYPE_PCI_BUS)
+
+typedef struct PCIBusClass {
+    /*< private >*/
+    BusClass parent_class;
+    /*< public >*/
+
+    bool (*is_root)(PCIBus *bus);
+} PCIBusClass;
 
 struct PCIBus {
     BusState qbus;
@@ -39,6 +49,11 @@ struct PCIBus {
     int *irq_count;
 };
 
+static inline bool pci_bus_is_root(PCIBus *bus)
+{
+    return PCI_BUS_GET_CLASS(bus)->is_root(bus);
+}
+
 typedef struct PCIBridgeWindows PCIBridgeWindows;
 
 /*
Signature · 1 line
2.1.0
#12 lore raw

RFC PATCH v2 8/17 hw/pci: made pci_bus_num a PCIBusClass method

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

Refactoring it as a method of PCIBusClass will allow different implementations for subclasses.

---
 hw/i386/kvm/pci-assign.c |  1 +
 hw/pci/pci-hotplug-old.c |  1 +
 hw/pci/pci.c             |  7 -------
 hw/pci/pci_bus.c         | 10 ++++++++++
 hw/scsi/megasas.c        |  1 +
 hw/xen/xen_pt.c          |  1 +
 include/hw/pci/pci.h     |  1 -
 include/hw/pci/pci_bus.h |  6 ++++++
 8 files changed, 20 insertions(+), 8 deletions(-)
hw/i386/kvm/pci-assign.c +1 -0
index bb206da..fa67b1d 100644
@@ -35,6 +35,7 @@
 #include "qemu/range.h"
 #include "sysemu/sysemu.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/pci/msi.h"
 #include "kvm_i386.h"
 
hw/pci/pci-hotplug-old.c +1 -0
index 0c09c72..6f208d8 100644
@@ -27,6 +27,7 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "net/net.h"
 #include "hw/i386/pc.h"
 #include "monitor/monitor.h"
hw/pci/pci.c +0 -7
index 0b4e138..dccb3d1 100644
@@ -300,13 +300,6 @@ PCIBus *pci_register_bus(DeviceState *parent, const char *name,
     return bus;
 }
 
-int pci_bus_num(PCIBus *s)
-{
-    if (pci_bus_is_root(s))
-        return 0;       /* pci host bridge */
-    return s->parent_dev->config[PCI_SECONDARY_BUS];
-}
-
 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
 {
     PCIDevice *s = container_of(pv, PCIDevice, config);
hw/pci/pci_bus.c +10 -0
index 0922a75..ed99208 100644
@@ -469,6 +469,15 @@ static bool pcibus_is_root(PCIBus *bus)
     return !bus->parent_dev;
 }
 
+static int pcibus_num(PCIBus *bus)
+{
+    if (pcibus_is_root(bus)) {
+        return 0;       /* pci host bridge */
+    }
+
+    return bus->parent_dev->config[PCI_SECONDARY_BUS];
+}
+
 static void pci_bus_class_init(ObjectClass *klass, void *data)
 {
     BusClass *k = BUS_CLASS(klass);
@@ -482,6 +491,7 @@ static void pci_bus_class_init(ObjectClass *klass, void *data)
     k->reset = pcibus_reset;
 
     pbc->is_root = pcibus_is_root;
+    pbc->bus_num = pcibus_num;
 }
 
 static const TypeInfo pci_bus_info = {
hw/scsi/megasas.c +1 -0
index 4852237..fa4e3d0 100644
@@ -20,6 +20,7 @@
 
 #include "hw/hw.h"
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "sysemu/dma.h"
 #include "sysemu/block-backend.h"
 #include "hw/pci/msi.h"
hw/xen/xen_pt.c +1 -0
index f2893b2..cf56a48 100644
@@ -55,6 +55,7 @@
 #include <sys/ioctl.h>
 
 #include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
 #include "hw/xen/xen.h"
 #include "hw/xen/xen_backend.h"
 #include "xen_pt.h"
include/hw/pci/pci.h +0 -1
index 7b2567a..b7e24d4 100644
@@ -376,7 +376,6 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
 
 PCIDevice *pci_vga_init(PCIBus *bus);
 
-int pci_bus_num(PCIBus *s);
 void pci_for_each_device(PCIBus *bus, int bus_num,
                          void (*fn)(PCIBus *bus, PCIDevice *d, void *opaque),
                          void *opaque);
include/hw/pci/pci_bus.h +6 -0
index 306ef10..553814e 100644
@@ -24,6 +24,7 @@ typedef struct PCIBusClass {
     /*< public >*/
 
     bool (*is_root)(PCIBus *bus);
+    int (*bus_num)(PCIBus *bus);
 } PCIBusClass;
 
 struct PCIBus {
@@ -54,6 +55,11 @@ static inline bool pci_bus_is_root(PCIBus *bus)
     return PCI_BUS_GET_CLASS(bus)->is_root(bus);
 }
 
+static inline int pci_bus_num(PCIBus *bus)
+{
+    return PCI_BUS_GET_CLASS(bus)->bus_num(bus);
+}
+
 typedef struct PCIBridgeWindows PCIBridgeWindows;
 
 /*
Signature · 1 line
2.1.0
#13 lore raw

RFC PATCH v2 9/17 hw/pci: introduce TYPE_PCI_MAIN_HOST_BRIDGE interface

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

This is a marker interface used to differentiate the "default" host bridge on a system with multiple host bridges. This differentiation is required only for pc machines for now by the ACPI subsystem.

---
 hw/i386/acpi-build.c      | 9 ++++++---
 hw/pci-host/piix.c        | 5 +++++
 hw/pci-host/q35.c         | 4 ++++
 hw/pci/pci_host.c         | 6 ++++++
 include/hw/pci/pci_host.h | 7 +++++++
 5 files changed, 28 insertions(+), 3 deletions(-)
hw/i386/acpi-build.c +6 -3
index b2b0036..cee150b 100644
@@ -250,7 +250,8 @@ static void acpi_get_pci_info(PcPciInfo *info)
     Object *pci_host;
     bool ambiguous;
 
-    pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous);
+    pci_host = object_resolve_path_type("", TYPE_PCI_MAIN_HOST_BRIDGE,
+                                        &ambiguous);
     g_assert(!ambiguous);
     g_assert(pci_host);
 
@@ -1267,7 +1268,8 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
             PCIBus *bus = NULL;
             bool ambiguous;
 
-            pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous);
+            pci_host = object_resolve_path_type("", TYPE_PCI_MAIN_HOST_BRIDGE,
+                                                &ambiguous);
             if (!ambiguous && pci_host) {
                 bus = PCI_HOST_BRIDGE(pci_host)->bus;
             }
@@ -1610,7 +1612,8 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
     QObject *o;
     bool ambiguous;
 
-    pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous);
+    pci_host = object_resolve_path_type("", TYPE_PCI_MAIN_HOST_BRIDGE,
+                                        &ambiguous);
     g_assert(!ambiguous);
     g_assert(pci_host);
 
hw/pci-host/piix.c +5 -0
index f7cfbc8..970b9e9 100644
@@ -767,6 +767,11 @@ static const TypeInfo i440fx_pcihost_info = {
     .instance_size = sizeof(I440FXState),
     .instance_init = i440fx_pcihost_initfn,
     .class_init    = i440fx_pcihost_class_init,
+    .interfaces    = (InterfaceInfo[]) {
+        { TYPE_PCI_MAIN_HOST_BRIDGE },
+        { }
+    }
+
 };
 
 static void i440fx_register_types(void)
hw/pci-host/q35.c +4 -0
index c36fefa..bd41465 100644
@@ -193,6 +193,10 @@ static const TypeInfo q35_host_info = {
     .instance_size = sizeof(Q35PCIHost),
     .instance_init = q35_host_initfn,
     .class_init = q35_host_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_PCI_MAIN_HOST_BRIDGE },
+        { }
+    }
 };
 
 /****************************************************************************
hw/pci/pci_host.c +6 -0
index 3e26f92..87180c8 100644
@@ -175,6 +175,11 @@ const MemoryRegionOps pci_host_data_be_ops = {
     .endianness = DEVICE_BIG_ENDIAN,
 };
 
+static const TypeInfo pci_main_host_interface_info = {
+    .name          = TYPE_PCI_MAIN_HOST_BRIDGE,
+    .parent        = TYPE_INTERFACE,
+};
+
 static const TypeInfo pci_host_type_info = {
     .name = TYPE_PCI_HOST_BRIDGE,
     .parent = TYPE_SYS_BUS_DEVICE,
@@ -185,6 +190,7 @@ static const TypeInfo pci_host_type_info = {
 
 static void pci_host_register_types(void)
 {
+    type_register_static(&pci_main_host_interface_info);
     type_register_static(&pci_host_type_info);
 }
 
include/hw/pci/pci_host.h +7 -0
index ba31595..3c72e26 100644
@@ -30,6 +30,13 @@
 
 #include "hw/sysbus.h"
 
+/**
+ * Marker interface for classes whose instances can
+ * be main host bridges. It is intended to be used
+ * when the QOM tree includes multiple host bridges.
+ */
+#define TYPE_PCI_MAIN_HOST_BRIDGE "pci-main-host-bridge"
+
 #define TYPE_PCI_HOST_BRIDGE "pci-host-bridge"
 #define PCI_HOST_BRIDGE(obj) \
     OBJECT_CHECK(PCIHostState, (obj), TYPE_PCI_HOST_BRIDGE)
Signature · 1 line
2.1.0
#14 lore raw

RFC PATCH v2 10/17 hw/pci: removed 'rootbus nr is 0' assumption from qmp_pci_query

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

Use the newer pci_bus_num to correctly get the root bus number.

---
 hw/pci/pci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/pci/pci.c +2 -1
index dccb3d1..bf31168 100644
@@ -1251,7 +1251,8 @@ PciInfoList *qmp_query_pci(Error **errp)
 
     QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
         info = g_malloc0(sizeof(*info));
-        info->value = qmp_query_pci_bus(host_bridge->bus, 0);
+        info->value = qmp_query_pci_bus(host_bridge->bus,
+                                        pci_bus_num(host_bridge->bus));
 
         /* XXX: waiting for the qapi to support GSList */
         if (!cur_item) {
Signature · 1 line
2.1.0
#15 lore raw

RFC PATCH v2 11/17 hw/pci: implement iteration over multiple host bridges

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

---
 hw/pci/pci.c              | 8 ++++----
 include/hw/pci/pci_host.h | 4 ++++
 2 files changed, 8 insertions(+), 4 deletions(-)
hw/pci/pci.c +4 -4
index bf31168..d0d0035 100644
@@ -68,7 +68,7 @@ static void pci_del_option_rom(PCIDevice *pdev);
 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
 
-static QLIST_HEAD(, PCIHostState) pci_host_bridges;
+struct PCIHostQ pci_host_bridges = QLIST_HEAD_INITIALIZER(pci_host_bridges);
 
 static int pci_bar(PCIDevice *d, int reg)
 {
@@ -189,7 +189,7 @@ PCIBus *pci_find_primary_bus(void)
     PCIBus *primary_bus = NULL;
     PCIHostState *host;
 
-    QLIST_FOREACH(host, &pci_host_bridges, next) {
+    HOST_BRIDGE_FOREACH(host) {
         if (primary_bus) {
             /* We have multiple root buses, refuse to select a primary */
             return NULL;
@@ -1249,7 +1249,7 @@ PciInfoList *qmp_query_pci(Error **errp)
     PciInfoList *info, *head = NULL, *cur_item = NULL;
     PCIHostState *host_bridge;
 
-    QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
+    HOST_BRIDGE_FOREACH(host_bridge) {
         info = g_malloc0(sizeof(*info));
         info->value = qmp_query_pci_bus(host_bridge->bus,
                                         pci_bus_num(host_bridge->bus));
@@ -1778,7 +1778,7 @@ int pci_qdev_find_device(const char *id, PCIDevice **pdev)
     PCIHostState *host_bridge;
     int rc = -ENODEV;
 
-    QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
+    HOST_BRIDGE_FOREACH(host_bridge) {
         int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev);
         if (!tmp) {
             rc = 0;
include/hw/pci/pci_host.h +4 -0
index 3c72e26..ba5272f 100644
@@ -63,6 +63,10 @@ typedef struct PCIHostBridgeClass {
     const char *(*root_bus_path)(PCIHostState *, PCIBus *);
 } PCIHostBridgeClass;
 
+QLIST_HEAD(PCIHostQ, PCIHostState);
+extern struct PCIHostQ pci_host_bridges;
+#define HOST_BRIDGE_FOREACH(host) QLIST_FOREACH(host, &pci_host_bridges, next)
+
 /* common internal helpers for PCI/PCIe hosts, cut off overflows */
 void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr,
                                   uint32_t limit, uint32_t val, uint32_t len);
Signature · 1 line
2.1.0
#16 lore raw

RFC PATCH v2 12/17 hw/pci: introduce PCI Expander Bridge (PXB)

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

PXB is a "light-weight" host bridge whose purpose is to enable the main host bridge to support multiple PCI root buses.

As oposed to PCI-2-PCI bridge's secondary bus, PXB's bus is a primary bus and can be associated with a NUMA node (different from the main host bridge) allowing the guest OS to recognize the proximity of a pass-through device to other resources as RAM and CPUs.

The PXB is composed from:
- A primary PCI bus (can be associated with a NUMA node) Acts like a normal pci bus and from the functionality point of view is an "expansion" of the bus behind the
main host bridge.
- A pci-2-pci bridge behind the primary PCI bus where the actual devices will be attached.
- A host-bridge PCI device
Situated on the bus behind the main host bridge, allows the BIOS to configure the bus number and IO/mem resources. It does not have its own config/data register for configuration cycles, this being handled by the main host bridge.
- A host-bridge sysbus to comply with QEMU current design.

---
 hw/pci-bridge/Makefile.objs         |   1 +
 hw/pci-bridge/pci_expander_bridge.c | 173 ++++++++++++++++++++++++++++++++++++
 include/hw/pci/pci.h                |   1 +
 3 files changed, 175 insertions(+)
 create mode 100644 hw/pci-bridge/pci_expander_bridge.c
hw/pci-bridge/Makefile.objs +1 -0
index 968b369..632e442 100644
@@ -1,4 +1,5 @@
 common-obj-y += pci_bridge_dev.o
+common-obj-y += pci_expander_bridge.o
 common-obj-y += ioh3420.o xio3130_upstream.o xio3130_downstream.o
 common-obj-y += i82801b11.o
 # NewWorld PowerMac
hw/pci-bridge/pci_expander_bridge.c new file +173 -0

173 lines not on this page · raw message

include/hw/pci/pci.h +1 -0
index b7e24d4..e936528 100644
@@ -89,6 +89,7 @@
 #define PCI_DEVICE_ID_REDHAT_SERIAL4     0x0004
 #define PCI_DEVICE_ID_REDHAT_TEST        0x0005
 #define PCI_DEVICE_ID_REDHAT_SDHCI       0x0007
+#define PCI_DEVICE_ID_REDHAT_PXB         0x0008
 #define PCI_DEVICE_ID_REDHAT_QXL         0x0100
 
 #define FMT_PCIBUS                      PRIx64
Signature · 1 line
2.1.0
#17 lore raw

Re: [Qemu-devel] [PATCH RFC V2 12/17] hw/pci: introduce PCI Expander Bridge (PXB)

Quoted 227 lines · Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

PXB is a "light-weight" host bridge whose purpose is to enable the main host bridge to support multiple PCI root buses.

As oposed to PCI-2-PCI bridge's secondary bus, PXB's bus is a primary bus and can be associated with a NUMA node (different from the main host bridge) allowing the guest OS to recognize the proximity of a pass-through device to other resources as RAM and CPUs.

The PXB is composed from:
- A primary PCI bus (can be associated with a NUMA node) Acts like a normal pci bus and from the functionality point of view is an "expansion" of the bus behind the
main host bridge.
- A pci-2-pci bridge behind the primary PCI bus where the actual devices will be attached.
- A host-bridge PCI device
Situated on the bus behind the main host bridge, allows the BIOS to configure the bus number and IO/mem resources. It does not have its own config/data register for configuration cycles, this being handled by the main host bridge.
- A host-bridge sysbus to comply with QEMU current design.

---
 hw/pci-bridge/Makefile.objs         |   1 +
 hw/pci-bridge/pci_expander_bridge.c | 173 ++++++++++++++++++++++++++++++++++++
 include/hw/pci/pci.h                |   1 +
 3 files changed, 175 insertions(+)
 create mode 100644 hw/pci-bridge/pci_expander_bridge.c
hw/pci-bridge/Makefile.objs +1 -0
index 968b369..632e442 100644
@@ -1,4 +1,5 @@
 common-obj-y += pci_bridge_dev.o
+common-obj-y += pci_expander_bridge.o
 common-obj-y += ioh3420.o xio3130_upstream.o xio3130_downstream.o
 common-obj-y += i82801b11.o
 # NewWorld PowerMac
hw/pci-bridge/pci_expander_bridge.c new file +173 -0
new file mode 100644
index 0000000..941f3c8
@@ -0,0 +1,173 @@
+/*
+ * PCI Expander Bridge Device Emulation
+ *
+ * Copyright (C) 2014 Red Hat Inc
+ *
+ * Authors:
+ *   Marcel Apfelbaum <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
+#include "hw/pci/pci_host.h"
+#include "hw/pci/pci_bus.h"
+#include "qemu/range.h"
+#include "qemu/error-report.h"
+
+#define TYPE_PXB_BUS "pxb-bus"
+#define PXB_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_BUS)
+
+typedef struct PXBBus {
+    /*< private >*/
+    PCIBus parent_obj;
+    /*< public >*/
+
+    char bus_path[8];
+} PXBBus;
+
+#define TYPE_PXB_DEVICE "pxb-device"
+#define PXB_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_DEVICE)
+
+typedef struct PXBDev {
+    /*< private >*/
+    PCIDevice parent_obj;
+    /*< public >*/
+
+    uint8_t bus_nr;
+} PXBDev;
+
+#define TYPE_PXB_HOST "pxb-host"
+
+static int pxb_bus_num(PCIBus *bus)
+{
+    PXBDev *pxb = PXB_DEV(bus->parent_dev);
+
+    return pxb->bus_nr;
+}
+
+static bool pxb_is_root(PCIBus *bus)
+{
+    return true; /* by definition */
+}
+
+static void pxb_bus_class_init(ObjectClass *class, void *data)
+{
+    PCIBusClass *pbc = PCI_BUS_CLASS(class);
+
+    pbc->bus_num = pxb_bus_num;
+    pbc->is_root = pxb_is_root;
+}
+
+static const TypeInfo pxb_bus_info = {
+    .name          = TYPE_PXB_BUS,
+    .parent        = TYPE_PCI_BUS,
+    .instance_size = sizeof(PXBBus),
+    .class_init    = pxb_bus_class_init,
+};
+
+static const char *pxb_host_root_bus_path(PCIHostState *host_bridge,
+                                          PCIBus *rootbus)
+{
+    PXBBus *bus = PXB_BUS(rootbus);
+
+    snprintf(bus->bus_path, 8, "0000:%02x", pxb_bus_num(rootbus));
+    return bus->bus_path;
+}
+
+static void pxb_host_class_init(ObjectClass *class, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(class);
+    PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class);
+
+    dc->fw_name = "pci";
+    hc->root_bus_path = pxb_host_root_bus_path;
+}
+
+static const TypeInfo pxb_host_info = {
+    .name          = TYPE_PXB_HOST,
+    .parent        = TYPE_PCI_HOST_BRIDGE,
+    .class_init    = pxb_host_class_init,
+};
+
+static int pxb_dev_initfn(PCIDevice *dev)
+{
+    PXBDev *pxb = PXB_DEV(dev);
+    DeviceState *ds, *bds;
+    PCIHostState *phs;
+    PCIBus *bus;
+    const char *dev_name = NULL;
+
+    HOST_BRIDGE_FOREACH(phs) {
+        if (pxb->bus_nr == pci_bus_num(phs->bus)) {
+            error_report("Bus nr %d is already used by %s.",
+                         pxb->bus_nr, phs->bus->qbus.name);
+            return -EINVAL;
+        }
+    }
+
+    if (dev->qdev.id && *dev->qdev.id) {
+        dev_name = dev->qdev.id;
+    }
+
+    ds = qdev_create(NULL, TYPE_PXB_HOST);
+    bus = pci_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS);
+
+    bus->parent_dev = dev;
+    bus->address_space_mem = dev->bus->address_space_mem;
+    bus->address_space_io = dev->bus->address_space_io;
+    bus->map_irq = pci_swizzle_map_irq_fn;
+
+    bds = qdev_create(BUS(bus), "pci-bridge");
+    bds->id = dev_name;
+    qdev_prop_set_uint8(bds, "chassis_nr", pxb->bus_nr);
+
+    PCI_HOST_BRIDGE(ds)->bus = bus;
+
+    qdev_init_nofail(ds);
+    qdev_init_nofail(bds);
+
+    pci_word_test_and_set_mask(dev->config + PCI_STATUS,
+                               PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
+    pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_HOST);
+
+    return 0;
+}
+
+static Property pxb_dev_properties[] = {
+    /* Note: 0 is not a legal a PXB bus number. */
+    DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void pxb_dev_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
+
+    k->init = pxb_dev_initfn;
+    k->vendor_id = PCI_VENDOR_ID_REDHAT;
+    k->device_id = PCI_DEVICE_ID_REDHAT_PXB;
+    k->class_id = PCI_CLASS_BRIDGE_HOST;
+
+    dc->desc = "PCI Expander Bridge";
+    dc->props = pxb_dev_properties;
+}
+
+static const TypeInfo pxb_dev_info = {
+    .name          = TYPE_PXB_DEVICE,
+    .parent        = TYPE_PCI_DEVICE,
+    .instance_size = sizeof(PXBDev),
+    .class_init    = pxb_dev_class_init,
+};
+
+static void pxb_register_types(void)
+{
+    type_register_static(&pxb_bus_info);
+    type_register_static(&pxb_host_info);
+    type_register_static(&pxb_dev_info);
+}
+
+type_init(pxb_register_types)
include/hw/pci/pci.h +0 -0
index b7e24d4..e936528 100644

include/hw/pci/pci.h line 92

Quoted 4 lines
include/hw/pci/pci.h +1 -0
@@ -89,6 +89,7 @@
 #define PCI_DEVICE_ID_REDHAT_SERIAL4     0x0004
 #define PCI_DEVICE_ID_REDHAT_TEST        0x0005
 #define PCI_DEVICE_ID_REDHAT_SDHCI       0x0007
+#define PCI_DEVICE_ID_REDHAT_PXB         0x0008

This collides with the new PCI root device that should be in the tree by now :).

Alex

#18 lore raw

Re: [Qemu-devel] [PATCH RFC V2 12/17] hw/pci: introduce PCI Expander Bridge (PXB)

Quoted 229 lines · Alexander Graf
Quoted 226 lines · Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

PXB is a "light-weight" host bridge whose purpose is to enable the main host bridge to support multiple PCI root buses.

As oposed to PCI-2-PCI bridge's secondary bus, PXB's bus is a primary bus and can be associated with a NUMA node (different from the main host bridge) allowing the guest OS to recognize the proximity of a pass-through device to other resources as RAM and CPUs.

The PXB is composed from:
- A primary PCI bus (can be associated with a NUMA node) Acts like a normal pci bus and from the functionality point of view is an "expansion" of the bus behind the
main host bridge.
- A pci-2-pci bridge behind the primary PCI bus where the actual devices will be attached.
- A host-bridge PCI device
Situated on the bus behind the main host bridge, allows the BIOS to configure the bus number and IO/mem resources. It does not have its own config/data register for configuration cycles, this being handled by the main host bridge.
- A host-bridge sysbus to comply with QEMU current design.

---
  hw/pci-bridge/Makefile.objs         |   1 +
  hw/pci-bridge/pci_expander_bridge.c | 173 ++++++++++++++++++++++++++++++++++++
  include/hw/pci/pci.h                |   1 +
  3 files changed, 175 insertions(+)

create mode 100644 hw/pci-bridge/pci_expander_bridge.c

hw/pci-bridge/Makefile.objs +1 -0
index 968b369..632e442 100644
@@ -1,4 +1,5 @@
  common-obj-y += pci_bridge_dev.o
+common-obj-y += pci_expander_bridge.o
  common-obj-y += ioh3420.o xio3130_upstream.o xio3130_downstream.o
  common-obj-y += i82801b11.o
  # NewWorld PowerMac
hw/pci-bridge/pci_expander_bridge.c new file +173 -0
new file mode 100644
index 0000000..941f3c8
@@ -0,0 +1,173 @@
+/*
+ * PCI Expander Bridge Device Emulation
+ *
+ * Copyright (C) 2014 Red Hat Inc
+ *
+ * Authors:
+ *   Marcel Apfelbaum <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
+#include "hw/pci/pci_host.h"
+#include "hw/pci/pci_bus.h"
+#include "qemu/range.h"
+#include "qemu/error-report.h"
+
+#define TYPE_PXB_BUS "pxb-bus"
+#define PXB_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_BUS)
+
+typedef struct PXBBus {
+    /*< private >*/
+    PCIBus parent_obj;
+    /*< public >*/
+
+    char bus_path[8];
+} PXBBus;
+
+#define TYPE_PXB_DEVICE "pxb-device"
+#define PXB_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_DEVICE)
+
+typedef struct PXBDev {
+    /*< private >*/
+    PCIDevice parent_obj;
+    /*< public >*/
+
+    uint8_t bus_nr;
+} PXBDev;
+
+#define TYPE_PXB_HOST "pxb-host"
+
+static int pxb_bus_num(PCIBus *bus)
+{
+    PXBDev *pxb = PXB_DEV(bus->parent_dev);
+
+    return pxb->bus_nr;
+}
+
+static bool pxb_is_root(PCIBus *bus)
+{
+    return true; /* by definition */
+}
+
+static void pxb_bus_class_init(ObjectClass *class, void *data)
+{
+    PCIBusClass *pbc = PCI_BUS_CLASS(class);
+
+    pbc->bus_num = pxb_bus_num;
+    pbc->is_root = pxb_is_root;
+}
+
+static const TypeInfo pxb_bus_info = {
+    .name          = TYPE_PXB_BUS,
+    .parent        = TYPE_PCI_BUS,
+    .instance_size = sizeof(PXBBus),
+    .class_init    = pxb_bus_class_init,
+};
+
+static const char *pxb_host_root_bus_path(PCIHostState *host_bridge,
+                                          PCIBus *rootbus)
+{
+    PXBBus *bus = PXB_BUS(rootbus);
+
+    snprintf(bus->bus_path, 8, "0000:%02x", pxb_bus_num(rootbus));
+    return bus->bus_path;
+}
+
+static void pxb_host_class_init(ObjectClass *class, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(class);
+    PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class);
+
+    dc->fw_name = "pci";
+    hc->root_bus_path = pxb_host_root_bus_path;
+}
+
+static const TypeInfo pxb_host_info = {
+    .name          = TYPE_PXB_HOST,
+    .parent        = TYPE_PCI_HOST_BRIDGE,
+    .class_init    = pxb_host_class_init,
+};
+
+static int pxb_dev_initfn(PCIDevice *dev)
+{
+    PXBDev *pxb = PXB_DEV(dev);
+    DeviceState *ds, *bds;
+    PCIHostState *phs;
+    PCIBus *bus;
+    const char *dev_name = NULL;
+
+    HOST_BRIDGE_FOREACH(phs) {
+        if (pxb->bus_nr == pci_bus_num(phs->bus)) {
+            error_report("Bus nr %d is already used by %s.",
+                         pxb->bus_nr, phs->bus->qbus.name);
+            return -EINVAL;
+        }
+    }
+
+    if (dev->qdev.id && *dev->qdev.id) {
+        dev_name = dev->qdev.id;
+    }
+
+    ds = qdev_create(NULL, TYPE_PXB_HOST);
+    bus = pci_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS);
+
+    bus->parent_dev = dev;
+    bus->address_space_mem = dev->bus->address_space_mem;
+    bus->address_space_io = dev->bus->address_space_io;
+    bus->map_irq = pci_swizzle_map_irq_fn;
+
+    bds = qdev_create(BUS(bus), "pci-bridge");
+    bds->id = dev_name;
+    qdev_prop_set_uint8(bds, "chassis_nr", pxb->bus_nr);
+
+    PCI_HOST_BRIDGE(ds)->bus = bus;
+
+    qdev_init_nofail(ds);
+    qdev_init_nofail(bds);
+
+    pci_word_test_and_set_mask(dev->config + PCI_STATUS,
+                               PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
+    pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_HOST);
+
+    return 0;
+}
+
+static Property pxb_dev_properties[] = {
+    /* Note: 0 is not a legal a PXB bus number. */
+    DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void pxb_dev_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
+
+    k->init = pxb_dev_initfn;
+    k->vendor_id = PCI_VENDOR_ID_REDHAT;
+    k->device_id = PCI_DEVICE_ID_REDHAT_PXB;
+    k->class_id = PCI_CLASS_BRIDGE_HOST;
+
+    dc->desc = "PCI Expander Bridge";
+    dc->props = pxb_dev_properties;
+}
+
+static const TypeInfo pxb_dev_info = {
+    .name          = TYPE_PXB_DEVICE,
+    .parent        = TYPE_PCI_DEVICE,
+    .instance_size = sizeof(PXBDev),
+    .class_init    = pxb_dev_class_init,
+};
+
+static void pxb_register_types(void)
+{
+    type_register_static(&pxb_bus_info);
+    type_register_static(&pxb_host_info);
+    type_register_static(&pxb_dev_info);
+}
+
+type_init(pxb_register_types)
include/hw/pci/pci.h +0 -0
index b7e24d4..e936528 100644
Quoted 8 lines

include/hw/pci/pci.h line 92

Quoted 5 lines
include/hw/pci/pci.h +1 -0
@@ -89,6 +89,7 @@
  #define PCI_DEVICE_ID_REDHAT_SERIAL4     0x0004
  #define PCI_DEVICE_ID_REDHAT_TEST        0x0005
  #define PCI_DEVICE_ID_REDHAT_SDHCI       0x0007
+#define PCI_DEVICE_ID_REDHAT_PXB         0x0008

This collides with the new PCI root device that should be in the tree by now :).

I'll try to keep up! :)
I'll handle it once I rebase.

Thanks,
Marcel

Quoted 4 lines

Alex

#19 lore raw

RFC PATCH v2 13/17 hw/pci: inform bios if the system has more than one pci bridge

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

The bios looks for 'etc/extra-pci-roots' to decide if is going to scan further buses after bus 0 tree.

---
 hw/i386/pc.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/i386/pc.c +13 -0
index e07f1fa..9ef0917 100644
@@ -1073,9 +1073,22 @@ typedef struct PcGuestInfoState {
 static
 void pc_guest_info_machine_done(Notifier *notifier, void *data)
 {
+    PCIHostState *host;
+    int hosts = 0;
     PcGuestInfoState *guest_info_state = container_of(notifier,
                                                       PcGuestInfoState,
                                                       machine_done);
+    HOST_BRIDGE_FOREACH(host) {
+        hosts++;
+    }
+
+    if (hosts && guest_info_state->info.fw_cfg) {
+        uint64_t *val = g_malloc(sizeof(*val));
+        *val = cpu_to_le64(hosts - 1);
+        fw_cfg_add_file(guest_info_state->info.fw_cfg,
+                        "etc/extra-pci-roots", val, sizeof(*val));
+    }
+
     acpi_setup(&guest_info_state->info);
 }
 
Signature · 1 line
2.1.0
#20 lore raw

RFC PATCH v2 14/17 hw/pci: piix - suport multiple host bridges

In reply to Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

Instead of assuming it has only one bus, it
enumerates all the host bridges until it finds
the one with bus number corresponding with the
config register.

---
 hw/pci-host/piix.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)
hw/pci-host/piix.c +56 -1
index 970b9e9..7310b4c 100644
@@ -255,6 +255,61 @@ static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
     visit_type_uint64(v, &w64.end, name, errp);
 }
 
+static PCIBus *i440fx_find_primary_bus(int bus_num)
+{
+    PCIHostState *host;
+    PCIBus *bus = NULL;
+    int current = -1;
+
+    HOST_BRIDGE_FOREACH(host) {
+        int b = pci_bus_num(host->bus);
+        if (b <= bus_num && b > current) {
+            current = b;
+            bus = host->bus;
+        }
+    }
+
+    return bus;
+}
+
+static void i440fx_pcihost_data_write(void *opaque, hwaddr addr,
+                                      uint64_t val, unsigned len)
+{
+    uint32_t config_reg = PCI_HOST_BRIDGE(opaque)->config_reg;
+
+    if (config_reg & (1u << 31)) {
+        int bus_num = (config_reg >> 16) & 0xFF;
+        PCIBus *bus = i440fx_find_primary_bus(bus_num);
+
+        if (bus) {
+            pci_data_write(bus, config_reg | (addr & 3), val, len);
+        }
+    }
+}
+
+static uint64_t i440fx_pcihost_data_read(void *opaque,
+                                         hwaddr addr, unsigned len)
+{
+    uint32_t config_reg = PCI_HOST_BRIDGE(opaque)->config_reg;
+
+    if (config_reg & (1U << 31)) {
+        int bus_num = (config_reg >> 16) & 0xFF;
+        PCIBus *bus = i440fx_find_primary_bus(bus_num);
+
+        if (bus) {
+            return pci_data_read(bus, config_reg | (addr & 3), len);
+        }
+    }
+
+    return 0xffffffff;
+}
+
+const MemoryRegionOps i440fx_pcihost_data_le_ops = {
+    .read = i440fx_pcihost_data_read,
+    .write = i440fx_pcihost_data_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+};
+
 static void i440fx_pcihost_initfn(Object *obj)
 {
     PCIHostState *s = PCI_HOST_BRIDGE(obj);
@@ -262,7 +317,7 @@ static void i440fx_pcihost_initfn(Object *obj)
 
     memory_region_init_io(&s->conf_mem, obj, &pci_host_conf_le_ops, s,
                           "pci-conf-idx", 4);
-    memory_region_init_io(&s->data_mem, obj, &pci_host_data_le_ops, s,
+    memory_region_init_io(&s->data_mem, obj, &i440fx_pcihost_data_le_ops, s,
                           "pci-conf-data", 4);
 
     object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_START, "int",
Signature · 1 line
2.1.0
#21 lore raw

Re: [Qemu-devel] [PATCH RFC V2 14/17] hw/pci: piix - suport multiple host bridges

Quoted 8 lines · Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

Instead of assuming it has only one bus, it
enumerates all the host bridges until it finds
the one with bus number corresponding with the
config register.

How 440 specific is this? Wouldn't we need similar code for q35 and gpxe?

Alex

#22 lore raw

Re: [Qemu-devel] [PATCH RFC V2 14/17] hw/pci: piix - suport multiple host bridges

Quoted 12 lines · Alexander Graf
Quoted 8 lines · Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

Instead of assuming it has only one bus, it
enumerates all the host bridges until it finds
the one with bus number corresponding with the
config register.

Hi Alexander,
Thank you for the review.

Quoted 1 line

How 440 specific is this? Wouldn't we need similar code for q35 and gpxe?

For gpxe: I have no idea.
For Q35: PCI Express have native support for extra root bridges by having multiple Root Complexes(RC), but in this case each RC handles its separate configuration space.

It may be possible to use the same hack as in PC to expose a PCIe Root Port as a different host bridge and a primary bus behind it, but it is out of this series scope.

The series aims to address the limitation that PC machines support NUMA nodes for CPU/memory but not PCI.

Anyway, we can move the code when neeeded, but since it will take some and the implementation is not certain, for the moment is 440 specific.

Thanks,
Marcel

Quoted 4 lines

Alex

#23 lore raw

Re: [Qemu-devel] [PATCH RFC V2 14/17] hw/pci: piix - suport multiple host bridges

Quoted 27 lines · Marcel Apfelbaum
Quoted 12 lines · Alexander Graf
Quoted 8 lines · Marcel Apfelbaum

From: Marcel Apfelbaum <[email protected]>

Instead of assuming it has only one bus, it
enumerates all the host bridges until it finds
the one with bus number corresponding with the
config register.

Hi Alexander,
Thank you for the review.

Quoted 1 line

How 440 specific is this? Wouldn't we need similar code for q35 and gpxe?

For gpxe: I have no idea.
For Q35: PCI Express have native support for extra root bridges by having multiple Root Complexes(RC), but in this case each RC handles its separate configuration space.

It may be possible to use the same hack as in PC to expose a PCIe Root Port as a different host bridge and a primary bus behind it, but it is out of this series scope.

Quoted 6 lines

The series aims to address the limitation that PC machines support NUMA nodes for CPU/memory but not PCI.

Anyway, we can move the code when neeeded, but since it will take some and the implementation is not certain, for the moment is 440 specific.

Sorry, I didn't realize that this was legacy PCI specific :).

Alex

#24 lore raw

RFC PATCH v2 15/17 hw/pxb: add map_irq func

In reply to Marcel Apfelbaum

The bios does not index the pxb slot number when it computes the IRQ because it resides on bus 0 and not on the current bus.
However Qemu routes the irq through bus 0 and adds the pxb slot to the IRQ computation.

Synchronize between bios and Qemu by canceling pxb's effect.

---
 hw/pci-bridge/pci_expander_bridge.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
hw/pci-bridge/pci_expander_bridge.c +19 -1
index 941f3c8..1126841 100644
@@ -92,6 +92,24 @@ static const TypeInfo pxb_host_info = {
     .class_init    = pxb_host_class_init,
 };
 
+
+static int pxb_map_irq_fn(PCIDevice *pci_dev, int pin)
+{
+    PCIDevice *pxb = pci_dev->bus->parent_dev;
+
+    /*
+     * The bios does not index the pxb slot number when
+     * it computes the IRQ because it resides on bus 0
+     * and not on the current bus.
+     * However Qemu routes the irq through bus 0 and adds
+     * the pxb slot to the IRQ computation.
+     *
+     * Synchronize between bios and Qemu by canceling
+     * pxb's effect.
+     */
+    return pin - PCI_SLOT(pxb->devfn);
+}
+
 static int pxb_dev_initfn(PCIDevice *dev)
 {
     PXBDev *pxb = PXB_DEV(dev);
@@ -118,7 +136,7 @@ static int pxb_dev_initfn(PCIDevice *dev)
     bus->parent_dev = dev;
     bus->address_space_mem = dev->bus->address_space_mem;
     bus->address_space_io = dev->bus->address_space_io;
-    bus->map_irq = pci_swizzle_map_irq_fn;
+    bus->map_irq = pxb_map_irq_fn;
 
     bds = qdev_create(BUS(bus), "pci-bridge");
     bds->id = dev_name;
Signature · 1 line
2.1.0
#25 lore raw

RFC PATCH v2 16/17 hw/pci_bus: add support for NUMA nodes

In reply to Marcel Apfelbaum

PCI root buses can be attached to a specific NUMA node. PCI buses are not attached be default to a NUMA node.

---
 hw/pci/pci_bus.c         | 7 +++++++
 include/hw/pci/pci_bus.h | 6 ++++++
 include/sysemu/sysemu.h  | 1 +
 3 files changed, 14 insertions(+)
hw/pci/pci_bus.c +7 -0
index ed99208..15882a7 100644
@@ -13,6 +13,7 @@
 #include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_bridge.h"
 #include "monitor/monitor.h"
+#include "sysemu/sysemu.h"
 
 typedef struct {
     uint16_t class;
@@ -478,6 +479,11 @@ static int pcibus_num(PCIBus *bus)
     return bus->parent_dev->config[PCI_SECONDARY_BUS];
 }
 
+static uint16_t pcibus_numa_node(PCIBus *bus)
+{
+    return NUMA_NODE_UNASSIGNED;
+}
+
 static void pci_bus_class_init(ObjectClass *klass, void *data)
 {
     BusClass *k = BUS_CLASS(klass);
@@ -492,6 +498,7 @@ static void pci_bus_class_init(ObjectClass *klass, void *data)
 
     pbc->is_root = pcibus_is_root;
     pbc->bus_num = pcibus_num;
+    pbc->numa_node = pcibus_numa_node;
 }
 
 static const TypeInfo pci_bus_info = {
include/hw/pci/pci_bus.h +6 -0
index 553814e..75cd1fa 100644
@@ -25,6 +25,7 @@ typedef struct PCIBusClass {
 
     bool (*is_root)(PCIBus *bus);
     int (*bus_num)(PCIBus *bus);
+    uint16_t (*numa_node)(PCIBus *bus);
 } PCIBusClass;
 
 struct PCIBus {
@@ -60,6 +61,11 @@ static inline int pci_bus_num(PCIBus *bus)
     return PCI_BUS_GET_CLASS(bus)->bus_num(bus);
 }
 
+static inline int pci_bus_numa_node(PCIBus *bus)
+{
+    return PCI_BUS_GET_CLASS(bus)->numa_node(bus);
+}
+
 typedef struct PCIBridgeWindows PCIBridgeWindows;
 
 /*
include/sysemu/sysemu.h +1 -0
index 748d059..252b9ea 100644
@@ -139,6 +139,7 @@ extern const char *mem_path;
 extern int mem_prealloc;
 
 #define MAX_NODES 128
+#define NUMA_NODE_UNASSIGNED MAX_NODES
 
 /* The following shall be true for all CPUs:
  *   cpu->cpu_index < max_cpus <= MAX_CPUMASK_BITS
Signature · 1 line
2.1.0
#26 lore raw

RFC PATCH v2 17/17 hw/pxb: add numa_node parameter

In reply to Marcel Apfelbaum

The pxb can be attach to and existing numa node by specifying numa_node option that equals the desired numa nodeid.

---
 hw/i386/acpi-build.c                | 12 ++++++++++++
 hw/pci-bridge/pci_expander_bridge.c | 17 +++++++++++++++++
 2 files changed, 29 insertions(+)
hw/i386/acpi-build.c +12 -0
index cee150b..3da1333 100644
@@ -896,6 +896,7 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
 
         for (info = info_list; info; info = info->next) {
             PciInfo *bus_info = info->value;
+            PCIHostState *host;
 
             if (bus_info->bus == 0) {
                 continue;
@@ -912,6 +913,17 @@ build_ssdt(AcpiAml *table_aml, GArray *linker,
             aml_append(&dev, acpi_name_decl("_HID", acpi_string("PNP0A03")));
             aml_append(&dev,
                 acpi_name_decl("_BBN", acpi_int((uint8_t)bus_info->bus)));
+
+            HOST_BRIDGE_FOREACH(host) {
+                if (pci_bus_num(host->bus) == bus_info->bus) {
+                    int numa_node = pci_bus_numa_node(host->bus);
+                    if (numa_node != NUMA_NODE_UNASSIGNED) {
+                        aml_append(&dev,
+                            acpi_name_decl("_PXM", acpi_int(numa_node)));
+                    }
+                }
+            }
+
             aml_append(&dev, build_prt());
             crs = build_crs(pci, bus_info, &io_ranges, &mem_ranges);
             aml_append(&dev, acpi_name_decl("_CRS", crs));
hw/pci-bridge/pci_expander_bridge.c +17 -0
index 1126841..2e793e2 100644
@@ -16,6 +16,7 @@
 #include "hw/pci/pci_bus.h"
 #include "qemu/range.h"
 #include "qemu/error-report.h"
+#include "sysemu/sysemu.h"
 
 #define TYPE_PXB_BUS "pxb-bus"
 #define PXB_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_BUS)
@@ -37,6 +38,7 @@ typedef struct PXBDev {
     /*< public >*/
 
     uint8_t bus_nr;
+    uint16_t numa_node;
 } PXBDev;
 
 #define TYPE_PXB_HOST "pxb-host"
@@ -53,12 +55,20 @@ static bool pxb_is_root(PCIBus *bus)
     return true; /* by definition */
 }
 
+static uint16_t pxb_bus_numa_node(PCIBus *bus)
+{
+    PXBDev *pxb = PXB_DEV(bus->parent_dev);
+
+    return pxb->numa_node;
+}
+
 static void pxb_bus_class_init(ObjectClass *class, void *data)
 {
     PCIBusClass *pbc = PCI_BUS_CLASS(class);
 
     pbc->bus_num = pxb_bus_num;
     pbc->is_root = pxb_is_root;
+    pbc->numa_node = pxb_bus_numa_node;
 }
 
 static const TypeInfo pxb_bus_info = {
@@ -126,6 +136,12 @@ static int pxb_dev_initfn(PCIDevice *dev)
         }
     }
 
+    if (pxb->numa_node != NUMA_NODE_UNASSIGNED &&
+        pxb->numa_node >= nb_numa_nodes) {
+        error_report("Illegal numa node %d.", pxb->numa_node);
+        return -EINVAL;
+    }
+
     if (dev->qdev.id && *dev->qdev.id) {
         dev_name = dev->qdev.id;
     }
@@ -157,6 +173,7 @@ static int pxb_dev_initfn(PCIDevice *dev)
 static Property pxb_dev_properties[] = {
     /* Note: 0 is not a legal a PXB bus number. */
     DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0),
+    DEFINE_PROP_UINT16("numa_node", PXBDev, numa_node, NUMA_NODE_UNASSIGNED),
     DEFINE_PROP_END_OF_LIST(),
 };
 
Signature · 1 line
2.1.0