Devicetree
 help / color / mirror / Atom feed
From: Bjorn Helgaas <helgaas@kernel.org>
To: Alex Elder <elder@riscstar.com>
Cc: bhelgaas@google.com, krzk+dt@kernel.org, conor+dt@kernel.org,
	daniel@riscstar.com, mohd.anwar@oss.qualcomm.com,
	lorenzo.bianconi@oss.qualcomm.com, linux-pci@vger.kernel.org,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Andrea della Porta <andrea.porta@suse.com>,
	Rob Herring <robh@kernel.org>
Subject: Re: [PATCH v4 2/2] PCI: of: introduce of_pci_update_endpoint_node_ranges()
Date: Sat, 5 Dec 2026 18:07:43 -0600	[thread overview]
Message-ID: <20261206000743.GA1177022@bhelgaas> (raw)
In-Reply-To: <20261205233426.GA1163688@bhelgaas>

[fix Rob H's email addr]

On Sat, Dec 05, 2026 at 05:34:27PM -0600, Bjorn Helgaas wrote:
> On Thu, Nov 05, 2026 at 03:24:12PM -0600, Alex Elder wrote:
> > Commit 407d1a51921e9 ("PCI: Create device tree node for bridge")
> > introduced the PCI_DYNAMIC_OF_NODES Kconfig option, which creates
> > a devicetree node for a PCI bridge as part of pci_bus_add_device().
> > Its successor commit ae9813db1dc5a ("PCI: Add quirks to generate
> > device tree node for Xilinx Alveo U50") shows how to use a PCI final
> > fixup quirk to also create a devicetree node for a non-bridge PCI
> > device.  In both cases, of_pci_make_dev_node() uses an OF changeset
> > to dynamically create a node populated with appropriate properties
> > and apply it to the live devicetree.
> > 
> > The dynamic devicetree node for a PCI device will include a "ranges"
> > property, and a new type of 3-cell address is introduced for use
> > within an endpoint.  The endpoint's ranges property will contain a
> > range entry for each of the endpoint's BARs.  The "child address"
> > portion of each range will use the BAR number in the "flags" (first)
> > cell in the address.  This allows addresses within the endpoint to
> > be expressed relative to whatever address gets assigned to each BAR.
> > 
> > Unfortunately, if a PCI endpoint device had a devicetree node set
> > up statically, its "ranges" property (if present) will be static,
> > and it cannot contain the addresses assigned to the endpoint's BARs
> > during enumeration.
> 
> It looks like this can update the DT "ranges" at the time of
> pci_bus_add_device(), but what about BARs assigned by
> pci_assign_unassigned_resources() and other later changes like ReBAR?
> 
> Can we also update the subject lines of both these patches to say
> something about the purpose and/or what the benefit is?
> 
> > This means that the "BAR number" based addressing scheme doesn't
> > work for PCI endpoints whose devicetree nodes are created statically.
> > 
> > To remedy this, modify of_pci_make_dev_node() to dynamically create
> > a "ranges" property even if the device already has a devicetree
> > node--just as is done when the endpoint has none.  A few conditions:
> >   - A PCI bridge node's ranges property is never updated
> >   - If a PCI endpoint node defines a ranges property with a
> >     non-empty value (i.e., it's not just "ranges;"), that
> >     ranges property is preserved
> > 
> > Otherwise a new ranges property is created using assigned addresses,
> > and it replaces (or adds) that property to the PCI endpoint node.
> > 
> > As a result, "BAR number" addresses work correctly even when the
> > endpoint's devicetree node is created statically.  This has been
> > verified by Andrea della Porta (with both static and dynamic ranges
> > properties) on the Raspberry Pi 5 RP1.
> > 
> > Tested-by: Andrea della Porta <andrea.porta@suse.com>
> > Signed-off-by: Alex Elder <elder@riscstar.com>
> > ---
> >  drivers/pci/of.c          | 97 +++++++++++++++++++++++++++++++++++----
> >  drivers/pci/of_property.c |  2 +-
> >  drivers/pci/pci.h         |  1 +
> >  3 files changed, 91 insertions(+), 9 deletions(-)
> > 
> > diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> > index 5a040ed836744..fa9e9377c3eb9 100644
> > --- a/drivers/pci/of.c
> > +++ b/drivers/pci/of.c
> > @@ -17,6 +17,9 @@
> >  #include <linux/of_pci.h>
> >  #include <linux/platform_device.h>
> >  #include <linux/pm_wakeirq.h>
> > +
> > +#include <asm/byteorder.h>
> > +
> >  #include "pci.h"
> >  
> >  #ifdef CONFIG_PCI
> > @@ -742,20 +745,98 @@ void of_pci_remove_node(struct pci_dev *pdev)
> >  	of_node_put(np);
> >  }
> >  
> > +/* Returns true if the ranges property was added or updated successfully */
> > +static bool of_pci_update_endpoint_node_ranges(struct pci_dev *pdev)
> > +{
> > +	struct device_node *np = pci_device_to_OF_node(pdev);
> > +	struct property *prop;
> > +	u32 *value;
> > +	u32 size;
> > +
> > +	prop = kzalloc_obj(*prop);
> > +	if (!prop)
> > +		return false;
> > +
> > +	value = of_pci_build_prop_ranges(pdev, &size);
> > +	if (!value) {
> > +		kfree(prop);
> > +		return false;
> > +	}
> > +
> > +	prop->name = "ranges";
> > +	prop->length = size * sizeof(u32);
> > +	prop->value = value;
> > +
> > +	/* The property value needs to be in big-endian byte order */
> > +	while (size--)
> > +		cpu_to_be32s(value++);
> > +
> > +	/* of_update_property() consumes the allocated property */
> > +	of_update_property(np, prop);
> > +
> > +	return true;
> > +}
> > +
> > +/*
> > + * Create a devicetree node for a PCI device.  If the device is a bridge
> > + * and it already has a devicetree node, there's nothing further to do.
> > + * If it is a bridge without an existing devicetree node, one is created
> > + * dynamically.
> > + *
> > + * This function can also be called (via PCI quirk) for a PCI endpoint
> > + * (function) that implements a PCI endpoint bus.  As with a PCI bridge,
> > + * if the endpoint has no existing devicetree node, one is created
> > + * dynamically.  The node will include a ranges property that maps
> > + * BAR-relative addresses in the child to the PCI address ranges
> > + * assigned to the PCI endpoint BARs.
> > + *
> > + * If an endpoint already has a devicetree node, and it includes a
> > + * "pci-ep-bus" sub-node, its ranges property must still be dynamically
> > + * populated so that it can take into account the BAR ranges assigned
> > + * during PCI enumeration.
> > + */
> >  void of_pci_make_dev_node(struct pci_dev *pdev)
> >  {
> > -	struct device_node *ppnode, *np = NULL;
> > +	struct device_node *np = pci_device_to_OF_node(pdev);
> > +	struct device *dev = &pdev->dev;
> > +	struct device_node *ppnode;
> > +	struct of_changeset *cset;
> >  	const char *pci_type;
> > -	struct of_changeset *cset;
> >  	const char *name;
> >  	int ret;
> >  
> > -	/*
> > -	 * If there is already a device tree node linked to this device,
> > -	 * return immediately.
> > -	 */
> > -	if (pci_device_to_OF_node(pdev))
> > +	/* See if the PCI device already has a devicetree node */
> > +	if (np) {
> > +		struct device_node *child;
> > +		unsigned int rlen = 0;
> > +
> > +		/* Nothing further needed for a bridge */
> > +		if (pci_is_bridge(pdev))
> > +			return;
> > +
> > +		/*
> > +		 * A ranges property is only needed if the endpoint's
> > +		 * devicetree node includes a "pci-ep-bus" sub-node.
> > +		 */
> > +		child = of_get_child_by_name(np, "pci-ep-bus");
> > +		if (!child)
> > +			return;
> > +		of_node_put(child);
> > +
> > +		/* If the ranges property is non-empty, just keep it.*/
> > +		if (of_get_property(np, "ranges", &rlen) && rlen)
> > +			return;
> > +
> > +		/*
> > +		 * Otherwise create a ranges property, defining an entry for
> > +		 * each BAR, and map BAR offsets to the PCI bus address based
> > +		 * on the BAR's assigned range.
> > +		 */
> > +		if (!of_pci_update_endpoint_node_ranges(pdev))
> > +			dev_err(dev, "failed to update ranges property\n");
> > +
> >  		return;
> > +	}
> >  
> >  	/* Check if there is device tree node for parent device */
> >  	if (!pdev->bus->self)
> > @@ -794,7 +875,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
> >  
> >  	np->data = cset;
> >  
> > -	ret = device_add_of_node(&pdev->dev, np);
> > +	ret = device_add_of_node(dev, np);
> >  	if (ret)
> >  		goto out_revert_cset;
> >  
> > diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c
> > index b202f45b3660b..44e169f4fbf4d 100644
> > --- a/drivers/pci/of_property.c
> > +++ b/drivers/pci/of_property.c
> > @@ -122,7 +122,7 @@ static int of_pci_prop_bus_range(struct pci_dev *pdev,
> >   *
> >   * Caller is responsible for ensuring the returned pointer gets freed.
> >   */
> > -static u32 *of_pci_build_prop_ranges(struct pci_dev *pdev, u32 *count)
> > +u32 *of_pci_build_prop_ranges(struct pci_dev *pdev, u32 *count)
> >  {
> >  	bool bridge_device = pci_is_bridge(pdev);
> >  	struct of_pci_range_entry *entries;
> > diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> > index e93f602b9ca48..af21a46cfdb3c 100644
> > --- a/drivers/pci/pci.h
> > +++ b/drivers/pci/pci.h
> > @@ -1324,6 +1324,7 @@ struct of_changeset;
> >  #ifdef CONFIG_PCI_DYNAMIC_OF_NODES
> >  void of_pci_make_dev_node(struct pci_dev *pdev);
> >  void of_pci_remove_node(struct pci_dev *pdev);
> > +u32 *of_pci_build_prop_ranges(struct pci_dev *pdev, u32 *count);
> >  int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs,
> >  			  struct device_node *np);
> >  void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge);
> > -- 
> > 2.53.0
> > 

  reply	other threads:[~2026-12-06  0:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05 21:24 [PATCH v4 0/2] PCI: of: update endpoint ranges dynamically Alex Elder
2026-11-05 21:24 ` [PATCH v4 1/2] PCI: of: introduce of_pci_build_prop_ranges() Alex Elder
2026-11-05 21:24 ` [PATCH v4 2/2] PCI: of: introduce of_pci_update_endpoint_node_ranges() Alex Elder
2026-12-05 23:34   ` Bjorn Helgaas
2026-12-06  0:07     ` Bjorn Helgaas [this message]
2026-12-04 10:58 ` [PATCH v4 0/2] PCI: of: update endpoint ranges dynamically Alexander Stein
2026-12-04 13:02   ` Alex Elder

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=20261206000743.GA1177022@bhelgaas \
    --to=helgaas@kernel.org \
    --cc=andrea.porta@suse.com \
    --cc=bhelgaas@google.com \
    --cc=conor+dt@kernel.org \
    --cc=daniel@riscstar.com \
    --cc=devicetree@vger.kernel.org \
    --cc=elder@riscstar.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lorenzo.bianconi@oss.qualcomm.com \
    --cc=mohd.anwar@oss.qualcomm.com \
    --cc=robh@kernel.org \
    /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