All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/5] hw/pci, hw/arm/virt: fixed PCI BAR placement
@ 2026-11-23 22:11 Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 1/5] hw/pci: introduce PCI fixed BAR properties Tushar Dave
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Tushar Dave @ 2026-11-23 22:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, peter.maydell, mst, marcel, devel, alwilliamson, jgg,
	skolothumtho, ardb, kraxel, clg, tdave

This patch series introduces a mechanism to specify Guest Physical Addresses
(GPAs) for PCI BARs, allowing explicit placement of guest MMIO BAR addresses
to match host physical addresses for assigned devices.

On some platforms, P2P DMA between devices in the same IOMMU group is required
for the desired data path and performance. The PCI fabric ACS is configured
to permit direct P2P without routing traffic through the host bridge.

To support this multi-device IOMMU group P2P scenario in virtualization, the
VM may need to preserve the host PCI BAR addresses in the guest.

A new fixed-bar-N property on a PCI endpoint device specifies the required
guest address for BAR N (N = 0 through 5):

    -device some-device,fixed-bar-0=<addr>[,fixed-bar-1=<addr>]...

Once any fixed-bar-N property is set on a device, every memory BAR on that
device must have a fixed address. Under a PCIe root port, fixed BAR placement
is all-or-nothing within that root port's subtree. If one endpoint below the
root port has a fixed BAR, every other endpoint with a memory BAR below that
same root port must also have all of its memory BARs fixed.

QEMU validates the configuration when the machine is done initializing. Each
address must be aligned to its BAR size, fall within the appropriate PCIe MMIO
aperture, and not overlap any other fixed BAR. QEMU aborts on any validation
failure. QEMU performs neither PCI enumeration nor BAR allocation. The
validated addresses reach firmware and the guest through the standard PCI
Enhanced Allocation (EA) capability: a device with fixed-bar-N properties gets
an EA capability with one entry for each fixed BAR, holding its address and
size.

EA is a conventional capability, so it has to fit in the first 256 bytes of the
configuration space. A device that has no room, or that already has an EA
capability, is rejected with an error.

The fixed-bar-N property and its validation are generic and apply to any PCI
device. A machine type opts in by setting the new pci_fixed_bar_supported
MachineClass flag. Machine types that leave it unset reject fixed-bar-N. This
series enables it on the virt machine only, by setting the flag and calling the
validation once the devices are realized.

On the firmware side nothing new is needed. Firmware that supports EA reads
the addresses from the capability, and EDK2 has had that support since
PR 13186 (merged 2026-10-27, see link below). The Linux kernel reads EA for
endpoint devices and reports each BAR as "from Enhanced Allocation".

Fixed BAR placement requires the guest to preserve the PCI resource
configuration established by firmware. This is requested through the PCI boot
configuration preservation mechanism using ACPI _DSM function 5. Fixed BAR
configuration is rejected when firmware is not loaded or ACPI is disabled.

Example 1: VFIO passthrough device with fixed BARs and the PCIe MMIO
aperture adjusted to accommodate the fixed addresses.

    -machine virt,...,highmem-mmio-base=4T,highmem-mmio-size=4T
    -device pcie-root-port,id=pcie.port1,bus=pcie.1,chassis=1,io-reserve=0
    -device x3130-upstream,id=upstream1,bus=pcie.port1
    -device xio3130-downstream,id=downstream1_1,bus=upstream1,chassis=1,slot=2
    -device vfio-pci-nohotplug,host=0018:06:00.0,bus=downstream1_1,id=dev0,\
            fixed-bar-0=0x48000000000,fixed-bar-2=0x50000000000,fixed-bar-4=0x58000000000

Example 2: VFIO and emulated devices under the same root port with fixed BARs
and the PCIe MMIO aperture adjusted using highmem-mmio-base.

    -machine virt,...,highmem-mmio-base=64T,highmem-mmio-size=64T
    -device pxb-pcie,id=pcie.1,bus_nr=1,bus=pcie.0
    -device pcie-root-port,id=pcie.port1,bus=pcie.1,chassis=1,io-reserve=0
    -device x3130-upstream,id=upstream1,bus=pcie.port1
    -device xio3130-downstream,id=downstream1_1,bus=upstream1,chassis=1,slot=1
    -device vfio-pci-nohotplug,host=0018:06:00.0,bus=downstream1_1,rombar=0,id=dev0,iommufd=iommufd0,\
            fixed-bar-0=0x6b4000000000,fixed-bar-2=0x6b8000000000,fixed-bar-4=0x6c0000000000
    -device xio3130-downstream,id=downstream1_2,bus=upstream1,chassis=1,slot=2
    -device vfio-pci,host=0012:03:00.1,bus=downstream1_2,id=nic1,iommufd=iommufd0,fixed-bar-0=0x6c8000000000
    -device xio3130-downstream,id=downstream1_3,bus=upstream1,chassis=1,slot=3
    -device e1000,netdev=net0,bus=downstream1_3,id=nic_emu,fixed-bar-0=0x20000000

Limitations:

* I/O BARs and the expansion ROM BAR are not covered by fixed-bar-N.
* SR-IOV VF BARs are not covered.

Testing:

The series was tested on the AArch64 virt machine with a range of PCIe
configurations, including emulated and VFIO passthrough devices, PCIe
switches, single and multiple fixed-BAR endpoints behind a shared root port,
devices attached directly to the host bridge, and endpoints behind nested
bridges. All rejection paths were also exercised, with the corresponding
error messages verified. These included unsupported machine types,
hot-plugged devices, non-existent BARs, I/O BARs, unaligned, overlapping, or
out-of-aperture BARs, incomplete BAR configurations, highmem-mmio=off,
missing firmware, and highmem-mmio-base values that cause address-space
overflow.

Changes since v2:

* Describe fixed BARs using an Enhanced Allocation (EA) capability in the
  device configuration space instead of the etc/fixed-bars fw_cfg blob. This
  replaces patch 3 and removes the blob, its format documentation, and the
  corresponding EDK2 change to IncompatiblePciDeviceSupportDxe, since EDK2
  already handles EA capabilities natively.
* Update patches 4 and 5 accordingly: patch 4 now performs validation instead
  of writing the fw_cfg blob, and patch 5 is adjusted to the updated
  object_class_property_add() API in current master.
* Patches 1 and 2 are unchanged.

A git branch with this series applied is available at:
https://github.com/tdavenvidia/upstream-qemu/tree/Upstream-fixed-bar-v3-EA-nov-20

EDK2 EA support, used by this series:
https://github.com/tianocore/edk2/pull/13186

v2:
https://lore.kernel.org/qemu-arm/20261102181241.3077981-1-tdave@nvidia.com/

v1:
https://lore.kernel.org/qemu-arm/20260916185554.917526-1-tdave@nvidia.com/

Tushar Dave (5):
  hw/pci: introduce PCI fixed BAR properties
  hw/pci: add validation for fixed BAR configuration
  hw/pci: describe fixed BARs with an Enhanced Allocation capability
  hw/arm/virt: validate fixed BARs and preserve the PCI configuration
  hw/arm/virt: add highmem-mmio-base property

 docs/system/arm/virt.rst        |   4 +
 hw/arm/virt.c                   |  82 ++++++-
 hw/pci/meson.build              |   1 +
 hw/pci/pci-fixed-bar-validate.c | 405 ++++++++++++++++++++++++++++++++
 hw/pci/pci-fixed-bar-validate.h |  22 ++
 hw/pci/pci.c                    | 219 ++++++++++++++++-
 hw/vfio/pci.c                   |  58 ++++-
 include/hw/core/boards.h        |   3 +
 include/hw/pci/pci-fixed-bar.h  |  28 +++
 include/hw/pci/pci.h            |   2 +
 include/hw/pci/pci_device.h     |   7 +
 11 files changed, 827 insertions(+), 4 deletions(-)
 create mode 100644 hw/pci/pci-fixed-bar-validate.c
 create mode 100644 hw/pci/pci-fixed-bar-validate.h
 create mode 100644 include/hw/pci/pci-fixed-bar.h

-- 
2.34.1



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 1/5] hw/pci: introduce PCI fixed BAR properties
  2026-11-23 22:11 [PATCH v3 0/5] hw/pci, hw/arm/virt: fixed PCI BAR placement Tushar Dave
@ 2026-11-23 22:11 ` Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 2/5] hw/pci: add validation for fixed BAR configuration Tushar Dave
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Tushar Dave @ 2026-11-23 22:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, peter.maydell, mst, marcel, devel, alwilliamson, jgg,
	skolothumtho, ardb, kraxel, clg, tdave

Add fixed-bar-N properties to PCIDevice to allow the guest address
of an individual PCI BAR to be explicitly specified. The properties
are available for PCI BARs 0 through 5 and accept QEMU size
values, for example fixed-bar-0=0x40000000 or fixed-bar-0=64G.

e.g.
-device some-device,fixed-bar-0=<addr>[,fixed-bar-1=<addr>]...

During realize, validate that each specified BAR exists and is a
memory BAR, and reject fixed BAR configuration for hot-plugged
devices.

The fixed BAR properties are only supported on machine types that
implement fixed BAR placement. Add the `pci_fixed_bar_supported`
MachineClass flag to advertise this support, and reject fixed BAR
properties on machine types that do not set it.

Signed-off-by: Tushar Dave <tdave@nvidia.com>
---
 hw/pci/pci.c                | 71 +++++++++++++++++++++++++++++++++++++
 include/hw/core/boards.h    |  3 ++
 include/hw/pci/pci_device.h |  7 ++++
 3 files changed, 81 insertions(+)

diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 57072590b1..8e6a3778ec 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -91,6 +91,18 @@ static const Property pci_props[] = {
     DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
     DEFINE_PROP_UINT32("romsize", PCIDevice, romsize, UINT32_MAX),
     DEFINE_PROP_INT32("rombar",  PCIDevice, rom_bar, -1),
+    DEFINE_PROP_SIZE("fixed-bar-0", PCIDevice, fixed_bar_addr[0],
+                     PCI_BAR_UNMAPPED),
+    DEFINE_PROP_SIZE("fixed-bar-1", PCIDevice, fixed_bar_addr[1],
+                     PCI_BAR_UNMAPPED),
+    DEFINE_PROP_SIZE("fixed-bar-2", PCIDevice, fixed_bar_addr[2],
+                     PCI_BAR_UNMAPPED),
+    DEFINE_PROP_SIZE("fixed-bar-3", PCIDevice, fixed_bar_addr[3],
+                     PCI_BAR_UNMAPPED),
+    DEFINE_PROP_SIZE("fixed-bar-4", PCIDevice, fixed_bar_addr[4],
+                     PCI_BAR_UNMAPPED),
+    DEFINE_PROP_SIZE("fixed-bar-5", PCIDevice, fixed_bar_addr[5],
+                     PCI_BAR_UNMAPPED),
     DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
                     QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
     DEFINE_PROP_BIT("x-pcie-lnksta-dllla", PCIDevice, cap_present,
@@ -230,6 +242,58 @@ static void pci_bus_unrealize(BusState *qbus)
     vmstate_unregister(NULL, &vmstate_pcibus, bus);
 }
 
+static bool pci_dev_has_fixed_bar(PCIDevice *pci_dev)
+{
+    int i;
+
+    for (i = 0; i < PCI_NUM_REGIONS - 1; i++) {
+        if (pci_dev->fixed_bar_addr[i] != PCI_BAR_UNMAPPED) {
+            return true;
+        }
+    }
+    return false;
+}
+
+static void pci_check_fixed_bars(PCIDevice *pci_dev, Error **errp)
+{
+    PCIIORegion *r;
+    int i;
+
+    if (!pci_dev_has_fixed_bar(pci_dev)) {
+        return;
+    }
+
+    if (!MACHINE_GET_CLASS(qdev_get_machine())->pci_fixed_bar_supported) {
+        error_setg(errp, "fixed BAR placement is not supported by this "
+                   "machine type");
+        return;
+    }
+
+    if (DEVICE(pci_dev)->hotplugged) {
+        error_setg(errp, "fixed BAR placement is not supported on "
+                   "hot-plugged devices");
+        return;
+    }
+
+    for (i = 0; i < PCI_NUM_REGIONS - 1; i++) {
+        if (pci_dev->fixed_bar_addr[i] == PCI_BAR_UNMAPPED) {
+            continue;
+        }
+
+        r = &pci_dev->io_regions[i];
+        if (!r->size) {
+            error_setg(errp, "fixed-bar-%d: bar%d does not exist on %s",
+                       i, i, pci_dev->name);
+            return;
+        }
+        if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
+            error_setg(errp, "fixed-bar-%d: bar%d on %s is an I/O BAR, "
+                       "not a memory BAR", i, i, pci_dev->name);
+            return;
+        }
+    }
+}
+
 static int pcibus_num(PCIBus *bus)
 {
     if (pci_bus_is_root(bus)) {
@@ -2408,6 +2472,13 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
         is_default_rom = true;
     }
 
+    pci_check_fixed_bars(pci_dev, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        pci_qdev_unrealize(DEVICE(pci_dev));
+        return;
+    }
+
     pci_add_option_rom(pci_dev, is_default_rom, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
diff --git a/include/hw/core/boards.h b/include/hw/core/boards.h
index d0e4ad9820..8092634eb3 100644
--- a/include/hw/core/boards.h
+++ b/include/hw/core/boards.h
@@ -242,6 +242,8 @@ typedef struct {
  *    is not needed.
  * @numa_mem_supported:
  *    true if '--numa node.mem' option is supported and false otherwise
+ * @pci_fixed_bar_supported:
+ *    true if the machine supports fixed PCI BAR placement and false otherwise
  * @hotplug_allowed:
  *    If the hook is provided, then it'll be called for each device
  *    hotplug to check whether the device hotplug is allowed.  Return
@@ -317,6 +319,7 @@ struct MachineClass {
     bool ignore_boot_device_suffixes;
     bool nvdimm_supported;
     bool numa_mem_supported;
+    bool pci_fixed_bar_supported;
     bool auto_enable_numa;
     bool cpu_cluster_has_numa_boundary;
     SMPCompatProps smp_props;
diff --git a/include/hw/pci/pci_device.h b/include/hw/pci/pci_device.h
index 5cac6e1688..db4a003ae3 100644
--- a/include/hw/pci/pci_device.h
+++ b/include/hw/pci/pci_device.h
@@ -187,6 +187,13 @@ struct PCIDevice {
     uint32_t max_bounce_buffer_size;
 
     char *sriov_pf;
+
+    /*
+     * fixed_bar_addr[N] holds the guest address configured via the
+     * fixed-bar-N property for memory BAR N. PCI_BAR_UNMAPPED means
+     * the BAR's address is not fixed.
+     */
+    uint64_t fixed_bar_addr[PCI_NUM_REGIONS - 1];
 };
 
 static inline int pci_intx(PCIDevice *pci_dev)
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/5] hw/pci: add validation for fixed BAR configuration
  2026-11-23 22:11 [PATCH v3 0/5] hw/pci, hw/arm/virt: fixed PCI BAR placement Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 1/5] hw/pci: introduce PCI fixed BAR properties Tushar Dave
@ 2026-11-23 22:11 ` Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 3/5] hw/pci: describe fixed BARs with an Enhanced Allocation capability Tushar Dave
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Tushar Dave @ 2026-11-23 22:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, peter.maydell, mst, marcel, devel, alwilliamson, jgg,
	skolothumtho, ardb, kraxel, clg, tdave

Validate the fixed BAR configuration specified through the
fixed-bar-N properties on PCI endpoint devices.

Configuring any fixed-bar-N property on an endpoint device requires
every memory BAR on that device to have a fixed address. Validate
that each assigned address:

- is aligned to its BAR size;
- is within the appropriate PCIe MMIO aperture; and
- does not overlap any other fixed BAR.

A 32-bit BAR must lie in the 32-bit aperture. A prefetchable 64-bit BAR
must lie in the 64-bit aperture so that all prefetchable BARs can be
accommodated in a single 64-bit prefetchable bridge window. A
non-prefetchable 64-bit BAR may lie in either aperture when attached
directly to the host bridge, but must lie below 4GiB when placed behind
a PCI bridge.

Under a PCIe topology with a root port, fixed BAR placement is
all-or-nothing within that root port's subtree. If any one endpoint
device below the root port has a fixed BAR, every other endpoint
device below that same root port with a memory BAR must have all of
its memory BARs fixed too.

Endpoint devices with no root port in their ancestry, such as those
attached directly to the host bridge, are not required to match
their sibling devices. One such device may use fixed-bar-N for all
of its memory BARs while another sibling device on the same host
bridge uses none at all. Per-device completeness still applies
regardless of ancestry. Once any fixed-bar-N property is set on a
device, every memory BAR on that device must be fixed.

Report a validation failure through the Error ** argument and leave
it to the caller to decide how to handle it.

Signed-off-by: Tushar Dave <tdave@nvidia.com>
---
 hw/pci/meson.build              |   1 +
 hw/pci/pci-fixed-bar-validate.c | 376 ++++++++++++++++++++++++++++++++
 hw/pci/pci-fixed-bar-validate.h |  22 ++
 3 files changed, 399 insertions(+)
 create mode 100644 hw/pci/pci-fixed-bar-validate.c
 create mode 100644 hw/pci/pci-fixed-bar-validate.h

diff --git a/hw/pci/meson.build b/hw/pci/meson.build
index 984c7327e5..150085c997 100644
--- a/hw/pci/meson.build
+++ b/hw/pci/meson.build
@@ -19,6 +19,7 @@ endif
 pci_ss.add(files('pcie.c', 'pcie_aer.c'))
 pci_ss.add(files('pcie_doe.c'))
 system_ss.add(when: 'CONFIG_PCI_EXPRESS', if_true: files('pcie_port.c', 'pcie_host.c'))
+system_ss.add(when: 'CONFIG_PCI_EXPRESS', if_true: files('pci-fixed-bar-validate.c'))
 system_ss.add_all(when: 'CONFIG_PCI', if_true: pci_ss)
 
 stub_ss.add(files('pci-stub.c'))
diff --git a/hw/pci/pci-fixed-bar-validate.c b/hw/pci/pci-fixed-bar-validate.c
new file mode 100644
index 0000000000..774f363572
--- /dev/null
+++ b/hw/pci/pci-fixed-bar-validate.c
@@ -0,0 +1,376 @@
+/*
+ * Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ * BAR address validation for fixed-BAR placement.
+ *
+ * Written by Tushar Dave
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/pci/pci.h"
+#include "hw/pci/pci_bridge.h"
+#include "hw/pci/pci_host.h"
+#include "hw/pci/pcie_port.h"
+#include "qapi/error.h"
+#include "qemu/range.h"
+#include "qemu/units.h"
+#include "pci-internal.h"
+#include "pci-fixed-bar-validate.h"
+
+/*
+ * Claimed BAR ranges — detect inter-device and inter-hierarchy overlaps
+ * across all fixed BARs system-wide.
+ */
+
+typedef struct {
+    uint64_t    start;
+    uint64_t    end;
+    const char *owner;  /* device name, for error messages */
+    int         bar;
+} FixedClaim;
+
+static void fixed_claims_init(FixedBarsInfo *info)
+{
+    info->claims = g_array_new(false, true, sizeof(FixedClaim));
+}
+
+static void fixed_claims_free(FixedBarsInfo *info)
+{
+    g_array_free(info->claims, true);
+    info->claims = NULL;
+}
+
+static bool fixed_claims_overlap(FixedBarsInfo *info,
+                                 uint64_t start, uint64_t end,
+                                 const char **owner_out, int *bar_out,
+                                 uint64_t *start_out, uint64_t *end_out)
+{
+    FixedClaim *c;
+    guint i;
+
+    for (i = 0; i < info->claims->len; i++) {
+        c = &g_array_index(info->claims, FixedClaim, i);
+        if (ranges_overlap(start, end - start + 1,
+                           c->start, c->end - c->start + 1)) {
+            *owner_out = c->owner;
+            *bar_out   = c->bar;
+            *start_out = c->start;
+            *end_out   = c->end;
+            return true;
+        }
+    }
+    return false;
+}
+
+static void fixed_claims_add(FixedBarsInfo *info,
+                             uint64_t start, uint64_t end,
+                             const char *owner, int bar)
+{
+    FixedClaim cl;
+
+    cl.start = start;
+    cl.end   = end;
+    cl.owner = owner;
+    cl.bar   = bar;
+    g_array_append_val(info->claims, cl);
+}
+
+/*
+ * Check that the BAR [start..end] fits in the window [base..limit].
+ * Both ranges are inclusive.
+ */
+static bool bar_in_window(uint64_t start, uint64_t end,
+                          uint64_t base, uint64_t limit)
+{
+    return start >= base && end <= limit;
+}
+
+static bool validate_bars(PCIDevice *pdev, FixedBarsInfo *info,
+                          bool is_fixed_subtree, Error **errp)
+{
+    const char  *devname = DEVICE(pdev)->id ? DEVICE(pdev)->id : pdev->name;
+    const char  *overlap_owner;
+    bool         has_fixed_bars = false;
+    bool         is_64bit;
+    uint64_t     addr, end, overlap_start, overlap_end;
+    PCIIORegion *r;
+    int          overlap_bar;
+    int          i;
+
+    for (i = 0; i < PCI_NUM_REGIONS - 1; i++) {
+        if (pdev->fixed_bar_addr[i] != PCI_BAR_UNMAPPED) {
+            has_fixed_bars = true;
+            break;
+        }
+    }
+
+    if (object_dynamic_cast(OBJECT(pdev), TYPE_PCI_BRIDGE)) {
+        if (has_fixed_bars) {
+            error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] is a PCI bridge, "
+                       "fixed-bar-N is only supported on endpoint devices",
+                       devname, pci_dev_bus_num(pdev),
+                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+            return false;
+        }
+        return true;
+    }
+
+    if (is_fixed_subtree && !has_fixed_bars) {
+        error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] under fixed-bar root "
+                   "port has memory BARs but no fixed-bar-N= specified",
+                   devname, pci_dev_bus_num(pdev),
+                   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+        return false;
+    }
+
+    if (!has_fixed_bars) {
+        return true;
+    }
+
+    /* Completeness: every memory BAR must have an address. */
+    for (i = 0; i < PCI_NUM_REGIONS - 1; i++) {
+        r = &pdev->io_regions[i];
+        if (!r->size || (r->type & PCI_BASE_ADDRESS_SPACE_IO)) {
+            continue;
+        }
+        if (pdev->fixed_bar_addr[i] == PCI_BAR_UNMAPPED) {
+            error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] BAR%d "
+                       "missing a fixed-bar-N= value",
+                       devname, pci_dev_bus_num(pdev),
+                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), i);
+            return false;
+        }
+    }
+
+    /* Per-BAR checks: alignment, MMIO window, overlap. */
+    for (i = 0; i < PCI_NUM_REGIONS - 1; i++) {
+        r = &pdev->io_regions[i];
+        if (!r->size || (r->type & PCI_BASE_ADDRESS_SPACE_IO)) {
+            continue;
+        }
+
+        is_64bit = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
+        addr     = (uint64_t)pdev->fixed_bar_addr[i];
+
+        if (r->size - 1 > UINT64_MAX - addr) {
+            error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] BAR%d "
+                       "addr=0x%"PRIx64" + size=0x%"PRIx64" overflows",
+                       devname, pci_dev_bus_num(pdev),
+                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
+                       i, addr, r->size);
+            return false;
+        }
+        end = addr + r->size - 1;
+
+        if (is_64bit && !(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH) &&
+            !pci_bus_is_root(pci_get_bus(pdev)) && end >= 4 * GiB) {
+            error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] BAR%d is a "
+                       "non-prefetchable 64-bit BAR behind a PCI bridge "
+                       "and cannot be placed above 4GB",
+                       devname, pci_dev_bus_num(pdev),
+                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), i);
+            return false;
+        }
+
+        if (addr & (r->size - 1)) {
+            error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] BAR%d "
+                       "addr=0x%"PRIx64" not aligned to size=0x%"PRIx64,
+                       devname, pci_dev_bus_num(pdev),
+                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
+                       i, addr, r->size);
+            return false;
+        }
+
+        /*
+         * A 32-bit BAR must fit in the 32-bit window; a 64-bit BAR in either,
+         * except a prefetchable one, which must be above 4GiB.
+         */
+        if (is_64bit && (r->type & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
+            if (!bar_in_window(addr, end,
+                               info->mmio64_base, info->mmio64_limit)) {
+                error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] BAR%d "
+                           "[0x%"PRIx64"..0x%"PRIx64"] outside the 64-bit "
+                           "window [0x%"PRIx64"..0x%"PRIx64"]",
+                           devname, pci_dev_bus_num(pdev),
+                           PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
+                           i, addr, end,
+                           info->mmio64_base, info->mmio64_limit);
+                return false;
+            }
+        } else if (is_64bit) {
+            if (!bar_in_window(addr, end,
+                               info->mmio32_base, info->mmio32_limit) &&
+                !bar_in_window(addr, end,
+                               info->mmio64_base, info->mmio64_limit)) {
+                error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] BAR%d "
+                           "[0x%"PRIx64"..0x%"PRIx64"] outside both the "
+                           "32-bit window [0x%"PRIx64"..0x%"PRIx64"] and "
+                           "the 64-bit window [0x%"PRIx64"..0x%"PRIx64"]",
+                           devname, pci_dev_bus_num(pdev),
+                           PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
+                           i, addr, end,
+                           info->mmio32_base, info->mmio32_limit,
+                           info->mmio64_base, info->mmio64_limit);
+                return false;
+            }
+        } else if (!bar_in_window(addr, end,
+                                  info->mmio32_base, info->mmio32_limit)) {
+            error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] BAR%d "
+                       "[0x%"PRIx64"..0x%"PRIx64"] outside the 32-bit "
+                       "window [0x%"PRIx64"..0x%"PRIx64"]",
+                       devname, pci_dev_bus_num(pdev),
+                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
+                       i, addr, end,
+                       info->mmio32_base, info->mmio32_limit);
+            return false;
+        }
+
+        if (fixed_claims_overlap(info, addr, end, &overlap_owner,
+                                 &overlap_bar, &overlap_start, &overlap_end)) {
+            error_setg(errp, "fixed-bar: %s [%02x:%02x.%x] BAR%d "
+                       "[0x%"PRIx64"..0x%"PRIx64"] overlaps %s BAR%d "
+                       "[0x%"PRIx64"..0x%"PRIx64"]",
+                       devname, pci_dev_bus_num(pdev),
+                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
+                       i, addr, end, overlap_owner, overlap_bar,
+                       overlap_start, overlap_end);
+            return false;
+        }
+
+        fixed_claims_add(info, addr, end, devname, i);
+    }
+
+    info->any_fixed = true;
+    return true;
+}
+
+typedef struct {
+    FixedBarsInfo *info;
+    bool           is_fixed_subtree;
+    Error         **errp;
+} ScanState;
+
+static void scan_bus(PCIBus *bus, ScanState *state);
+
+static void mark_if_fixed(PCIBus *bus, PCIDevice *pdev, void *opaque)
+{
+    bool *found = opaque;
+    PCIBus *sec;
+    int i;
+
+    if (*found) {
+        return;
+    }
+    for (i = 0; i < PCI_NUM_REGIONS - 1; i++) {
+        if (pdev->fixed_bar_addr[i] != PCI_BAR_UNMAPPED) {
+            *found = true;
+            return;
+        }
+    }
+    if (object_dynamic_cast(OBJECT(pdev), TYPE_PCI_BRIDGE)) {
+        sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
+        if (sec) {
+            pci_for_each_device_under_bus(sec, mark_if_fixed, found);
+        }
+    }
+}
+
+/*
+ * True if any device in rp_bus's subtree (the bus behind a root port)
+ * has a fixed BAR.
+ */
+static bool root_port_subtree_has_fixed_bar(PCIBus *rp_bus)
+{
+    bool found = false;
+
+    pci_for_each_device_under_bus(rp_bus, mark_if_fixed, &found);
+    return found;
+}
+
+static void scan_bus_device(PCIBus *bus, PCIDevice *pdev, void *opaque)
+{
+    ScanState   *state = opaque;
+    bool         has_mem_bar = false;
+    PCIIORegion *r;
+    PCIBus      *sec;
+    int          i;
+
+    if (*state->errp) {
+        return;
+    }
+
+    for (i = 0; i < PCI_NUM_REGIONS - 1; i++) {
+        r = &pdev->io_regions[i];
+        if (r->size && !(r->type & PCI_BASE_ADDRESS_SPACE_IO)) {
+            has_mem_bar = true;
+            break;
+        }
+    }
+
+    if (has_mem_bar &&
+        !validate_bars(pdev, state->info, state->is_fixed_subtree,
+                       state->errp)) {
+        return;
+    }
+
+    if (!object_dynamic_cast(OBJECT(pdev), TYPE_PCI_BRIDGE)) {
+        return;
+    }
+    sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
+    if (!sec) {
+        return;
+    }
+
+    if (object_dynamic_cast(OBJECT(pdev), TYPE_PCIE_ROOT_PORT)) {
+        ScanState child = {
+            .info = state->info,
+            .is_fixed_subtree = root_port_subtree_has_fixed_bar(sec),
+            .errp = state->errp,
+        };
+        scan_bus(sec, &child);
+    } else {
+        scan_bus(sec, state);
+    }
+}
+
+static void scan_bus(PCIBus *bus, ScanState *state)
+{
+    pci_for_each_device_under_bus(bus, scan_bus_device, state);
+}
+
+static void scan_all_host_bridges(FixedBarsInfo *info, Error **errp)
+{
+    PCIHostState *hb;
+    ScanState state = {
+        .info = info,
+        .is_fixed_subtree = false,
+        .errp = errp,
+    };
+
+    fixed_claims_init(info);
+    QLIST_FOREACH(hb, &pci_host_bridges, next) {
+        if (hb->bus) {
+            scan_bus(hb->bus, &state);
+        }
+    }
+    fixed_claims_free(info);
+}
+
+/*
+ * fixed_bars_validate - scan all PCI devices, validate fixed BAR addresses.
+ *
+ * @info: carries MMIO window bounds for validation.
+ * @errp: set on a validation error.
+ *
+ * Returns true if every BAR validated, false with @errp set otherwise.
+ * Whether any fixed BAR device was found is left in info->any_fixed.
+ */
+bool fixed_bars_validate(FixedBarsInfo *info, Error **errp)
+{
+    ERRP_GUARD();
+
+    info->any_fixed = false;
+    scan_all_host_bridges(info, errp);
+    return !*errp;
+}
diff --git a/hw/pci/pci-fixed-bar-validate.h b/hw/pci/pci-fixed-bar-validate.h
new file mode 100644
index 0000000000..f4996f27d9
--- /dev/null
+++ b/hw/pci/pci-fixed-bar-validate.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ *
+ * Written by Tushar Dave
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_PCI_FIXED_BAR_VALIDATE_H
+#define HW_PCI_FIXED_BAR_VALIDATE_H
+
+typedef struct {
+    bool      any_fixed;
+    uint64_t  mmio32_base;
+    uint64_t  mmio32_limit;
+    uint64_t  mmio64_base;
+    uint64_t  mmio64_limit;
+    GArray    *claims;
+} FixedBarsInfo;
+
+bool fixed_bars_validate(FixedBarsInfo *info, Error **errp);
+
+#endif /* HW_PCI_FIXED_BAR_VALIDATE_H */
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 3/5] hw/pci: describe fixed BARs with an Enhanced Allocation capability
  2026-11-23 22:11 [PATCH v3 0/5] hw/pci, hw/arm/virt: fixed PCI BAR placement Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 1/5] hw/pci: introduce PCI fixed BAR properties Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 2/5] hw/pci: add validation for fixed BAR configuration Tushar Dave
