From: Niklas Cassel <cassel@kernel.org>
To: Frank Li <Frank.li@nxp.com>
Cc: "Jingoo Han" <jingoohan1@gmail.com>,
"Manivannan Sadhasivam" <mani@kernel.org>,
"Lorenzo Pieralisi" <lpieralisi@kernel.org>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Randolph Lin" <randolph@andestech.com>,
"Samuel Holland" <samuel.holland@sifive.com>,
"Charles Mirabile" <cmirabil@redhat.com>,
tim609@andestech.com,
"Krishna Chaitanya Chundru" <krishna.chundru@oss.qualcomm.com>,
linux-pci@vger.kernel.org
Subject: Re: [PATCH 3/3] PCI: dwc: Clean up iatu index usage in dw_pcie_iatu_setup()
Date: Thu, 22 Jan 2026 22:02:38 +0100 [thread overview]
Message-ID: <aXKQbo5WYstLY2T9@ryzen> (raw)
In-Reply-To: <aXKMKAfhkOy6bP0G@lizhi-Precision-Tower-5810>
On Thu, Jan 22, 2026 at 03:44:24PM -0500, Frank Li wrote:
> On Thu, Jan 22, 2026 at 03:54:14PM +0100, Niklas Cassel wrote:
> > The current iatu index usage in dw_pcie_iatu_setup() is a mess.
> >
> > For outbound address translation the index is incremented before usage.
> > For inbound address translation the index is incremented after usage.
> >
> > Incrementing the index after usage make much more sense, and:
> > Make the index usage consistent for both outbound and inbound address
> > translation.
> >
> > Most likely, the overly complicated logic for the outbound address
> > translation is because the iatu at index 0 is reserved for CFG IOs
> > (dw_pcie_other_conf_map_bus()), however, we should be able to use the
> > exact same logic for the indexing of the outbound and inbound iatus.
> > (Only the starting index should be different.)
> >
> > Create two new variables ob_iatu_index_to_use, ib_iatu_index_to_use,
> > which makes it clear from the name itself that it is the index before
> > increment.
> >
> > Since we always check if there is an index available immediately before
> > programming the iATU, we can remove the useless "ranges exceed outbound
> > iATU size" warnings, as the code is already unreachable.
> >
> > Because we always check if there is an index available immediately before
> > programming the iATU, we can also remove the useless breaks outside of
> > while loop.
>
> The condition is the same. So combine two paragraph
Sure.
>
> >
> > Also switch to use the more logical, but equivalent check if index is
> > smaller than length, which is the most common pattern when e.g. looping
> > through an array which has length items (0 to length-1), such that it is
> > even clearer to the reader that this is a zeroes based index.
> >
> > No functional changes intended.
> >
> > Signed-off-by: Niklas Cassel <cassel@kernel.org>
> > ---
> > .../pci/controller/dwc/pcie-designware-host.c | 59 ++++++++++---------
> > 1 file changed, 32 insertions(+), 27 deletions(-)
> >
> > diff --git a/drivers/pci/controller/dwc/pcie-designware-host.c b/drivers/pci/controller/dwc/pcie-designware-host.c
> > index 991fe5b9a7b3..eda94db04b63 100644
> > --- a/drivers/pci/controller/dwc/pcie-designware-host.c
> > +++ b/drivers/pci/controller/dwc/pcie-designware-host.c
> > @@ -892,9 +892,10 @@ static int dw_pcie_iatu_setup(struct dw_pcie_rp *pp)
> > struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
> > struct dw_pcie_ob_atu_cfg atu = { 0 };
> > struct resource_entry *entry;
> > + int ob_iatu_index_to_use = 0;
> > + int ib_iatu_index_to_use = 0;
> > int i, ret;
> >
> > - /* Note the very first outbound ATU is used for CFG IOs */
> > if (!pci->num_ob_windows) {
> > dev_err(pci->dev, "No outbound iATU found\n");
> > return -EINVAL;
> > @@ -910,16 +911,19 @@ static int dw_pcie_iatu_setup(struct dw_pcie_rp *pp)
> > for (i = 0; i < pci->num_ib_windows; i++)
> > dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, i);
> >
> > - i = 0;
> > + /*
> > + * NOTE: For outbound address translation, outbound iATU at index 0 is
> > + * reserved for CFG IOs (dw_pcie_other_conf_map_bus()), thus start at
> > + * index 1.
> > + */
> > + ob_iatu_index_to_use++;
> > +
> > resource_list_for_each_entry(entry, &pp->bridge->windows) {
> > resource_size_t res_size;
> >
> > if (resource_type(entry->res) != IORESOURCE_MEM)
> > continue;
> >
> > - if (pci->num_ob_windows <= i + 1)
> > - break;
> > -
> > atu.type = PCIE_ATU_TYPE_MEM;
> > atu.parent_bus_addr = entry->res->start - pci->parent_bus_offset;
> > atu.pci_addr = entry->res->start - entry->offset;
> > @@ -937,13 +941,13 @@ static int dw_pcie_iatu_setup(struct dw_pcie_rp *pp)
> > * middle. Otherwise, we would end up only partially
> > * mapping a single resource.
> > */
> > - if (pci->num_ob_windows <= ++i) {
> > - dev_err(pci->dev, "Exhausted outbound windows for region: %pr\n",
> > + if (!(ob_iatu_index_to_use < pci->num_ob_windows)) {
>
> Is it better "if (ob_iatu_index_to_use >= pci->num_ob_windows)"
Personally, I think no, since if you look at the condition
now used (after this patch) in:
if (pp->io_size) {
if (ob_iatu_index_to_use < pci->num_ob_windows) {
...
if (pp->use_atu_msg) {
if (ob_iatu_index_to_use < pci->num_ob_windows) {
...
The difference is that
if (pp->io_size) { and
if (pp->use_atu_msg) {
only does something if (condition)
while the loops over the memory windows have:
if (!condition)
return -ENOMEM;
For consistency, and to try to avoid this function becoming a mess again,
I think it makes sense to use the exact same condition everywhere, and
only negate the condition if needed.
Kind regards,
Niklas
next prev parent reply other threads:[~2026-01-22 21:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-22 14:54 [PATCH 1/3] PCI: dwc: Fix msg_atu_index assignment Niklas Cassel
2026-01-22 14:54 ` [PATCH 2/3] PCI: dwc: Improve msg_atu_index error handling Niklas Cassel
2026-01-22 20:33 ` Frank Li
2026-01-22 14:54 ` [PATCH 3/3] PCI: dwc: Clean up iatu index usage in dw_pcie_iatu_setup() Niklas Cassel
2026-01-22 20:44 ` Frank Li
2026-01-22 21:02 ` Niklas Cassel [this message]
2026-01-22 20:31 ` [PATCH 1/3] PCI: dwc: Fix msg_atu_index assignment Frank Li
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=aXKQbo5WYstLY2T9@ryzen \
--to=cassel@kernel.org \
--cc=Frank.li@nxp.com \
--cc=bhelgaas@google.com \
--cc=cmirabil@redhat.com \
--cc=jingoohan1@gmail.com \
--cc=krishna.chundru@oss.qualcomm.com \
--cc=kwilczynski@kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lpieralisi@kernel.org \
--cc=mani@kernel.org \
--cc=randolph@andestech.com \
--cc=robh@kernel.org \
--cc=samuel.holland@sifive.com \
--cc=tim609@andestech.com \
/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