git: 01111efd9815 - stable/15 - pci: restore resizable BAR sizes in pci_cfg_restore()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 26 Nov 2026 20:33:29 UTC
The branch stable/15 has been updated by nprice:
URL: https://cgit.FreeBSD.org/src/commit/?id=01111efd98158bc3f7f5d5fbb8cf12c86eb643f0
commit 01111efd98158bc3f7f5d5fbb8cf12c86eb643f0
Author: Nick Price <nprice@FreeBSD.org>
AuthorDate: 2026-11-08 01:36:01 +0000
Commit: Nick Price <nprice@FreeBSD.org>
CommitDate: 2026-11-26 19:58:55 +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)
(cherry picked from commit 665afd2a9d6469ce692de4018c7c015a194c201d)
---
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 766efb633f66..0beaa5a7f62d 100644
--- a/sys/dev/pci/pci.c
+++ b/sys/dev/pci/pci.c
@@ -6869,6 +6869,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)
{
@@ -6919,6 +6967,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 9a48e9183201..1a7280cbfe2c 100644
--- a/sys/dev/pci/pcireg.h
+++ b/sys/dev/pci/pcireg.h
@@ -1079,6 +1079,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)