[PATCH v3 2/3] of: factor arguments passed to of_map_id() into a struct

Vijayanand Jitta posted 3 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH v3 2/3] of: factor arguments passed to of_map_id() into a struct
Posted by Vijayanand Jitta 1 month, 2 weeks ago
From: Charan Teja Kalla <charan.kalla@oss.qualcomm.com>

Introduce a new struct type where the optional arguments passed to
of_map_id() are Currently embedded as of_phandle_args struct.
Subsequent patches add additional arguments to the struct that the
caller expects to be filled of_map_id().

Suggested-by: Rob Herring (Arm) <robh@kernel.org>
Signed-off-by: Charan Teja Kalla <charan.kalla@oss.qualcomm.com>
Signed-off-by: Vijayanand Jitta <vijayanand.jitta@oss.qualcomm.com>
---
 drivers/iommu/of_iommu.c              |  5 +++-
 drivers/of/base.c                     | 39 ++++++++++++++-------------
 drivers/pci/controller/dwc/pci-imx6.c |  6 ++++-
 drivers/pci/controller/pcie-apple.c   |  5 +++-
 drivers/xen/grant-dma-ops.c           |  4 ++-
 include/linux/of.h                    | 24 ++++++++++++-----
 6 files changed, 53 insertions(+), 30 deletions(-)