@ 2026-11-23 22:11 ` Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 4/5] hw/arm/virt: validate fixed BARs and preserve the PCI configuration Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 5/5] hw/arm/virt: add highmem-mmio-base property Tushar Dave
  4 siblings, 0 replies; 6+ messages in thread
From: Tushar Dave @ 2026-11-23 22:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, peter.maydell, mst, marcel, devel, alwilliamson, jgg,
	skolothumtho, ardb, kraxel, clg, tdave

Give a device that has fixed-bar-N properties an Enhanced Allocation
(EA) capability with one entry for each fixed memory BAR. The entry holds
the guest address and the size of the BAR. Firmware and the guest read the
address from the entry instead of assigning one, the BAR register of such
a BAR is hardwired to zero, and the BAR decodes at its EA base, so the
guest cannot move it.

vfio-pci adds the capability itself, after it has built the capability
chain of the host device, since the capability has to fit into it. vfio
gives each host capability all of the space up to the next one, so the
last capability owns the rest of the standard configuration space. If it
is a vendor-specific capability, its length is known and the unused tail
is given back for the EA capability.

Add fixed_bars_check(), which validates the fixed-bar-N configuration of
all devices against the PCIe MMIO apertures of a machine, for the machine
to call once its devices are realized.

Signed-off-by: Tushar Dave <tdave@nvidia.com>
---
 hw/pci/pci-fixed-bar-validate.c |  29 ++++++
 hw/pci/pci.c                    | 150 +++++++++++++++++++++++++++++++-
 hw/vfio/pci.c                   |  58 +++++++++++-
 include/hw/pci/pci-fixed-bar.h  |  28 ++++++
 include/hw/pci/pci.h            |   2 +
 5 files changed, 263 insertions(+), 4 deletions(-)
 create mode 100644 include/hw/pci/pci-fixed-bar.h

diff --git a/hw/pci/pci-fixed-bar-validate.c b/hw/pci/pci-fixed-bar-validate.c
index 774f363572..c4026c7c1a 100644
--- a/hw/pci/pci-fixed-bar-validate.c
+++ b/hw/pci/pci-fixed-bar-validate.c
@@ -17,6 +17,7 @@
 #include "qemu/units.h"
 #include "pci-internal.h"
 #include "pci-fixed-bar-validate.h"
+#include "hw/pci/pci-fixed-bar.h"
 
 /*
  * Claimed BAR ranges — detect inter-device and inter-hierarchy overlaps
@@ -374,3 +375,31 @@ bool fixed_bars_validate(FixedBarsInfo *info, Error **errp)
     scan_all_host_bridges(info, errp);
     return !*errp;
 }
+
+bool fixed_bars_check(uint64_t mmio32_base, uint64_t mmio32_size,
+                      uint64_t mmio64_base, uint64_t mmio64_size,
+                      Error **errp)
+{
+    FixedBarsInfo info;
+
+    if (!mmio32_size) {
+        error_setg(errp, "fixed-bar: zero-size 32-bit PCIe MMIO aperture");
+        return false;
+    }
+    if (mmio32_size - 1 > UINT64_MAX - mmio32_base ||
+        (mmio64_size && mmio64_size - 1 > UINT64_MAX - mmio64_base)) {
+        error_setg(errp, "fixed-bar: PCIe MMIO aperture base+size overflows "
+                   "(32-bit base=0x%"PRIx64" size=0x%"PRIx64", "
+                   "64-bit base=0x%"PRIx64" size=0x%"PRIx64")",
+                   mmio32_base, mmio32_size, mmio64_base, mmio64_size);
+        return false;
+    }
+
+    info.mmio32_base = mmio32_base;
+    info.mmio32_limit = mmio32_base + mmio32_size - 1;
+    /* An absent 64-bit aperture is an empty window, no BAR fits in it. */
+    info.mmio64_base = mmio64_size ? mmio64_base : 1;
+    info.mmio64_limit = mmio64_size ? mmio64_base + mmio64_size - 1 : 0;
+
+    return fixed_bars_validate(&info, errp) && info.any_fixed;
+}
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 8e6a3778ec..e19dce2b53 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -242,7 +242,7 @@ static void pci_bus_unrealize(BusState *qbus)
     vmstate_unregister(NULL, &vmstate_pcibus, bus);
 }
 
-static bool pci_dev_has_fixed_bar(PCIDevice *pci_dev)
+bool pci_dev_has_fixed_bar(PCIDevice *pci_dev)
 {
     int i;
 
@@ -294,6 +294,133 @@ static void pci_check_fixed_bars(PCIDevice *pci_dev, Error **errp)
     }
 }
 
+static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size);
+
+/* Place @val in the bit field of an EA dword selected by @mask. */
+static uint32_t pci_ea_field(uint32_t mask, uint32_t val)
+{
+    return (val << ctz32(mask)) & mask;
+}
+
+/*
+ * The upper halves of Base and MaxOffset of a 64-bit EA entry follow
+ * MaxOffset, pci_regs.h has no definitions for them.
+ */
+#define PCI_EA_BASE_UPPER        (PCI_EA_MAX_OFFSET + sizeof(uint32_t))
+#define PCI_EA_MAX_OFFSET_UPPER  (PCI_EA_BASE_UPPER + sizeof(uint32_t))
+
+/*
+ * Dwords in an EA entry, which ends with MaxOffset, or with the upper half
+ * of MaxOffset for a 64-bit BAR.
+ */
+static unsigned pci_ea_entry_dwords(bool is64)
+{
+    return ((is64 ? PCI_EA_MAX_OFFSET_UPPER : PCI_EA_MAX_OFFSET) +
+            sizeof(uint32_t)) / sizeof(uint32_t);
+}
+
+/*
+ * Describe the fixed-bar-N addresses of a device with an Enhanced Allocation
+ * (EA) capability. Firmware and the guest read the address of each fixed BAR
+ * from the capability instead of assigning one, and the BAR register of such
+ * a BAR is hardwired to zero.
+ *
+ * Returns the offset of the capability, 0 if the device has no fixed BAR to
+ * describe, or -1 on error.
+ */
+int pci_add_fixed_bar_ea_cap(PCIDevice *d, uint8_t *cap_size, Error **errp)
+{
+    int i, n = 0, pos;
+    unsigned total = PCI_EA_FIRST_ENT;
+    uint8_t size, *p;
+
+    /*
+     * A BAR that does not exist or is not a memory BAR is reported by
+     * pci_check_fixed_bars(), skip it here.
+     */
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        if (d->fixed_bar_addr[i] != PCI_BAR_UNMAPPED &&
+            d->io_regions[i].size &&
+            !(d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO)) {
+            n++;
+            total += pci_ea_entry_dwords(d->io_regions[i].type &
+                                         PCI_BASE_ADDRESS_MEM_TYPE_64) *
+                     sizeof(uint32_t);
+        }
+    }
+    if (!n) {
+        return 0;
+    }
+
+    size = total;
+    pos = pci_find_space(d, size);
+    if (!pos) {
+        error_setg(errp, "fixed-bar: no room in the configuration space of "
+                   "%s for an Enhanced Allocation capability", d->name);
+        return -1;
+    }
+    pos = pci_add_capability(d, PCI_CAP_ID_EA, pos, size, errp);
+    if (pos < 0) {
+        return -1;
+    }
+
+    pci_set_word(d->config + pos + PCI_EA_NUM_ENT, n);
+    p = d->config + pos + PCI_EA_FIRST_ENT;
+
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        PCIIORegion *r = &d->io_regions[i];
+        uint64_t base, max;
+        uint32_t hdr;
+        bool is64 = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64;
+
+        if (d->fixed_bar_addr[i] == PCI_BAR_UNMAPPED || !r->size ||
+            (r->type & PCI_BASE_ADDRESS_SPACE_IO)) {
+            continue;
+        }
+
+        base = d->fixed_bar_addr[i];
+        max = r->size - 1;
+        /*
+         * Entry header: BEI = BAR number, primary properties = memory
+         * (prefetchable or not), secondary properties unused, enabled.
+         * A 64-bit BAR has an entry of four dwords after the header, Base and
+         * MaxOffset each with an upper dword. A 32-bit BAR has two dwords.
+         * Bit 1 of the low dword of Base and MaxOffset marks a 64-bit field,
+         * so it is set only for a 64-bit BAR. The entry size field is the
+         * number of dwords after the header.
+         */
+        hdr = pci_ea_field(PCI_EA_ES, pci_ea_entry_dwords(is64) - 1) |
+              pci_ea_field(PCI_EA_BEI, PCI_EA_BEI_BAR0 + i) |
+              pci_ea_field(PCI_EA_PP, r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ?
+                           PCI_EA_P_MEM_PREFETCH : PCI_EA_P_MEM) |
+              pci_ea_field(PCI_EA_SP, PCI_EA_P_UNAVAILABLE) |
+              PCI_EA_ENABLE;
+        pci_set_long(p, hdr);
+        pci_set_long(p + PCI_EA_BASE,
+                     (base & PCI_EA_FIELD_MASK) | (is64 ? PCI_EA_IS_64 : 0));
+        pci_set_long(p + PCI_EA_MAX_OFFSET,
+                     (max & PCI_EA_FIELD_MASK) | (is64 ? PCI_EA_IS_64 : 0));
+        if (is64) {
+            pci_set_long(p + PCI_EA_BASE_UPPER, base >> 32);
+            pci_set_long(p + PCI_EA_MAX_OFFSET_UPPER, max >> 32);
+        }
+        p += pci_ea_entry_dwords(is64) * sizeof(uint32_t);
+
+        /* The BAR register is hardwired to zero for an EA BAR. */
+        pci_set_long(d->config + pci_bar(d, i), 0);
+        pci_set_long(d->wmask + pci_bar(d, i), 0);
+        if (is64) {
+            pci_set_long(d->config + pci_bar(d, i + 1), 0);
+            pci_set_long(d->wmask + pci_bar(d, i + 1), 0);
+        }
+    }
+
+    if (cap_size) {
+        *cap_size = size;
+    }
+    return pos;
+}
+
 static int pcibus_num(PCIBus *bus)
 {
     if (pci_bus_is_root(bus)) {
@@ -616,6 +743,7 @@ static uint8_t pci_pm_update(PCIDevice *d, uint32_t addr, int l, uint8_t old)
 
 static void pci_reset_regions(PCIDevice *dev)
 {
+    uint8_t type;
     int r;
     if (pci_is_vf(dev)) {
         return;
@@ -627,11 +755,15 @@ static void pci_reset_regions(PCIDevice *dev)
             continue;
         }
 
+        /* A fixed BAR is described by EA, all bits of its BAR are 0. */
+        type = (r < PCI_ROM_SLOT &&
+                dev->fixed_bar_addr[r] != PCI_BAR_UNMAPPED) ? 0 : region->type;
+
         if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
             region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
-            pci_set_quad(dev->config + pci_bar(dev, r), region->type);
+            pci_set_quad(dev->config + pci_bar(dev, r), type);
         } else {
-            pci_set_long(dev->config + pci_bar(dev, r), region->type);
+            pci_set_long(dev->config + pci_bar(dev, r), type);
         }
     }
 }
@@ -1776,6 +1908,10 @@ pcibus_t pci_bar_address(PCIDevice *d,
     if (!(cmd & PCI_COMMAND_MEMORY)) {
         return PCI_BAR_UNMAPPED;
     }
+    if (reg < PCI_ROM_SLOT && d->fixed_bar_addr[reg] != PCI_BAR_UNMAPPED) {
+        /* A fixed BAR decodes at its EA base, not at the BAR register. */
+        return d->fixed_bar_addr[reg];
+    }
     new_addr = pci_config_get_bar_addr(d, reg, type, size);
     /* the ROM slot has a specific enable bit */
     if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
@@ -2479,6 +2615,14 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
         return;
     }
 
+    /* vfio-pci adds the EA capability itself, to fit its capability chain */
+    if (!pci_find_capability(pci_dev, PCI_CAP_ID_EA) &&
+        pci_add_fixed_bar_ea_cap(pci_dev, NULL, &local_err) < 0) {
+        error_propagate(errp, local_err);
+        pci_qdev_unrealize(DEVICE(pci_dev));
+        return;
+    }
+
     pci_add_option_rom(pci_dev, is_default_rom, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 9607866077..5497fb229e 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -2759,6 +2759,58 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev, bool ats_needed)
     g_free(config);
 }
 
+static bool vfio_add_fixed_bar_ea_cap(VFIOPCIDevice *vdev, Error **errp)
+{
+    PCIDevice *pdev = PCI_DEVICE(vdev);
+    uint8_t size = 0, last = 0, tmp;
+    int pos;
+
+    if (!pci_dev_has_fixed_bar(pdev)) {
+        return true;
+    }
+
+    /* The device already has an EA capability, we cannot add ours. */
+    if (pci_find_capability(pdev, PCI_CAP_ID_EA)) {
+        error_setg(errp, "%s already has an Enhanced Allocation capability, "
+                   "fixed-bar-N cannot be used", vdev->vbasedev.name);
+        return false;
+    }
+
+    /*
+     * vfio gives each capability all the space up to the next one, so the
+     * last capability owns the rest of the standard config space. If it is
+     * a vendor-specific capability its real length is known, give the
+     * unused tail back.
+     */
+    for (tmp = pdev->config[PCI_CAPABILITY_LIST]; tmp;
+         tmp = pdev->config[tmp + PCI_CAP_LIST_NEXT]) {
+        if (tmp > last) {
+            last = tmp;
+        }
+    }
+    if (last && pdev->config[last + PCI_CAP_LIST_ID] == PCI_CAP_ID_VNDR) {
+        uint8_t len = pdev->config[last + PCI_CAP_FLAGS];
+
+        /* The length comes from the host device, use it only if sane. */
+        if (len > PCI_CAP_FLAGS && last + len <= PCI_CONFIG_SPACE_SIZE) {
+            memset(pdev->used + last + len, 0,
+                   PCI_CONFIG_SPACE_SIZE - last - len);
+        }
+    }
+
+    pos = pci_add_fixed_bar_ea_cap(pdev, &size, errp);
+    if (pos < 0) {
+        return false;
+    }
+    if (pos) {
+        /* Emulate the whole capability, including its next pointer. */
+        memset(vdev->emulated_config_bits + pos, 0xff, size);
+        /* The new capability is the head of the chain, so is the list. */
+        vdev->emulated_config_bits[PCI_CAPABILITY_LIST] = 0xff;
+    }
+    return true;
+}
+
 bool vfio_pci_add_capabilities(VFIOPCIDevice *vdev, Error **errp)
 {
     PCIDevice *pdev = PCI_DEVICE(vdev);
@@ -2766,13 +2818,17 @@ bool vfio_pci_add_capabilities(VFIOPCIDevice *vdev, Error **errp)
 
     if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST) ||
         !pdev->config[PCI_CAPABILITY_LIST]) {
-        return true; /* Nothing to add */
+        return vfio_add_fixed_bar_ea_cap(vdev, errp);
     }
 
     if (!vfio_add_std_cap(vdev, pdev->config[PCI_CAPABILITY_LIST], errp)) {
         return false;
     }
 
+    if (!vfio_add_fixed_bar_ea_cap(vdev, errp)) {
+        return false;
+    }
+
     if (!vfio_pci_ats_requested_and_supported(vdev, &ats_needed, errp)) {
         return false;
     }
diff --git a/include/hw/pci/pci-fixed-bar.h b/include/hw/pci/pci-fixed-bar.h
new file mode 100644
index 0000000000..55441e329f
--- /dev/null
+++ b/include/hw/pci/pci-fixed-bar.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ * Check the fixed BAR configuration of a machine.
+ *
+ * Written by Tushar Dave
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_PCI_FIXED_BAR_H
+#define HW_PCI_FIXED_BAR_H
+
+#include "qapi/error.h"
+
+/**
+ * fixed_bars_check: validate the fixed-bar-N configuration of all devices
+ * @mmio32_base, @mmio32_size: the 32-bit PCIe MMIO aperture
+ * @mmio64_base, @mmio64_size: the 64-bit PCIe MMIO aperture, size 0 if absent
+ * @errp: set on a validation failure
+ *
+ * Returns true if at least one device has a fixed BAR and the configuration
+ * is valid, false if no device has one or on failure (@errp is set).
+ */
+bool fixed_bars_check(uint64_t mmio32_base, uint64_t mmio32_size,
+                      uint64_t mmio64_base, uint64_t mmio64_size,
+                      Error **errp);
+
+#endif /* HW_PCI_FIXED_BAR_H */
diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
index 359331102f..cf05f78abc 100644
--- a/include/hw/pci/pci.h
+++ b/include/hw/pci/pci.h
@@ -259,6 +259,8 @@ void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
 void pci_unregister_vga(PCIDevice *pci_dev);
 pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num);
 
+bool pci_dev_has_fixed_bar(PCIDevice *pci_dev);
+int pci_add_fixed_bar_ea_cap(PCIDevice *d, uint8_t *cap_size, Error **errp);
 int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
                        uint8_t offset, uint8_t size,
                        Error **errp);
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 4/5] hw/arm/virt: validate fixed BARs and preserve the PCI configuration
  2026-11-23 22:11 [PATCH v3 0/5] hw/pci, hw/arm/virt: fixed PCI BAR placement Tushar Dave
                   ` (2 preceding siblings ...)
  2026-11-23 22:11 ` [PATCH v3 3/5] hw/pci: describe fixed BARs with an Enhanced Allocation capability Tushar Dave
@ 2026-11-23 22:11 ` Tushar Dave
  2026-11-23 22:11 ` [PATCH v3 5/5] hw/arm/virt: add highmem-mmio-base property Tushar Dave
  4 siblings, 0 replies; 6+ messages in thread
From: Tushar Dave @ 2026-11-23 22:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, peter.maydell, mst, marcel, devel, alwilliamson, jgg,
	skolothumtho, ardb, kraxel, clg, tdave

Validate the fixed BAR configuration with fixed_bars_check() and set
pci_fixed_bar_supported to allow the fixed-bar-N properties on the virt
machine.

Fixed BAR placement requires the guest to preserve the PCI resource
configuration established by firmware, preventing the fixed BAR
addresses from being reassigned during guest boot. ACPI provides
this through the PCI boot configuration preservation mechanism
(_DSM function 5).

Reject fixed BAR configuration if firmware is not loaded or ACPI
is disabled, since PCI boot configuration preservation cannot
otherwise be guaranteed.

Signed-off-by: Tushar Dave <tdave@nvidia.com>
---
 hw/arm/virt.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 0b11f7e328..dc487f9ea0 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -94,6 +94,7 @@
 #include "hw/cxl/cxl_host.h"
 #include "qemu/guest-random.h"
 #include "hw/watchdog/sbsa_gwdt.h"
+#include "hw/pci/pci-fixed-bar.h"
 
 static GlobalProperty arm_virt_compat_defaults[] = {
     { TYPE_VIRTIO_IOMMU_PCI, "aw-bits", "48" },
@@ -2451,6 +2452,22 @@ void virt_machine_done(Notifier *notifier, void *data)
                                        vms->memmap[VIRT_PLATFORM_BUS].size,
                                        vms->irqmap[VIRT_PLATFORM_BUS]);
     }
+    if (fixed_bars_check(vms->memmap[VIRT_PCIE_MMIO].base,
+                         vms->memmap[VIRT_PCIE_MMIO].size,
+                         vms->memmap[VIRT_HIGH_PCIE_MMIO].base,
+                         vms->highmem_mmio ?
+                         vms->memmap[VIRT_HIGH_PCIE_MMIO].size : 0,
+                         &error_fatal)) {
+        if (!(vms->bootinfo.firmware_loaded && virt_is_acpi_enabled(vms))) {
+            error_report("fixed-bar: fixed BAR placement requires firmware "
+                         "and ACPI to preserve the PCI boot configuration, "
+                         "otherwise these BAR assignments could be "
+                         "reassigned later with no protection");
+            exit(1);
+        }
+        vms->pci_preserve_config = true;
+    }
+
     if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms, cpu) < 0) {
         exit(1);
     }
@@ -4203,6 +4220,7 @@ static void virt_machine_class_init(ObjectClass *oc, const void *data)
     hc->unplug_request = virt_machine_device_unplug_request_cb;
     hc->unplug = virt_machine_device_unplug_cb;
     mc->nvdimm_supported = true;
+    mc->pci_fixed_bar_supported = true;
     mc->smp_props.clusters_supported = true;
 
     /* Supported caches */
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 5/5] hw/arm/virt: add highmem-mmio-base property
  2026-11-23 22:11 [PATCH v3 0/5] hw/pci, hw/arm/virt: fixed PCI BAR placement Tushar Dave
                   ` (3 preceding siblings ...)
  2026-11-23 22:11 ` [PATCH v3 4/5] hw/arm/virt: validate fixed BARs and preserve the PCI configuration Tushar Dave
@ 2026-11-23 22:11 ` Tushar Dave
  4 siblings, 0 replies; 6+ messages in thread
