From: "Clément Léger" <clement.leger@bootlin.com>
To: Rob Herring <robh+dt@kernel.org>,
Frank Rowand <frowand.list@gmail.com>,
Pantelis Antoniou <pantelis.antoniou@konsulko.com>,
Bjorn Helgaas <bhelgaas@google.com>
Cc: "Clément Léger" <clement.leger@bootlin.com>,
"Allan Nielsen" <allan.nielsen@microchip.com>,
"Horatiu Vultur" <horatiu.vultur@microchip.com>,
"Steen Hegelund" <steen.hegelund@microchip.com>,
"Thomas Petazzoni" <thomas.petazonni@bootlin.com>,
"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
"Mark Brown" <broonie@kernel.org>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Jakub Kicinski" <kuba@kernel.org>,
"Hans de Goede" <hdegoede@redhat.com>,
"Andrew Lunn" <andrew@lunn.ch>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-pci@vger.kernel.org, "Rob Herring" <robh@kernel.org>
Subject: [PATCH 2/3] PCI: of: create DT nodes for PCI devices if they do not exists
Date: Wed, 27 Apr 2022 11:45:01 +0200 [thread overview]
Message-ID: <20220427094502.456111-3-clement.leger@bootlin.com> (raw)
In-Reply-To: <20220427094502.456111-1-clement.leger@bootlin.com>
In order to apply overlays to PCI device nodes, the nodes must first
exist. This commit add support to populate a skeleton tree for PCI bus
and devices. These nodes can then be used by drivers to apply overlays.
Co-developed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Rob Herring <robh@kernel.org>
Signed-off-by: Clément Léger <clement.leger@bootlin.com>
---
drivers/pci/of.c | 184 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 184 insertions(+)
diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index cb2e8351c2cc..f2325708726e 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -16,12 +16,194 @@
#include "pci.h"
#ifdef CONFIG_PCI
+static int of_pci_add_property(struct of_changeset *ocs, struct device_node *np,
+ const char *name, const void *value, int length)
+{
+ struct property *prop;
+ int ret = -ENOMEM;
+
+ prop = kzalloc(sizeof(*prop), GFP_KERNEL);
+ if (!prop)
+ return -ENOMEM;
+
+ prop->name = kstrdup(name, GFP_KERNEL);
+ if (!prop->name)
+ goto out_err;
+
+ if (value) {
+ prop->value = kmemdup(value, length, GFP_KERNEL);
+ if (!prop->value)
+ goto out_err;
+ } else {
+ /*
+ * Even if the property has no value, it must be set to a
+ * non-null value since of_get_property() is used to check
+ * some values that might or not have a values (ranges for
+ * instance). Moreover, when the node is released, prop->value
+ * is kfreed so the memory must come from kmalloc.
+ */
+ prop->value = kmalloc(1, GFP_KERNEL);
+ if (!prop->value)
+ goto out_err;
+ }
+
+ of_property_set_flag(prop, OF_DYNAMIC);
+
+ prop->length = length;
+
+ ret = of_changeset_add_property(ocs, np, prop);
+ if (!ret)
+ return 0;
+
+out_err:
+ kfree(prop->value);
+ kfree(prop->name);
+ kfree(prop);
+
+ return ret;
+}
+
+static struct device_node *of_pci_make_node(void)
+{
+ struct device_node *node;
+
+ node = kzalloc(sizeof(*node), GFP_KERNEL);
+ if (!node)
+ return NULL;
+
+ of_node_set_flag(node, OF_DYNAMIC);
+ of_node_set_flag(node, OF_DETACHED);
+ of_node_init(node);
+
+ return node;
+}
+
+static int of_pci_add_cells_props(struct device_node *node,
+ struct of_changeset *cs, int n_addr_cells,
+ int n_size_cells)
+{
+ __be32 val;
+ int ret;
+
+ ret = of_pci_add_property(cs, node, "ranges", NULL, 0);
+ if (ret)
+ return ret;
+
+ val = __cpu_to_be32(n_addr_cells);
+ ret = of_pci_add_property(cs, node, "#address-cells", &val,
+ sizeof(__be32));
+ if (ret)
+ return ret;
+
+ val = __cpu_to_be32(n_size_cells);
+ return of_pci_add_property(cs, node, "#size-cells", &val,
+ sizeof(__be32));
+}
+
+static int of_pci_add_pci_bus_props(struct device_node *node,
+ struct of_changeset *cs)
+{
+ int ret;
+
+ ret = of_pci_add_property(cs, node, "device_type", "pci",
+ strlen("pci") + 1);
+ if (ret)
+ return ret;
+
+ return of_pci_add_cells_props(node, cs, 3, 2);
+}
+
+static void of_pci_make_dev_node(struct pci_dev *dev)
+{
+ static struct of_changeset cs;
+ const char *pci_type = "dev";
+ struct device_node *node;
+ __be32 val[5] = {0};
+ int ret;
+
+ node = of_pci_make_node();
+ if (!node)
+ return;
+
+ node->parent = dev->bus->dev.of_node;
+ of_changeset_init(&cs);
+
+ if (pci_is_bridge(dev)) {
+ ret = of_pci_add_pci_bus_props(node, &cs);
+ if (ret)
+ goto changeset_destroy;
+ pci_type = "pci";
+ }
+
+ node->full_name = kasprintf(GFP_KERNEL, "%s@%x,%x", pci_type,
+ PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
+
+ val[0] = __cpu_to_be32(dev->devfn << 8);
+ val[4] = __cpu_to_be32(SZ_4K);
+ ret = of_pci_add_property(&cs, node, "reg", val, 5 * sizeof(__be32));
+ if (ret)
+ goto changeset_destroy;
+
+ ret = of_changeset_attach_node(&cs, node);
+ if (ret)
+ goto changeset_destroy;
+
+ ret = of_changeset_apply(&cs);
+ if (ret)
+ goto changeset_destroy;
+
+ dev->dev.of_node = node;
+
+ return;
+
+changeset_destroy:
+ of_changeset_destroy(&cs);
+ kfree(node);
+}
+
+static struct device_node *of_pci_create_root_bus_node(struct pci_bus *bus)
+{
+ static struct of_changeset cs;
+ struct device_node *node;
+ int ret;
+
+ node = of_pci_make_node();
+ if (!node)
+ return NULL;
+
+ node->full_name = "pci";
+ node->parent = of_root;
+
+ of_changeset_init(&cs);
+ ret = of_pci_add_pci_bus_props(node, &cs);
+ if (ret)
+ goto changeset_destroy;
+
+ ret = of_changeset_attach_node(&cs, node);
+ if (ret)
+ goto changeset_destroy;
+
+ ret = of_changeset_apply(&cs);
+ if (ret)
+ goto changeset_destroy;
+
+ return node;
+
+changeset_destroy:
+ of_changeset_destroy(&cs);
+ kfree(node);
+
+ return NULL;
+}
+
void pci_set_of_node(struct pci_dev *dev)
{
if (!dev->bus->dev.of_node)
return;
dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
dev->devfn);
+ if (!dev->dev.of_node)
+ of_pci_make_dev_node(dev);
if (dev->dev.of_node)
dev->dev.fwnode = &dev->dev.of_node->fwnode;
}
@@ -39,6 +221,8 @@ void pci_set_bus_of_node(struct pci_bus *bus)
if (bus->self == NULL) {
node = pcibios_get_phb_of_node(bus);
+ if (!node)
+ node = of_pci_create_root_bus_node(bus);
} else {
node = of_node_get(bus->self->dev.of_node);
if (node && of_property_read_bool(node, "external-facing"))
--
2.34.1
next prev parent reply other threads:[~2022-04-27 11:02 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-27 9:44 [PATCH 0/3] add dynamic PCI device of_node creation for overlay Clément Léger
2022-04-27 9:45 ` [PATCH 1/3] of: always populate a root node Clément Léger
2022-05-03 13:45 ` Rob Herring
2022-05-03 15:38 ` Clément Léger
2022-05-03 17:22 ` Frank Rowand
2022-05-17 3:11 ` Frank Rowand
2022-05-17 7:37 ` Clément Léger
2022-05-17 15:03 ` Frank Rowand
2022-05-18 10:03 ` Clément Léger
2022-04-27 9:45 ` Clément Léger [this message]
2022-04-27 17:37 ` [PATCH 2/3] PCI: of: create DT nodes for PCI devices if they do not exists kernel test robot
2022-04-27 17:47 ` kernel test robot
2022-05-03 14:12 ` Rob Herring
2022-05-03 16:05 ` Clément Léger
2022-05-03 22:53 ` Bjorn Helgaas
2022-05-04 13:43 ` Clément Léger
2022-05-18 19:22 ` Lizhi Hou
2022-04-27 9:45 ` [PATCH 3/3] of: overlay: add of_overlay_fdt_apply_to_node() Clément Léger
2022-05-06 18:33 ` [PATCH 0/3] add dynamic PCI device of_node creation for overlay Frank Rowand
2022-05-09 12:16 ` Clément Léger
2022-05-09 15:56 ` Frank Rowand
2022-05-09 16:09 ` Clément Léger
2022-05-09 17:00 ` Andy Shevchenko
2022-05-09 20:11 ` Frank Rowand
2022-05-09 20:40 ` Andy Shevchenko
2022-05-10 7:22 ` Christoph Hellwig
2022-05-09 20:07 ` Frank Rowand
2022-05-10 7:20 ` Clément Léger
2022-05-09 18:36 ` Rob Herring
2022-05-09 20:35 ` Frank Rowand
2022-05-10 14:43 ` Rob Herring
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=20220427094502.456111-3-clement.leger@bootlin.com \
--to=clement.leger@bootlin.com \
--cc=alexandre.belloni@bootlin.com \
--cc=allan.nielsen@microchip.com \
--cc=andrew@lunn.ch \
--cc=andriy.shevchenko@linux.intel.com \
--cc=bhelgaas@google.com \
--cc=broonie@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=frowand.list@gmail.com \
--cc=hdegoede@redhat.com \
--cc=horatiu.vultur@microchip.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=pantelis.antoniou@konsulko.com \
--cc=robh+dt@kernel.org \
--cc=robh@kernel.org \
--cc=steen.hegelund@microchip.com \
--cc=thomas.petazonni@bootlin.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;
as well as URLs for NNTP newsgroup(s).