[PATCH v3 1/3] tpm: of: Make of-tree specific function commonly available

From: Stefan Berger
Date: Tue Jun 28 2022 - 12:59:15 EST


Simplify tpm_read_log_of() by moving reusable parts of the code into
its own file to make the code commonly available so it can be used also
for kexec support. Call the new of_tpm_get_sml_parameters() function
from the TPM Open Firmware driver.

Compile the new file when CONFIG_OF is enabled so that
of_tpm_get_sml_parameters() can be called from kexec even if the TPM
subsystem is not enabled.

Signed-off-by: Stefan Berger <stefanb@xxxxxxxxxxxxx>
Cc: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
Cc: Jason Gunthorpe <jgg@xxxxxxxx>
Cc: Rob Herring <robh+dt@xxxxxxxxxx>
Cc: Frank Rowand <frowand.list@xxxxxxxxx>
---
drivers/char/Makefile | 1 +
drivers/char/tpm/Makefile | 1 +
drivers/char/tpm/eventlog/of.c | 31 +++++-------------------------
drivers/char/tpm/eventlog/tpm_of.c | 27 ++++++++++++++++++++++++++
include/linux/tpm.h | 5 +++++
5 files changed, 39 insertions(+), 26 deletions(-)
create mode 100644 drivers/char/tpm/eventlog/tpm_of.c

diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index 264eb398fdd4..af9edafb944e 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_PCMCIA) += pcmcia/

obj-$(CONFIG_HANGCHECK_TIMER) += hangcheck-timer.o
obj-$(CONFIG_TCG_TPM) += tpm/
+obj-$(CONFIG_OF) += tpm/

obj-$(CONFIG_PS3_FLASH) += ps3flash.o

diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index 66d39ea6bd10..3c0eaac81f5c 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -19,6 +19,7 @@ tpm-y += eventlog/tpm2.o
tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o
tpm-$(CONFIG_EFI) += eventlog/efi.o
tpm-$(CONFIG_OF) += eventlog/of.o
+obj-$(CONFIG_OF) += eventlog/tpm_of.o
obj-$(CONFIG_TCG_TIS_CORE) += tpm_tis_core.o
obj-$(CONFIG_TCG_TIS) += tpm_tis.o
obj-$(CONFIG_TCG_TIS_SYNQUACER) += tpm_tis_synquacer.o
diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
index a9ce66d09a75..f9462d19632e 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/tpm.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/char/tpm/eventlog/tpm_of.c b/drivers/char/tpm/eventlog/tpm_of.c
new file mode 100644
index 000000000000..2e6cfcc87035
--- /dev/null
+++ b/drivers/char/tpm/eventlog/tpm_of.c
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/export.h>
+#include <linux/tpm.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/tpm.h b/include/linux/tpm.h
index dfeb25a0362d..429bf045db80 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -460,4 +460,9 @@ static inline struct tpm_chip *tpm_default_chip(void)
return NULL;
}
#endif
+
+#ifdef CONFIG_OF
+int of_tpm_get_sml_parameters(struct device_node *np, u64 *base, u32 *size);
+#endif
+
#endif
--
2.35.1