From: Tushar Dave @ 2026-11-23 22:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, peter.maydell, mst, marcel, devel, alwilliamson, jgg,
	skolothumtho, ardb, kraxel, clg, tdave

Add a highmem-mmio-base property to override the base of the virt
machine's high PCIe MMIO window, alongside the existing
highmem-mmio-size:

  -machine virt,highmem-mmio-base=<addr>,highmem-mmio-size=<size>

Some PCI devices require MMIO resources to be placed within a
specific address range that cannot be satisfied by the default
placement. The configured base is validated to be non-zero, aligned
to highmem-mmio-size, non-overlapping with the existing high memory
layout, and within the configured physical address space.

Document the property in docs/system/arm/virt.rst.

Signed-off-by: Tushar Dave <tdave@nvidia.com>
---
 docs/system/arm/virt.rst |  4 +++
 hw/arm/virt.c            | 64 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/docs/system/arm/virt.rst b/docs/system/arm/virt.rst
index 5f6dd17978..7ae51bee13 100644
--- a/docs/system/arm/virt.rst
+++ b/docs/system/arm/virt.rst
@@ -152,6 +152,10 @@ highmem-mmio-size
   Set the high memory region size for PCI MMIO. Must be a power of 2 and
   greater than or equal to the default size (512G).
 
