]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
PCI/IOV: Restore VF resizable BAR state after reset
authorMichał Winiarski <michal.winiarski@intel.com>
Wed, 2 Jul 2025 09:35:18 +0000 (11:35 +0200)
committerBjorn Helgaas <bhelgaas@google.com>
Mon, 14 Jul 2025 19:58:13 +0000 (14:58 -0500)
Similar to regular resizable BARs, VF BARs can also be resized, e.g. by the
system firmware or the PCI subsystem itself.

The capability layout is the same as PCI_EXT_CAP_ID_REBAR.

Add the capability ID and restore it as a part of IOV state.

See PCIe r6.2, sec 7.8.7.

Signed-off-by: Michał Winiarski <michal.winiarski@intel.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Link: https://patch.msgid.link/20250702093522.518099-2-michal.winiarski@intel.com
drivers/pci/iov.c
drivers/pci/pci.h
include/uapi/linux/pci_regs.h

index 10693b5d7eb66bbbfb9b70ffe6e89eb89c8dc3a3..10ccef8afe1454940d67746da3bd2a0c5c675fe1 100644 (file)
@@ -7,6 +7,7 @@
  * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
  */
 
+#include <linux/bitfield.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/export.h>
@@ -850,6 +851,7 @@ found:
        pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
        if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
                iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
+       iov->vf_rebar_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VF_REBAR);
 
        if (pdev)
                iov->dev = pci_dev_get(pdev);
@@ -888,6 +890,30 @@ static void sriov_release(struct pci_dev *dev)
        dev->sriov = NULL;
 }
 
+static void sriov_restore_vf_rebar_state(struct pci_dev *dev)
+{
+       unsigned int pos, nbars, i;
+       u32 ctrl;
+
+       pos = pci_iov_vf_rebar_cap(dev);
+       if (!pos)
+               return;
+
+       pci_read_config_dword(dev, pos + PCI_VF_REBAR_CTRL, &ctrl);
+       nbars = FIELD_GET(PCI_VF_REBAR_CTRL_NBAR_MASK, ctrl);
+
+       for (i = 0; i < nbars; i++, pos += 8) {
+               int bar_idx, size;
+
+               pci_read_config_dword(dev, pos + PCI_VF_REBAR_CTRL, &ctrl);
+               bar_idx = FIELD_GET(PCI_VF_REBAR_CTRL_BAR_IDX, ctrl);
+               size = pci_rebar_bytes_to_size(dev->sriov->barsz[bar_idx]);
+               ctrl &= ~PCI_VF_REBAR_CTRL_BAR_SIZE;
+               ctrl |= FIELD_PREP(PCI_VF_REBAR_CTRL_BAR_SIZE, size);
+               pci_write_config_dword(dev, pos + PCI_VF_REBAR_CTRL, ctrl);
+       }
+}
+
 static void sriov_restore_state(struct pci_dev *dev)
 {
        int i;
@@ -1047,8 +1073,10 @@ resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
  */
 void pci_restore_iov_state(struct pci_dev *dev)
 {
-       if (dev->is_physfn)
+       if (dev->is_physfn) {
+               sriov_restore_vf_rebar_state(dev);
                sriov_restore_state(dev);
+       }
 }
 
 /**
index 12215ee72afb682b669c0e3a582b5379828e70c4..69258c445b2881c9288fd06150a3a6e107569294 100644 (file)
@@ -486,6 +486,7 @@ struct pci_sriov {
        u16             subsystem_vendor; /* VF subsystem vendor */
        u16             subsystem_device; /* VF subsystem device */
        resource_size_t barsz[PCI_SRIOV_NUM_BARS];      /* VF BAR size */
+       u16             vf_rebar_cap;   /* VF Resizable BAR capability offset */
        bool            drivers_autoprobe; /* Auto probing of VFs by driver */
 };
 
@@ -710,6 +711,13 @@ void pci_iov_update_resource(struct pci_dev *dev, int resno);
 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno);
 void pci_restore_iov_state(struct pci_dev *dev);
 int pci_iov_bus_range(struct pci_bus *bus);
+static inline u16 pci_iov_vf_rebar_cap(struct pci_dev *dev)
+{
+       if (!dev->is_physfn)
+               return 0;
+
+       return dev->sriov->vf_rebar_cap;
+}
 static inline bool pci_resource_is_iov(int resno)
 {
        return resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END;
@@ -734,6 +742,10 @@ static inline int pci_iov_bus_range(struct pci_bus *bus)
 {
        return 0;
 }
+static inline u16 pci_iov_vf_rebar_cap(struct pci_dev *dev)
+{
+       return 0;
+}
 static inline bool pci_resource_is_iov(int resno)
 {
        return false;
index a3a3e942dedffcf517a9025bc1f7cf3c83de01ff..f5b17745de607d92ad6a2db3383bfcb7d911ad0e 100644 (file)
 #define PCI_EXT_CAP_ID_L1SS    0x1E    /* L1 PM Substates */
 #define PCI_EXT_CAP_ID_PTM     0x1F    /* Precision Time Measurement */
 #define PCI_EXT_CAP_ID_DVSEC   0x23    /* Designated Vendor-Specific */
+#define PCI_EXT_CAP_ID_VF_REBAR 0x24   /* VF Resizable BAR */
 #define PCI_EXT_CAP_ID_DLF     0x25    /* Data Link Feature */
 #define PCI_EXT_CAP_ID_PL_16GT 0x26    /* Physical Layer 16.0 GT/s */
 #define PCI_EXT_CAP_ID_NPEM    0x29    /* Native PCIe Enclosure Management */
 #define PCI_DVSEC_HEADER2              0x8 /* Designated Vendor-Specific Header2 */
 #define  PCI_DVSEC_HEADER2_ID(x)               ((x) & 0xffff)
 
+/* VF Resizable BARs, same layout as PCI_REBAR */
+#define PCI_VF_REBAR_CAP       PCI_REBAR_CAP
+#define  PCI_VF_REBAR_CAP_SIZES                PCI_REBAR_CAP_SIZES
+#define PCI_VF_REBAR_CTRL      PCI_REBAR_CTRL
+#define  PCI_VF_REBAR_CTRL_BAR_IDX     PCI_REBAR_CTRL_BAR_IDX
+#define  PCI_VF_REBAR_CTRL_NBAR_MASK   PCI_REBAR_CTRL_NBAR_MASK
+#define  PCI_VF_REBAR_CTRL_BAR_SIZE    PCI_REBAR_CTRL_BAR_SIZE
+
 /* Data Link Feature */
 #define PCI_DLF_CAP            0x04    /* Capabilities Register */
 #define  PCI_DLF_EXCHANGE_ENABLE       0x80000000  /* Data Link Feature Exchange Enable */