diff mbox series

[net-next,v4,09/15] device property: Introduce fwnode_get_id()

Message ID 20210122154300.7628-10-calvin.johnson@oss.nxp.com (mailing list archive)
State New, archived
Headers show
Series ACPI support for dpaa2 driver | expand

Commit Message

Calvin Johnson Jan. 22, 2021, 3:42 p.m. UTC
Using fwnode_get_id(), get the reg property value for DT node
or get the _ADR object value for ACPI node.

Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
---

Changes in v4:
- Improve code structure to handle all cases

Changes in v3:
- Modified to retrieve reg property value for ACPI as well
- Resolved compilation issue with CONFIG_ACPI = n
- Added more info into documentation

Changes in v2: None

 drivers/base/property.c  | 34 ++++++++++++++++++++++++++++++++++
 include/linux/property.h |  1 +
 2 files changed, 35 insertions(+)

Comments

Andy Shevchenko Jan. 22, 2021, 4:24 p.m. UTC | #1
On Fri, Jan 22, 2021 at 09:12:54PM +0530, Calvin Johnson wrote:
> Using fwnode_get_id(), get the reg property value for DT node
> or get the _ADR object value for ACPI node.

...

> +/**
> + * fwnode_get_id - Get the id of a fwnode.
> + * @fwnode: firmware node
> + * @id: id of the fwnode
> + *
> + * This function provides the id of a fwnode which can be either
> + * DT or ACPI node. For ACPI, "reg" property value, if present will
> + * be provided or else _ADR value will be provided.
> + * Returns 0 on success or a negative errno.
> + */
> +int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id)
> +{

> +#ifdef CONFIG_ACPI
> +	unsigned long long adr;
> +	acpi_status status;
> +#endif

Instead you may do...

> +	int ret;
> +
> +	ret = fwnode_property_read_u32(fwnode, "reg", id);
> +	if (ret) {
> +#ifdef CONFIG_ACPI

...it here like

		unsigned long long adr;
		acpi_status status;

> +		status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwnode),
> +					       METHOD_NAME__ADR, NULL, &adr);
> +		if (ACPI_FAILURE(status))
> +			return -EINVAL;
> +		*id = (u32)adr;
> +#else
> +		return ret;
> +#endif
> +	}
> +	return 0;
> +}
Rafael J. Wysocki Jan. 22, 2021, 4:40 p.m. UTC | #2
On Fri, Jan 22, 2021 at 4:46 PM Calvin Johnson
<calvin.johnson@oss.nxp.com> wrote:
>
> Using fwnode_get_id(), get the reg property value for DT node
> or get the _ADR object value for ACPI node.

So I'm not really sure if this is going to be generically useful.

First of all, the meaning of the _ADR return value is specific to a
given bus type (e.g. the PCI encoding of it is different from the I2C
encoding of it) and it just happens to be matching the definition of
the "reg" property for this particular binding.

IOW, not everyone may expect the "reg" property and the _ADR return
value to have the same encoding and belong to the same set of values,
so maybe put this function somewhere closer to the code that's going
to use it, because it seems to be kind of specific to this particular
use case?

> Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
> ---
>
> Changes in v4:
> - Improve code structure to handle all cases
>
> Changes in v3:
> - Modified to retrieve reg property value for ACPI as well
> - Resolved compilation issue with CONFIG_ACPI = n
> - Added more info into documentation
>
> Changes in v2: None
>
>  drivers/base/property.c  | 34 ++++++++++++++++++++++++++++++++++
>  include/linux/property.h |  1 +
>  2 files changed, 35 insertions(+)
>
> diff --git a/drivers/base/property.c b/drivers/base/property.c
> index 35b95c6ac0c6..f0581bbf7a4b 100644
> --- a/drivers/base/property.c
> +++ b/drivers/base/property.c
> @@ -580,6 +580,40 @@ const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
>         return fwnode_call_ptr_op(fwnode, get_name_prefix);
>  }
>
> +/**
> + * fwnode_get_id - Get the id of a fwnode.
> + * @fwnode: firmware node
> + * @id: id of the fwnode
> + *
> + * This function provides the id of a fwnode which can be either
> + * DT or ACPI node. For ACPI, "reg" property value, if present will
> + * be provided or else _ADR value will be provided.
> + * Returns 0 on success or a negative errno.
> + */
> +int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id)
> +{
> +#ifdef CONFIG_ACPI
> +       unsigned long long adr;
> +       acpi_status status;
> +#endif
> +       int ret;
> +
> +       ret = fwnode_property_read_u32(fwnode, "reg", id);
> +       if (ret) {
> +#ifdef CONFIG_ACPI
> +               status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwnode),
> +                                              METHOD_NAME__ADR, NULL, &adr);
> +               if (ACPI_FAILURE(status))
> +                       return -EINVAL;
> +               *id = (u32)adr;
> +#else
> +               return ret;
> +#endif
> +       }
> +       return 0;
> +}
> +EXPORT_SYMBOL_GPL(fwnode_get_id);
> +
>  /**
>   * fwnode_get_parent - Return parent firwmare node
>   * @fwnode: Firmware whose parent is retrieved
> diff --git a/include/linux/property.h b/include/linux/property.h
> index 0a9001fe7aea..3f41475f010b 100644
> --- a/include/linux/property.h
> +++ b/include/linux/property.h
> @@ -82,6 +82,7 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
>
>  const char *fwnode_get_name(const struct fwnode_handle *fwnode);
>  const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
> +int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id);
>  struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
>  struct fwnode_handle *fwnode_get_next_parent(
>         struct fwnode_handle *fwnode);
> --
> 2.17.1
>
Andy Shevchenko Jan. 22, 2021, 5:13 p.m. UTC | #3
On Fri, Jan 22, 2021 at 05:40:41PM +0100, Rafael J. Wysocki wrote:
> On Fri, Jan 22, 2021 at 4:46 PM Calvin Johnson
> <calvin.johnson@oss.nxp.com> wrote:
> >
> > Using fwnode_get_id(), get the reg property value for DT node
> > or get the _ADR object value for ACPI node.
> 
> So I'm not really sure if this is going to be generically useful.
> 
> First of all, the meaning of the _ADR return value is specific to a
> given bus type (e.g. the PCI encoding of it is different from the I2C
> encoding of it) and it just happens to be matching the definition of
> the "reg" property for this particular binding.

> IOW, not everyone may expect the "reg" property and the _ADR return
> value to have the same encoding and belong to the same set of values,

I have counted three or even four attempts to open code exact this scenario
in the past couple of years. And I have no idea where to put a common base for
them so they will not duplicate this in each case.

> so maybe put this function somewhere closer to the code that's going
> to use it, because it seems to be kind of specific to this particular
> use case?
Rafael J. Wysocki Jan. 22, 2021, 6:11 p.m. UTC | #4
On Fri, Jan 22, 2021 at 6:12 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Fri, Jan 22, 2021 at 05:40:41PM +0100, Rafael J. Wysocki wrote:
> > On Fri, Jan 22, 2021 at 4:46 PM Calvin Johnson
> > <calvin.johnson@oss.nxp.com> wrote:
> > >
> > > Using fwnode_get_id(), get the reg property value for DT node
> > > or get the _ADR object value for ACPI node.
> >
> > So I'm not really sure if this is going to be generically useful.
> >
> > First of all, the meaning of the _ADR return value is specific to a
> > given bus type (e.g. the PCI encoding of it is different from the I2C
> > encoding of it) and it just happens to be matching the definition of
> > the "reg" property for this particular binding.
>
> > IOW, not everyone may expect the "reg" property and the _ADR return
> > value to have the same encoding and belong to the same set of values,
>
> I have counted three or even four attempts to open code exact this scenario
> in the past couple of years. And I have no idea where to put a common base for
> them so they will not duplicate this in each case.

In that case it makes sense to have it in the core, but calling the
_ADR return value an "id" generically is a stretch to put it lightly.

It may be better to call the function something like
fwnode_get_local_bus_id() end explain in the kerneldoc comment that
the return value provides a way to distinguish the given device from
the other devices on the same bus segment.

Otherwise it may cause people to expect that the "reg" property and
_ADR are generally equivalent, which is not the case AFAICS.

At least the kerneldoc should say something like "use only if it is
known for a fact that the _ADR return value can be treated as a
fallback replacement for the "reg" property that is missing in the
given use case".

> > so maybe put this function somewhere closer to the code that's going
> > to use it, because it seems to be kind of specific to this particular
> > use case?
Rafael J. Wysocki Jan. 22, 2021, 6:13 p.m. UTC | #5
On Fri, Jan 22, 2021 at 4:46 PM Calvin Johnson
<calvin.johnson@oss.nxp.com> wrote:
>
> Using fwnode_get_id(), get the reg property value for DT node
> or get the _ADR object value for ACPI node.
>
> Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
> ---
>
> Changes in v4:
> - Improve code structure to handle all cases
>
> Changes in v3:
> - Modified to retrieve reg property value for ACPI as well
> - Resolved compilation issue with CONFIG_ACPI = n
> - Added more info into documentation
>
> Changes in v2: None
>
>  drivers/base/property.c  | 34 ++++++++++++++++++++++++++++++++++
>  include/linux/property.h |  1 +
>  2 files changed, 35 insertions(+)
>
> diff --git a/drivers/base/property.c b/drivers/base/property.c
> index 35b95c6ac0c6..f0581bbf7a4b 100644
> --- a/drivers/base/property.c
> +++ b/drivers/base/property.c
> @@ -580,6 +580,40 @@ const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
>         return fwnode_call_ptr_op(fwnode, get_name_prefix);
>  }
>
> +/**
> + * fwnode_get_id - Get the id of a fwnode.
> + * @fwnode: firmware node
> + * @id: id of the fwnode
> + *
> + * This function provides the id of a fwnode which can be either
> + * DT or ACPI node. For ACPI, "reg" property value, if present will
> + * be provided or else _ADR value will be provided.
> + * Returns 0 on success or a negative errno.
> + */
> +int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id)
> +{
> +#ifdef CONFIG_ACPI
> +       unsigned long long adr;
> +       acpi_status status;
> +#endif
> +       int ret;
> +
> +       ret = fwnode_property_read_u32(fwnode, "reg", id);
> +       if (ret) {
> +#ifdef CONFIG_ACPI
> +               status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwnode),
> +                                              METHOD_NAME__ADR, NULL, &adr);
> +               if (ACPI_FAILURE(status))
> +                       return -EINVAL;

