lkml.org 
[lkml]   [2022]   [May]   [3]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [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@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)

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
>
>

\
 
 \ /
  Last update: 2022-05-03 16:12    [W:0.314 / U:0.228 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site