Generate device tree node for pci devices
Subsystems (2)
v2 cover 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
Signature · 1 line
2.17.1
RFC PATCH 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.
--- drivers/of/dynamic.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/of.h | 4 +++ 2 files changed, 84 insertions(+)
drivers/of/dynamic.c +80 -0
@@ -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); +}
include/linux/of.h +4 -0
@@ -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)
Signature · 1 line
2.17.1
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 :
Quoted 39 lines
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 byof_create_node()from the base tree and free it. It gets the changeset pointer from device node private data and callof_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(+)drivers/of/dynamic.c +10
-0@@ -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. +*/
drivers/of/dynamic.c int of_changeset_action(struct of_changeset *ocs, unsigned long action, · line 956
Quoted 10 lines
drivers/of/dynamic.c +10
-0+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().
drivers/of/dynamic.c int of_changeset_action(struct of_changeset *ocs, unsigned long action, · line 964
Quoted 8 lines
drivers/of/dynamic.c +8
-0+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.
Quoted 67 lines
drivers/of/dynamic.c +52
-0+ 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); +}include/linux/of.h +4
-0@@ -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)
Re: [PATCH RFC V2 1/3] of: dynamic: Add of_create_node() and of_destroy_node()
Quoted 56 lines · Christophe JAILLET
Le 12/10/2022 à 20:13, Lizhi Hou a écrit :
Quoted 42 lines
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 byof_create_node()from the base tree and free it. It gets the changeset pointer from device node private data and callof_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(+)drivers/of/dynamic.c +0
-0@@ -934,3 +934,83 @@ int of_changeset_action(struct of_changeset *ocs, unsigned long action, return 0; }
EXPORT_SYMBOL_GPL(of_changeset_action);drivers/of/dynamic.c +3
-0+ +/** +* 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.
+ */drivers/of/dynamic.c int of_changeset_action(struct of_changeset *ocs, unsigned long action, · line 956
Quoted 10 lines
drivers/of/dynamic.c +10
-0+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,
Quoted 1 line
kmalloc()would be enough.of_changeset_init()below already callsmemset().
Ok.
Quoted 11 lines
drivers/of/dynamic.c int of_changeset_action(struct of_changeset *ocs, unsigned long action, · line 964
Quoted 9 lines
+ if (!cset)
+ return NULL;drivers/of/dynamic.c +6
-0+ + 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
Quoted 69 lines
Quoted 68 lines
+
np->parent= parent;drivers/of/dynamic.c +51
-0+ + 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); +}include/linux/of.h +4
-0@@ -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)
RFC PATCH v2 2/3 PCI: Create device tree node for selected devices
In reply to Lizhi HouThe 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.
--- 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(-)
drivers/pci/Kconfig +12 -0
@@ -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
drivers/pci/bus.c +2 -0
@@ -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);
drivers/pci/msi/irqdomain.c +4 -2
@@ -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; }
drivers/pci/of.c +61 -0
73 lines not on this page · raw message
drivers/pci/pci-driver.c +2 -1
@@ -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);
drivers/pci/pci.h +16 -0
@@ -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);
drivers/pci/quirks.c +11 -0
@@ -5956,3 +5956,14 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x56b1, aspm_l1_acceptable_latencyDECLARE_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);
drivers/pci/remove.c +1 -0
@@ -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); }
Signature · 1 line
2.17.1
RFC PATCH v2 3/3 PCI: Add basic properties for dynamically generated PCI OF node
In reply to Lizhi HouThis patch addes 'reg', 'compatible' and 'device_typ' properties for dynamically generated PCI device tree node
--- 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
drivers/pci/Makefile +1 -0
@@ -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/
drivers/pci/of.c +9 -1
@@ -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
drivers/pci/of_property.c new file +177 -0
177 lines not on this page · raw message
drivers/pci/pci.h +2 -1
@@ -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)
Signature · 1 line
2.17.1
Rob Herring
maintainerRe: [PATCH RFC V2 3/3] PCI: Add basic properties for dynamically generated PCI OF node
Quoted 1 line · Lizhi Hou
This patch addes 'reg', 'compatible' and '
device_typ' properties for
typo
Please read submitting-patches.rst and what it says about 'This patch'.
Quoted 10 lines
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.
Quoted 157 lines
drivers/pci/pci.h | 3 +- 4 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 drivers/pci/of_property.cdrivers/pci/Makefile +1
-0@@ -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/drivers/pci/of.c +9
-1@@ -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 drivers/pci/of_property.c new file +97
-0@@ -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;
drivers/pci/of_property.c line 107
Quoted 10 lines
drivers/pci/of_property.c new file +10
-0+ *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.
Quoted 13 lines
drivers/pci/of_property.c new file +13
-0+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,
drivers/pci/of_property.c line 130
Quoted 10 lines
drivers/pci/of_property.c new file +10
-0+ 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...
drivers/pci/of_property.c line 138
Quoted 8 lines
drivers/pci/of_property.c new file +8
-0+ + *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.
Quoted 16 lines
drivers/pci/of_property.c new file +16
-0+ { .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 ; +
drivers/pci/of_property.c line 164
Quoted 10 lines
drivers/pci/of_property.c new file +10
-0+ 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
Re: [PATCH RFC V2 3/3] PCI: Add basic properties for dynamically generated PCI OF node
Quoted 5 lines · Rob Herring
Quoted 1 line · Lizhi Hou
This patch addes 'reg', 'compatible' and '
device_typ' properties fortypo
Please read
submitting-patches.rstand what it says about 'This patch'.
Sure. And I will merge patch 2 and 3.
Quoted 176 lines
Quoted 11 lines
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.
Quoted 161 lines
drivers/pci/pci.h | 3 +- 4 files changed, 189 insertions(+), 2 deletions(-)create mode 100644
drivers/pci/of_property.cdrivers/pci/Makefile +1
-0@@ -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/drivers/pci/of.c +9
-1@@ -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 drivers/pci/of_property.c new file +101
-0@@ -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 ; +}
Quoted 8 lines
drivers/pci/of_property.c line 107
Quoted 6 lines
drivers/pci/of_property.c new file +6
-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.
Quoted 26 lines
Quoted 14 lines
drivers/pci/of_property.c new file +13
-0+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,drivers/pci/of_property.c line 130
Quoted 10 lines
drivers/pci/of_property.c new file +10
-0+ 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?
Quoted 3 lines
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.
Quoted 10 lines
drivers/pci/of_property.c line 138
Quoted 9 lines
drivers/pci/of_property.c new file +8
-0+ + *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.
Quoted 38 lines
Quoted 17 lines
drivers/pci/of_property.c new file +16
-0+ { .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 ; +drivers/pci/of_property.c line 164
Quoted 10 lines
drivers/pci/of_property.c new file +10
-0+ 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_changesetAPI 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);
Quoted 5 lines
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.
Quoted 4 lines
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
Quoted 2 lines
Rob