2022-10-12 18:18:55

by Lizhi Hou

[permalink] [raw]
Subject: [PATCH RFC V2 0/3] Generate device tree node for pci devices

This patch series introduces OF overlay support for PCI devices which
primarily addresses two use cases. First, it provides a data driven method
to describe hardware peripherals that are present in a PCI endpoint and
hence can be accessed by the PCI host. Second, it allows reuse of a OF
compatible driver -- often used in SoC platforms -- in a PCI host based
system.

There are 2 series devices rely on this patch:

1) Xilinx Alveo Accelerator cards (FPGA based device)
2) Microchip LAN9662 Ethernet Controller

Please see: https://lore.kernel.org/lkml/[email protected]/

Normally, the PCI core discovers PCI devices and their BARs using the
PCI enumeration process. However, the process does not provide a way to
discover the hardware peripherals that are present in a PCI device, and
which can be accessed through the PCI BARs. Also, the enumeration process
does not provide a way to associate MSI-X vectors of a PCI device with the
hardware peripherals that are present in the device. PCI device drivers
often use header files to describe the hardware peripherals and their
resources as there is no standard data driven way to do so. This patch
series proposes to use flattened device tree blob to describe the
peripherals in a data driven way. Based on previous discussion, using
device tree overlay is the best way to unflatten the blob and populate
platform devices. To use device tree overlay, there are three obvious
problems that need to be resolved.

First, we need to create a base tree for non-DT system such as x86_64. A
patch series has been submitted for this:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

Second, a device tree node corresponding to the PCI endpoint is required
for overlaying the flattened device tree blob for that PCI endpoint.
Because PCI is a self-discoverable bus, a device tree node is usually not
created for PCI devices. This series adds support to generate a device
tree node for a PCI device which advertises itself using PCI quirks
infrastructure.

Third, we need to generate device tree nodes for PCI bridges since a child
PCI endpoint may choose to have a device tree node created.

This patch series is made up of three patches.

The first patch is adding OF interface to create or destroy OF node
dynamically.

The second patch introduces a kernel option, CONFIG_DYNAMIC_PCI_OF_NODEX.
When the option is turned on, the kernel will generate device tree nodes
for all PCI bridges unconditionally. The patch also shows how to use the
PCI quirks infrastructure, DECLARE_PCI_FIXUP_FINAL to generate a device
tree node for a device. Specifically, the patch generates a device tree
node for Xilinx Alveo U50 PCIe accelerator device. The generated device
tree nodes do not have any property.

The third patch adds basic properties ('reg', 'compatible' and
'device_type') to the dynamically generated device tree nodes. More
properties can be added in the future.

Here is the example of device tree nodes generated within the ARM64 QEMU.
# lspci -t
-[0000:00]-+-00.0
+-01.0-[01]--
+-01.1-[02]----00.0
+-01.2-[03]----00.0
+-01.3-[04]----00.0
+-01.4-[05]----00.0
+-01.5-[06]--
+-01.6-[07]--
+-01.7-[08]--
+-02.0-[09-0b]----00.0-[0a-0b]----00.0-[0b]--+-00.0
| \-00.1
+-02.1-[0c]--
\-03.0-[0d-0e]----00.0-[0e]----01.0

# tree /sys/firmware/devicetree/base/pcie\@10000000/
/sys/firmware/devicetree/base/pcie@10000000/
|-- #address-cells
|-- #interrupt-cells
|-- #size-cells
|-- bus-range
|-- compatible
|-- device_type
|-- dma-coherent
|-- interrupt-map
|-- interrupt-map-mask
|-- linux,pci-domain
|-- msi-parent
|-- name
|-- pci@1,0
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@1,1
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@1,2
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@1,3
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@1,4
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@1,5
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@1,6
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@1,7
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@2,0
| |-- compatible
| |-- device_type
| |-- pci@0,0
| | |-- compatible
| | |-- device_type
| | |-- pci@0,0
| | | |-- compatible
| | | |-- dev@0,0
| | | | |-- compatible
| | | | `-- reg
| | | |-- dev@0,1
| | | | |-- compatible
| | | | `-- reg
| | | |-- device_type
| | | `-- reg
| | `-- reg
| `-- reg
|-- pci@2,1
| |-- compatible
| |-- device_type
| `-- reg
|-- pci@3,0
| |-- compatible
| |-- device_type
| |-- pci@0,0
| | |-- compatible
| | |-- device_type
| | `-- reg
| `-- reg
|-- ranges
`-- reg

Changes since RFC v1:
- Added one patch to create basic properties.
- To move DT related code out of PCI subsystem, replaced of_node_alloc()
with of_create_node()/of_destroy_node()

Lizhi Hou (3):
of: dynamic: Add of_create_node() and of_destroy_node()
PCI: Create device tree node for selected devices
PCI: Add basic properties for dynamically generated PCI OF node

drivers/of/dynamic.c | 80 ++++++++++++++++
drivers/pci/Kconfig | 12 +++
drivers/pci/Makefile | 1 +
drivers/pci/bus.c | 2 +
drivers/pci/msi/irqdomain.c | 6 +-
drivers/pci/of.c | 69 ++++++++++++++
drivers/pci/of_property.c | 177 ++++++++++++++++++++++++++++++++++++
drivers/pci/pci-driver.c | 3 +-
drivers/pci/pci.h | 17 ++++
drivers/pci/quirks.c | 11 +++
drivers/pci/remove.c | 1 +
include/linux/of.h | 4 +
12 files changed, 380 insertions(+), 3 deletions(-)
create mode 100644 drivers/pci/of_property.c

--
2.17.1


2022-10-12 18:23:04

by Lizhi Hou

[permalink] [raw]
Subject: [PATCH RFC V2 2/3] PCI: Create device tree node for selected devices

The PCIe endpoint device such as Xilinx Alveo PCIe card maps the register
spaces from multiple hardware peripherals to its PCIe BAR. Normally,
the PCI core discovers devices and BARs using the PCI enumeration process.
There is no infrastructure to discover the hardware peripherals that are
present in a PCI device, and which can be accessed through the PCI BARs.

For Alveo PCIe card, the card firmware provides a flattened device tree to
describe the hardware peripherals on its BARs. The Alveo card driver can
load this flattened device tree and leverage device tree framework to
generate platform devices for the hardware peripherals eventually.

Apparently, the device tree framework requires a device tree node for the
PCIe device. Thus, it can generate the device tree nodes for hardware
peripherals underneath. Because PCIe is self discoverable bus, there might
not be a device tree node created for PCIe devices. This patch is to add
support to generate device tree node for PCIe devices. It introduces a
kernel option. When the option is turned on, the kernel will generate
device tree nodes for PCI bridges unconditionally. It will also generate
a device tree node for Xilinx Alveo U50 by using PCI quirks. The generated
device tree nodes do not have any property. The future patches will add
necessary properties.

Signed-off-by: Lizhi Hou <[email protected]>
Signed-off-by: Sonal Santan <[email protected]>
Signed-off-by: Max Zhen <[email protected]>
Signed-off-by: Brian Xu <[email protected]>
---
drivers/pci/Kconfig | 12 ++++++++
drivers/pci/bus.c | 2 ++
drivers/pci/msi/irqdomain.c | 6 ++--
drivers/pci/of.c | 61 +++++++++++++++++++++++++++++++++++++
drivers/pci/pci-driver.c | 3 +-
drivers/pci/pci.h | 16 ++++++++++
drivers/pci/quirks.c | 11 +++++++
drivers/pci/remove.c | 1 +
8 files changed, 109 insertions(+), 3 deletions(-)

diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig
index 55c028af4bd9..8243a92fd97d 100644
--- a/drivers/pci/Kconfig
+++ b/drivers/pci/Kconfig
@@ -198,6 +198,18 @@ config PCI_HYPERV
The PCI device frontend driver allows the kernel to import arbitrary
PCI devices from a PCI backend to support PCI driver domains.

+config PCI_DYNAMIC_OF_NODES
+ bool "Device tree node for PCI devices"
+ depends on OF
+ select OF_DYNAMIC
+ help
+ This option enables support for generating device tree nodes for some
+ PCI devices. Thus, the driver of this kind can load and overlay
+ flattened device tree for its downstream devices.
+
+ Once this option is selected, the device tree nodes will be generated
+ for all PCI/PCIE bridges.
+
choice
prompt "PCI Express hierarchy optimization setting"
default PCIE_BUS_DEFAULT
diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index 3cef835b375f..8507cc32b61d 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -316,6 +316,8 @@ void pci_bus_add_device(struct pci_dev *dev)
*/
pcibios_bus_add_device(dev);
pci_fixup_device(pci_fixup_final, dev);
+ if (pci_is_bridge(dev))
+ of_pci_make_dev_node(dev);
pci_create_sysfs_dev_files(dev);
pci_proc_attach_device(dev);
pci_bridge_d3_update(dev);
diff --git a/drivers/pci/msi/irqdomain.c b/drivers/pci/msi/irqdomain.c
index e9cf318e6670..eeaf44169bfd 100644
--- a/drivers/pci/msi/irqdomain.c
+++ b/drivers/pci/msi/irqdomain.c
@@ -230,8 +230,10 @@ u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);

of_node = irq_domain_get_of_node(domain);
- rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
- iort_msi_map_id(&pdev->dev, rid);
+ if (of_node && !of_node_check_flag(of_node, OF_DYNAMIC))
+ rid = of_msi_map_id(&pdev->dev, of_node, rid);
+ else
+ rid = iort_msi_map_id(&pdev->dev, rid);

return rid;
}
diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index 196834ed44fe..83e042f495a6 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -469,6 +469,8 @@ static int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *
} else {
/* We found a P2P bridge, check if it has a node */
ppnode = pci_device_to_OF_node(ppdev);
+ if (of_node_check_flag(ppnode, OF_DYNAMIC))
+ ppnode = NULL;
}

/*
@@ -599,6 +601,65 @@ int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
return pci_parse_request_of_pci_ranges(dev, bridge);
}

+#if IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES)
+
+void of_pci_remove_node(struct pci_dev *pdev)
+{
+ struct device_node *dt_node;
+
+ dt_node = pci_device_to_OF_node(pdev);
+ if (!dt_node || !of_node_check_flag(dt_node, OF_DYNAMIC))
+ return;
+ pdev->dev.of_node = NULL;
+
+ of_destroy_node(dt_node);
+}
+
+void of_pci_make_dev_node(struct pci_dev *pdev)
+{
+ struct device_node *parent, *dt_node;
+ const char *pci_type = "dev";
+ char *full_name;
+
+ /*
+ * if there is already a device tree node linked to this device,
+ * return immediately.
+ */
+ if (pci_device_to_OF_node(pdev))
+ return;
+
+ /* check if there is device tree node for parent device */
+ if (!pdev->bus->self)
+ parent = pdev->bus->dev.of_node;
+ else
+ parent = pdev->bus->self->dev.of_node;
+ if (!parent)
+ return;
+
+ if (pci_is_bridge(pdev))
+ pci_type = "pci";
+
+ full_name = kasprintf(GFP_KERNEL, "%s@%x,%x", pci_type,
+ PCI_SLOT(pdev->devfn),
+ PCI_FUNC(pdev->devfn));
+ if (!full_name)
+ goto failed;
+
+ dt_node = of_create_node(parent, full_name, NULL);
+ if (!dt_node)
+ goto failed;
+
+ kfree(full_name);
+
+ pdev->dev.of_node = dt_node;
+
+ return;
+
+failed:
+ kfree(full_name);
+}
+#endif
+
#endif /* CONFIG_PCI */

/**
diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c
index 49238ddd39ee..1540c4c9a770 100644
--- a/drivers/pci/pci-driver.c
+++ b/drivers/pci/pci-driver.c
@@ -1628,7 +1628,8 @@ static int pci_dma_configure(struct device *dev)
bridge = pci_get_host_bridge_device(to_pci_dev(dev));

if (IS_ENABLED(CONFIG_OF) && bridge->parent &&
- bridge->parent->of_node) {
+ bridge->parent->of_node &&
+ !of_node_check_flag(bridge->parent->of_node, OF_DYNAMIC)) {
ret = of_dma_configure(dev, bridge->parent->of_node, true);
} else if (has_acpi_companion(bridge)) {
struct acpi_device *adev = to_acpi_device_node(bridge->fwnode);
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 785f31086313..e0a11497b1ad 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -678,6 +678,22 @@ static inline int devm_of_pci_bridge_init(struct device *dev, struct pci_host_br

#endif /* CONFIG_OF */

+#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);
+
+#else
+static inline void
+of_pci_make_dev_node(struct pci_dev *pdev)
+{
+}
+
+static inline void
+of_pci_remove_node(struct pci_dev *pdev)
+{
+}
+#endif /* CONFIG_OF_DYNAMIC */
+
#ifdef CONFIG_PCIEAER
void pci_no_aer(void);
void pci_aer_init(struct pci_dev *dev);
diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
index 4944798e75b5..5d76932f59ec 100644
--- a/drivers/pci/quirks.c
+++ b/drivers/pci/quirks.c
@@ -5956,3 +5956,14 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x56b1, aspm_l1_acceptable_latency
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x56c0, aspm_l1_acceptable_latency);
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x56c1, aspm_l1_acceptable_latency);
#endif
+
+/*
+ * For PCI device which have multiple downstream devices, its driver may use
+ * a flattened device tree to describe the downstream devices.
+ * To overlay the flattened device tree, the PCI device and all its ancestor
+ * devices need to have device tree nodes on system base device tree. Thus,
+ * before driver probing, it might need to add a device tree node as the final
+ * fixup.
+ */
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_XILINX, 0x5020, of_pci_make_dev_node);
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_XILINX, 0x5021, of_pci_make_dev_node);
diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c
index 4c54c75050dc..0eaa9d9a3609 100644
--- a/drivers/pci/remove.c
+++ b/drivers/pci/remove.c
@@ -23,6 +23,7 @@ static void pci_stop_dev(struct pci_dev *dev)
device_release_driver(&dev->dev);
pci_proc_detach_device(dev);
pci_remove_sysfs_dev_files(dev);
+ of_pci_remove_node(dev);

pci_dev_assign_added(dev, false);
}
--
2.17.1

2022-10-12 18:55:58

by Lizhi Hou

[permalink] [raw]
Subject: [PATCH RFC V2 3/3] PCI: Add basic properties for dynamically generated PCI OF node

This patch addes 'reg', 'compatible' and 'device_typ' properties for
dynamically generated PCI device tree node

Signed-off-by: Lizhi Hou <[email protected]>
Signed-off-by: Sonal Santan <[email protected]>
Signed-off-by: Max Zhen <[email protected]>
Signed-off-by: Brian Xu <[email protected]>
---
drivers/pci/Makefile | 1 +
drivers/pci/of.c | 10 ++-
drivers/pci/of_property.c | 177 ++++++++++++++++++++++++++++++++++++++
drivers/pci/pci.h | 3 +-
4 files changed, 189 insertions(+), 2 deletions(-)
create mode 100644 drivers/pci/of_property.c

diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
index 2680e4c92f0a..cc8b4e01e29d 100644
--- a/drivers/pci/Makefile
+++ b/drivers/pci/Makefile
@@ -32,6 +32,7 @@ obj-$(CONFIG_PCI_P2PDMA) += p2pdma.o
obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o
obj-$(CONFIG_VGA_ARB) += vgaarb.o
obj-$(CONFIG_PCI_DOE) += doe.o
+obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o

# Endpoint library must be initialized before its users
obj-$(CONFIG_PCI_ENDPOINT) += endpoint/
diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index 83e042f495a6..00d716589660 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -619,6 +619,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
{
struct device_node *parent, *dt_node;
const char *pci_type = "dev";
+ struct property *props;
char *full_name;

/*
@@ -645,10 +646,15 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
if (!full_name)
goto failed;

- dt_node = of_create_node(parent, full_name, NULL);
+ props = of_pci_props_create(pdev);
+ if (!props)
+ goto failed;
+
+ dt_node = of_create_node(parent, full_name, props);
if (!dt_node)
goto failed;

+ of_pci_props_destroy(props);
kfree(full_name);

pdev->dev.of_node = dt_node;
@@ -656,6 +662,8 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
return;

failed:
+ if (props)
+ of_pci_props_destroy(props);
kfree(full_name);
}
#endif
diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c
new file mode 100644
index 000000000000..693a08323aa4
--- /dev/null
+++ b/drivers/pci/of_property.c
@@ -0,0 +1,177 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2022, Advanced Micro Devices, Inc.
+ */
+
+#include <linux/pci.h>
+#include <linux/of.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+
+struct of_pci_prop {
+ char *name;
+ int (*prop_val)(struct pci_dev *pdev, void **val, u32 *len);
+};
+
+struct of_pci_addr_pair {
+ __be32 phys_hi;
+ __be32 phys_mid;
+ __be32 phys_lo;
+ __be32 size_hi;
+ __be32 size_lo;
+};
+
+#define OF_PCI_ADDR_SPACE_CONFIG 0x0
+#define OF_PCI_ADDR_SPACE_IO 0x1
+#define OF_PCI_ADDR_SPACE_MEM32 0x2
+#define OF_PCI_ADDR_SPACE_MEM64 0x3
+
+#define OF_PCI_ADDR_FIELD_SS GENMASK(25, 24)
+#define OF_PCI_ADDR_FIELD_PREFETCH BIT(30)
+#define OF_PCI_ADDR_FIELD_BUS GENMASK(23, 16)
+#define OF_PCI_ADDR_FIELD_DEV GENMASK(15, 11)
+#define OF_PCI_ADDR_FIELD_FUNC GENMASK(10, 8)
+#define OF_PCI_ADDR_FIELD_REG GENMASK(7, 0)
+
+#define OF_PCI_SIZE_HI GENMASK_ULL(63, 32)
+#define OF_PCI_SIZE_LO GENMASK_ULL(31, 0)
+
+#define OF_PCI_PROP_COMPAT_LEN_MAX 256
+static int of_pci_prop_device_type(struct pci_dev *pdev, void **val, u32 *len)
+{
+ if (!pci_is_bridge(pdev))
+ return 0;
+
+ *val = kasprintf(GFP_KERNEL, "pci");
+ if (!*val)
+ return -ENOMEM;
+
+ *len = strlen(*val) + 1;
+
+ return 0;
+}
+
+static int of_pci_prop_reg(struct pci_dev *pdev, void **val, u32 *len)
+{
+ struct of_pci_addr_pair *reg;
+ u32 reg_val, base_addr, ss;
+ resource_size_t sz;
+ int i = 1, resno;
+
+ reg = kzalloc(sizeof(*reg) * (PCI_STD_NUM_BARS + 1), GFP_KERNEL);
+ if (!reg)
+ return -ENOMEM;
+
+ reg_val = FIELD_PREP(OF_PCI_ADDR_FIELD_SS, OF_PCI_ADDR_SPACE_CONFIG) |
+ FIELD_PREP(OF_PCI_ADDR_FIELD_BUS, pdev->bus->number) |
+ FIELD_PREP(OF_PCI_ADDR_FIELD_DEV, PCI_SLOT(pdev->devfn)) |
+ FIELD_PREP(OF_PCI_ADDR_FIELD_FUNC, PCI_FUNC(pdev->devfn));
+ reg[0].phys_hi = cpu_to_be32(reg_val);
+
+ base_addr = PCI_BASE_ADDRESS_0;
+ for (resno = PCI_STD_RESOURCES; resno <= PCI_STD_RESOURCE_END;
+ resno++, base_addr += 4) {
+ sz = pci_resource_len(pdev, resno);
+ if (!sz)
+ continue;
+
+ if (pci_resource_flags(pdev, resno) & IORESOURCE_IO)
+ ss = OF_PCI_ADDR_SPACE_IO;
+ else if (pci_resource_flags(pdev, resno) & IORESOURCE_MEM_64)
+ ss = OF_PCI_ADDR_SPACE_MEM64;
+ else
+ ss = OF_PCI_ADDR_SPACE_MEM32;
+
+ reg_val &= ~(OF_PCI_ADDR_FIELD_SS | OF_PCI_ADDR_FIELD_PREFETCH |
+ OF_PCI_ADDR_FIELD_REG);
+ reg_val |= FIELD_PREP(OF_PCI_ADDR_FIELD_SS, ss) |
+ FIELD_PREP(OF_PCI_ADDR_FIELD_REG, base_addr);
+ if (pci_resource_flags(pdev, resno) & IORESOURCE_PREFETCH)
+ reg_val |= OF_PCI_ADDR_FIELD_PREFETCH;
+ reg[i].phys_hi = cpu_to_be32(reg_val);
+ reg[i].size_hi = cpu_to_be32(FIELD_GET(OF_PCI_SIZE_HI, sz));
+ reg[i].size_lo = cpu_to_be32(FIELD_GET(OF_PCI_SIZE_LO, sz));
+ i++;
+ }
+
+ *val = reg;
+ *len = i * sizeof(*reg);
+
+ return 0;
+}
+
+static int of_pci_prop_compatible(struct pci_dev *pdev, void **val, u32 *len)
+{
+ char *compat;
+
+ compat = kzalloc(OF_PCI_PROP_COMPAT_LEN_MAX, GFP_KERNEL);
+ if (!compat)
+ return -ENOMEM;
+
+ *val = compat;
+ if (pdev->subsystem_vendor) {
+ compat += sprintf(compat, "pci%x,%x.%x.%x.%x",
+ pdev->vendor, pdev->device,
+ pdev->subsystem_vendor,
+ pdev->subsystem_device,
+ pdev->revision) + 1;
+ compat += sprintf(compat, "pci%x,%x.%x.%x",
+ pdev->vendor, pdev->device,
+ pdev->subsystem_vendor,
+ pdev->subsystem_device) + 1;
+ compat += sprintf(compat, "pci%x,%x",
+ pdev->subsystem_vendor,
+ pdev->subsystem_device) + 1;
+ }
+ compat += sprintf(compat, "pci%x,%x.%x",
+ pdev->vendor, pdev->device, pdev->revision) + 1;
+ compat += sprintf(compat, "pci%x,%x", pdev->vendor, pdev->device) + 1;
+ compat += sprintf(compat, "pciclass,%06x", pdev->class) + 1;
+ compat += sprintf(compat, "pciclass,%04x", pdev->class >> 8) + 1;
+
+ *len = (u32)(compat - (char *)*val);
+
+ return 0;
+}
+
+struct of_pci_prop of_pci_props[] = {
+ { .name = "device_type", .prop_val = of_pci_prop_device_type },
+ { .name = "reg", .prop_val = of_pci_prop_reg },
+ { .name = "compatible", .prop_val = of_pci_prop_compatible },
+ {},
+};
+
+struct property *of_pci_props_create(struct pci_dev *pdev)
+{
+ struct property *props, *pp;
+ void *val;
+ u32 len;
+ int i;
+
+ props = kcalloc(ARRAY_SIZE(of_pci_props), sizeof(*props), GFP_KERNEL);
+ if (!props)
+ return NULL;
+
+ pp = props;
+ for (i = 0; of_pci_props[i].name; i++) {
+ len = 0;
+ of_pci_props[i].prop_val(pdev, &val, &len);
+ if (!len)
+ continue;
+ props->name = of_pci_props[i].name;
+ props->value = val;
+ props->length = len;
+ props++;
+ }
+
+ return pp;
+}
+
+void of_pci_props_destroy(struct property *props)
+{
+ int i;
+
+ for (i = 0; props[i].name; i++)
+ kfree(props[i].value);
+ kfree(props);
+}
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index e0a11497b1ad..37841241ceea 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -681,7 +681,8 @@ static inline int devm_of_pci_bridge_init(struct device *dev, struct pci_host_br
#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);
-
+struct property *of_pci_props_create(struct pci_dev *pdev);
+void of_pci_props_destroy(struct property *props);
#else
static inline void
of_pci_make_dev_node(struct pci_dev *pdev)
--
2.17.1

2022-10-12 18:56:21

by Lizhi Hou

[permalink] [raw]
Subject: [PATCH RFC V2 1/3] of: dynamic: Add of_create_node() and of_destroy_node()

of_create_node() creates device node and apply to base tree dynamically.
The parent device node and full name are required for creating the node.
And the caller can also provide a property array for the node.

Inside this function, it creates a changeset. Then the new device node
and properties are added to the changeset and applied to base tree. The
pointer of this changeset is saved in device node private data.

of_destroy_node() removes the node created by of_create_node() from the
base tree and free it. It gets the changeset pointer from device node
private data and call of_changeset_destroy() to free everything.

Signed-off-by: Lizhi Hou <[email protected]>
Signed-off-by: Sonal Santan <[email protected]>
Signed-off-by: Max Zhen <[email protected]>
Signed-off-by: Brian Xu <[email protected]>
---
drivers/of/dynamic.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
include/linux/of.h | 4 +++
2 files changed, 84 insertions(+)

diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index cd3821a6444f..eca28b723706 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -934,3 +934,83 @@ int of_changeset_action(struct of_changeset *ocs, unsigned long action,
return 0;
}
EXPORT_SYMBOL_GPL(of_changeset_action);
+
+/**
+ * of_create_node - Dynamically create a device node and apply it to base tree
+ *
+ * @parent: Pointer to parent device node
+ * @full_name: Full name of device node
+ * @props: Pointer to property array
+ *
+ * Return: Pointer to the created device node or NULL in case of an error.
+ */
+struct device_node *of_create_node(struct device_node *parent,
+ const char *full_name,
+ struct property *props)
+{
+ struct of_changeset *cset;
+ struct property *new_pp;
+ struct device_node *np;
+ int ret, i;
+
+ cset = kzalloc(sizeof(*cset), GFP_KERNEL);
+ if (!cset)
+ return NULL;
+
+ of_changeset_init(cset);
+
+ np = __of_node_dup(NULL, full_name);
+ if (!np)
+ goto failed;
+ np->parent = parent;
+
+ ret = of_changeset_attach_node(cset, np);
+ if (ret)
+ goto failed;
+
+ if (props) {
+ for (i = 0; props[i].name; i++) {
+ new_pp = __of_prop_dup(&props[i], GFP_KERNEL);
+ if (!new_pp)
+ goto failed;
+ ret = of_changeset_add_property(cset, np, new_pp);
+ if (ret) {
+ kfree(new_pp->name);
+ kfree(new_pp->value);
+ kfree(new_pp);
+ goto failed;
+ }
+ }
+ }
+
+ ret = of_changeset_apply(cset);
+ if (ret)
+ goto failed;
+
+ np->data = cset;
+
+ return np;
+
+failed:
+ of_changeset_destroy(cset);
+ if (np)
+ of_node_put(np);
+
+ return NULL;
+}
+
+/**
+ * of_destroy_node - Destroy a dynamically created device node
+ *
+ * @np: Pointer to dynamically created device node
+ *
+ */
+void of_destroy_node(struct device_node *np)
+{
+ struct of_changeset *cset;
+
+ cset = (struct of_changeset *)np->data;
+ of_changeset_destroy(cset);
+ of_node_put(np);
+ kfree(cset);
+}
diff --git a/include/linux/of.h b/include/linux/of.h
index 766d002bddb9..493ef957c1a8 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1475,6 +1475,10 @@ extern int of_changeset_revert(struct of_changeset *ocs);
extern int of_changeset_action(struct of_changeset *ocs,
unsigned long action, struct device_node *np,
struct property *prop);
+struct device_node *of_create_node(struct device_node *parent,
+ const char *full_name,
+ struct property *props);
+void of_destroy_node(struct device_node *np);

static inline int of_changeset_attach_node(struct of_changeset *ocs,
struct device_node *np)
--
2.17.1

2022-10-26 19:44:42

by Rob Herring (Arm)

[permalink] [raw]
Subject: Re: [PATCH RFC V2 3/3] PCI: Add basic properties for dynamically generated PCI OF node

On Wed, Oct 12, 2022 at 11:14:00AM -0700, Lizhi Hou wrote:
> This patch addes 'reg', 'compatible' and 'device_typ' properties for

typo

Please read submitting-patches.rst and what it says about 'This patch'.

> dynamically generated PCI device tree node
>
> Signed-off-by: Lizhi Hou <[email protected]>
> Signed-off-by: Sonal Santan <[email protected]>
> Signed-off-by: Max Zhen <[email protected]>
> Signed-off-by: Brian Xu <[email protected]>
> ---
> drivers/pci/Makefile | 1 +
> drivers/pci/of.c | 10 ++-
> drivers/pci/of_property.c | 177 ++++++++++++++++++++++++++++++++++++++

I don't think we need a separate file here and patches 2 and 3 should be
combined. Patch 2 alone doesn't really make sense.


> drivers/pci/pci.h | 3 +-
> 4 files changed, 189 insertions(+), 2 deletions(-)
> create mode 100644 drivers/pci/of_property.c
>
> diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
> index 2680e4c92f0a..cc8b4e01e29d 100644
> --- a/drivers/pci/Makefile
> +++ b/drivers/pci/Makefile
> @@ -32,6 +32,7 @@ obj-$(CONFIG_PCI_P2PDMA) += p2pdma.o
> obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o
> obj-$(CONFIG_VGA_ARB) += vgaarb.o
> obj-$(CONFIG_PCI_DOE) += doe.o
> +obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o
>
> # Endpoint library must be initialized before its users
> obj-$(CONFIG_PCI_ENDPOINT) += endpoint/
> diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> index 83e042f495a6..00d716589660 100644
> --- a/drivers/pci/of.c
> +++ b/drivers/pci/of.c
> @@ -619,6 +619,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
> {
> struct device_node *parent, *dt_node;
> const char *pci_type = "dev";
> + struct property *props;
> char *full_name;
>
> /*
> @@ -645,10 +646,15 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
> if (!full_name)
> goto failed;
>
> - dt_node = of_create_node(parent, full_name, NULL);
> + props = of_pci_props_create(pdev);
> + if (!props)
> + goto failed;
> +
> + dt_node = of_create_node(parent, full_name, props);
> if (!dt_node)
> goto failed;
>
> + of_pci_props_destroy(props);
> kfree(full_name);
>
> pdev->dev.of_node = dt_node;
> @@ -656,6 +662,8 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
> return;
>
> failed:
> + if (props)
> + of_pci_props_destroy(props);
> kfree(full_name);
> }
> #endif
> diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c
> new file mode 100644
> index 000000000000..693a08323aa4
> --- /dev/null
> +++ b/drivers/pci/of_property.c
> @@ -0,0 +1,177 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (C) 2022, Advanced Micro Devices, Inc.
> + */
> +
> +#include <linux/pci.h>
> +#include <linux/of.h>
> +#include <linux/bitfield.h>
> +#include <linux/bits.h>
> +
> +struct of_pci_prop {
> + char *name;
> + int (*prop_val)(struct pci_dev *pdev, void **val, u32 *len);
> +};
> +
> +struct of_pci_addr_pair {
> + __be32 phys_hi;
> + __be32 phys_mid;
> + __be32 phys_lo;
> + __be32 size_hi;
> + __be32 size_lo;
> +};
> +
> +#define OF_PCI_ADDR_SPACE_CONFIG 0x0
> +#define OF_PCI_ADDR_SPACE_IO 0x1
> +#define OF_PCI_ADDR_SPACE_MEM32 0x2
> +#define OF_PCI_ADDR_SPACE_MEM64 0x3
> +
> +#define OF_PCI_ADDR_FIELD_SS GENMASK(25, 24)
> +#define OF_PCI_ADDR_FIELD_PREFETCH BIT(30)
> +#define OF_PCI_ADDR_FIELD_BUS GENMASK(23, 16)
> +#define OF_PCI_ADDR_FIELD_DEV GENMASK(15, 11)
> +#define OF_PCI_ADDR_FIELD_FUNC GENMASK(10, 8)
> +#define OF_PCI_ADDR_FIELD_REG GENMASK(7, 0)
> +
> +#define OF_PCI_SIZE_HI GENMASK_ULL(63, 32)
> +#define OF_PCI_SIZE_LO GENMASK_ULL(31, 0)
> +
> +#define OF_PCI_PROP_COMPAT_LEN_MAX 256
> +static int of_pci_prop_device_type(struct pci_dev *pdev, void **val, u32 *len)
> +{
> + if (!pci_is_bridge(pdev))
> + return 0;
> +
> + *val = kasprintf(GFP_KERNEL, "pci");
> + if (!*val)
> + return -ENOMEM;
> +
> + *len = strlen(*val) + 1;
> +
> + return 0;
> +}
> +
> +static int of_pci_prop_reg(struct pci_dev *pdev, void **val, u32 *len)
> +{
> + struct of_pci_addr_pair *reg;
> + u32 reg_val, base_addr, ss;
> + resource_size_t sz;
> + int i = 1, resno;
> +
> + reg = kzalloc(sizeof(*reg) * (PCI_STD_NUM_BARS + 1), GFP_KERNEL);
> + if (!reg)
> + return -ENOMEM;
> +
> + reg_val = FIELD_PREP(OF_PCI_ADDR_FIELD_SS, OF_PCI_ADDR_SPACE_CONFIG) |
> + FIELD_PREP(OF_PCI_ADDR_FIELD_BUS, pdev->bus->number) |
> + FIELD_PREP(OF_PCI_ADDR_FIELD_DEV, PCI_SLOT(pdev->devfn)) |
> + FIELD_PREP(OF_PCI_ADDR_FIELD_FUNC, PCI_FUNC(pdev->devfn));
> + reg[0].phys_hi = cpu_to_be32(reg_val);
> +
> + base_addr = PCI_BASE_ADDRESS_0;
> + for (resno = PCI_STD_RESOURCES; resno <= PCI_STD_RESOURCE_END;
> + resno++, base_addr += 4) {
> + sz = pci_resource_len(pdev, resno);
> + if (!sz)
> + continue;
> +
> + if (pci_resource_flags(pdev, resno) & IORESOURCE_IO)
> + ss = OF_PCI_ADDR_SPACE_IO;
> + else if (pci_resource_flags(pdev, resno) & IORESOURCE_MEM_64)
> + ss = OF_PCI_ADDR_SPACE_MEM64;
> + else
> + ss = OF_PCI_ADDR_SPACE_MEM32;
> +
> + reg_val &= ~(OF_PCI_ADDR_FIELD_SS | OF_PCI_ADDR_FIELD_PREFETCH |
> + OF_PCI_ADDR_FIELD_REG);
> + reg_val |= FIELD_PREP(OF_PCI_ADDR_FIELD_SS, ss) |
> + FIELD_PREP(OF_PCI_ADDR_FIELD_REG, base_addr);
> + if (pci_resource_flags(pdev, resno) & IORESOURCE_PREFETCH)
> + reg_val |= OF_PCI_ADDR_FIELD_PREFETCH;
> + reg[i].phys_hi = cpu_to_be32(reg_val);
> + reg[i].size_hi = cpu_to_be32(FIELD_GET(OF_PCI_SIZE_HI, sz));
> + reg[i].size_lo = cpu_to_be32(FIELD_GET(OF_PCI_SIZE_LO, sz));
> + i++;
> + }
> +
> + *val = reg;
> + *len = i * sizeof(*reg);
> +
> + return 0;
> +}
> +
> +static int of_pci_prop_compatible(struct pci_dev *pdev, void **val, u32 *len)
> +{
> + char *compat;
> +
> + compat = kzalloc(OF_PCI_PROP_COMPAT_LEN_MAX, GFP_KERNEL);

The size here looks pretty arbitrary yet we should be able to calculate
the worst case.

> + if (!compat)
> + return -ENOMEM;
> +
> + *val = compat;
> + if (pdev->subsystem_vendor) {
> + compat += sprintf(compat, "pci%x,%x.%x.%x.%x",
> + pdev->vendor, pdev->device,
> + pdev->subsystem_vendor,
> + pdev->subsystem_device,
> + pdev->revision) + 1;
> + compat += sprintf(compat, "pci%x,%x.%x.%x",
> + pdev->vendor, pdev->device,
> + pdev->subsystem_vendor,
> + pdev->subsystem_device) + 1;
> + compat += sprintf(compat, "pci%x,%x",
> + pdev->subsystem_vendor,
> + pdev->subsystem_device) + 1;
> + }
> + compat += sprintf(compat, "pci%x,%x.%x",
> + pdev->vendor, pdev->device, pdev->revision) + 1;
> + compat += sprintf(compat, "pci%x,%x", pdev->vendor, pdev->device) + 1;
> + compat += sprintf(compat, "pciclass,%06x", pdev->class) + 1;
> + compat += sprintf(compat, "pciclass,%04x", pdev->class >> 8) + 1;

No checking/preventing overrunning the compat buffer?

I don't think we need all these compatible strings. One with VID/PID and
one with the class should be sufficient. But I'm not sure offhand what
subsystem_vendor/device device is...

> +
> + *len = (u32)(compat - (char *)*val);
> +
> + return 0;
> +}
> +
> +struct of_pci_prop of_pci_props[] = {
> + { .name = "device_type", .prop_val = of_pci_prop_device_type },

This only only applies to bridge nodes.

> + { .name = "reg", .prop_val = of_pci_prop_reg },
> + { .name = "compatible", .prop_val = of_pci_prop_compatible },
> + {},
> +};
> +
> +struct property *of_pci_props_create(struct pci_dev *pdev)
> +{
> + struct property *props, *pp;
> + void *val;
> + u32 len;
> + int i;
> +
> + props = kcalloc(ARRAY_SIZE(of_pci_props), sizeof(*props), GFP_KERNEL);
> + if (!props)
> + return NULL;
> +
> + pp = props;
> + for (i = 0; of_pci_props[i].name; i++) {
> + len = 0;
> + of_pci_props[i].prop_val(pdev, &val, &len);
> + if (!len)
> + continue;
> + props->name = of_pci_props[i].name;
> + props->value = val;
> + props->length = len;
> + props++;

This creates an array of properties and then copies each one, and it
also exposes the internals of 'struct property' which we want to make
opaque. Neither of these is great.

I'd rather see the of_changeset API expanded to handle specific types of
properties. Something like this:

of_changeset_add_prop_string(cset, node, "device_type", "pci");
of_changeset_add_prop_string_array(cset, node, "compatible", compats, cnt);
of_changeset_add_prop_u32_array(cset, node, "reg", reg, cnt);

And perhaps these functions just wrap similar non-changeset functions
that produce a struct property.

IOW, it should be similar to the of_property_read_* APIs, but to
set/add rather than get.


You are also missing 'ranges', '#address-cells, and '#size-cells' in
bridge nodes.

Rob

2022-10-31 23:46:50

by Lizhi Hou

[permalink] [raw]
Subject: Re: [PATCH RFC V2 3/3] PCI: Add basic properties for dynamically generated PCI OF node


On 10/26/22 12:39, Rob Herring wrote:
> On Wed, Oct 12, 2022 at 11:14:00AM -0700, Lizhi Hou wrote:
>> This patch addes 'reg', 'compatible' and 'device_typ' properties for
> typo
>
> Please read submitting-patches.rst and what it says about 'This patch'.
Sure. And I will merge patch 2 and 3.
>
>> dynamically generated PCI device tree node
>>
>> Signed-off-by: Lizhi Hou <[email protected]>
>> Signed-off-by: Sonal Santan <[email protected]>
>> Signed-off-by: Max Zhen <[email protected]>
>> Signed-off-by: Brian Xu <[email protected]>
>> ---
>> drivers/pci/Makefile | 1 +
>> drivers/pci/of.c | 10 ++-
>> drivers/pci/of_property.c | 177 ++++++++++++++++++++++++++++++++++++++
> I don't think we need a separate file here and patches 2 and 3 should be
> combined. Patch 2 alone doesn't really make sense.
>
>
>> drivers/pci/pci.h | 3 +-
>> 4 files changed, 189 insertions(+), 2 deletions(-)
>> create mode 100644 drivers/pci/of_property.c
>>
>> diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
>> index 2680e4c92f0a..cc8b4e01e29d 100644
>> --- a/drivers/pci/Makefile
>> +++ b/drivers/pci/Makefile
>> @@ -32,6 +32,7 @@ obj-$(CONFIG_PCI_P2PDMA) += p2pdma.o
>> obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o
>> obj-$(CONFIG_VGA_ARB) += vgaarb.o
>> obj-$(CONFIG_PCI_DOE) += doe.o
>> +obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o
>>
>> # Endpoint library must be initialized before its users
>> obj-$(CONFIG_PCI_ENDPOINT) += endpoint/
>> diff --git a/drivers/pci/of.c b/drivers/pci/of.c
>> index 83e042f495a6..00d716589660 100644
>> --- a/drivers/pci/of.c
>> +++ b/drivers/pci/of.c
>> @@ -619,6 +619,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
>> {
>> struct device_node *parent, *dt_node;
>> const char *pci_type = "dev";
>> + struct property *props;
>> char *full_name;
>>
>> /*
>> @@ -645,10 +646,15 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
>> if (!full_name)
>> goto failed;
>>
>> - dt_node = of_create_node(parent, full_name, NULL);
>> + props = of_pci_props_create(pdev);
>> + if (!props)
>> + goto failed;
>> +
>> + dt_node = of_create_node(parent, full_name, props);
>> if (!dt_node)
>> goto failed;
>>
>> + of_pci_props_destroy(props);
>> kfree(full_name);
>>
>> pdev->dev.of_node = dt_node;
>> @@ -656,6 +662,8 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
>> return;
>>
>> failed:
>> + if (props)
>> + of_pci_props_destroy(props);
>> kfree(full_name);
>> }
>> #endif
>> diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c
>> new file mode 100644
>> index 000000000000..693a08323aa4
>> --- /dev/null
>> +++ b/drivers/pci/of_property.c
>> @@ -0,0 +1,177 @@
>> +// SPDX-License-Identifier: GPL-2.0+
>> +/*
>> + * Copyright (C) 2022, Advanced Micro Devices, Inc.
>> + */
>> +
>> +#include <linux/pci.h>
>> +#include <linux/of.h>
>> +#include <linux/bitfield.h>
>> +#include <linux/bits.h>
>> +
>> +struct of_pci_prop {
>> + char *name;
>> + int (*prop_val)(struct pci_dev *pdev, void **val, u32 *len);
>> +};
>> +
>> +struct of_pci_addr_pair {
>> + __be32 phys_hi;
>> + __be32 phys_mid;
>> + __be32 phys_lo;
>> + __be32 size_hi;
>> + __be32 size_lo;
>> +};
>> +
>> +#define OF_PCI_ADDR_SPACE_CONFIG 0x0
>> +#define OF_PCI_ADDR_SPACE_IO 0x1
>> +#define OF_PCI_ADDR_SPACE_MEM32 0x2
>> +#define OF_PCI_ADDR_SPACE_MEM64 0x3
>> +
>> +#define OF_PCI_ADDR_FIELD_SS GENMASK(25, 24)
>> +#define OF_PCI_ADDR_FIELD_PREFETCH BIT(30)
>> +#define OF_PCI_ADDR_FIELD_BUS GENMASK(23, 16)
>> +#define OF_PCI_ADDR_FIELD_DEV GENMASK(15, 11)
>> +#define OF_PCI_ADDR_FIELD_FUNC GENMASK(10, 8)
>> +#define OF_PCI_ADDR_FIELD_REG GENMASK(7, 0)
>> +
>> +#define OF_PCI_SIZE_HI GENMASK_ULL(63, 32)
>> +#define OF_PCI_SIZE_LO GENMASK_ULL(31, 0)
>> +
>> +#define OF_PCI_PROP_COMPAT_LEN_MAX 256
>> +static int of_pci_prop_device_type(struct pci_dev *pdev, void **val, u32 *len)
>> +{
>> + if (!pci_is_bridge(pdev))
>> + return 0;
>> +
>> + *val = kasprintf(GFP_KERNEL, "pci");
>> + if (!*val)
>> + return -ENOMEM;
>> +
>> + *len = strlen(*val) + 1;
>> +
>> + return 0;
>> +}
>> +
>> +static int of_pci_prop_reg(struct pci_dev *pdev, void **val, u32 *len)
>> +{
>> + struct of_pci_addr_pair *reg;
>> + u32 reg_val, base_addr, ss;
>> + resource_size_t sz;
>> + int i = 1, resno;
>> +
>> + reg = kzalloc(sizeof(*reg) * (PCI_STD_NUM_BARS + 1), GFP_KERNEL);
>> + if (!reg)
>> + return -ENOMEM;
>> +
>> + reg_val = FIELD_PREP(OF_PCI_ADDR_FIELD_SS, OF_PCI_ADDR_SPACE_CONFIG) |
>> + FIELD_PREP(OF_PCI_ADDR_FIELD_BUS, pdev->bus->number) |
>> + FIELD_PREP(OF_PCI_ADDR_FIELD_DEV, PCI_SLOT(pdev->devfn)) |
>> + FIELD_PREP(OF_PCI_ADDR_FIELD_FUNC, PCI_FUNC(pdev->devfn));
>> + reg[0].phys_hi = cpu_to_be32(reg_val);
>> +
>> + base_addr = PCI_BASE_ADDRESS_0;
>> + for (resno = PCI_STD_RESOURCES; resno <= PCI_STD_RESOURCE_END;
>> + resno++, base_addr += 4) {
>> + sz = pci_resource_len(pdev, resno);
>> + if (!sz)
>> + continue;
>> +
>> + if (pci_resource_flags(pdev, resno) & IORESOURCE_IO)
>> + ss = OF_PCI_ADDR_SPACE_IO;
>> + else if (pci_resource_flags(pdev, resno) & IORESOURCE_MEM_64)
>> + ss = OF_PCI_ADDR_SPACE_MEM64;
>> + else
>> + ss = OF_PCI_ADDR_SPACE_MEM32;
>> +
>> + reg_val &= ~(OF_PCI_ADDR_FIELD_SS | OF_PCI_ADDR_FIELD_PREFETCH |
>> + OF_PCI_ADDR_FIELD_REG);
>> + reg_val |= FIELD_PREP(OF_PCI_ADDR_FIELD_SS, ss) |
>> + FIELD_PREP(OF_PCI_ADDR_FIELD_REG, base_addr);
>> + if (pci_resource_flags(pdev, resno) & IORESOURCE_PREFETCH)
>> + reg_val |= OF_PCI_ADDR_FIELD_PREFETCH;
>> + reg[i].phys_hi = cpu_to_be32(reg_val);
>> + reg[i].size_hi = cpu_to_be32(FIELD_GET(OF_PCI_SIZE_HI, sz));
>> + reg[i].size_lo = cpu_to_be32(FIELD_GET(OF_PCI_SIZE_LO, sz));
>> + i++;
>> + }
>> +
>> + *val = reg;
>> + *len = i * sizeof(*reg);
>> +
>> + return 0;
>> +}
>> +
>> +static int of_pci_prop_compatible(struct pci_dev *pdev, void **val, u32 *len)
>> +{
>> + char *compat;
>> +
>> + compat = kzalloc(OF_PCI_PROP_COMPAT_LEN_MAX, GFP_KERNEL);
> The size here looks pretty arbitrary yet we should be able to calculate
> the worst case.
I will remove this.
>
>> + if (!compat)
>> + return -ENOMEM;
>> +
>> + *val = compat;
>> + if (pdev->subsystem_vendor) {
>> + compat += sprintf(compat, "pci%x,%x.%x.%x.%x",
>> + pdev->vendor, pdev->device,
>> + pdev->subsystem_vendor,
>> + pdev->subsystem_device,
>> + pdev->revision) + 1;
>> + compat += sprintf(compat, "pci%x,%x.%x.%x",
>> + pdev->vendor, pdev->device,
>> + pdev->subsystem_vendor,
>> + pdev->subsystem_device) + 1;
>> + compat += sprintf(compat, "pci%x,%x",
>> + pdev->subsystem_vendor,
>> + pdev->subsystem_device) + 1;
>> + }
>> + compat += sprintf(compat, "pci%x,%x.%x",
>> + pdev->vendor, pdev->device, pdev->revision) + 1;
>> + compat += sprintf(compat, "pci%x,%x", pdev->vendor, pdev->device) + 1;
>> + compat += sprintf(compat, "pciclass,%06x", pdev->class) + 1;
>> + compat += sprintf(compat, "pciclass,%04x", pdev->class >> 8) + 1;
> No checking/preventing overrunning the compat buffer?
>
> I don't think we need all these compatible strings. One with VID/PID and
> one with the class should be sufficient. But I'm not sure offhand what
> subsystem_vendor/device device is...
Ok. I will keep pci%x,%x, pciclass,%06x, pciclass,%04x.
>
>> +
>> + *len = (u32)(compat - (char *)*val);
>> +
>> + return 0;
>> +}
>> +
>> +struct of_pci_prop of_pci_props[] = {
>> + { .name = "device_type", .prop_val = of_pci_prop_device_type },
> This only only applies to bridge nodes.

In of_pci_prop_device_type, it returns immediately for pci endpoint.

To make it obvious, I will separate bridge and endpoint property arrays.

>
>> + { .name = "reg", .prop_val = of_pci_prop_reg },
>> + { .name = "compatible", .prop_val = of_pci_prop_compatible },
>> + {},
>> +};
>> +
>> +struct property *of_pci_props_create(struct pci_dev *pdev)
>> +{
>> + struct property *props, *pp;
>> + void *val;
>> + u32 len;
>> + int i;
>> +
>> + props = kcalloc(ARRAY_SIZE(of_pci_props), sizeof(*props), GFP_KERNEL);
>> + if (!props)
>> + return NULL;
>> +
>> + pp = props;
>> + for (i = 0; of_pci_props[i].name; i++) {
>> + len = 0;
>> + of_pci_props[i].prop_val(pdev, &val, &len);
>> + if (!len)
>> + continue;
>> + props->name = of_pci_props[i].name;
>> + props->value = val;
>> + props->length = len;
>> + props++;
> This creates an array of properties and then copies each one, and it
> also exposes the internals of 'struct property' which we want to make
> opaque. Neither of these is great.
>
> I'd rather see the of_changeset API expanded to handle specific types of
> properties. Something like this:
>
> of_changeset_add_prop_string(cset, node, "device_type", "pci");
> of_changeset_add_prop_string_array(cset, node, "compatible", compats, cnt);
> of_changeset_add_prop_u32_array(cset, node, "reg", reg, cnt);
>
> And perhaps these functions just wrap similar non-changeset functions
> that produce a struct property.
>
> IOW, it should be similar to the of_property_read_* APIs, but to
> set/add rather than get.
Okay. I will add above 3 APIs and use them to create properties.
>
>
> You are also missing 'ranges', '#address-cells, and '#size-cells' in
> bridge nodes.

I will add them. Do you have more in mind that I need to add with this
patch?


Thanks,

Lizhi

>
> Rob

2022-11-01 08:40:14

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH RFC V2 1/3] of: dynamic: Add of_create_node() and of_destroy_node()

Le 12/10/2022 à 20:13, Lizhi Hou a écrit :
> of_create_node() creates device node and apply to base tree dynamically.
> The parent device node and full name are required for creating the node.
> And the caller can also provide a property array for the node.
>
> Inside this function, it creates a changeset. Then the new device node
> and properties are added to the changeset and applied to base tree. The
> pointer of this changeset is saved in device node private data.
>
> of_destroy_node() removes the node created by of_create_node() from the
> base tree and free it. It gets the changeset pointer from device node
> private data and call of_changeset_destroy() to free everything.
>
> Signed-off-by: Lizhi Hou <[email protected]>
> Signed-off-by: Sonal Santan <[email protected]>
> Signed-off-by: Max Zhen <[email protected]>
> Signed-off-by: Brian Xu <[email protected]>
> ---
> drivers/of/dynamic.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
> include/linux/of.h | 4 +++
> 2 files changed, 84 insertions(+)
>
> diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
> index cd3821a6444f..eca28b723706 100644
> --- a/drivers/of/dynamic.c
> +++ b/drivers/of/dynamic.c
> @@ -934,3 +934,83 @@ int of_changeset_action(struct of_changeset *ocs, unsigned long action,
> return 0;
> }
> EXPORT_SYMBOL_GPL(of_changeset_action);
> +
> +/**
> + * of_create_node - Dynamically create a device node and apply it to base tree
> + *
> + * @parent: Pointer to parent device node
> + * @full_name: Full name of device node
> + * @props: Pointer to property array
> + *
> + * Return: Pointer to the created device node or NULL in case of an error.
> + */
> +struct device_node *of_create_node(struct device_node *parent,
> + const char *full_name,
> + struct property *props)
> +{
> + struct of_changeset *cset;
> + struct property *new_pp;
> + struct device_node *np;
> + int ret, i;
> +
> + cset = kzalloc(sizeof(*cset), GFP_KERNEL);

Hi,

kmalloc() would be enough. of_changeset_init() below already calls
memset().

> + if (!cset)
> + return NULL;
> +
> + of_changeset_init(cset);
> +
> + np = __of_node_dup(NULL, full_name);
> + if (!np)
> + goto failed;

'cset' seems to be leaking if __of_node_dup() fails.

> + np->parent = parent;
> +
> + ret = of_changeset_attach_node(cset, np);
> + if (ret)
> + goto failed;
> +
> + if (props) {
> + for (i = 0; props[i].name; i++) {
> + new_pp = __of_prop_dup(&props[i], GFP_KERNEL);
> + if (!new_pp)
> + goto failed;
> + ret = of_changeset_add_property(cset, np, new_pp);
> + if (ret) {
> + kfree(new_pp->name);
> + kfree(new_pp->value);
> + kfree(new_pp);
> + goto failed;
> + }
> + }
> + }
> +
> + ret = of_changeset_apply(cset);
> + if (ret)
> + goto failed;
> +
> + np->data = cset;
> +
> + return np;
> +
> +failed:
> + of_changeset_destroy(cset);
> + if (np)
> + of_node_put(np);
> +
> + return NULL;
> +}
> +
> +/**
> + * of_destroy_node - Destroy a dynamically created device node
> + *
> + * @np: Pointer to dynamically created device node
> + *
> + */
> +void of_destroy_node(struct device_node *np)
> +{
> + struct of_changeset *cset;
> +
> + cset = (struct of_changeset *)np->data;
> + of_changeset_destroy(cset);
> + of_node_put(np);
> + kfree(cset);
> +}
> diff --git a/include/linux/of.h b/include/linux/of.h
> index 766d002bddb9..493ef957c1a8 100644
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -1475,6 +1475,10 @@ extern int of_changeset_revert(struct of_changeset *ocs);
> extern int of_changeset_action(struct of_changeset *ocs,
> unsigned long action, struct device_node *np,
> struct property *prop);
> +struct device_node *of_create_node(struct device_node *parent,
> + const char *full_name,
> + struct property *props);
> +void of_destroy_node(struct device_node *np);
>
> static inline int of_changeset_attach_node(struct of_changeset *ocs,
> struct device_node *np)


2022-11-01 18:24:04

by Lizhi Hou

[permalink] [raw]
Subject: Re: [PATCH RFC V2 1/3] of: dynamic: Add of_create_node() and of_destroy_node()


On 11/1/22 00:47, Christophe JAILLET wrote:
> Le 12/10/2022 à 20:13, Lizhi Hou a écrit :
>> of_create_node() creates device node and apply to base tree dynamically.
>> The parent device node and full name are required for creating the node.
>> And the caller can also provide a property array for the node.
>>
>> Inside this function, it creates a changeset. Then the new device node
>> and properties are added to the changeset and applied to base tree. The
>> pointer of this changeset is saved in device node private data.
>>
>> of_destroy_node() removes the node created by of_create_node() from the
>> base tree and free it. It gets the changeset pointer from device node
>> private data and call of_changeset_destroy() to free everything.
>>
>> Signed-off-by: Lizhi Hou <[email protected]>
>> Signed-off-by: Sonal Santan <[email protected]>
>> Signed-off-by: Max Zhen <[email protected]>
>> Signed-off-by: Brian Xu <[email protected]>
>> ---
>>   drivers/of/dynamic.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
>>   include/linux/of.h   |  4 +++
>>   2 files changed, 84 insertions(+)
>>
>> diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
>> index cd3821a6444f..eca28b723706 100644
>> --- a/drivers/of/dynamic.c
>> +++ b/drivers/of/dynamic.c
>> @@ -934,3 +934,83 @@ int of_changeset_action(struct of_changeset
>> *ocs, unsigned long action,
>>       return 0;
>>   }
>>   EXPORT_SYMBOL_GPL(of_changeset_action);
>> +
>> +/**
>> + * of_create_node - Dynamically create a device node and apply it to
>> base tree
>> + *
>> + * @parent: Pointer to parent device node
>> + * @full_name: Full name of device node
>> + * @props: Pointer to property array
>> + *
>> + * Return: Pointer to the created device node or NULL in case of an
>> error.
>> + */
>> +struct device_node *of_create_node(struct device_node *parent,
>> +                   const char *full_name,
>> +                   struct property *props)
>> +{
>> +    struct of_changeset *cset;
>> +    struct property *new_pp;
>> +    struct device_node *np;
>> +    int ret, i;
>> +
>> +    cset = kzalloc(sizeof(*cset), GFP_KERNEL);
>
> Hi,
>
> kmalloc() would be enough. of_changeset_init() below already calls
> memset().
Ok.
>
>> +    if (!cset)
>> +        return NULL;
>> +
>> +    of_changeset_init(cset);
>> +
>> +    np = __of_node_dup(NULL, full_name);
>> +    if (!np)
>> +        goto failed;
>
> 'cset' seems to be leaking if __of_node_dup() fails.

Will fix this. Thanks.


Lizhi

>
>> +    np->parent = parent;
>> +
>> +    ret = of_changeset_attach_node(cset, np);
>> +    if (ret)
>> +        goto failed;
>> +
>> +    if (props) {
>> +        for (i = 0; props[i].name; i++) {
>> +            new_pp = __of_prop_dup(&props[i], GFP_KERNEL);
>> +            if (!new_pp)
>> +                goto failed;
>> +            ret = of_changeset_add_property(cset, np, new_pp);
>> +            if (ret) {
>> +                kfree(new_pp->name);
>> +                kfree(new_pp->value);
>> +                kfree(new_pp);
>> +                goto failed;
>> +            }
>> +        }
>> +    }
>> +
>> +    ret = of_changeset_apply(cset);
>> +    if (ret)
>> +        goto failed;
>> +
>> +    np->data = cset;
>> +
>> +    return np;
>> +
>> +failed:
>> +    of_changeset_destroy(cset);
>> +    if (np)
>> +        of_node_put(np);
>> +
>> +    return NULL;
>> +}
>> +
>> +/**
>> + * of_destroy_node - Destroy a dynamically created device node
>> + *
>> + * @np: Pointer to dynamically created device node
>> + *
>> + */
>> +void of_destroy_node(struct device_node *np)
>> +{
>> +    struct of_changeset *cset;
>> +
>> +    cset = (struct of_changeset *)np->data;
>> +    of_changeset_destroy(cset);
>> +    of_node_put(np);
>> +    kfree(cset);
>> +}
>> diff --git a/include/linux/of.h b/include/linux/of.h
>> index 766d002bddb9..493ef957c1a8 100644
>> --- a/include/linux/of.h
>> +++ b/include/linux/of.h
>> @@ -1475,6 +1475,10 @@ extern int of_changeset_revert(struct
>> of_changeset *ocs);
>>   extern int of_changeset_action(struct of_changeset *ocs,
>>           unsigned long action, struct device_node *np,
>>           struct property *prop);
>> +struct device_node *of_create_node(struct device_node *parent,
>> +                   const char *full_name,
>> +                   struct property *props);
>> +void of_destroy_node(struct device_node *np);
>>     static inline int of_changeset_attach_node(struct of_changeset *ocs,
>>           struct device_node *np)
>