diff mbox series

[v2,1/3] tpm: of: Move of-tree specific code from tpm driver into of driver

Message ID 20220616154130.2052541-2-stefanb@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series tpm: Preserve TPM measurement log across kexec | expand

Commit Message

Stefan Berger June 16, 2022, 3:41 p.m. UTC
Simplify tpm_read_log_of() by moving Openfirmware-specific code into
the Openfirmware driver to make the code reusable. Call the new
of_tpm_get_sml_parameters() function from the TPM Openfirmware driver.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Cc: Jarkko Sakkinen <jarkko@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Rob Herring <robh+dt@kernel.org>
Cc: Frank Rowand <frowand.list@gmail.com>
---
 drivers/char/tpm/eventlog/of.c | 31 +++++--------------------------
 drivers/of/Makefile            |  2 +-
 drivers/of/device_node.c       | 27 +++++++++++++++++++++++++++
 include/linux/of_device_node.h |  9 +++++++++
 4 files changed, 42 insertions(+), 27 deletions(-)
 create mode 100644 drivers/of/device_node.c
 create mode 100644 include/linux/of_device_node.h

Comments

Rob Herring June 27, 2022, 10:43 p.m. UTC | #1
On Thu, Jun 16, 2022 at 11:41:28AM -0400, Stefan Berger wrote:
> Simplify tpm_read_log_of() by moving Openfirmware-specific code into
> the Openfirmware driver to make the code reusable. Call the new

There is no such 'Openfirmware driver'.

> of_tpm_get_sml_parameters() function from the TPM Openfirmware driver.
> 
> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
> Cc: Jarkko Sakkinen <jarkko@kernel.org>
> Cc: Jason Gunthorpe <jgg@ziepe.ca>
> Cc: Rob Herring <robh+dt@kernel.org>
> Cc: Frank Rowand <frowand.list@gmail.com>
> ---
>  drivers/char/tpm/eventlog/of.c | 31 +++++--------------------------
>  drivers/of/Makefile            |  2 +-
>  drivers/of/device_node.c       | 27 +++++++++++++++++++++++++++

Humm, definitely the wrong direction. Generally, code for specific 
bindings does not go in drivers/of/. There used to be some, but we've 
moved it to the appropriate subsystems. kexec was an exception to not 
have 2 copies of the same code in arch/.

>  include/linux/of_device_node.h |  9 +++++++++

of_tpm.h would be the right name assuming we kept this structure which 
we shouldn't. Probably linux/tpm.h? Just a guess, I'm not familar with 
the TPM code really.