Please don't return -EINVAL from here, because this means "invalid
argument" to the caller, but there may be nothing wrong with the
fwnode and id pointers.

I would return -ENODATA instead.

> +               *id = (u32)adr;
> +#else
> +               return ret;
> +#endif
> +       }
> +       return 0;
> +}
> +EXPORT_SYMBOL_GPL(fwnode_get_id);
> +
>  /**
>   * fwnode_get_parent - Return parent firwmare node
>   * @fwnode: Firmware whose parent is retrieved
> diff --git a/include/linux/property.h b/include/linux/property.h
> index 0a9001fe7aea..3f41475f010b 100644
> --- a/include/linux/property.h
> +++ b/include/linux/property.h
> @@ -82,6 +82,7 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
>
>  const char *fwnode_get_name(const struct fwnode_handle *fwnode);
>  const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
> +int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id);
>  struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
>  struct fwnode_handle *fwnode_get_next_parent(
>         struct fwnode_handle *fwnode);
> --
> 2.17.1
>
Rafael J. Wysocki Jan. 22, 2021, 6:21 p.m. UTC | #6
On Fri, Jan 22, 2021 at 7:11 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Fri, Jan 22, 2021 at 6:12 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> >
> > On Fri, Jan 22, 2021 at 05:40:41PM +0100, Rafael J. Wysocki wrote:
> > > On Fri, Jan 22, 2021 at 4:46 PM Calvin Johnson
> > > <calvin.johnson@oss.nxp.com> wrote:
> > > >
> > > > Using fwnode_get_id(), get the reg property value for DT node
> > > > or get the _ADR object value for ACPI node.
> > >
> > > So I'm not really sure if this is going to be generically useful.
> > >
> > > First of all, the meaning of the _ADR return value is specific to a
> > > given bus type (e.g. the PCI encoding of it is different from the I2C
> > > encoding of it) and it just happens to be matching the definition of
> > > the "reg" property for this particular binding.
> >
> > > IOW, not everyone may expect the "reg" property and the _ADR return
> > > value to have the same encoding and belong to the same set of values,
> >
> > I have counted three or even four attempts to open code exact this scenario
> > in the past couple of years. And I have no idea where to put a common base for
> > them so they will not duplicate this in each case.
>
> In that case it makes sense to have it in the core, but calling the
> _ADR return value an "id" generically is a stretch to put it lightly.
>
> It may be better to call the function something like
> fwnode_get_local_bus_id()

Or fwnode_get_local_address() for that matter.
Rafael J. Wysocki Jan. 22, 2021, 6:35 p.m. UTC | #7
On Fri, Jan 22, 2021 at 7:13 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Fri, Jan 22, 2021 at 4:46 PM Calvin Johnson
> <calvin.johnson@oss.nxp.com> wrote:
> >
> > Using fwnode_get_id(), get the reg property value for DT node
> > or get the _ADR object value for ACPI node.

This is not accurate AFAICS, because if the "reg" property is present
in the ACPI case, it will be returned then too.

> >
> > Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
> > ---
> >
> > Changes in v4:
> > - Improve code structure to handle all cases
> >
> > Changes in v3:
> > - Modified to retrieve reg property value for ACPI as well
> > - Resolved compilation issue with CONFIG_ACPI = n
> > - Added more info into documentation
> >
> > Changes in v2: None
> >
> >  drivers/base/property.c  | 34 ++++++++++++++++++++++++++++++++++
> >  include/linux/property.h |  1 +
> >  2 files changed, 35 insertions(+)
> >
> > diff --git a/drivers/base/property.c b/drivers/base/property.c
> > index 35b95c6ac0c6..f0581bbf7a4b 100644
> > --- a/drivers/base/property.c
> > +++ b/drivers/base/property.c
> > @@ -580,6 +580,40 @@ const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
> >         return fwnode_call_ptr_op(fwnode, get_name_prefix);
> >  }
> >
> > +/**
> > + * fwnode_get_id - Get the id of a fwnode.
> > + * @fwnode: firmware node
> > + * @id: id of the fwnode
> > + *
> > + * This function provides the id of a fwnode which can be either
> > + * DT or ACPI node. For ACPI, "reg" property value, if present will
> > + * be provided or else _ADR value will be provided.
> > + * Returns 0 on success or a negative errno.

What about using the following description instead of the above:

"Retrieve the value of the "reg" property for @fwnode which can be
either DT or ACPI node.  In the ACPI case, if the "reg" property is
missing, evaluate the _ADR object located under the given node, if
present, and provide its return value to the caller.

Return 0 on success or a negative error code.

This function can be used only if it is known valid to treat the _ADR
return value as a fallback replacement for the value of the "reg"
property that is missing in the given use case."

> > + */
> > +int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id)
> > +{
> > +#ifdef CONFIG_ACPI
> > +       unsigned long long adr;
> > +       acpi_status status;
> > +#endif
> > +       int ret;
> > +
> > +       ret = fwnode_property_read_u32(fwnode, "reg", id);
> > +       if (ret) {
> > +#ifdef CONFIG_ACPI
> > +               status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwnode),
> > +                                              METHOD_NAME__ADR, NULL, &adr);
> > +               if (ACPI_FAILURE(status))
> > +                       return -EINVAL;
>
> Please don't return -EINVAL from here, because this means "invalid
> argument" to the caller, but there may be nothing wrong with the
> fwnode and id pointers.
>
> I would return -ENODATA instead.
>
> > +               *id = (u32)adr;
> > +#else
> > +               return ret;
> > +#endif
> > +       }
> > +       return 0;
> > +}
> > +EXPORT_SYMBOL_GPL(fwnode_get_id);
> > +
> >  /**
> >   * fwnode_get_parent - Return parent firwmare node
> >   * @fwnode: Firmware whose parent is retrieved
> > diff --git a/include/linux/property.h b/include/linux/property.h
> > index 0a9001fe7aea..3f41475f010b 100644
> > --- a/include/linux/property.h
> > +++ b/include/linux/property.h
> > @@ -82,6 +82,7 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
> >
> >  const char *fwnode_get_name(const struct fwnode_handle *fwnode);
> >  const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
> > +int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id);
> >  struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
> >  struct fwnode_handle *fwnode_get_next_parent(
> >         struct fwnode_handle *fwnode);
> > --
> > 2.17.1
> >
diff mbox series

Patch

diff --git a/drivers/base/property.c b/drivers/base/property.c
index 35b95c6ac0c6..f0581bbf7a4b 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -580,6 +580,40 @@  const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
 	return fwnode_call_ptr_op(fwnode, get_name_prefix);
 }
 
+/**
+ * fwnode_get_id - Get the id of a fwnode.
+ * @fwnode: firmware node
+ * @id: id of the fwnode
+ *
+ * This function provides the id of a fwnode which can be either
+ * DT or ACPI node. For ACPI, "reg" property value, if present will
+ * be provided or else _ADR value will be provided.
+ * Returns 0 on success or a negative errno.
+ */
+int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id)
+{
+#ifdef CONFIG_ACPI
+	unsigned long long adr;
+	acpi_status status;
+#endif
+	int ret;
+
+	ret = fwnode_property_read_u32(fwnode, "reg", id);
+	if (ret) {
+#ifdef CONFIG_ACPI
+		status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwnode),
+					       METHOD_NAME__ADR, NULL, &adr);
+		if (ACPI_FAILURE(status))
+			return -EINVAL;
+		*id = (u32)adr;
+#else
+		return ret;
+#endif
+	}
+	return 0;
+}
+EXPORT_SYMBOL_GPL(fwnode_get_id);
+
 /**
  * fwnode_get_parent - Return parent firwmare node
  * @fwnode: Firmware whose parent is retrieved
diff --git a/include/linux/property.h b/include/linux/property.h
index 0a9001fe7aea..3f41475f010b 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -82,6 +82,7 @@  struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
 
 const char *fwnode_get_name(const struct fwnode_handle *fwnode);
 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
+int fwnode_get_id(struct fwnode_handle *fwnode, u32 *id);
 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
 struct fwnode_handle *fwnode_get_next_parent(
 	struct fwnode_handle *fwnode);