From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: linux-pci@vger.kernel.org, Bjorn Helgaas <bhelgaas@google.com>,
Dominik Brodowski <linux@dominikbrodowski.net>,
linux-kernel@vger.kernel.org
Cc: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Subject: [PATCH 13/23] PCI: Add pbus_mem_size_optional() to handle optional sizes
Date: Fri, 19 Dec 2025 19:40:26 +0200 [thread overview]
Message-ID: <20251219174036.16738-14-ilpo.jarvinen@linux.intel.com> (raw)
In-Reply-To: <20251219174036.16738-1-ilpo.jarvinen@linux.intel.com>
The resource loop in pbus_size_mem() handles optional resources that
are either fully optional (SRIOV and disabled Expansion ROMs) or bridge
windows that may be optional only for a part. The logic is little
inconsistent when it comes to a bridge window that has only optional
children resources as it would be more natural to treat it similar to
any fully optional resource. As resource size should be zero in that
case, it shouldn't cause any bugs but it still seems useful to address
the inconsistency.
Place the optional size related code of pbus_size_mem() into
pbus_mem_size_optional() and add check into pci_resource_is_optional()
for entirely optional bridge windows. Reorder the logic inside
pbus_mem_size_optional() such that fully optional resources are handled
the same irrespective to whether the resource is a bridge window or
not.
Additional motivation for this are the upcoming changes that add
complexity to the optional sizing logic due to Resizable BAR awareness.
The extra logic would exceed any reasonable indentation level if the
optional sizing code is kept within the loop body.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
drivers/pci/setup-bus.c | 77 +++++++++++++++++++++++++++++------------
1 file changed, 54 insertions(+), 23 deletions(-)
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 3d1d3cefcdba..3fcc7641c374 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -125,15 +125,6 @@ static resource_size_t get_res_add_size(struct list_head *head,
return dev_res ? dev_res->add_size : 0;
}
-static resource_size_t get_res_add_align(struct list_head *head,
- struct resource *res)
-{
- struct pci_dev_resource *dev_res;
-
- dev_res = res_to_dev_res(head, res);
- return dev_res ? dev_res->min_align : 0;
-}
-
static void restore_dev_resource(struct pci_dev_resource *dev_res)
{
struct resource *res = dev_res->res;
@@ -386,6 +377,8 @@ bool pci_resource_is_optional(const struct pci_dev *dev, int resno)
return true;
if (resno == PCI_ROM_RESOURCE && !(res->flags & IORESOURCE_ROM_ENABLE))
return true;
+ if (pci_resource_is_bridge_win(resno) && !resource_size(res))
+ return true;
return false;
}
@@ -1258,6 +1251,54 @@ static resource_size_t calculate_head_align(resource_size_t *aligns,
return head_align;
}
+/*
+ * pbus_size_mem_optional - Account optional resources in bridge window
+ *
+ * Account an optional resource or the optional part of the resource in bridge
+ * window size.
+ *
+ * Return: %true if the resource is entirely optional.
+ */
+static bool pbus_size_mem_optional(struct pci_dev *dev, int resno,
+ resource_size_t align,
+ struct list_head *realloc_head,
+ resource_size_t *add_align,
+ resource_size_t *children_add_size)
+{
+ struct resource *res = pci_resource_n(dev, resno);
+ bool optional = pci_resource_is_optional(dev, resno);
+ resource_size_t r_size = resource_size(res);
+ struct pci_dev_resource *dev_res;
+
+ if (!realloc_head)
+ return false;
+
+ if (!optional) {
+ /*
+ * Only bridges have optional sizes in realloc_head at this
+ * point. As res_to_dev_res() walks the entire realloc_head
+ * list, skip calling it when known unnecessary.
+ */
+ if (!pci_resource_is_bridge_win(resno))
+ return false;
+
+ dev_res = res_to_dev_res(realloc_head, res);
+ if (dev_res) {
+ *children_add_size += dev_res->add_size;
+ *add_align = max(*add_align, dev_res->min_align);
+ }
+
+ return false;
+ }
+
+ /* Put SRIOV requested res to the optional list */
+ add_to_list(realloc_head, dev, res, 0, align);
+ *children_add_size += r_size;
+ *add_align = max(align, *add_align);
+
+ return true;
+}
+
/**
* pbus_size_mem() - Size the memory window of a given bus
*
@@ -1284,7 +1325,6 @@ static void pbus_size_mem(struct pci_bus *bus, struct resource *b_res,
resource_size_t aligns[28] = {}; /* Alignments from 1MB to 128TB */
int order, max_order;
resource_size_t children_add_size = 0;
- resource_size_t children_add_align = 0;
resource_size_t add_align = 0;
if (!b_res)
@@ -1311,7 +1351,6 @@ static void pbus_size_mem(struct pci_bus *bus, struct resource *b_res,
if (b_res != pbus_select_window(bus, r))
continue;
- r_size = resource_size(r);
align = pci_resource_alignment(dev, r);
/*
* aligns[0] is for 1MB (since bridge memory
@@ -1327,25 +1366,17 @@ static void pbus_size_mem(struct pci_bus *bus, struct resource *b_res,
continue;
}
- /* Put SRIOV requested res to the optional list */
- if (realloc_head && pci_resource_is_optional(dev, i)) {
- add_align = max(align, add_align);
- add_to_list(realloc_head, dev, r, 0, 0 /* Don't care */);
- children_add_size += r_size;
+ if (pbus_size_mem_optional(dev, i, align,
+ realloc_head, &add_align,
+ &children_add_size))
continue;
- }
+ r_size = resource_size(r);
size += max(r_size, align);
aligns[order] += align;
if (order > max_order)
max_order = order;
-
- if (realloc_head) {
- children_add_size += get_res_add_size(realloc_head, r);
- children_add_align = get_res_add_align(realloc_head, r);
- add_align = max(add_align, children_add_align);
- }
}
}
--
2.39.5
next prev parent reply other threads:[~2025-12-19 17:42 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-19 17:40 [PATCH 00/23] PCI: Resource code fixes (supercedes earlier series) & cleanups Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 01/23] PCI: Fix bridge window alignment with optional resources Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 02/23] PCI: Rewrite bridge window head alignment function Ilpo Järvinen
2026-01-26 22:17 ` Bjorn Helgaas
2026-01-27 11:22 ` Ilpo Järvinen
2026-01-27 22:39 ` Bjorn Helgaas
2025-12-19 17:40 ` [PATCH 03/23] PCI: Stop over-estimating bridge window size Ilpo Järvinen
2026-03-05 15:13 ` Guenter Roeck
2026-03-05 16:28 ` Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 04/23] resource: Increase MAX_IORES_LEVEL to 8 Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 05/23] PCI: Remove old_size limit from bridge window sizing Ilpo Järvinen
2026-01-26 17:16 ` Bjorn Helgaas
2026-01-26 20:09 ` Bjorn Helgaas
2026-01-27 11:39 ` Ilpo Järvinen
2026-01-27 22:42 ` Bjorn Helgaas
2026-01-27 10:16 ` Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 06/23] PCI: Push realloc check into pbus_size_mem() Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 07/23] PCI: Pass bridge window resource to pbus_size_mem() Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 08/23] PCI: Use res_to_dev_res() in reassign_resources_sorted() Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 09/23] PCI: Fetch dev_res to local var in __assign_resources_sorted() Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 10/23] PCI: Add pci_resource_is_bridge_win() Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 11/23] PCI: Log reset and restore of resources Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 12/23] PCI: Check invalid align earlier in pbus_size_mem() Ilpo Järvinen
2025-12-19 17:40 ` Ilpo Järvinen [this message]
2025-12-19 17:40 ` [PATCH 14/23] resource: Mark res given to resource_assigned() as const Ilpo Järvinen
2025-12-19 17:47 ` Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 15/23] PCI: Use resource_assigned() in setup-bus.c algorithm Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 16/23] PCI: Properly prefix struct pci_dev_resource handling functions Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 17/23] PCI: Separate cardbus setup & build it only with CONFIG_CARDBUS Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 18/23] PCI: Handle CardBus specific params in setup-cardbus.c Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 19/23] PCI: Use scnprintf() instead of sprintf() Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 20/23] PCI: Add Bus Number + Secondary Latency Timer as dword fields Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 21/23] PCI: Convert to use Bus Number field defines Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 22/23] PCI: Add pbus_validate_busn() for Bus Number validation Ilpo Järvinen
2025-12-19 17:40 ` [PATCH 23/23] PCI: Move scanbus bridge scanning to setup-cardbus.c Ilpo Järvinen
2026-01-26 17:39 ` [PATCH 00/23] PCI: Resource code fixes (supercedes earlier series) & cleanups Bjorn Helgaas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251219174036.16738-14-ilpo.jarvinen@linux.intel.com \
--to=ilpo.jarvinen@linux.intel.com \
--cc=bhelgaas@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux@dominikbrodowski.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox