[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YnE4Ni+QUJT/CXV4@robh.at.kernel.org>
Date: Tue, 3 May 2022 09:12:06 -0500
From: Rob Herring <robh@...nel.org>
To: Clément Léger <clement.leger@...tlin.com>
Cc: Frank Rowand <frowand.list@...il.com>,
Pantelis Antoniou <pantelis.antoniou@...sulko.com>,
Bjorn Helgaas <bhelgaas@...gle.com>,
Allan Nielsen <allan.nielsen@...rochip.com>,
Horatiu Vultur <horatiu.vultur@...rochip.com>,
Steen Hegelund <steen.hegelund@...rochip.com>,
Thomas Petazzoni <thomas.petazonni@...tlin.com>,
Alexandre Belloni <alexandre.belloni@...tlin.com>,
Mark Brown <broonie@...nel.org>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Jakub Kicinski <kuba@...nel.org>,
Hans de Goede <hdegoede@...hat.com>,
Andrew Lunn <andrew@...n.ch>, devicetree@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-pci@...r.kernel.org
Subject: Re: [PATCH 2/3] PCI: of: create DT nodes for PCI devices if they do
not exists
On Wed, Apr 27, 2022 at 11:45:01AM +0200, Clément Léger wrote:
> 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.
>
While I implemented this creating the nodes as the PCI devices are
created, I think probably we're going to want to create the device node
and any needed parent nodes on demand. Otherwise, just turning on
CONFIG_OF could break platforms.
One potential issue is that fwnode assumes there is either a DT node or
ACPI node. With this, we have the potential for both. I'm not sure how
much that's going to be an issue.
> Co-developed-by: Rob Herring <robh@...nel.org>
> Signed-off-by: Rob Herring <robh@...nel.org>
> Signed-off-by: Clément Léger <clement.leger@...tlin.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)
Nothing really PCI specific about this function.
The kernel support for creating nodes and properties is pretty poor. We
should improve it with functions like this (in drivers/of/). Maybe the
changeset part should be separate though. We have some cases of creating
properties or nodes already, and whatever new APIs we make those
cases should be able to use them. And if they are converted, then it can
be merged sooner rather than when all the PCI parts are ready.
> +{
> + 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)
> +{
This isn't PCI specific either.
> + 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);
The host bridge node is going to need to fill in 'ranges'. Empty ranges
is not valid when there's a change in number of cells.
The root node also will need "#address-cells" and "#size-cells".
> + 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
>
>
Powered by blists - more mailing lists