+highmem-mmio-base
+  Set the high memory region base for PCI MMIO. Must be non-zero and
+  aligned to ``highmem-mmio-size``.
+
 gic-version
   Specify the version of the Generic Interrupt Controller (GIC) to provide.
   Valid values are:
diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index dc487f9ea0..ab34d1ab05 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -2522,14 +2522,40 @@ static void virt_set_high_memmap(VirtMachineState *vms,
                                  hwaddr base, int pa_bits)
 {
     hwaddr region_base, region_size;
+    hwaddr pa_limit = BIT_ULL(pa_bits);
     bool *region_enabled, fits;
     int i;
 
     for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
         region_enabled = virt_get_high_memmap_enabled(vms, i);
-        region_base = ROUND_UP(base, extended_memmap[i].size);
         region_size = extended_memmap[i].size;
 
+        if (i == VIRT_HIGH_PCIE_MMIO && extended_memmap[i].base) {
+            region_base = extended_memmap[i].base;
+
+            if (region_base < base) {
+                error_report("highmem-mmio-base 0x%"PRIx64" overlaps prior "
+                             "high memory layout (must be >= 0x%"PRIx64")",
+                             region_base, base);
+                exit(1);
+            }
+            if (region_base % region_size != 0) {
+                error_report("highmem-mmio-base 0x%"PRIx64" must be "
+                             "aligned to highmem-mmio-size 0x%"PRIx64,
+                             region_base, region_size);
+                exit(1);
+            }
+            if (region_base >= pa_limit ||
+                region_size > pa_limit - region_base) {
+                error_report("highmem-mmio-base 0x%"PRIx64" + "
+                             "highmem-mmio-size 0x%"PRIx64" exceeds %d-bit "
+                             "PA space", region_base, region_size, pa_bits);
+                exit(1);
+            }
+        } else {
+            region_base = ROUND_UP(base, region_size);
+        }
+
         vms->memmap[i].base = region_base;
         vms->memmap[i].size = region_size;
 
@@ -3430,6 +3456,33 @@ static void virt_set_highmem_mmio_size(Object *obj, Visitor *v,
     extended_memmap[VIRT_HIGH_PCIE_MMIO].size = size;
 }
 
+static void virt_get_highmem_mmio_base(Object *obj, Visitor *v,
+                                       const char *name, void *opaque,
+                                       Error **errp)
+{
+    uint64_t base = extended_memmap[VIRT_HIGH_PCIE_MMIO].base;
+
+    visit_type_size(v, name, &base, errp);
+}
+
+static void virt_set_highmem_mmio_base(Object *obj, Visitor *v,
+                                       const char *name, void *opaque,
+                                       Error **errp)
+{
+    uint64_t base;
+
+    if (!visit_type_size(v, name, &base, errp)) {
+        return;
+    }
+
+    if (base == 0) {
+        error_setg(errp, "highmem-mmio-base cannot be 0");
+        return;
+    }
+
+    extended_memmap[VIRT_HIGH_PCIE_MMIO].base = base;
+}
+
 static char *virt_get_msi(Object *obj, Error **errp)
 {
     VirtMachineState *vms = VIRT_MACHINE(obj);
@@ -4297,6 +4350,15 @@ static void virt_machine_class_init(ObjectClass *oc, const void *data)
                                           "Set the high memory region size "
                                           "for PCI MMIO");
 
+    object_class_property_add(oc, "highmem-mmio-base", "size",
+                              &size_type_info,
+                              virt_get_highmem_mmio_base,
+                              virt_set_highmem_mmio_base,
+                              NULL, NULL);
+    object_class_property_set_description(oc, "highmem-mmio-base",
+                                          "Set the high memory region base "
+                                          "for PCI MMIO");
+
     object_class_property_add(oc, "virtio-mmio-transports", "uint8",
                               &uint8_type_info,
                               virt_get_virtio_transports,
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-23 22:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 22:11 [PATCH v3 0/5] hw/pci, hw/arm/virt: fixed PCI BAR placement Tushar Dave
2026-11-23 22:11 ` [PATCH v3 1/5] hw/pci: introduce PCI fixed BAR properties Tushar Dave
2026-11-23 22:11 ` [PATCH v3 2/5] hw/pci: add validation for fixed BAR configuration Tushar Dave
2026-11-23 22:11 ` [PATCH v3 3/5] hw/pci: describe fixed BARs with an Enhanced Allocation capability Tushar Dave
2026-11-23 22:11 ` [PATCH v3 4/5] hw/arm/virt: validate fixed BARs and preserve the PCI configuration Tushar Dave
2026-11-23 22:11 ` [PATCH v3 5/5] hw/arm/virt: add highmem-mmio-base property Tushar Dave

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.