diff --git a/drivers/iommu/of_iommu.c b/drivers/iommu/of_iommu.c
index a511ecf21fcd..74779b77ba13 100644
--- a/drivers/iommu/of_iommu.c
+++ b/drivers/iommu/of_iommu.c
@@ -46,9 +46,12 @@ static int of_iommu_configure_dev_id(struct device_node *master_np,
 				     const u32 *id)
 {
 	struct of_phandle_args iommu_spec = { .args_count = 1 };
+	struct of_map_id_arg arg = {
+		.map_args = iommu_spec,
+	};
 	int err;
 
-	err = of_map_iommu_id(master_np, *id, &iommu_spec.np, iommu_spec.args);
+	err = of_map_iommu_id(master_np, *id, &arg);
 	if (err)
 		return err;
 
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 7043acd971a0..4dca3d37a34b 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2051,8 +2051,11 @@ int of_find_last_cache_level(unsigned int cpu)
  * @id: device ID to map.
  * @map_name: property name of the map to use.
  * @map_mask_name: optional property name of the mask to use.
- * @target: optional pointer to a target device node.
- * @id_out: optional pointer to receive the translated ID.
+ * @arg: contains the optional params, wrapped in a struct of_phandle_args,
+ *	which includes:
+ *	np: pointer to the target device node
+ *	args_count: number of arguments
+ *	args[]: array to receive the translated ID(s).
  *
  * Given a device ID, look up the appropriate implementation-defined
  * platform ID and/or the target device which receives transactions on that
@@ -2066,21 +2069,21 @@ int of_find_last_cache_level(unsigned int cpu)
  */
 int of_map_id(const struct device_node *np, u32 id,
 	       const char *map_name, const char *map_mask_name,
-	       struct device_node **target, u32 *id_out)
+	       struct of_map_id_arg *arg)
 {
 	u32 map_mask, masked_id;
 	int map_len;
 	const __be32 *map = NULL;
 
-	if (!np || !map_name || (!target && !id_out))
+	if (!np || !map_name || !arg || (!arg->map_args.np && !arg->map_args.args))
 		return -EINVAL;
 
 	map = of_get_property(np, map_name, &map_len);
 	if (!map) {
-		if (target)
+		if (arg->map_args.np)
 			return -ENODEV;
 		/* Otherwise, no map implies no translation */
-		*id_out = id;
+		*arg->map_args.args = id;
 		return 0;
 	}
 
@@ -2122,18 +2125,16 @@ int of_map_id(const struct device_node *np, u32 id,
 		if (!phandle_node)
 			return -ENODEV;
 
-		if (target) {
-			if (*target)
-				of_node_put(phandle_node);
-			else
-				*target = phandle_node;
+		if (arg->map_args.np)
+			of_node_put(phandle_node);
+		else
+			arg->map_args.np = phandle_node;
 
-			if (*target != phandle_node)
-				continue;
-		}
+		if (arg->map_args.np != phandle_node)
+			continue;
 
-		if (id_out)
-			*id_out = masked_id - id_base + out_base;
+		if (arg->map_args.args)
+			*arg->map_args.args = masked_id - id_base + out_base;
 
 		pr_debug("%pOF: %s, using mask %08x, id-base: %08x, out-base: %08x, length: %08x, id: %08x -> %08x\n",
 			np, map_name, map_mask, id_base, out_base,
@@ -2142,11 +2143,11 @@ int of_map_id(const struct device_node *np, u32 id,
 	}
 
 	pr_info("%pOF: no %s translation for id 0x%x on %pOF\n", np, map_name,
-		id, target && *target ? *target : NULL);
+		id, arg->map_args.np  ? arg->map_args.np : NULL);
 
 	/* Bypasses translation */
-	if (id_out)
-		*id_out = id;
+	if (arg->map_args.args)
+		*arg->map_args.args = id;
 	return 0;
 }
 EXPORT_SYMBOL_GPL(of_map_id);
diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c
index c8da2e88e9c6..8dcdde2efb8a 100644
--- a/drivers/pci/controller/dwc/pci-imx6.c
+++ b/drivers/pci/controller/dwc/pci-imx6.c
@@ -1101,12 +1101,16 @@ static int imx_pcie_add_lut_by_rid(struct imx_pcie *imx_pcie, u32 rid)
 {
 	struct device *dev = imx_pcie->pci->dev;
 	struct device_node *target;
+	struct of_map_id_arg arg = {};
 	u32 sid_i, sid_m;
 	int err_i, err_m;
 	u32 sid = 0;
 
 	target = NULL;
-	err_i = of_map_iommu_id(dev->of_node, rid, &target, &sid_i);
+
+	arg.map_args.np = target;
+	arg.map_args.args[0] = sid_i;
+	err_i = of_map_iommu_id(dev->of_node, rid, &arg);
 	if (target) {
 		of_node_put(target);
 	} else {
diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c
index ce21728d6e51..dea4a38cb4bb 100644
--- a/drivers/pci/controller/pcie-apple.c
+++ b/drivers/pci/controller/pcie-apple.c
@@ -782,6 +782,7 @@ static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_d
 {
 	u32 sid, rid = pci_dev_id(pdev);
 	struct apple_pcie_port *port;
+	struct of_map_id_arg arg = {};
 	int idx, err;
 
 	port = apple_pcie_get_port(pdev);
@@ -791,7 +792,9 @@ static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_d
 	dev_dbg(&pdev->dev, "added to bus %s, index %d\n",
 		pci_name(pdev->bus->self), port->idx);
 
-	err = of_map_iommu_id(port->pcie->dev->of_node, rid, NULL, &sid);
+	arg.map_args.np = NULL;
+	arg.map_args.args[0] = sid;
+	err = of_map_iommu_id(port->pcie->dev->of_node, rid, &arg);
 	if (err)
 		return err;
 
diff --git a/drivers/xen/grant-dma-ops.c b/drivers/xen/grant-dma-ops.c
index b661f9c1f4fe..d455104de159 100644
--- a/drivers/xen/grant-dma-ops.c
+++ b/drivers/xen/grant-dma-ops.c
@@ -319,9 +319,11 @@ static int xen_dt_grant_init_backend_domid(struct device *dev,
 
 	if (dev_is_pci(dev)) {
 		struct pci_dev *pdev = to_pci_dev(dev);
+		struct of_map_id_arg arg = {};
 		u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
 
-		if (of_map_iommu_id(np, rid, &iommu_spec.np, iommu_spec.args)) {
+		arg.map_args = iommu_spec;
+		if (of_map_iommu_id(np, rid, &arg)) {
 			dev_dbg(dev, "Cannot translate ID\n");
 			return -ESRCH;
 		}
diff --git a/include/linux/of.h b/include/linux/of.h
index 8cd486d89da2..0b0d545b80a3 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -74,6 +74,10 @@ struct of_phandle_args {
 	uint32_t args[MAX_PHANDLE_ARGS];
 };
 
+struct of_map_id_arg {
+	struct of_phandle_args map_args;
+};
+
 struct of_phandle_iterator {
 	/* Common iterator information */
 	const char *cells_name;
@@ -458,7 +462,7 @@ bool of_console_check(const struct device_node *dn, char *name, int index);
 
 int of_map_id(const struct device_node *np, u32 id,
 	       const char *map_name, const char *map_mask_name,
-	       struct device_node **target, u32 *id_out);
+	       struct of_map_id_arg *arg);
 
 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
 
@@ -907,7 +911,7 @@ static inline void of_property_clear_flag(struct property *p, unsigned long flag
 
 static inline int of_map_id(const struct device_node *np, u32 id,
 			     const char *map_name, const char *map_mask_name,
-			     struct device_node **target, u32 *id_out)
+			     struct of_map_id_arg *arg)
 {
 	return -EINVAL;
 }
@@ -1436,17 +1440,23 @@ static inline int of_property_read_s32(const struct device_node *np,
 }
 
 static inline int of_map_iommu_id(const struct device_node *np, u32 id,
-				  struct device_node **target, u32 *id_out)
+				  struct of_map_id_arg *arg)
 {
-	return of_map_id(np, id, "iommu-map", "iommu-map-mask",
-			 target, id_out);
+	return of_map_id(np, id, "iommu-map", "iommu-map-mask", arg);
 }
 
 static inline int of_map_msi_id(const struct device_node *np, u32 id,
 				struct device_node **target, u32 *id_out)
 {
-	return of_map_id(np, id, "msi-map", "msi-map-mask",
-			 target, id_out);
+	struct of_map_id_arg arg = {
+		.map_args = {
+			.np = *target,
+			.args_count = 1,
+			.args = { *id_out },
+		},
+	};
+
+	return of_map_id(np, id, "msi-map", "msi-map-mask", &arg);
 }
 
 #define of_for_each_phandle(it, err, np, ln, cn, cc)			\
-- 
2.34.1
Re: [PATCH v3 2/3] of: factor arguments passed to of_map_id() into a struct
Posted by kernel test robot 1 month, 2 weeks ago
Hi Vijayanand,

kernel test robot noticed the following build warnings:

[auto build test WARNING on xen-tip/linux-next]
[cannot apply to robh/for-next pci/next pci/for-linus linus/master v6.19-rc2 next-20251219]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vijayanand-Jitta/of-Add-convenience-wrappers-for-of_map_id/20251222-053941
base:   https://git.kernel.org/pub/scm/linux/kernel/git/xen/tip.git linux-next
patch link:    https://lore.kernel.org/r/20251221213602.2413124-3-vijayanand.jitta%40oss.qualcomm.com
patch subject: [PATCH v3 2/3] of: factor arguments passed to of_map_id() into a struct
config: arm-allnoconfig (https://download.01.org/0day-ci/archive/20251224/202512241124.Tl95CT1C-lkp@intel.com/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 4ef602d446057dabf5f61fb221669ecbeda49279)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251224/202512241124.Tl95CT1C-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512241124.Tl95CT1C-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/of/base.c:2078:71: warning: address of array 'arg->map_args.args' will always evaluate to 'true' [-Wpointer-bool-conversion]
    2078 |         if (!np || !map_name || !arg || (!arg->map_args.np && !arg->map_args.args))
         |                                                               ~~~~~~~~~~~~~~~^~~~
   drivers/of/base.c:2136:21: warning: address of array 'arg->map_args.args' will always evaluate to 'true' [-Wpointer-bool-conversion]
    2136 |                 if (arg->map_args.args)
         |                 ~~  ~~~~~~~~~~~~~~^~~~
   drivers/of/base.c:2149:20: warning: address of array 'arg->map_args.args' will always evaluate to 'true' [-Wpointer-bool-conversion]
    2149 |         if (arg->map_args.args)
         |         ~~  ~~~~~~~~~~~~~~^~~~
   3 warnings generated.


vim +2078 drivers/of/base.c

  2047	
  2048	/**
  2049	 * of_map_id - Translate an ID through a downstream mapping.
  2050	 * @np: root complex device node.
  2051	 * @id: device ID to map.
  2052	 * @map_name: property name of the map to use.
  2053	 * @map_mask_name: optional property name of the mask to use.
  2054	 * @arg: contains the optional params, wrapped in a struct of_phandle_args,
  2055	 *	which includes:
  2056	 *	np: pointer to the target device node
  2057	 *	args_count: number of arguments
  2058	 *	args[]: array to receive the translated ID(s).
  2059	 *
  2060	 * Given a device ID, look up the appropriate implementation-defined
  2061	 * platform ID and/or the target device which receives transactions on that
  2062	 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
  2063	 * @id_out may be NULL if only the other is required. If @target points to
  2064	 * a non-NULL device node pointer, only entries targeting that node will be
  2065	 * matched; if it points to a NULL value, it will receive the device node of
  2066	 * the first matching target phandle, with a reference held.
  2067	 *
  2068	 * Return: 0 on success or a standard error code on failure.
  2069	 */
  2070	int of_map_id(const struct device_node *np, u32 id,
  2071		       const char *map_name, const char *map_mask_name,
  2072		       struct of_map_id_arg *arg)
  2073	{
  2074		u32 map_mask, masked_id;
  2075		int map_len;
  2076		const __be32 *map = NULL;
  2077	
> 2078		if (!np || !map_name || !arg || (!arg->map_args.np && !arg->map_args.args))

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v3 2/3] of: factor arguments passed to of_map_id() into a struct
Posted by kernel test robot 1 month, 2 weeks ago
Hi Vijayanand,

kernel test robot noticed the following build warnings:

[auto build test WARNING on xen-tip/linux-next]
[cannot apply to robh/for-next pci/next pci/for-linus linus/master v6.19-rc2 next-20251219]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vijayanand-Jitta/of-Add-convenience-wrappers-for-of_map_id/20251222-053941
base:   https://git.kernel.org/pub/scm/linux/kernel/git/xen/tip.git linux-next
patch link:    https://lore.kernel.org/r/20251221213602.2413124-3-vijayanand.jitta%40oss.qualcomm.com
patch subject: [PATCH v3 2/3] of: factor arguments passed to of_map_id() into a struct
config: arc-allnoconfig (https://download.01.org/0day-ci/archive/20251224/202512241116.41ldSEgi-lkp@intel.com/config)
compiler: arc-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251224/202512241116.41ldSEgi-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512241116.41ldSEgi-lkp@intel.com/

All warnings (new ones prefixed by >>):

   drivers/of/base.c: In function 'of_map_id':
>> drivers/of/base.c:2078:63: warning: the comparison will always evaluate as 'true' for the address of 'args' will never be NULL [-Waddress]
    2078 |         if (!np || !map_name || !arg || (!arg->map_args.np && !arg->map_args.args))
         |                                                               ^
   In file included from drivers/of/base.c:24:
   include/linux/of.h:74:18: note: 'args' declared here
      74 |         uint32_t args[MAX_PHANDLE_ARGS];
         |                  ^~~~
   drivers/of/base.c:2136:21: warning: the comparison will always evaluate as 'true' for the address of 'args' will never be NULL [-Waddress]
    2136 |                 if (arg->map_args.args)
         |                     ^~~
   include/linux/of.h:74:18: note: 'args' declared here
      74 |         uint32_t args[MAX_PHANDLE_ARGS];
         |                  ^~~~
   drivers/of/base.c:2149:13: warning: the comparison will always evaluate as 'true' for the address of 'args' will never be NULL [-Waddress]
    2149 |         if (arg->map_args.args)
         |             ^~~
   include/linux/of.h:74:18: note: 'args' declared here
      74 |         uint32_t args[MAX_PHANDLE_ARGS];
         |                  ^~~~


vim +2078 drivers/of/base.c

  2047	
  2048	/**
  2049	 * of_map_id - Translate an ID through a downstream mapping.
  2050	 * @np: root complex device node.
  2051	 * @id: device ID to map.
  2052	 * @map_name: property name of the map to use.
  2053	 * @map_mask_name: optional property name of the mask to use.
  2054	 * @arg: contains the optional params, wrapped in a struct of_phandle_args,
  2055	 *	which includes:
  2056	 *	np: pointer to the target device node
  2057	 *	args_count: number of arguments
  2058	 *	args[]: array to receive the translated ID(s).
  2059	 *
  2060	 * Given a device ID, look up the appropriate implementation-defined
  2061	 * platform ID and/or the target device which receives transactions on that
  2062	 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
  2063	 * @id_out may be NULL if only the other is required. If @target points to
  2064	 * a non-NULL device node pointer, only entries targeting that node will be
  2065	 * matched; if it points to a NULL value, it will receive the device node of
  2066	 * the first matching target phandle, with a reference held.
  2067	 *
  2068	 * Return: 0 on success or a standard error code on failure.
  2069	 */
  2070	int of_map_id(const struct device_node *np, u32 id,
  2071		       const char *map_name, const char *map_mask_name,
  2072		       struct of_map_id_arg *arg)
  2073	{
  2074		u32 map_mask, masked_id;
  2075		int map_len;
  2076		const __be32 *map = NULL;
  2077	
> 2078		if (!np || !map_name || !arg || (!arg->map_args.np && !arg->map_args.args))

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki