Skip to contentSkip to conversation

tpm: Add driver for cr50

PATCH Superseded Stephen Boyd 3 participants 20 messages Aug 12, 2019 – Aug 27, 2019

Subsystems (3)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover tpm: Add driver for cr50

This patch series adds support for the the H1 secure microcontroller running cr50 firmware found on various recent Chromebooks. This driver is necessary to boot into a ChromeOS userspace environment. It implements support for several functions, including TPM-like functionality over a SPI interface.

The last time this was series sent looks to be [1]. I've looked over the patches and review comments and tried to address any feedback that Andrey didn't address (really minor things like newlines). I've reworked the patches from the last version to layer on top of the existing TPM TIS SPI implementation in tpm_tis_spi.c. Hopefully this is more palatable than combining the two drivers together into one file.

[1] https://lkml.kernel.org/r/[email protected]

TODO:
* Add a patch to spit out WARN_ON() when TPM is suspended and some kernel code attempts to use it
* Rework the i2c driver per Alexander's comments on v2

Changes from v3:
* Split out hooks into separate patches
* Update commit text to not say "libify"
* Collapse if statement into one for first patch
* Update commit text on first patch to mention flag
* Drop TIS_IS_CR50 as it's unused

Changes from v2:
* Sent khwrng thread patch separately
* New patch to expose TPM SPI functionality from tpm_tis_spi.c
* Usage of that new patch in cr50 SPI driver
* Drop i2c version of cr50 SPI driver for now (will resend later)
* New patch to add a TPM chip flag indicating TPM shouldn't be reset over suspend. Allows us to get rid of the cr50 suspend/resume functions that are mostly generic

Changes from v1:
* Dropped symlink and sysfs patches
* Removed 'is_suspended' bits
* Added new patch to freeze khwrng thread
* Moved binding to google,cr50.txt and added Reviewed-by tag from Rob

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>

Andrey Pronin (2):
dt-bindings: tpm: document properties for cr50
tpm: add driver for cr50 on SPI

Stephen Boyd (4):
tpm: Add a flag to indicate TPM power is managed by firmware
tpm: tpm_tis_spi: Introduce a flow control callback
tpm: tpm_tis_spi: Add a pre-transfer callback
tpm: tpm_tis_spi: Export functionality to other drivers

 .../bindings/security/tpm/google,cr50.txt     |  19 +
 drivers/char/tpm/Kconfig                      |   9 +
 drivers/char/tpm/Makefile                     |   1 +
 drivers/char/tpm/cr50_spi.c                   | 372 ++++++++++++++++++
 drivers/char/tpm/tpm-interface.c              |   8 +-
 drivers/char/tpm/tpm.h                        |   1 +
 drivers/char/tpm/tpm_tis_spi.c                |  98 +++--
 drivers/char/tpm/tpm_tis_spi.h                |  37 ++
 8 files changed, 502 insertions(+), 43 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/security/tpm/google,cr50.txt
 create mode 100644 drivers/char/tpm/cr50_spi.c
 create mode 100644 drivers/char/tpm/tpm_tis_spi.h

base-commit: 0ecfebd2b52404ae0c54a878c872bb93363ada36
prerequisite-patch-id: ce0cac49be5e67df1427e4207cf38c6e31091445

Signature · 1 line
Sent by a computer through tubes
#1 lore raw

PATCH v4 1/6 tpm: Add a flag to indicate TPM power is managed by firmware

On some platforms, the TPM power is managed by firmware and therefore we don't need to stop the TPM on suspend when going to a light version of suspend such as S0ix ("freeze" suspend state). Add a chip flag, TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED, to indicate this so that certain platforms can probe for the usage of this light suspend and avoid touching the TPM state across suspend/resume.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 drivers/char/tpm/tpm-interface.c | 8 +++++++-
 drivers/char/tpm/tpm.h           | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm-interface.c +7 -1
index 1b4f95c13e00..0b3def8e8186 100644
@@ -23,6 +23,7 @@
 #include <linux/slab.h>
 #include <linux/mutex.h>
 #include <linux/spinlock.h>
+#include <linux/suspend.h>
 #include <linux/freezer.h>
 #include <linux/tpm_eventlog.h>
 
@@ -395,7 +396,11 @@ int tpm_pm_suspend(struct device *dev)
 		return -ENODEV;
 
 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
-		return 0;
+		goto suspended;
+
+	if ((chip->flags & TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED) &&
+	    !pm_suspend_via_firmware())
+		goto suspended;
 
 	if (!tpm_chip_start(chip)) {
 		if (chip->flags & TPM_CHIP_FLAG_TPM2)
@@ -406,6 +411,7 @@ int tpm_pm_suspend(struct device *dev)
 		tpm_chip_stop(chip);
 	}
 
+suspended:
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
drivers/char/tpm/tpm.h +1 -0
index e503ffc3aa39..28f73331aa2e 100644
@@ -162,6 +162,7 @@ enum tpm_chip_flags {
 	TPM_CHIP_FLAG_VIRTUAL		= BIT(3),
 	TPM_CHIP_FLAG_HAVE_TIMEOUTS	= BIT(4),
 	TPM_CHIP_FLAG_ALWAYS_POWERED	= BIT(5),
+	TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED	= BIT(6),
 };
 
 #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
Signature · 1 line
Sent by a computer through tubes
Reviewed-by on v4 1/6
#2 lore raw

Re: [PATCH v4 1/6] tpm: Add a flag to indicate TPM power is managed by firmware

Quoted 7 lines · Stephen Boyd

On some platforms, the TPM power is managed by firmware and therefore we don't need to stop the TPM on suspend when going to a light version of suspend such as S0ix ("freeze" suspend state). Add a chip flag, TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED, to indicate this so that certain platforms can probe for the usage of this light suspend and avoid touching the TPM state across suspend/resume.

Quoted 8 lines
Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>

LGTM

/Jarkko

#3 lore raw

PATCH v4 2/6 tpm: tpm_tis_spi: Introduce a flow control callback

In reply to Stephen Boyd

Cr50 firmware has a different flow control protocol than the one used by this TPM PTP SPI driver. Introduce a flow control callback so we can override the standard sequence with the custom one that Cr50 uses.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 drivers/char/tpm/tpm_tis_spi.c | 55 +++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 21 deletions(-)
drivers/char/tpm/tpm_tis_spi.c +34 -21
index 19513e622053..819602e85b34 100644
@@ -42,6 +42,8 @@
 struct tpm_tis_spi_phy {
 	struct tpm_tis_data priv;
 	struct spi_device *spi_device;
+	int (*flow_control)(struct tpm_tis_spi_phy *phy,
+			    struct spi_transfer *xfer);
 	u8 *iobuf;
 };
 
@@ -50,12 +52,39 @@ static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *da
 	return container_of(data, struct tpm_tis_spi_phy, priv);
 }
 
+static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy *phy,
+				    struct spi_transfer *spi_xfer)
+{
+	struct spi_message m;
+	int ret, i;
+
+	if ((phy->iobuf[3] & 0x01) == 0) {
+		// handle SPI wait states
+		phy->iobuf[0] = 0;
+
+		for (i = 0; i < TPM_RETRY; i++) {
+			spi_xfer->len = 1;
+			spi_message_init(&m);
+			spi_message_add_tail(spi_xfer, &m);
+			ret = spi_sync_locked(phy->spi_device, &m);
+			if (ret < 0)
+				return ret;
+			if (phy->iobuf[0] & 0x01)
+				break;
+		}
+
+		if (i == TPM_RETRY)
+			return -ETIMEDOUT;
+	}
+
+	return 0;
+}
+
 static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
 				u8 *in, const u8 *out)
 {
 	struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
 	int ret = 0;
-	int i;
 	struct spi_message m;
 	struct spi_transfer spi_xfer;
 	u8 transfer_len;
@@ -82,26 +111,9 @@ static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
 		if (ret < 0)
 			goto exit;
 
-		if ((phy->iobuf[3] & 0x01) == 0) {
-			// handle SPI wait states
-			phy->iobuf[0] = 0;
-
-			for (i = 0; i < TPM_RETRY; i++) {
-				spi_xfer.len = 1;
-				spi_message_init(&m);
-				spi_message_add_tail(&spi_xfer, &m);
-				ret = spi_sync_locked(phy->spi_device, &m);
-				if (ret < 0)
-					goto exit;
-				if (phy->iobuf[0] & 0x01)
-					break;
-			}
-
-			if (i == TPM_RETRY) {
-				ret = -ETIMEDOUT;
-				goto exit;
-			}
-		}
+		ret = phy->flow_control(phy, &spi_xfer);
+		if (ret < 0)
+			goto exit;
 
 		spi_xfer.cs_change = 0;
 		spi_xfer.len = transfer_len;
@@ -207,6 +219,7 @@ static int tpm_tis_spi_probe(struct spi_device *dev)
 	phy->iobuf = devm_kmalloc(&dev->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
 	if (!phy->iobuf)
 		return -ENOMEM;
+	phy->flow_control = tpm_tis_spi_flow_control;
 
 	/* If the SPI device has an IRQ then use that */
 	if (dev->irq > 0)
Signature · 1 line
Sent by a computer through tubes
#4 lore raw

Re: [PATCH v4 2/6] tpm: tpm_tis_spi: Introduce a flow control callback

Quoted 50 lines · Stephen Boyd

Cr50 firmware has a different flow control protocol than the one used by this TPM PTP SPI driver. Introduce a flow control callback so we can override the standard sequence with the custom one that Cr50 uses.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 drivers/char/tpm/tpm_tis_spi.c | 55 +++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 21 deletions(-)
drivers/char/tpm/tpm_tis_spi.c +19 -0
index 19513e622053..819602e85b34 100644
@@ -42,6 +42,8 @@
 struct tpm_tis_spi_phy {
 	struct tpm_tis_data priv;
 	struct spi_device *spi_device;
+	int (*flow_control)(struct tpm_tis_spi_phy *phy,
+			    struct spi_transfer *xfer);
 	u8 *iobuf;
 };
 
@@ -50,12 +52,39 @@ static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *da
 	return container_of(data, struct tpm_tis_spi_phy, priv);
 }
 
+static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy *phy,
+				    struct spi_transfer *spi_xfer)
+{
+	struct spi_message m;
+	int ret, i;
+
+	if ((phy->iobuf[3] & 0x01) == 0) {
+		// handle SPI wait states
+		phy->iobuf[0] = 0;
+
+		for (i = 0; i < TPM_RETRY; i++) {
+			spi_xfer->len = 1;
+			spi_message_init(&m);
+			spi_message_add_tail(spi_xfer, &m);
+			ret = spi_sync_locked(phy->spi_device, &m);
+			if (ret < 0)
+				return ret;

drivers/char/tpm/tpm_tis_spi.c static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *da · line 81

Quoted 10 lines
drivers/char/tpm/tpm_tis_spi.c +10 -0
+			if (phy->iobuf[0] & 0x01)
+				break;
+		}
+
+		if (i == TPM_RETRY)
+			return -ETIMEDOUT;
+	}
+
+	return 0;
+}

AFAIK the flow control is not part of the SPI standard itself but is proprietary for each slave device. Thus, the flow control should be documented to the source code. I do not want flow control mechanisms to be multiplied before this is done.

The magic number 0x01 would be also good to get rid off.

/Jarkko

#5 lore raw

Re: [PATCH v4 2/6] tpm: tpm_tis_spi: Introduce a flow control callback

Quoting Jarkko Sakkinen (2019-08-19 09:32:40)

Quoted 58 lines · Stephen Boyd
Quoted 57 lines

Cr50 firmware has a different flow control protocol than the one used by this TPM PTP SPI driver. Introduce a flow control callback so we can override the standard sequence with the custom one that Cr50 uses.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 drivers/char/tpm/tpm_tis_spi.c | 55 +++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 21 deletions(-)
drivers/char/tpm/tpm_tis_spi.c +26 -0
index 19513e622053..819602e85b34 100644
@@ -42,6 +42,8 @@
 struct tpm_tis_spi_phy {
      struct tpm_tis_data priv;
      struct spi_device *spi_device;
+     int (*flow_control)(struct tpm_tis_spi_phy *phy,
+                         struct spi_transfer *xfer);
      u8 *iobuf;
 };
 
@@ -50,12 +52,39 @@ static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *da
      return container_of(data, struct tpm_tis_spi_phy, priv);
 }
 
+static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy *phy,
+                                 struct spi_transfer *spi_xfer)
+{
+     struct spi_message m;
+     int ret, i;
+
+     if ((phy->iobuf[3] & 0x01) == 0) {
+             // handle SPI wait states
+             phy->iobuf[0] = 0;
+
+             for (i = 0; i < TPM_RETRY; i++) {
+                     spi_xfer->len = 1;
+                     spi_message_init(&m);
+                     spi_message_add_tail(spi_xfer, &m);
+                     ret = spi_sync_locked(phy->spi_device, &m);
+                     if (ret < 0)
+                             return ret;
+                     if (phy->iobuf[0] & 0x01)
+                             break;
+             }
+
+             if (i == TPM_RETRY)
+                     return -ETIMEDOUT;
+     }
Quoted 8 lines

drivers/char/tpm/tpm_tis_spi.c static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *da · line 81

Quoted 3 lines
drivers/char/tpm/tpm_tis_spi.c +3 -0
+
+     return 0;
+}

AFAIK the flow control is not part of the SPI standard itself but is proprietary for each slave device. Thus, the flow control should be documented to the source code. I do not want flow control mechanisms to be multiplied before this is done.

Can you clarify this please? I don't understand what "the flow control should be documented to the source code" means.

Quoted 3 lines

The magic number 0x01 would be also good to get rid off.

Ok. What name should the #define be? I can make that another patch.

#6 lore raw

Re: [PATCH v4 2/6] tpm: tpm_tis_spi: Introduce a flow control callback

Quoted 7 lines · Stephen Boyd
Quoted 4 lines

AFAIK the flow control is not part of the SPI standard itself but is proprietary for each slave device. Thus, the flow control should be documented to the source code. I do not want flow control mechanisms to be multiplied before this is done.

Can you clarify this please? I don't understand what "the flow control should be documented to the source code" means.

Off the top of my head:

/* TCG SPI flow control is documented in the section 6.4 of [1]. However,
* Google's CR50 chip has its own proprietary flow control. This struct
* is used to bind the appropriate flow control mechanism.
*
* [1] https://trustedcomputinggroup.org/resource/pc-client-platform-tpm-profile-ptp-specification/
*/

Quoted 5 lines
Quoted 3 lines

The magic number 0x01 would be also good to get rid off.

Ok. What name should the #define be? I can make that another patch.

Do nothing. Not part of your patch set scope, was a stupid comment from my side.

/Jarkko

#7 lore raw

PATCH v4 3/6 tpm: tpm_tis_spi: Add a pre-transfer callback

In reply to Stephen Boyd

Cr50 firmware has a requirement to wait for the TPM to wakeup before sending commands over the SPI bus. Otherwise, the firmware could be in deep sleep and not respond. Add a hook to tpm_tis_spi_transfer() before we start a SPI transfer so we can keep track of the last time the TPM driver accessed the SPI bus.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 drivers/char/tpm/tpm_tis_spi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/char/tpm/tpm_tis_spi.c +3 -0
index 819602e85b34..93f49b1941f0 100644
@@ -44,6 +44,7 @@ struct tpm_tis_spi_phy {
 	struct spi_device *spi_device;
 	int (*flow_control)(struct tpm_tis_spi_phy *phy,
 			    struct spi_transfer *xfer);
+	void (*pre_transfer)(struct tpm_tis_spi_phy *phy);
 	u8 *iobuf;
 };
 
@@ -129,6 +130,8 @@ static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
 
 		spi_message_init(&m);
 		spi_message_add_tail(&spi_xfer, &m);
+		if (phy->pre_transfer)
+			phy->pre_transfer(phy);
 		ret = spi_sync_locked(phy->spi_device, &m);
 		if (ret < 0)
 			goto exit;
Signature · 1 line
Sent by a computer through tubes
#8 lore raw

Re: [PATCH v4 3/6] tpm: tpm_tis_spi: Add a pre-transfer callback

Quoted 23 lines · Stephen Boyd

Cr50 firmware has a requirement to wait for the TPM to wakeup before sending commands over the SPI bus. Otherwise, the firmware could be in deep sleep and not respond. Add a hook to tpm_tis_spi_transfer() before we start a SPI transfer so we can keep track of the last time the TPM driver accessed the SPI bus.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 drivers/char/tpm/tpm_tis_spi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/char/tpm/tpm_tis_spi.c +0 -0
index 819602e85b34..93f49b1941f0 100644

drivers/char/tpm/tpm_tis_spi.c struct tpm_tis_spi_phy { · line 47

Quoted 4 lines
drivers/char/tpm/tpm_tis_spi.c +1 -0
@@ -44,6 +44,7 @@ struct tpm_tis_spi_phy {
 	struct spi_device *spi_device;
 	int (*flow_control)(struct tpm_tis_spi_phy *phy,
 			    struct spi_transfer *xfer);
+	void (*pre_transfer)(struct tpm_tis_spi_phy *phy);

A callback should have somewhat well defined purpose. A callback named as pre_transfer() could have any purpose.

/Jarkko

#9 lore raw

Re: [PATCH v4 3/6] tpm: tpm_tis_spi: Add a pre-transfer callback

Quoting Jarkko Sakkinen (2019-08-19 09:35:05)

Quoted 19 lines · Stephen Boyd
Quoted 18 lines

Cr50 firmware has a requirement to wait for the TPM to wakeup before sending commands over the SPI bus. Otherwise, the firmware could be in deep sleep and not respond. Add a hook to tpm_tis_spi_transfer() before we start a SPI transfer so we can keep track of the last time the TPM driver accessed the SPI bus.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 drivers/char/tpm/tpm_tis_spi.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 13 lines

drivers/char/tpm/tpm_tis_spi.c struct tpm_tis_spi_phy { · line 47

Quoted 9 lines
drivers/char/tpm/tpm_tis_spi.c +1 -0
index 819602e85b34..93f49b1941f0 100644
@@ -44,6 +44,7 @@ struct tpm_tis_spi_phy {
      struct spi_device *spi_device;
      int (*flow_control)(struct tpm_tis_spi_phy *phy,
                          struct spi_transfer *xfer);
+     void (*pre_transfer)(struct tpm_tis_spi_phy *phy);

A callback should have somewhat well defined purpose. A callback named as pre_transfer() could have any purpose.

Any name is fine for me. Any suggestions?

#10 lore raw

Re: [PATCH v4 3/6] tpm: tpm_tis_spi: Add a pre-transfer callback

Quoted 1 line · Stephen Boyd

Any name is fine for me. Any suggestions?

What if just add @ready to struct tpm_tis_spi_phy add drop this patch altogether?

It is only used only by CR50 but I think it is less of an overkill than adding a callback.

/Jarkko

#11 lore raw

Re: [PATCH v4 3/6] tpm: tpm_tis_spi: Add a pre-transfer callback

Quoting Jarkko Sakkinen (2019-08-21 12:11:31)

Quoted 9 lines · Stephen Boyd
Quoted 1 line

Any name is fine for me. Any suggestions?

What if just add @ready to struct tpm_tis_spi_phy add drop this patch altogether?

It is only used only by CR50 but I think it is less of an overkill than adding a callback.

Ok, sounds good.

#12 lore raw

PATCH v4 4/6 tpm: tpm_tis_spi: Export functionality to other drivers

In reply to Stephen Boyd

Export a new function, tpm_tis_spi_init(), and the associated read/write/transfer APIs so that we can create variant drivers based on the core functionality of this TPM SPI driver. Variant drivers can wrap the tpm_tis_spi_phy struct with their own struct and override the behavior of tpm_tis_spi_transfer() by supplying their own flow control and pre-transfer hooks. This shares the most code between the core driver and any variants that want to override certain behavior without cluttering the core driver.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 drivers/char/tpm/tpm_tis_spi.c | 52 ++++++++++++++++------------------
 drivers/char/tpm/tpm_tis_spi.h | 37 ++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 27 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_tis_spi.h
drivers/char/tpm/tpm_tis_spi.c +25 -27
index 93f49b1941f0..fd3fb4f9f506 100644
@@ -36,23 +36,10 @@
 #include <linux/tpm.h>
 #include "tpm.h"
 #include "tpm_tis_core.h"
+#include "tpm_tis_spi.h"
 
 #define MAX_SPI_FRAMESIZE 64
 
-struct tpm_tis_spi_phy {
-	struct tpm_tis_data priv;
-	struct spi_device *spi_device;
-	int (*flow_control)(struct tpm_tis_spi_phy *phy,
-			    struct spi_transfer *xfer);
-	void (*pre_transfer)(struct tpm_tis_spi_phy *phy);
-	u8 *iobuf;
-};
-
-static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data)
-{
-	return container_of(data, struct tpm_tis_spi_phy, priv);
-}
-
 static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy *phy,
 				    struct spi_transfer *spi_xfer)
 {
@@ -81,7 +68,7 @@ static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy *phy,
 	return 0;
 }
 
-static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
+int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
 				u8 *in, const u8 *out)
 {
 	struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
@@ -148,9 +135,10 @@ static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
 	spi_bus_unlock(phy->spi_device->master);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(tpm_tis_spi_transfer);
 
-static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
-				  u16 len, u8 *result)
+static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
+				  u8 *result)
 {
 	return tpm_tis_spi_transfer(data, addr, len, result, NULL);
 }
@@ -161,7 +149,7 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 	return tpm_tis_spi_transfer(data, addr, len, NULL, value);
 }
 
-static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
+int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
 {
 	__le16 result_le;
 	int rc;
@@ -173,8 +161,9 @@ static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
 
 	return rc;
 }
+EXPORT_SYMBOL_GPL(tpm_tis_spi_read16);
 
-static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
+int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
 {
 	__le32 result_le;
 	int rc;
@@ -186,8 +175,9 @@ static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
 
 	return rc;
 }
+EXPORT_SYMBOL_GPL(tpm_tis_spi_read32);
 
-static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
+int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
 {
 	__le32 value_le;
 	int rc;
@@ -198,6 +188,20 @@ static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
 
 	return rc;
 }
+EXPORT_SYMBOL_GPL(tpm_tis_spi_write32);
+
+int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
+		     int irq, const struct tpm_tis_phy_ops *phy_ops)
+{
+	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
+	if (!phy->iobuf)
+		return -ENOMEM;
+
+	phy->spi_device = spi;
+
+	return tpm_tis_core_init(&spi->dev, &phy->priv, irq, phy_ops, NULL);
+}
+EXPORT_SYMBOL_GPL(tpm_tis_spi_init);
 
 static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
 	.read_bytes = tpm_tis_spi_read_bytes,
@@ -217,11 +221,6 @@ static int tpm_tis_spi_probe(struct spi_device *dev)
 	if (!phy)
 		return -ENOMEM;
 
-	phy->spi_device = dev;
-
-	phy->iobuf = devm_kmalloc(&dev->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
-	if (!phy->iobuf)
-		return -ENOMEM;
 	phy->flow_control = tpm_tis_spi_flow_control;
 
 	/* If the SPI device has an IRQ then use that */
@@ -230,8 +229,7 @@ static int tpm_tis_spi_probe(struct spi_device *dev)
 	else
 		irq = -1;
 
-	return tpm_tis_core_init(&dev->dev, &phy->priv, irq, &tpm_spi_phy_ops,
-				 NULL);
+	return tpm_tis_spi_init(dev, phy, irq, &tpm_spi_phy_ops);
 }
 
 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
drivers/char/tpm/tpm_tis_spi.h new file +37 -0
new file mode 100644
index 000000000000..48be5130794a
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2015 Infineon Technologies AG
+ * Copyright (C) 2016 STMicroelectronics SAS
+ */
+
+#ifndef TPM_TIS_SPI_H
+#define TPM_TIS_SPI_H
+
+#include "tpm_tis_core.h"
+
+struct tpm_tis_spi_phy {
+	struct tpm_tis_data priv;
+	struct spi_device *spi_device;
+	int (*flow_control)(struct tpm_tis_spi_phy *phy,
+			     struct spi_transfer *xfer);
+	void (*pre_transfer)(struct tpm_tis_spi_phy *phy);
+
+	u8 *iobuf;
+};
+
+static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data)
+{
+	return container_of(data, struct tpm_tis_spi_phy, priv);
+}
+
+extern int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
+			    int irq, const struct tpm_tis_phy_ops *phy_ops);
+
+extern int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
+				u8 *in, const u8 *out);
+
+extern int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result);
+extern int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result);
+extern int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value);
+
+#endif
Signature · 1 line
Sent by a computer through tubes
#13 lore raw

Re: [PATCH v4 4/6] tpm: tpm_tis_spi: Export functionality to other drivers

Quoted 8 lines · Stephen Boyd

Export a new function, tpm_tis_spi_init(), and the associated read/write/transfer APIs so that we can create variant drivers based on the core functionality of this TPM SPI driver. Variant drivers can wrap the tpm_tis_spi_phy struct with their own struct and override the behavior of tpm_tis_spi_transfer() by supplying their own flow control and pre-transfer hooks. This shares the most code between the core driver and any variants that want to override certain behavior without cluttering the core driver.

I think this is adding way too much complexity for the purpose. We definitely do want this three layer architecture here.

Instead there should be a single tpm_tis_spi driver that dynamically either TCG or CR50. I rather take some extra bytes in the LKM than the added complexity.

/Jarkko

#14 lore raw

Re: [PATCH v4 4/6] tpm: tpm_tis_spi: Export functionality to other drivers

Quoting Jarkko Sakkinen (2019-08-19 09:40:05)

Quoted 10 lines · Stephen Boyd
Quoted 8 lines

Export a new function, tpm_tis_spi_init(), and the associated read/write/transfer APIs so that we can create variant drivers based on the core functionality of this TPM SPI driver. Variant drivers can wrap the tpm_tis_spi_phy struct with their own struct and override the behavior of tpm_tis_spi_transfer() by supplying their own flow control and pre-transfer hooks. This shares the most code between the core driver and any variants that want to override certain behavior without cluttering the core driver.

Quoted 7 lines

I think this is adding way too much complexity for the purpose. We definitely do want this three layer architecture here.

Instead there should be a single tpm_tis_spi driver that dynamically either TCG or CR50. I rather take some extra bytes in the LKM than the added complexity.

Ok. I had that patch originally[1]. Do you want me to resend that patch and start review over from there?

[1] https://lkml.kernel.org/r/[email protected]

#15 lore raw

Re: [PATCH v4 4/6] tpm: tpm_tis_spi: Export functionality to other drivers

Quoted 19 lines · Stephen Boyd

Quoting Jarkko Sakkinen (2019-08-19 09:40:05)

Quoted 10 lines · Stephen Boyd
Quoted 8 lines

Export a new function, tpm_tis_spi_init(), and the associated read/write/transfer APIs so that we can create variant drivers based on the core functionality of this TPM SPI driver. Variant drivers can wrap the tpm_tis_spi_phy struct with their own struct and override the behavior of tpm_tis_spi_transfer() by supplying their own flow control and pre-transfer hooks. This shares the most code between the core driver and any variants that want to override certain behavior without cluttering the core driver.

Quoted 7 lines

I think this is adding way too much complexity for the purpose. We definitely do want this three layer architecture here.

Instead there should be a single tpm_tis_spi driver that dynamically either TCG or CR50. I rather take some extra bytes in the LKM than the added complexity.

Quoted 4 lines

Ok. I had that patch originally[1]. Do you want me to resend that patch and start review over from there?

[1] https://lkml.kernel.org/r/[email protected]

What if:

1. You mostly use this solution but have it as a separate source module only.
2. Use TPM_IS_CR50 only once to bind the callbacks.

/Jarkko

#16 lore raw

Re: [PATCH v4 4/6] tpm: tpm_tis_spi: Export functionality to other drivers

Quoting Jarkko Sakkinen (2019-08-21 10:58:46)

Quoted 11 lines · Stephen Boyd
Quoted 10 lines

Quoting Jarkko Sakkinen (2019-08-19 09:40:05)

Quoted 5 lines

Instead there should be a single tpm_tis_spi driver that dynamically either TCG or CR50. I rather take some extra bytes in the LKM than the added complexity.

Ok. I had that patch originally[1]. Do you want me to resend that patch and start review over from there?

Quoted 8 lines
Quoted 1 line

[1] https://lkml.kernel.org/r/[email protected]

What if:

1. You mostly use this solution but have it as a separate source module only.
2. Use TPM_IS_CR50 only once to bind the callbacks.

Ok I think I understand. Take the callback approach from these patches and combine that with the TPM_IS_CR50 changes I made in [1]. I'll try it out and resend.

#17 lore raw

PATCH v4 5/6 dt-bindings: tpm: document properties for cr50

In reply to Stephen Boyd

From: Andrey Pronin <[email protected]>

Add TPM2.0 PTP FIFO compatible SPI interface for chips with Cr50 firmware.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>
---
 .../bindings/security/tpm/google,cr50.txt     | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/security/tpm/google,cr50.txt
Documentation/devicetree/bindings/security/tpm/google,cr50.txt new file +19 -0
new file mode 100644
index 000000000000..7aa65224c8b9
@@ -0,0 +1,19 @@
+* H1 Secure Microcontroller with Cr50 Firmware on SPI Bus.
+
+H1 Secure Microcontroller running Cr50 firmware provides several
+functions, including TPM-like functionality. It communicates over
+SPI using the FIFO protocol described in the PTP Spec, section 6.
+
+Required properties:
+- compatible: Should be "google,cr50".
+- spi-max-frequency: Maximum SPI frequency.
+
+Example:
+
+&spi0 {
+        tpm@0 {
+                compatible = "google,cr50";
+                reg = <0>;
+                spi-max-frequency = <800000>;
+        };
+};
Signature · 1 line
Sent by a computer through tubes
#18 lore raw

PATCH v4 6/6 tpm: add driver for cr50 on SPI

In reply to Stephen Boyd

From: Andrey Pronin <[email protected]>

Add TPM2.0 PTP FIFO compatible SPI interface for chips with Cr50 firmware. The firmware running on the currently supported H1 Secure Microcontroller requires a special driver to handle its specifics:

- need to ensure a certain delay between spi transactions, or else the chip may miss some part of the next transaction;
- if there is no spi activity for some time, it may go to sleep, and needs to be waken up before sending further commands;
- access to vendor-specific registers.

Cc Andrey Pronin <[email protected]>
Cc Duncan Laurie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Alexander Steffen <[email protected]>

[[email protected]: Replace boilerplate with SPDX tag, drop suspended bit and remove ifdef checks in cr50.h, push tpm.h include into cr50.c, migrate to functions exported from tpm_tis_spi.h]

---
 drivers/char/tpm/Kconfig    |   9 +
 drivers/char/tpm/Makefile   |   1 +
 drivers/char/tpm/cr50_spi.c | 372 ++++++++++++++++++++++++++++++++++++
 3 files changed, 382 insertions(+)
 create mode 100644 drivers/char/tpm/cr50_spi.c
drivers/char/tpm/Kconfig +9 -0
index 88a3c06fc153..1fb23f145ef6 100644
@@ -114,6 +114,15 @@ config TCG_ATMEL
 	  will be accessible from within Linux.  To compile this driver 
 	  as a module, choose M here; the module will be called tpm_atmel.
 
+config TCG_CR50_SPI
+	tristate "Cr50 SPI Interface"
+	depends on TCG_TIS_SPI
+	---help---
+	  If you have a H1 secure module running Cr50 firmware on SPI bus,
+	  say Yes and it will be accessible from within Linux. To compile
+	  this driver as a module, choose M here; the module will be called
+	  cr50_spi.
+
 config TCG_INFINEON
 	tristate "Infineon Technologies TPM Interface"
 	depends on PNP
drivers/char/tpm/Makefile +1 -0
index a01c4cab902a..4713e52ce1b0 100644
@@ -27,6 +27,7 @@ obj-$(CONFIG_TCG_TIS_I2C_INFINEON) += tpm_i2c_infineon.o
 obj-$(CONFIG_TCG_TIS_I2C_NUVOTON) += tpm_i2c_nuvoton.o
 obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
 obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
+obj-$(CONFIG_TCG_CR50_SPI) += cr50_spi.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_TIS_ST33ZP24) += st33zp24/
drivers/char/tpm/cr50_spi.c new file +372 -0

372 lines not on this page · raw message

Signature · 1 line
Sent by a computer through tubes
Tested-by on the v4 series
#19 lore raw
In reply to Stephen Boyd

Am Dienstag, 13. August 2019, 00:36:16 CEST schrieb Stephen Boyd:

Quoted 6 lines

This patch series adds support for the the H1 secure microcontroller running cr50 firmware found on various recent Chromebooks. This driver is necessary to boot into a ChromeOS userspace environment. It implements support for several functions, including TPM-like functionality over a SPI interface.

Quoted 8 lines

The last time this was series sent looks to be [1]. I've looked over the patches and review comments and tried to address any feedback that Andrey didn't address (really minor things like newlines). I've reworked the patches from the last version to layer on top of the existing TPM TIS SPI implementation in tpm_tis_spi.c. Hopefully this is more palatable than combining the two drivers together into one file.

[1] https://lkml.kernel.org/r/[email protected]

Gave this a spin on a rk3399-gru-scarlet and it seems to have worked fine and tpm2-tools was happy talking to it, so

From looking through the patches everything also looks nice and peachy but my tpm-insights are limited so I don't really feel comfortable with a RB.

Heiko