git: 665afd2a9d64 - main - pci: restore resizable BAR sizes in pci_cfg_restore()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 08 Nov 2026 01:36:18 UTC
The branch main has been updated by nprice:
URL: https://cgit.FreeBSD.org/src/commit/?id=665afd2a9d6469ce692de4018c7c015a194c201d
commit 665afd2a9d6469ce692de4018c7c015a194c201d
Author: Nick Price <nprice@FreeBSD.org>
AuthorDate: 2026-11-08 01:36:01 +0000
Commit: Nick Price <nprice@FreeBSD.org>
CommitDate: 2026-11-08 01:36:05 +0000
pci: restore resizable BAR sizes in pci_cfg_restore()
After a device reset, resizable BARs return to their default sizes.
Restore them to the sizes they had prior to reset.
Reviewed by: imp, adrian
Differential Revision: https://reviews.freebsd.org/D61110
Approved by: adrian (mentor)
Signed-off-by: Nick Price <nprice@FreeBSD.org>
---
sys/dev/pci/pci.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
sys/dev/pci/pcireg.h | 9 +++++++++
2 files changed, 58 insertions(+)
diff --git a/sys/dev/pci/pci.c b/sys/dev/pci/pci.c
index 88a1bb376fca..35f8e1154d6c 100644
--- a/sys/dev/pci/pci.c
+++ b/sys/dev/pci/pci.c
@@ -6868,6 +6868,54 @@ pci_cfg_restore_pcix(device_t dev, struct pci_devinfo *dinfo)
dinfo->cfg.pcix.pcix_command, 2);
}
+static void
+pci_restore_bar_sizes(device_t dev)
+{
+ struct pci_devinfo *dinfo;
+ struct pci_map *pm;
+ uint32_t ctrl;
+ uint16_t cmd;
+ int cur, i, nbars, ptr, size;
+
+ dinfo = device_get_ivars(dev);
+ if (pci_find_extcap(dev, PCIZ_RESIZE_BAR, &ptr) != 0)
+ return;
+ ctrl = pci_read_config(dev, ptr + PCIR_REBAR_CTRL(0), 4);
+ nbars = (ctrl & PCIM_REBAR_CTRL_NBAR_MASK) >>
+ PCIM_REBAR_CTRL_NBAR_SHIFT;
+ if (nbars < 1 || nbars > PCI_MAXMAPS_0)
+ return;
+
+ cmd = pci_read_config(dev, PCIR_COMMAND, 2);
+ for (i = 0; i < nbars; i++) {
+ ctrl = pci_read_config(dev, ptr + PCIR_REBAR_CTRL(i), 4);
+ if ((ctrl & PCIM_REBAR_CTRL_BAR_IDX) > PCIR_MAX_BAR_0)
+ continue;
+ pm = pci_find_bar(dev,
+ PCIR_BAR(ctrl & PCIM_REBAR_CTRL_BAR_IDX));
+ if (pm == NULL || pm->pm_size < 20 || pm->pm_size > 63)
+ continue;
+ size = pm->pm_size - 20;
+ cur = (ctrl & PCIM_REBAR_CTRL_SIZE_MASK) >>
+ PCIM_REBAR_CTRL_SIZE_SHIFT;
+ if (cur == size)
+ continue;
+ /* Disable decoding before resizing. */
+ if ((cmd & PCIM_CMD_MEMEN) != 0) {
+ cmd &= ~PCIM_CMD_MEMEN;
+ pci_write_config(dev, PCIR_COMMAND, cmd, 2);
+ }
+ ctrl &= ~PCIM_REBAR_CTRL_SIZE_MASK;
+ ctrl |= size << PCIM_REBAR_CTRL_SIZE_SHIFT;
+ pci_write_config(dev, ptr + PCIR_REBAR_CTRL(i), ctrl, 4);
+ if (bootverbose)
+ pci_printf(&dinfo->cfg,
+ "map[%02x]: resized to %ju MB from %ju MB\n",
+ pm->pm_reg, (uintmax_t)1 << size,
+ (uintmax_t)1 << cur);
+ }
+}
+
void
pci_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
{
@@ -6918,6 +6966,7 @@ pci_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
dinfo->cfg.bridge.br_control, 2);
break;
}
+ pci_restore_bar_sizes(dev);
pci_restore_bars(dev);
if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_BRIDGE)
diff --git a/sys/dev/pci/pcireg.h b/sys/dev/pci/pcireg.h
index 5faaf3d7c793..dc73807e7ff1 100644
--- a/sys/dev/pci/pcireg.h
+++ b/sys/dev/pci/pcireg.h
@@ -1080,6 +1080,15 @@
#define PCIR_SRIOV_BARS 0x24
#define PCIR_SRIOV_BAR(x) (PCIR_SRIOV_BARS + (x) * 4)
+/* Resizable BAR definitions */
+#define PCIR_REBAR_CAP(n) (0x04 + (n) * 8)
+#define PCIR_REBAR_CTRL(n) (0x08 + (n) * 8)
+#define PCIM_REBAR_CTRL_BAR_IDX 0x00000007
+#define PCIM_REBAR_CTRL_NBAR_MASK 0x000000e0
+#define PCIM_REBAR_CTRL_NBAR_SHIFT 5
+#define PCIM_REBAR_CTRL_SIZE_MASK 0x00003f00
+#define PCIM_REBAR_CTRL_SIZE_SHIFT 8
+
/* Extended Capability Vendor-Specific definitions */
#define PCIR_VSEC_HEADER 0x04
#define PCIR_VSEC_ID(hdr) ((hdr) & 0xffff)