>  4 files changed, 42 insertions(+), 27 deletions(-)
>  create mode 100644 drivers/of/device_node.c
>  create mode 100644 include/linux/of_device_node.h
> 
> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
> index a9ce66d09a75..5b18f4333ad1 100644
> --- a/drivers/char/tpm/eventlog/of.c
> +++ b/drivers/char/tpm/eventlog/of.c
> @@ -12,6 +12,7 @@
>  
>  #include <linux/slab.h>
>  #include <linux/of.h>
> +#include <linux/of_device_node.h>
>  #include <linux/tpm_eventlog.h>
>  
>  #include "../tpm.h"
> @@ -20,11 +21,10 @@
>  int tpm_read_log_of(struct tpm_chip *chip)
>  {
>  	struct device_node *np;
> -	const u32 *sizep;
> -	const u64 *basep;
>  	struct tpm_bios_log *log;
>  	u32 size;
>  	u64 base;
> +	int ret;
>  
>  	log = &chip->log;
>  	if (chip->dev.parent && chip->dev.parent->of_node)
> @@ -35,30 +35,9 @@ int tpm_read_log_of(struct tpm_chip *chip)
>  	if (of_property_read_bool(np, "powered-while-suspended"))
>  		chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
>  
> -	sizep = of_get_property(np, "linux,sml-size", NULL);
> -	basep = of_get_property(np, "linux,sml-base", NULL);
> -	if (sizep == NULL && basep == NULL)
> -		return -ENODEV;
> -	if (sizep == NULL || basep == NULL)
> -		return -EIO;
> -
> -	/*
> -	 * For both vtpm/tpm, firmware has log addr and log size in big
> -	 * endian format. But in case of vtpm, there is a method called
> -	 * sml-handover which is run during kernel init even before
> -	 * device tree is setup. This sml-handover function takes care
> -	 * of endianness and writes to sml-base and sml-size in little
> -	 * endian format. For this reason, vtpm doesn't need conversion
> -	 * but physical tpm needs the conversion.
> -	 */
> -	if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
> -	    of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
> -		size = be32_to_cpup((__force __be32 *)sizep);
> -		base = be64_to_cpup((__force __be64 *)basep);
> -	} else {
> -		size = *sizep;
> -		base = *basep;
> -	}
> +	ret = of_tpm_get_sml_parameters(np, &base, &size);
> +	if (ret < 0)
> +		return ret;
>  
>  	if (size == 0) {
>  		dev_warn(&chip->dev, "%s: Event log area empty\n", __func__);
> diff --git a/drivers/of/Makefile b/drivers/of/Makefile
> index e0360a44306e..1c9feac450ad 100644
> --- a/drivers/of/Makefile
> +++ b/drivers/of/Makefile
> @@ -1,5 +1,5 @@
>  # SPDX-License-Identifier: GPL-2.0
> -obj-y = base.o device.o platform.o property.o
> +obj-y = base.o device.o platform.o property.o device_node.o
>  obj-$(CONFIG_OF_KOBJ) += kobj.o
>  obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
>  obj-$(CONFIG_OF_FLATTREE) += fdt.o
> diff --git a/drivers/of/device_node.c b/drivers/of/device_node.c
> new file mode 100644
> index 000000000000..71a19bc1bac2
> --- /dev/null
> +++ b/drivers/of/device_node.c
> @@ -0,0 +1,27 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <linux/export.h>
> +#include <linux/of_device_node.h>
> +
> +int of_tpm_get_sml_parameters(struct device_node *np, u64 *base, u32 *size)
> +{
> +	const u32 *sizep;
> +	const u64 *basep;
> +
> +	sizep = of_get_property(np, "linux,sml-size", NULL);
> +	basep = of_get_property(np, "linux,sml-base", NULL);
> +	if (sizep == NULL && basep == NULL)
> +		return -ENODEV;
> +	if (sizep == NULL || basep == NULL)
> +		return -EIO;
> +
> +	if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
> +	    of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
> +		*size = be32_to_cpup((__force __be32 *)sizep);
> +		*base = be64_to_cpup((__force __be64 *)basep);
> +	} else {
> +		*size = *sizep;
> +		*base = *basep;
> +	}
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(of_tpm_get_sml_parameters);
> diff --git a/include/linux/of_device_node.h b/include/linux/of_device_node.h
> new file mode 100644
> index 000000000000..ae3faf023aab
> --- /dev/null
> +++ b/include/linux/of_device_node.h
> @@ -0,0 +1,9 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef _LINUX_OF_DEVICE_NODE_H
> +#define _LINUX_OF_DEVICE_NODE_H
> +
> +#include <linux/of.h>
> +
> +int of_tpm_get_sml_parameters(struct device_node *np, u64 *base, u32 *size);
> +
> +#endif /* _LINUX_OF_DEVICE_NODE_H */
> -- 
> 2.35.1
> 
>
Stefan Berger June 28, 2022, 1:29 p.m. UTC | #2
On 6/27/22 18:43, Rob Herring wrote:
> On Thu, Jun 16, 2022 at 11:41:28AM -0400, Stefan Berger wrote:
>> Simplify tpm_read_log_of() by moving Openfirmware-specific code into
>> the Openfirmware driver to make the code reusable. Call the new
> 
> There is no such 'Openfirmware driver'.
> 
>> of_tpm_get_sml_parameters() function from the TPM Openfirmware driver.
>>
>> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
>> Cc: Jarkko Sakkinen <jarkko@kernel.org>
>> Cc: Jason Gunthorpe <jgg@ziepe.ca>
>> Cc: Rob Herring <robh+dt@kernel.org>
>> Cc: Frank Rowand <frowand.list@gmail.com>
>> ---
>>   drivers/char/tpm/eventlog/of.c | 31 +++++--------------------------
>>   drivers/of/Makefile            |  2 +-
>>   drivers/of/device_node.c       | 27 +++++++++++++++++++++++++++
> 
> Humm, definitely the wrong direction. Generally, code for specific
> bindings does not go in drivers/of/. There used to be some, but we've
> moved it to the appropriate subsystems. kexec was an exception to not
> have 2 copies of the same code in arch/.

The function I am moving here is called by the TPM subsystem and also 
now by of/kexec.c. The latter is compiled under the following conditions:

ifdef CONFIG_KEXEC_FILE
ifdef CONFIG_OF_FLATTREE
obj-y	+= kexec.o
endif
endif

The code that current calls it is compiled under the following conditions:

tpm-$(CONFIG_OF) += eventlog/of.o

To make it available to both I could keep it in the TPM subsystem like this:

obj-$(CONFIG_OF) = tpm_of.o


Jarrko, if you read this, any comment?


    Stefan


> 
>>   include/linux/of_device_node.h |  9 +++++++++
> 
> of_tpm.h would be the right name assuming we kept this structure which
> we shouldn't. Probably linux/tpm.h? Just a guess, I'm not familar with
> the TPM code really.
> 
> 
>>   4 files changed, 42 insertions(+), 27 deletions(-)
>>   create mode 100644 drivers/of/device_node.c
>>   create mode 100644 include/linux/of_device_node.h
>>
>> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
>> index a9ce66d09a75..5b18f4333ad1 100644
>> --- a/drivers/char/tpm/eventlog/of.c
>> +++ b/drivers/char/tpm/eventlog/of.c
>> @@ -12,6 +12,7 @@
>>   
>>   #include <linux/slab.h>
>>   #include <linux/of.h>
>> +#include <linux/of_device_node.h>
>>   #include <linux/tpm_eventlog.h>
>>   
>>   #include "../tpm.h"
>> @@ -20,11 +21,10 @@
>>   int tpm_read_log_of(struct tpm_chip *chip)
>>   {
>>   	struct device_node *np;
>> -	const u32 *sizep;
>> -	const u64 *basep;
>>   	struct tpm_bios_log *log;
>>   	u32 size;
>>   	u64 base;
>> +	int ret;
>>   
>>   	log = &chip->log;
>>   	if (chip->dev.parent && chip->dev.parent->of_node)
>> @@ -35,30 +35,9 @@ int tpm_read_log_of(struct tpm_chip *chip)
>>   	if (of_property_read_bool(np, "powered-while-suspended"))
>>   		chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
>>   
>> -	sizep = of_get_property(np, "linux,sml-size", NULL);
>> -	basep = of_get_property(np, "linux,sml-base", NULL);
>> -	if (sizep == NULL && basep == NULL)
>> -		return -ENODEV;
>> -	if (sizep == NULL || basep == NULL)
>> -		return -EIO;
>> -
>> -	/*
>> -	 * For both vtpm/tpm, firmware has log addr and log size in big
>> -	 * endian format. But in case of vtpm, there is a method called
>> -	 * sml-handover which is run during kernel init even before
>> -	 * device tree is setup. This sml-handover function takes care
>> -	 * of endianness and writes to sml-base and sml-size in little
>> -	 * endian format. For this reason, vtpm doesn't need conversion
>> -	 * but physical tpm needs the conversion.
>> -	 */
>> -	if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
>> -	    of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
>> -		size = be32_to_cpup((__force __be32 *)sizep);
>> -		base = be64_to_cpup((__force __be64 *)basep);
>> -	} else {
>> -		size = *sizep;
>> -		base = *basep;
>> -	}
>> +	ret = of_tpm_get_sml_parameters(np, &base, &size);
>> +	if (ret < 0)
>> +		return ret;
>>   
>>   	if (size == 0) {
>>   		dev_warn(&chip->dev, "%s: Event log area empty\n", __func__);
>> diff --git a/drivers/of/Makefile b/drivers/of/Makefile
>> index e0360a44306e..1c9feac450ad 100644
>> --- a/drivers/of/Makefile
>> +++ b/drivers/of/Makefile
>> @@ -1,5 +1,5 @@
>>   # SPDX-License-Identifier: GPL-2.0
>> -obj-y = base.o device.o platform.o property.o
>> +obj-y = base.o device.o platform.o property.o device_node.o
>>   obj-$(CONFIG_OF_KOBJ) += kobj.o
>>   obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
>>   obj-$(CONFIG_OF_FLATTREE) += fdt.o
>> diff --git a/drivers/of/device_node.c b/drivers/of/device_node.c
>> new file mode 100644
>> index 000000000000..71a19bc1bac2
>> --- /dev/null
>> +++ b/drivers/of/device_node.c
>> @@ -0,0 +1,27 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +#include <linux/export.h>
>> +#include <linux/of_device_node.h>
>> +
>> +int of_tpm_get_sml_parameters(struct device_node *np, u64 *base, u32 *size)
>> +{
>> +	const u32 *sizep;
>> +	const u64 *basep;
>> +
>> +	sizep = of_get_property(np, "linux,sml-size", NULL);
>> +	basep = of_get_property(np, "linux,sml-base", NULL);
>> +	if (sizep == NULL && basep == NULL)
>> +		return -ENODEV;
>> +	if (sizep == NULL || basep == NULL)
>> +		return -EIO;
>> +
>> +	if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
>> +	    of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
>> +		*size = be32_to_cpup((__force __be32 *)sizep);
>> +		*base = be64_to_cpup((__force __be64 *)basep);
>> +	} else {
>> +		*size = *sizep;
>> +		*base = *basep;
>> +	}
>> +	return 0;
>> +}
>> +EXPORT_SYMBOL_GPL(of_tpm_get_sml_parameters);
>> diff --git a/include/linux/of_device_node.h b/include/linux/of_device_node.h
>> new file mode 100644
>> index 000000000000..ae3faf023aab
>> --- /dev/null
>> +++ b/include/linux/of_device_node.h
>> @@ -0,0 +1,9 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
>> +#ifndef _LINUX_OF_DEVICE_NODE_H
>> +#define _LINUX_OF_DEVICE_NODE_H
>> +
>> +#include <linux/of.h>
>> +
>> +int of_tpm_get_sml_parameters(struct device_node *np, u64 *base, u32 *size);
>> +
>> +#endif /* _LINUX_OF_DEVICE_NODE_H */
>> -- 
>> 2.35.1
>>
>>
Jarkko Sakkinen June 29, 2022, 2:45 a.m. UTC | #3
On Tue, Jun 28, 2022 at 09:29:48AM -0400, Stefan Berger wrote:
> 
> 
> On 6/27/22 18:43, Rob Herring wrote:
> > On Thu, Jun 16, 2022 at 11:41:28AM -0400, Stefan Berger wrote:
> > > Simplify tpm_read_log_of() by moving Openfirmware-specific code into
> > > the Openfirmware driver to make the code reusable. Call the new
> > 
> > There is no such 'Openfirmware driver'.
> > 
> > > of_tpm_get_sml_parameters() function from the TPM Openfirmware driver.
> > > 
> > > Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
> > > Cc: Jarkko Sakkinen <jarkko@kernel.org>
> > > Cc: Jason Gunthorpe <jgg@ziepe.ca>
> > > Cc: Rob Herring <robh+dt@kernel.org>
> > > Cc: Frank Rowand <frowand.list@gmail.com>
> > > ---
> > >   drivers/char/tpm/eventlog/of.c | 31 +++++--------------------------
> > >   drivers/of/Makefile            |  2 +-
> > >   drivers/of/device_node.c       | 27 +++++++++++++++++++++++++++
> > 
> > Humm, definitely the wrong direction. Generally, code for specific
> > bindings does not go in drivers/of/. There used to be some, but we've
> > moved it to the appropriate subsystems. kexec was an exception to not
> > have 2 copies of the same code in arch/.
> 
> The function I am moving here is called by the TPM subsystem and also now by
> of/kexec.c. The latter is compiled under the following conditions:
> 
> ifdef CONFIG_KEXEC_FILE
> ifdef CONFIG_OF_FLATTREE
> obj-y	+= kexec.o
> endif
> endif
> 
> The code that current calls it is compiled under the following conditions:
> 
> tpm-$(CONFIG_OF) += eventlog/of.o
> 
> To make it available to both I could keep it in the TPM subsystem like this:
> 
> obj-$(CONFIG_OF) = tpm_of.o
> 
> 
> Jarrko, if you read this, any comment?
> 
> 
>    Stefan

Why can't you convert of_tpm_get_sml_parameters() to inline function?

BR, Jarkko
Stefan Berger June 29, 2022, 2:16 p.m. UTC | #4
On 6/28/22 22:45, Jarkko Sakkinen wrote:
> On Tue, Jun 28, 2022 at 09:29:48AM -0400, Stefan Berger wrote:
>>
>>
>> On 6/27/22 18:43, Rob Herring wrote:
>>> On Thu, Jun 16, 2022 at 11:41:28AM -0400, Stefan Berger wrote:
>>>> Simplify tpm_read_log_of() by moving Openfirmware-specific code into
>>>> the Openfirmware driver to make the code reusable. Call the new
>>>
>>> There is no such 'Openfirmware driver'.
>>>
>>>> of_tpm_get_sml_parameters() function from the TPM Openfirmware driver.
>>>>
>>>> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
>>>> Cc: Jarkko Sakkinen <jarkko@kernel.org>
>>>> Cc: Jason Gunthorpe <jgg@ziepe.ca>
>>>> Cc: Rob Herring <robh+dt@kernel.org>
>>>> Cc: Frank Rowand <frowand.list@gmail.com>
>>>> ---
>>>>    drivers/char/tpm/eventlog/of.c | 31 +++++--------------------------
>>>>    drivers/of/Makefile            |  2 +-
>>>>    drivers/of/device_node.c       | 27 +++++++++++++++++++++++++++
>>>
>>> Humm, definitely the wrong direction. Generally, code for specific
>>> bindings does not go in drivers/of/. There used to be some, but we've
>>> moved it to the appropriate subsystems. kexec was an exception to not
>>> have 2 copies of the same code in arch/.
>>
>> The function I am moving here is called by the TPM subsystem and also now by
>> of/kexec.c. The latter is compiled under the following conditions:
>>
>> ifdef CONFIG_KEXEC_FILE
>> ifdef CONFIG_OF_FLATTREE
>> obj-y	+= kexec.o
>> endif
>> endif
>>
>> The code that current calls it is compiled under the following conditions:
>>
>> tpm-$(CONFIG_OF) += eventlog/of.o
>>
>> To make it available to both I could keep it in the TPM subsystem like this:
>>
>> obj-$(CONFIG_OF) = tpm_of.o
>>
>>
>> Jarrko, if you read this, any comment?
>>
>>
>>     Stefan
> 
> Why can't you convert of_tpm_get_sml_parameters() to inline function?

I can do that and put it into include/linux/tpm.h. The only concern 
would have been the size of the function.

Thanks,
    Stefan

> 
> BR, Jarkko
Jarkko Sakkinen June 30, 2022, 11:16 p.m. UTC | #5
On Wed, Jun 29, 2022 at 10:16:37AM -0400, Stefan Berger wrote:
> 
> 
> On 6/28/22 22:45, Jarkko Sakkinen wrote:
> > On Tue, Jun 28, 2022 at 09:29:48AM -0400, Stefan Berger wrote:
> > > 
> > > 
> > > On 6/27/22 18:43, Rob Herring wrote:
> > > > On Thu, Jun 16, 2022 at 11:41:28AM -0400, Stefan Berger wrote:
> > > > > Simplify tpm_read_log_of() by moving Openfirmware-specific code into
> > > > > the Openfirmware driver to make the code reusable. Call the new
> > > > 
> > > > There is no such 'Openfirmware driver'.
> > > > 
> > > > > of_tpm_get_sml_parameters() function from the TPM Openfirmware driver.
> > > > > 
> > > > > Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
> > > > > Cc: Jarkko Sakkinen <jarkko@kernel.org>
> > > > > Cc: Jason Gunthorpe <jgg@ziepe.ca>
> > > > > Cc: Rob Herring <robh+dt@kernel.org>
> > > > > Cc: Frank Rowand <frowand.list@gmail.com>
> > > > > ---
> > > > >    drivers/char/tpm/eventlog/of.c | 31 +++++--------------------------
> > > > >    drivers/of/Makefile            |  2 +-
> > > > >    drivers/of/device_node.c       | 27 +++++++++++++++++++++++++++
> > > > 
> > > > Humm, definitely the wrong direction. Generally, code for specific
> > > > bindings does not go in drivers/of/. There used to be some, but we've
> > > > moved it to the appropriate subsystems. kexec was an exception to not
> > > > have 2 copies of the same code in arch/.
> > > 
> > > The function I am moving here is called by the TPM subsystem and also now by
> > > of/kexec.c. The latter is compiled under the following conditions:
> > > 
> > > ifdef CONFIG_KEXEC_FILE
> > > ifdef CONFIG_OF_FLATTREE
> > > obj-y	+= kexec.o
> > > endif
> > > endif
> > > 
> > > The code that current calls it is compiled under the following conditions:
> > > 
> > > tpm-$(CONFIG_OF) += eventlog/of.o
> > > 
> > > To make it available to both I could keep it in the TPM subsystem like this:
> > > 
> > > obj-$(CONFIG_OF) = tpm_of.o
> > > 
> > > 
> > > Jarrko, if you read this, any comment?
> > > 
> > > 
> > >     Stefan
> > 
> > Why can't you convert of_tpm_get_sml_parameters() to inline function?
> 
> I can do that and put it into include/linux/tpm.h. The only concern would
> have been the size of the function.

It is somewhat insignificant amount (dozens of bytes at most).

BR, Jarkko
diff mbox series

Patch

diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
index a9ce66d09a75..5b18f4333ad1 100644
--- a/drivers/char/tpm/eventlog/of.c
+++ b/drivers/char/tpm/eventlog/of.c
@@ -12,6 +12,7 @@ 
 
 #include <linux/slab.h>
 #include <linux/of.h>
+#include <linux/of_device_node.h>
 #include <linux/tpm_eventlog.h>
 
 #include "../tpm.h"
@@ -20,11 +21,10 @@ 
 int tpm_read_log_of(struct tpm_chip *chip)
 {
 	struct device_node *np;
-	const u32 *sizep;
-	const u64 *basep;
 	struct tpm_bios_log *log;
 	u32 size;
 	u64 base;
+	int ret;
 
 	log = &chip->log;
 	if (chip->dev.parent && chip->dev.parent->of_node)
@@ -35,30 +35,9 @@  int tpm_read_log_of(struct tpm_chip *chip)
 	if (of_property_read_bool(np, "powered-while-suspended"))
 		chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
 
-	sizep = of_get_property(np, "linux,sml-size", NULL);
-	basep = of_get_property(np, "linux,sml-base", NULL);
-	if (sizep == NULL && basep == NULL)
-		return -ENODEV;
-	if (sizep == NULL || basep == NULL)
-		return -EIO;
-
-	/*
-	 * For both vtpm/tpm, firmware has log addr and log size in big
-	 * endian format. But in case of vtpm, there is a method called
-	 * sml-handover which is run during kernel init even before
-	 * device tree is setup. This sml-handover function takes care
-	 * of endianness and writes to sml-base and sml-size in little
-	 * endian format. For this reason, vtpm doesn't need conversion
-	 * but physical tpm needs the conversion.
-	 */
-	if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
-	    of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
-		size = be32_to_cpup((__force __be32 *)sizep);
-		base = be64_to_cpup((__force __be64 *)basep);
-	} else {
-		size = *sizep;
-		base = *basep;
-	}
+	ret = of_tpm_get_sml_parameters(np, &base, &size);
+	if (ret < 0)
+		return ret;
 
 	if (size == 0) {
 		dev_warn(&chip->dev, "%s: Event log area empty\n", __func__);
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index e0360a44306e..1c9feac450ad 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -1,5 +1,5 @@ 
 # SPDX-License-Identifier: GPL-2.0
-obj-y = base.o device.o platform.o property.o
+obj-y = base.o device.o platform.o property.o device_node.o
 obj-$(CONFIG_OF_KOBJ) += kobj.o
 obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
 obj-$(CONFIG_OF_FLATTREE) += fdt.o
diff --git a/drivers/of/device_node.c b/drivers/of/device_node.c
new file mode 100644
index 000000000000..71a19bc1bac2
--- /dev/null
+++ b/drivers/of/device_node.c
@@ -0,0 +1,27 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/export.h>
+#include <linux/of_device_node.h>
+
+int of_tpm_get_sml_parameters(struct device_node *np, u64 *base, u32 *size)
+{
+	const u32 *sizep;
+	const u64 *basep;
+
+	sizep = of_get_property(np, "linux,sml-size", NULL);
+	basep = of_get_property(np, "linux,sml-base", NULL);
+	if (sizep == NULL && basep == NULL)
+		return -ENODEV;
+	if (sizep == NULL || basep == NULL)
+		return -EIO;
+
+	if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
+	    of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
+		*size = be32_to_cpup((__force __be32 *)sizep);
+		*base = be64_to_cpup((__force __be64 *)basep);
+	} else {
+		*size = *sizep;
+		*base = *basep;
+	}
+	return 0;
+}
+EXPORT_SYMBOL_GPL(of_tpm_get_sml_parameters);
diff --git a/include/linux/of_device_node.h b/include/linux/of_device_node.h
new file mode 100644
index 000000000000..ae3faf023aab
--- /dev/null
+++ b/include/linux/of_device_node.h
@@ -0,0 +1,9 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_OF_DEVICE_NODE_H
+#define _LINUX_OF_DEVICE_NODE_H
+
+#include <linux/of.h>
+
+int of_tpm_get_sml_parameters(struct device_node *np, u64 *base, u32 *size);
+
+#endif /* _LINUX_OF_DEVICE_NODE_H */