Skip to contentSkip to conversation

tpm: fix PS/2 devices not working on Braswell systems due CLKRUN enabled

PATCH Superseded Javier Martinez Canillas 8 participants 39 messages Dec 20, 2017 – Dec 24, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover tpm: fix PS/2 devices not working on Braswell systems due CLKRUN enabled

Hello,

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell systems") added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

The bug was reported to the Fedora kernel [0] and the kernel bugzilla [1]. This issue and the propossed solution were discussed in this [2] thread, and the reporter (Jame Ettle) confirmed that his system works again after the fix in this series.

The patches are based on top or Jarkko Sakkinen's linux-tpmdd [3] tree.

James,

Even when there shouldn't be any functional changes, I included some other fixes / cleanups in this series so it would be great if you can test them again. I can't because I don't have access to a machine affected by this.

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/
[3]: git.infradead.org/users/jjs/linux-tpmdd.git

Best regards,
Javier

Javier Martinez Canillas (4):
tpm: fix access attempt to an already unmapped I/O memory region
tpm: delete the TPM_TIS_CLK_ENABLE flag
tpm: follow coding style for variable declaration in

    tpm_tis_core_init()

tpm: only attempt to disable the LPC CLKRUN if is already enabled

 drivers/char/tpm/tpm_tis.c      | 17 +----------------
 drivers/char/tpm/tpm_tis_core.c | 24 +++++++++++++++++-------
 drivers/char/tpm/tpm_tis_core.h |  1 -
 3 files changed, 18 insertions(+), 24 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/4 tpm: fix access attempt to an already unmapped I/O memory region

The driver maps the I/O memory address to control the LPC bus CLKRUN_EN, but on the error path the memory is accessed by the .clk_enable handler after this was already unmapped. So only unmap the I/O memory region if it will not be used anymore.

Also, the correct thing to do is to cleanup the resources in the inverse order that were acquired to prevent issues like these.

---

 drivers/char/tpm/tpm_tis_core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis_core.c +4 -3
index c2227983ed88..3455abbb2035 100644
@@ -923,7 +923,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 
 	rc = tpm_chip_register(chip);
 	if (rc && is_bsw())
-		iounmap(priv->ilb_base_addr);
+		goto out_err;
 
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
@@ -931,12 +931,13 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 	return rc;
 out_err:
 	tpm_tis_remove(chip);
-	if (is_bsw())
-		iounmap(priv->ilb_base_addr);
 
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
 
+	if (is_bsw())
+		iounmap(priv->ilb_base_addr);
+
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
Signature · 1 line
2.14.3
#13 lore raw

PATCH 2/4 tpm: delete the TPM_TIS_CLK_ENABLE flag

In reply to Javier Martinez Canillas

This flag is only used to warn if CLKRUN_EN wasn't disabled on Braswell systems, but the only way this can happen is if the code is not correct.

So it's an unnecessary check that just makes the code harder to read.

---

 drivers/char/tpm/tpm_tis.c      | 15 ---------------
 drivers/char/tpm/tpm_tis_core.c |  2 --
 drivers/char/tpm/tpm_tis_core.h |  1 -
 3 files changed, 18 deletions(-)
drivers/char/tpm/tpm_tis.c +0 -15
index d29add49b033..d0ad9a89b02b 100644
@@ -138,9 +138,6 @@ static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
 {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	while (len--)
 		*result++ = ioread8(phy->iobase + addr);
 
@@ -152,9 +149,6 @@ static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
 {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	while (len--)
 		iowrite8(*value++, phy->iobase + addr);
 
@@ -165,9 +159,6 @@ static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
 {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	*result = ioread16(phy->iobase + addr);
 
 	return 0;
@@ -177,9 +168,6 @@ static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
 {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	*result = ioread32(phy->iobase + addr);
 
 	return 0;
@@ -189,9 +177,6 @@ static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
 {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	iowrite32(value, phy->iobase + addr);
 
 	return 0;
drivers/char/tpm/tpm_tis_core.c +0 -2
index 3455abbb2035..09da1e1adc40 100644
@@ -746,7 +746,6 @@ static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
 		return;
 
 	if (value) {
-		data->flags |= TPM_TIS_CLK_ENABLE;
 		data->clkrun_enabled++;
 		if (data->clkrun_enabled > 1)
 			return;
@@ -777,7 +776,6 @@ static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
 		 * sure LPC clock is running before sending any TPM command.
 		 */
 		outb(0xCC, 0x80);
-		data->flags &= ~TPM_TIS_CLK_ENABLE;
 	}
 }
 
drivers/char/tpm/tpm_tis_core.h +0 -1
index afc50cde1ba6..d5c6a2e952b3 100644
@@ -86,7 +86,6 @@ enum tis_defaults {
 
 enum tpm_tis_flags {
 	TPM_TIS_ITPM_WORKAROUND		= BIT(0),
-	TPM_TIS_CLK_ENABLE		= BIT(1),
 };
 
 struct tpm_tis_data {
Signature · 1 line
2.14.3
#19 lore raw

PATCH 3/4 tpm: follow coding style for variable declaration in tpm_tis_core_init()

In reply to Javier Martinez Canillas

The coding style says "use just one data declaration per line (no commas for multiple data declarations)" so follow this convention.

---

 drivers/char/tpm/tpm_tis_core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm_tis_core.c +3 -1
index 09da1e1adc40..534cd46fdfae 100644
@@ -798,7 +798,9 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 		      const struct tpm_tis_phy_ops *phy_ops,
 		      acpi_handle acpi_dev_handle)
 {
-	u32 vendor, intfcaps, intmask;
+	u32 vendor;
+	u32 intfcaps;
+	u32 intmask;
 	u8 rid;
 	int rc, probe;
 	struct tpm_chip *chip;
Signature · 1 line
2.14.3
#21 lore raw

PATCH 4/4 tpm: only attempt to disable the LPC CLKRUN if is already enabled

In reply to Javier Martinez Canillas

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell systems") added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

One flaw with the logic is that it assumes that the CLKRUN is always enabled, and so it unconditionally enables it after a TPM transaction.

But it could be that the CLKRUN# signal was already disabled in the LPC bus and so after the driver probes, CLKRUN_EN will remain enabled which may break other devices that are attached to the LPC bus but don't have support for the CLKRUN protocol.

Fixes 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell systems")

---

This patch fixes the bug reported for the Fedora kernel [0] and the kernel bugzilla [1]. The issue and the propossed solution were discussed in this
[2] thread.

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/

 drivers/char/tpm/tpm_tis.c      |  2 +-
 drivers/char/tpm/tpm_tis_core.c | 13 +++++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis.c +1 -1
index d0ad9a89b02b..c79193f6d092 100644
@@ -314,7 +314,7 @@ static int tpm_tis_plat_remove(struct platform_device *pdev)
 	tpm_chip_unregister(chip);
 	tpm_tis_remove(chip);
 
-	if (is_bsw())
+	if (is_bsw() && priv->ilb_base_addr)
 		iounmap(priv->ilb_base_addr);
 
 	return 0;
drivers/char/tpm/tpm_tis_core.c +11 -2
index 534cd46fdfae..17fb9c600d0e 100644
@@ -742,7 +742,8 @@ static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
 	struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
 	u32 clkrun_val;
 
-	if (!IS_ENABLED(CONFIG_X86) || !is_bsw())
+	if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
+	    !data->ilb_base_addr)
 		return;
 
 	if (value) {
@@ -801,6 +802,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 	u32 vendor;
 	u32 intfcaps;
 	u32 intmask;
+	u32 clkrun_val;
 	u8 rid;
 	int rc, probe;
 	struct tpm_chip *chip;
@@ -826,6 +828,13 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 					ILB_REMAP_SIZE);
 		if (!priv->ilb_base_addr)
 			return -ENOMEM;
+
+		clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
+		/* Check if CLKRUN# is already not enabled in the LPC bus */
+		if (!(clkrun_val & LPC_CLKRUN_EN)) {
+			iounmap(priv->ilb_base_addr);
+			priv->ilb_base_addr = NULL;
+		}
 	}
 
 	if (chip->ops->clk_enable != NULL)
@@ -935,7 +944,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
 
-	if (is_bsw())
+	if (is_bsw() && priv->ilb_base_addr)
 		iounmap(priv->ilb_base_addr);
 
 	return rc;
Signature · 1 line
2.14.3
#23 lore raw
In reply to Javier Martinez Canillas

Hi,

Quoted 35 lines · Javier Martinez Canillas

Hello,

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell systems") added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

The bug was reported to the Fedora kernel [0] and the kernel bugzilla [1]. This issue and the propossed solution were discussed in this [2] thread, and the reporter (Jame Ettle) confirmed that his system works again after the fix in this series.

The patches are based on top or Jarkko Sakkinen's linux-tpmdd [3] tree.

James,

Even when there shouldn't be any functional changes, I included some other fixes / cleanups in this series so it would be great if you can test them again. I can't because I don't have access to a machine affected by this.

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/
[3]: git.infradead.org/users/jjs/linux-tpmdd.git

Best regards,
Javier

Javier Martinez Canillas (4):
tpm: fix access attempt to an already unmapped I/O memory region
tpm: delete the TPM_TIS_CLK_ENABLE flag
tpm: follow coding style for variable declaration in

Quoted 7 lines
     tpm_tis_core_init()

tpm: only attempt to disable the LPC CLKRUN if is already enabled

  drivers/char/tpm/tpm_tis.c      | 17 +----------------
  drivers/char/tpm/tpm_tis_core.c | 24 +++++++++++++++++-------
  drivers/char/tpm/tpm_tis_core.h |  1 -
  3 files changed, 18 insertions(+), 24 deletions(-)

Note I'm just reading along here, but I'm wondering if both the TPM and now also some PS/2 controllers need CLK_RUN to be disabled, why don't we just disable it once permanently and be done with it?

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware there and we should just disable it everywhere (and probably find a better place then the TPM driver to do the disabling).

Note this is just an observation, I could be completely wrong here, but I've a feeling that just disabling CLKRUN all together is the right thing to do and that seems like an easier fix to me.

Specifically what worries me is: what if another driver also takes the temporarily disable CLK_RUN approach because of similar issues? Now we've 2 drivers playing with the CLKRUN state and racing with each others.

Regards,

Hans

#24 lore raw

Hi Hans,

Thanks a lot for your feedback.

Quoted 41 lines · Hans de Goede

Hi,

Quoted 38 lines · Javier Martinez Canillas

Hello,

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell systems") added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

The bug was reported to the Fedora kernel [0] and the kernel bugzilla [1]. This issue and the propossed solution were discussed in this [2] thread, and the reporter (Jame Ettle) confirmed that his system works again after the fix in this series.

The patches are based on top or Jarkko Sakkinen's linux-tpmdd [3] tree.

James,

Even when there shouldn't be any functional changes, I included some other fixes / cleanups in this series so it would be great if you can test them again. I can't because I don't have access to a machine affected by this.

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/
[3]: git.infradead.org/users/jjs/linux-tpmdd.git

Best regards,
Javier

Javier Martinez Canillas (4):
tpm: fix access attempt to an already unmapped I/O memory region
tpm: delete the TPM_TIS_CLK_ENABLE flag
tpm: follow coding style for variable declaration in

     tpm_tis_core_init()

tpm: only attempt to disable the LPC CLKRUN if is already enabled

Quoted 9 lines
Quoted 4 lines
  drivers/char/tpm/tpm_tis.c      | 17 +----------------
  drivers/char/tpm/tpm_tis_core.c | 24 +++++++++++++++++-------
  drivers/char/tpm/tpm_tis_core.h |  1 -
  3 files changed, 18 insertions(+), 24 deletions(-)

Note I'm just reading along here, but I'm wondering if both the TPM and now also some PS/2 controllers need CLK_RUN to be disabled, why don't we just disable it once permanently and be done with it?

That's the same question I asked to Azhar who authored the patch that added the CLKRUN toggle logic to the driver, but he didn't answer yet:

https://www.spinics.net/lists/linux-integrity/msg01107.html

My understanding is that by permanently disabling it, then other devices that do support the CLKRUN protocol will continue to work correctly since the CLKRUN# signal is optional and only used if the host LCLK is stopped.

The disadvantage though will be that the power management feature of stopping the LPC host LCLK clock when entering into low-power states will not be used.

Quoted 3 lines

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware

Absolutely agree, system integratos should make sure that all the devices connected to the LPC either have CLKRUN protocol support and is enabled or disable the CLKRUN protocol permanently.

Quoted 3 lines

there and we should just disable it everywhere (and probably find a better place then the TPM driver to do the disabling).

Indeed. Touching a global bus configuration in a driver for a single device isn't safe to say the least. One problem is that we don't have a LPC bus subsystem so that's why these sort of quirks are done at the driver level.

Quoted 4 lines

Note this is just an observation, I could be completely wrong here, but I've a feeling that just disabling CLKRUN all together is the right thing to do and that seems like an easier fix to me.

I think your observation is correct. The only problem is the power management feature being disabled as mentioned. Although as you said it seems that most BIOS do that (as shown by the patch I posted that just avoids toggling the CLKRUN state if it's already disabled).

Quoted 5 lines

Specifically what worries me is: what if another driver also takes the temporarily disable CLK_RUN approach because of similar issues? Now we've 2 drivers playing with the CLKRUN state and racing with each others.

Agreed. You don't even need another driver, if a Braswell system has 2 TPMs then you have a race condition and one driver could enable the CLKRUN state while the other driver thinks that's already disabled, and TPM transactions won't work in that case.

So yeah, it's not a great situation.

Quoted 4 lines

Regards,

Hans

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#25 lore raw
Quoted 72 lines

Hi Hans,

Thanks a lot for your feedback.

Quoted 6 lines · Hans de Goede

Hi,

Quoted 3 lines · Javier Martinez Canillas

Hello,

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell

systems")

Quoted 11 lines
Quoted 11 lines

added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

The bug was reported to the Fedora kernel [0] and the kernel bugzilla [1]. This issue and the propossed solution were discussed in this [2] thread, and the reporter (Jame Ettle) confirmed that his system works again after the fix in this series.

Quoted 5 lines
Quoted 5 lines

The patches are based on top or Jarkko Sakkinen's linux-tpmdd [3] tree.

James,

Even when there shouldn't be any functional changes, I included some

other

Quoted 19 lines
Quoted 19 lines

fixes / cleanups in this series so it would be great if you can test them again. I can't because I don't have access to a machine affected by this.

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/
[3]: git.infradead.org/users/jjs/linux-tpmdd.git

Best regards,
Javier

Javier Martinez Canillas (4):
tpm: fix access attempt to an already unmapped I/O memory region
tpm: delete the TPM_TIS_CLK_ENABLE flag
tpm: follow coding style for variable declaration in

     tpm_tis_core_init()

tpm: only attempt to disable the LPC CLKRUN if is already enabled

Quoted 9 lines
Quoted 4 lines
  drivers/char/tpm/tpm_tis.c      | 17 +----------------
  drivers/char/tpm/tpm_tis_core.c | 24 +++++++++++++++++-------
  drivers/char/tpm/tpm_tis_core.h |  1 -
  3 files changed, 18 insertions(+), 24 deletions(-)

Note I'm just reading along here, but I'm wondering if both the TPM and now also some PS/2 controllers need CLK_RUN to be disabled, why don't we just disable it once permanently and be done with it?

That's the same question I asked to Azhar who authored the patch that added the CLKRUN toggle logic to the driver, but he didn't answer yet:

https://www.spinics.net/lists/linux-integrity/msg01107.html

My understanding is that by permanently disabling it, then other devices that do support the CLKRUN protocol will continue to work correctly since the CLKRUN# signal is optional and only used if the host LCLK is stopped.

The disadvantage though will be that the power management feature of stopping
the LPC host LCLK clock when entering into low-power states will not be used.

Quoted 7 lines
Quoted 3 lines

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware

Absolutely agree, system integratos should make sure that all the devices connected to the LPC either have CLKRUN protocol support and is enabled or disable the CLKRUN protocol permanently.

As far as I understand it, this is exactly the issue here: They know that there are devices that do not support the CLKRUN protocol (the TPM in this case), but they still need to enable it to prevent other issues. So for the TPM to continue to work, CLKRUN needs to be disabled temporarily while the TPM is active.

Quoted 26 lines
Quoted 3 lines

there and we should just disable it everywhere (and probably find a better place then the TPM driver to do the disabling).

Indeed. Touching a global bus configuration in a driver for a single device isn't safe to say the least. One problem is that we don't have a LPC bus subsystem so that's why these sort of quirks are done at the driver level.

Quoted 4 lines

Note this is just an observation, I could be completely wrong here, but I've a feeling that just disabling CLKRUN all together is the right thing to do and that seems like an easier fix to me.

I think your observation is correct. The only problem is the power management
feature being disabled as mentioned. Although as you said it seems that most
BIOS do that (as shown by the patch I posted that just avoids toggling the CLKRUN state if it's already disabled).

Quoted 5 lines

Specifically what worries me is: what if another driver also takes the temporarily disable CLK_RUN approach because of similar issues? Now we've 2 drivers playing with the CLKRUN state and racing with each others.

Quoted 5 lines

Agreed. You don't even need another driver, if a Braswell system has 2 TPMs then you have a race condition and one driver could enable the CLKRUN state
while the other driver thinks that's already disabled, and TPM transactions won't work in that case.

Even though it is an unusual configuration to have multiple TPMs in a system, this is something that we could fix in the driver by putting locks around the state changes, right? And if other drivers also want to change the CLKRUN state, at the very least they'd need to use the same (global) lock.

Quoted 12 lines

So yeah, it's not a great situation.

Quoted 4 lines

Regards,

Hans

Best regards,
--
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat

#26 lore raw

Hello Alexander,

Quoted 76 lines · On 12/20/2017 03:07 PM, [email protected] wrote:
Quoted 76 lines

Hi Hans,

Thanks a lot for your feedback.

Quoted 6 lines · Hans de Goede

Hi,

Quoted 3 lines · Javier Martinez Canillas

Hello,

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell

systems")

Quoted 11 lines
Quoted 11 lines

added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

The bug was reported to the Fedora kernel [0] and the kernel bugzilla [1]. This issue and the propossed solution were discussed in this [2] thread, and the reporter (Jame Ettle) confirmed that his system works again after the fix in this series.

Quoted 5 lines
Quoted 5 lines

The patches are based on top or Jarkko Sakkinen's linux-tpmdd [3] tree.

James,

Even when there shouldn't be any functional changes, I included some

other

Quoted 19 lines
Quoted 19 lines

fixes / cleanups in this series so it would be great if you can test them again. I can't because I don't have access to a machine affected by this.

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/
[3]: git.infradead.org/users/jjs/linux-tpmdd.git

Best regards,
Javier

Javier Martinez Canillas (4):
tpm: fix access attempt to an already unmapped I/O memory region
tpm: delete the TPM_TIS_CLK_ENABLE flag
tpm: follow coding style for variable declaration in

     tpm_tis_core_init()

tpm: only attempt to disable the LPC CLKRUN if is already enabled

Quoted 9 lines
Quoted 4 lines
  drivers/char/tpm/tpm_tis.c      | 17 +----------------
  drivers/char/tpm/tpm_tis_core.c | 24 +++++++++++++++++-------
  drivers/char/tpm/tpm_tis_core.h |  1 -
  3 files changed, 18 insertions(+), 24 deletions(-)

Note I'm just reading along here, but I'm wondering if both the TPM and now also some PS/2 controllers need CLK_RUN to be disabled, why don't we just disable it once permanently and be done with it?

That's the same question I asked to Azhar who authored the patch that added the CLKRUN toggle logic to the driver, but he didn't answer yet:

https://www.spinics.net/lists/linux-integrity/msg01107.html

My understanding is that by permanently disabling it, then other devices that do support the CLKRUN protocol will continue to work correctly since the CLKRUN# signal is optional and only used if the host LCLK is stopped.

The disadvantage though will be that the power management feature of stopping
the LPC host LCLK clock when entering into low-power states will not be used.

Quoted 3 lines

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware

Quoted 5 lines
Quoted 3 lines

Absolutely agree, system integratos should make sure that all the devices connected to the LPC either have CLKRUN protocol support and is enabled or disable the CLKRUN protocol permanently.

As far as I understand it, this is exactly the issue here: They know that there are devices that do not support the CLKRUN protocol (the TPM in this case), but they still need to enable it to prevent other issues. So for the TPM to continue to work, CLKRUN needs to be disabled temporarily while the TPM is active.

I do understand that part. What's not clear to me is if those other issues as you said exist in practice. My understanding from reading the Low Pin Count Specification is that the CLKRUN# signal is optional and that the peripherals that support the CLKRUN protocol should still work even when the CLKRUN_EN is disabled.

With the disadvantage that the system will be less power efficient of course.

But yes, the reason why I proposed this minimal change as a fix was to preserve the behavior of the original patch that caused the regression, while allowing systems that already have the CLKRUN protocol disabled to also work (which was what caused the issue since the CLKRUN protocol was enabled unconditionally after a TPM transaction).

Quoted 27 lines
Quoted 27 lines
Quoted 4 lines

there and we should just disable it everywhere (and probably find a better place then the TPM driver to do the disabling).

Indeed. Touching a global bus configuration in a driver for a single device isn't safe to say the least. One problem is that we don't have a LPC bus subsystem so that's why these sort of quirks are done at the driver level.

Quoted 4 lines

Note this is just an observation, I could be completely wrong here, but I've a feeling that just disabling CLKRUN all together is the right thing to do and that seems like an easier fix to me.

I think your observation is correct. The only problem is the power management
feature being disabled as mentioned. Although as you said it seems that most
BIOS do that (as shown by the patch I posted that just avoids toggling the CLKRUN state if it's already disabled).

Quoted 5 lines

Specifically what worries me is: what if another driver also takes the temporarily disable CLK_RUN approach because of similar issues? Now we've 2 drivers playing with the CLKRUN state and racing with each others.

Quoted 8 lines
Quoted 5 lines

Agreed. You don't even need another driver, if a Braswell system has 2 TPMs then you have a race condition and one driver could enable the CLKRUN state
while the other driver thinks that's already disabled, and TPM transactions won't work in that case.

Even though it is an unusual configuration to have multiple TPMs in a system, this is something that we could fix in the driver by putting locks around the state changes, right? And if other drivers also want to change the CLKRUN state, at the very least they'd need to use the same (global) lock.

Yes, I didn't say that this couldn't be solved with a synchronization mechanism to prevent concurrent access to the LPC control registers. What I meant is that there isn't such a mechanism in place now so the code is racy and that the lack of a LPC subsystem forces us to have an (ugly) global lock if we want to fix it.

That's a separate issue though and should be addressed in a different patch-set.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#27 lore raw
In reply to [email protected]
Quoted 97 lines

-----Original Message-----
From: [email protected] [mailto:linux-integrity- [email protected]] On Behalf Of [email protected]
Sent: Wednesday, December 20, 2017 6:07 AM
To: [email protected]; [email protected]; linux- [email protected]
Cc: [email protected]; Shaikh, Azhar <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH 0/4] tpm: fix PS/2 devices not working on Braswell systems due CLKRUN enabled

Quoted 23 lines

Hi Hans,

Thanks a lot for your feedback.

Quoted 6 lines · Hans de Goede

Hi,

Quoted 3 lines · Javier Martinez Canillas

Hello,

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell

systems")

Quoted 11 lines
Quoted 11 lines

added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

The bug was reported to the Fedora kernel [0] and the kernel bugzilla [1]. This issue and the propossed solution were discussed in this [2] thread, and the reporter (Jame Ettle) confirmed that his system works again after the fix in this series.

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

The patches are based on top or Jarkko Sakkinen's linux-tpmdd [3] tree.

James,

Even when there shouldn't be any functional changes, I included some

other

Quoted 2 lines
Quoted 2 lines

fixes / cleanups in this series so it would be great if you can test them again. I can't because I don't have access to a machine affected

by this.

Quoted 28 lines
Quoted 18 lines
Quoted 18 lines

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/
[3]: git.infradead.org/users/jjs/linux-tpmdd.git

Best regards,
Javier

Javier Martinez Canillas (4):
tpm: fix access attempt to an already unmapped I/O memory region
tpm: delete the TPM_TIS_CLK_ENABLE flag
tpm: follow coding style for variable declaration in

     tpm_tis_core_init()

tpm: only attempt to disable the LPC CLKRUN if is already
enabled

Quoted 9 lines
Quoted 4 lines
  drivers/char/tpm/tpm_tis.c      | 17 +----------------
  drivers/char/tpm/tpm_tis_core.c | 24 +++++++++++++++++-------
  drivers/char/tpm/tpm_tis_core.h |  1 -
  3 files changed, 18 insertions(+), 24 deletions(-)

Note I'm just reading along here, but I'm wondering if both the TPM and now also some PS/2 controllers need CLK_RUN to be disabled, why don't we just disable it once permanently and be done with it?

Quoted 8 lines

That's the same question I asked to Azhar who authored the patch that added the CLKRUN toggle logic to the driver, but he didn't answer yet:

https://www.spinics.net/lists/linux-integrity/msg01107.html

My understanding is that by permanently disabling it, then other devices that do support the CLKRUN protocol will continue to work correctly since the CLKRUN# signal is optional and only used if the host LCLK

is stopped.

Quoted 5 lines

The disadvantage though will be that the power management feature of stopping the LPC host LCLK clock when entering into low-power states will not be used.

Quoted 7 lines
Quoted 3 lines

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware

Absolutely agree, system integratos should make sure that all the devices connected to the LPC either have CLKRUN protocol support and is enabled or disable the CLKRUN protocol permanently.

Quoted 6 lines

As far as I understand it, this is exactly the issue here: They know that there are devices that do not support the CLKRUN protocol (the TPM in this case), but they still need to enable it to prevent other issues. So for the TPM to continue to work, CLKRUN needs to be disabled temporarily while the TPM is active.

Yes that was the reason to have this fix. We needed CLKRUN to be enabled for Braswell SOC . But the TPM in this case SLB9655 does not support CLKRUN (please check this public documentation https://www.infineon.com/dgdl/Infineon-TPM+SLB+9665-DS-v10_15-EN.pdf?fileId=5546d4625185e0e201518b83d9273d87 section 2.3 Power Management). So as Alexander mentioned CLKRUN is disabled while TPM transactions are in progress.

Quoted 46 lines
Quoted 7 lines
Quoted 3 lines

there and we should just disable it everywhere (and probably find a better place then the TPM driver to do the disabling).

Indeed. Touching a global bus configuration in a driver for a single device isn't safe to say the least. One problem is that we don't have a LPC bus subsystem so that's why these sort of quirks are done at the driver

level.

Quoted 17 lines
Quoted 5 lines

Note this is just an observation, I could be completely wrong here, but I've a feeling that just disabling CLKRUN all together is the right thing to do and that seems like an easier fix to me.

I think your observation is correct. The only problem is the power management feature being disabled as mentioned. Although as you said it seems that most BIOS do that (as shown by the patch I posted that just avoids toggling the CLKRUN state if it's already disabled).

Quoted 5 lines

Specifically what worries me is: what if another driver also takes the temporarily disable CLK_RUN approach because of similar issues? Now we've 2 drivers playing with the CLKRUN state and racing with each others.

Quoted 4 lines

Agreed. You don't even need another driver, if a Braswell system has 2 TPMs then you have a race condition and one driver could enable the CLKRUN state while the other driver thinks that's already disabled, and TPM transactions won't work in that case.

Even though it is an unusual configuration to have multiple TPMs in a system, this is something that we could fix in the driver by putting locks around the state changes, right? And if other drivers also want to change the CLKRUN state, at the very least they'd need to use the same (global) lock.

Quoted 11 lines

So yeah, it's not a great situation.

Quoted 4 lines

Regards,

Hans

Best regards,
--
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement Red Hat

#14 lore raw

RE: [PATCH 2/4] tpm: delete the TPM_TIS_CLK_ENABLE flag

In reply to Javier Martinez Canillas
Quoted 14 lines

-----Original Message-----
From: Javier Martinez Canillas [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 3:36 AM
To: [email protected]
Cc: James Ettle <[email protected]>; Hans de Goede <[email protected]>; Shaikh, Azhar <[email protected]>; Javier Martinez Canillas <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Jason Gunthorpe <[email protected]>; Greg Kroah- Hartman <[email protected]>; [email protected]
Subject: [PATCH 2/4] tpm: delete the TPM_TIS_CLK_ENABLE flag

Quoted 5 lines

This flag is only used to warn if CLKRUN_EN wasn't disabled on Braswell systems, but the only way this can happen is if the code is not correct.

So it's an unnecessary check that just makes the code harder to read.

Hi Javier,

This code was implemented as a suggestion from Jason on the previous patches.
https://www.spinics.net/lists/linux-integrity/msg00827.html

Quoted 102 lines

---

drivers/char/tpm/tpm_tis.c | 15 ---------------
drivers/char/tpm/tpm_tis_core.c | 2 -- drivers/char/tpm/tpm_tis_core.h | 1 -

3 files changed, 18 deletions(-)
drivers/char/tpm/tpm_tis.c index renamed from drivers/char/tpm/tpm_tis.c +0 -0

d29add49b033..d0ad9a89b02b 100644

drivers/char/tpm/tpm_tis.c +0 -15
@@ -138,9 +138,6 @@ static int tpm_tcg_read_bytes(struct tpm_tis_data
 *data, u32 addr, u16 len,  {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	while (len--)
 		*result++ = ioread8(phy->iobase + addr);
 
@@ -152,9 +149,6 @@ static int tpm_tcg_write_bytes(struct tpm_tis_data
 *data, u32 addr, u16 len,  {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	while (len--)
 		iowrite8(*value++, phy->iobase + addr);
 
@@ -165,9 +159,6 @@ static int tpm_tcg_read16(struct tpm_tis_data *data,
 u32 addr, u16 *result)  {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	*result = ioread16(phy->iobase + addr);
 
 	return 0;
@@ -177,9 +168,6 @@ static int tpm_tcg_read32(struct tpm_tis_data *data,
 u32 addr, u32 *result)  {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	*result = ioread32(phy->iobase + addr);
 
 	return 0;
@@ -189,9 +177,6 @@ static int tpm_tcg_write32(struct tpm_tis_data *data,
 u32 addr, u32 value)  {
 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
 
-	if (is_bsw() && !(data->flags & TPM_TIS_CLK_ENABLE))
-		WARN(1, "CLKRUN not enabled!\n");
-
 	iowrite32(value, phy->iobase + addr);
 
 	return 0;
(quoted diff) +0 -0

b/drivers/char/tpm/tpm_tis_core.c index 3455abbb2035..09da1e1adc40 100644

drivers/char/tpm/tpm_tis_core.c +0 -1
@@ -746,7 +746,6 @@ static void tpm_tis_clkrun_enable(struct tpm_chip
 *chip, bool value)
 		return;
 
 	if (value) {
-		data->flags |= TPM_TIS_CLK_ENABLE;
 		data->clkrun_enabled++;
 		if (data->clkrun_enabled > 1)
			return;
drivers/char/tpm/tpm_tis_core.c +0 -0
@@ -777,7 +776,6 @@ static void tpm_tis_clkrun_enable(struct tpm_chip
 *chip, bool value)
 		 * sure LPC clock is running before sending any TPM

command.

		 */
		outb(0xCC, 0x80);
drivers/char/tpm/tpm_tis_core.c +0 -1
-		data->flags &= ~TPM_TIS_CLK_ENABLE;
	}
}
(quoted diff) +0 -0

b/drivers/char/tpm/tpm_tis_core.h index afc50cde1ba6..d5c6a2e952b3 100644

drivers/char/tpm/tpm_tis_core.h +0 -2
@@ -86,7 +86,6 @@ enum tis_defaults {
 
 enum tpm_tis_flags {
 	TPM_TIS_ITPM_WORKAROUND		= BIT(0),
-	TPM_TIS_CLK_ENABLE		= BIT(1),
 };
 
 struct tpm_tis_data {
--

2.14.3

#2 lore raw

RE: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

In reply to Javier Martinez Canillas
Quoted 15 lines

-----Original Message-----
From: Javier Martinez Canillas [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 3:36 AM
To: [email protected]
Cc: James Ettle <[email protected]>; Hans de Goede <[email protected]>; Shaikh, Azhar <[email protected]>; Javier Martinez Canillas <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Jason Gunthorpe <[email protected]>; Greg Kroah- Hartman <[email protected]>; [email protected]
Subject: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Quoted 8 lines

The driver maps the I/O memory address to control the LPC bus CLKRUN_EN, but on the error path the memory is accessed by the .clk_enable handler after this was already unmapped. So only unmap the I/O memory region if it will not be used anymore.

Also, the correct thing to do is to cleanup the resources in the inverse order that were acquired to prevent issues like these.

Thanks for catching this!

Quoted 40 lines

---

drivers/char/tpm/tpm_tis_core.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/char/tpm/tpm_tis_core.c index c2227983ed88..3455abbb2035 100644

drivers/char/tpm/tpm_tis_core.c +1 -1
@@ -923,7 +923,7 @@ int tpm_tis_core_init(struct device *dev, struct
 tpm_tis_data *priv, int irq,
 
 	rc = tpm_chip_register(chip);
 	if (rc && is_bsw())
-		iounmap(priv->ilb_base_addr);
+		goto out_err;
 
 	if (chip->ops->clk_enable != NULL)
		chip->ops->clk_enable(chip, false);
drivers/char/tpm/tpm_tis_core.c +0 -0
@@ -931,12 +931,13 @@ int tpm_tis_core_init(struct device *dev, struct
 tpm_tis_data *priv, int irq,
 	return rc;

out_err:

	tpm_tis_remove(chip);
drivers/char/tpm/tpm_tis_core.c +0 -2
-	if (is_bsw())
-		iounmap(priv->ilb_base_addr);
	if (chip->ops->clk_enable != NULL)
		chip->ops->clk_enable(chip, false);
drivers/char/tpm/tpm_tis_core.c +3 -0
+	if (is_bsw())
+		iounmap(priv->ilb_base_addr);
+
	return rc;
}

EXPORT_SYMBOL_GPL(tpm_tis_core_init);
--
2.14.3

#28 lore raw
In reply to Shaikh, Azhar

Hello Azhar,

On 12/20/2017 04:08 PM, Shaikh, Azhar wrote:

[snip]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware

Absolutely agree, system integratos should make sure that all the devices connected to the LPC either have CLKRUN protocol support and is enabled or disable the CLKRUN protocol permanently.

Quoted 9 lines
Quoted 6 lines

As far as I understand it, this is exactly the issue here: They know that there are devices that do not support the CLKRUN protocol (the TPM in this case), but they still need to enable it to prevent other issues. So for the TPM to continue to work, CLKRUN needs to be disabled temporarily while the TPM is active.

Yes that was the reason to have this fix. We needed CLKRUN to be enabled for Braswell SOC . But the TPM in this case SLB9655 does not support CLKRUN (please check this public documentation https://www.infineon.com/dgdl/Infineon-TPM+SLB+9665-DS-v10_15-EN.pdf?fileId=5546d4625185e0e201518b83d9273d87 section 2.3 Power Management). So as Alexander mentioned CLKRUN is disabled while TPM transactions are in progress.

Yes I do understand that. Please read my answer to Alexander's email and also my question (and Hans') about keeping the CLKRUN protocol permanently disabled.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#29 lore raw
Quoted 45 lines

-----Original Message-----
From: Javier Martinez Canillas [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 7:31 AM
To: Shaikh, Azhar <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH 0/4] tpm: fix PS/2 devices not working on Braswell systems due CLKRUN enabled

Hello Azhar,

On 12/20/2017 04:08 PM, Shaikh, Azhar wrote:

[snip]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware

Absolutely agree, system integratos should make sure that all the devices connected to the LPC either have CLKRUN protocol support and is enabled or disable the CLKRUN protocol permanently.

Quoted 8 lines
Quoted 6 lines

As far as I understand it, this is exactly the issue here: They know that there are devices that do not support the CLKRUN protocol (the TPM in this case), but they still need to enable it to prevent other issues. So for the TPM to continue to work, CLKRUN needs to be disabled temporarily while the TPM is active.

Yes that was the reason to have this fix. We needed CLKRUN to be enabled

for Braswell SOC . But the TPM in this case SLB9655 does not support CLKRUN (please check this public documentation
https://www.infineon.com/dgdl/Infineon-TPM+SLB+9665-DS-v10_15-
EN.pdf?fileId=5546d4625185e0e201518b83d9273d87 section 2.3 Power Management). So as Alexander mentioned CLKRUN is disabled while TPM transactions are in progress.

Quoted 1 line
Quoted 4 lines

Yes I do understand that. Please read my answer to Alexander's email and also my question (and Hans') about keeping the CLKRUN protocol permanently disabled.

We had to enable CLKRUN for BSW issues as mentioned here https://www.intel.com/content/www/us/en/processors/pentium/pentium-celeron-n-series-spec-update.html on Page 24 CHP 49 and Page 25 CHP 51

Quoted 4 lines

Best regards,
--
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement Red Hat

#30 lore raw

Hello Azhar,

Quoted 45 lines · Azhar
Quoted 45 lines

-----Original Message-----
From: Javier Martinez Canillas [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 7:31 AM
To: Shaikh, Azhar <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH 0/4] tpm: fix PS/2 devices not working on Braswell systems due CLKRUN enabled

Hello Azhar,

On 12/20/2017 04:08 PM, Shaikh, Azhar wrote:

[snip]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware

Absolutely agree, system integratos should make sure that all the devices connected to the LPC either have CLKRUN protocol support and is enabled or disable the CLKRUN protocol permanently.

Quoted 8 lines
Quoted 6 lines

As far as I understand it, this is exactly the issue here: They know that there are devices that do not support the CLKRUN protocol (the TPM in this case), but they still need to enable it to prevent other issues. So for the TPM to continue to work, CLKRUN needs to be disabled temporarily while the TPM is active.

Yes that was the reason to have this fix. We needed CLKRUN to be enabled

for Braswell SOC . But the TPM in this case SLB9655 does not support CLKRUN (please check this public documentation
https://www.infineon.com/dgdl/Infineon-TPM+SLB+9665-DS-v10_15-
EN.pdf?fileId=5546d4625185e0e201518b83d9273d87 section 2.3 Power Management). So as Alexander mentioned CLKRUN is disabled while TPM transactions are in progress.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

Yes I do understand that. Please read my answer to Alexander's email and also my question (and Hans') about keeping the CLKRUN protocol permanently disabled.

We had to enable CLKRUN for BSW issues as mentioned here https://www.intel.com/content/www/us/en/processors/pentium/pentium-celeron-n-series-spec-update.html on Page 24 CHP 49 and Page 25 CHP 51

Thanks for the pointer. But it's still not clear to me after reading the mentioned erratas why the CLKRUN protocol must be enabled.

CHP49 says "System May Experience Inability to Boot or May Cease Operation" and that it may be related to the LPC circuitry to stop functioning which causes the inability to boot when activity is high for several years.

And that the workaround is:

"Firmware code changes for LPC and RTC circuitry and mitigations for SD Card circuitry have been identified and may be implemented for this erratum."

Is this Firmware code changes to enable the CLKRUN protocol to minimize the LPC bus activity and so prevent the LPC circuitry to stop functioning?

CHP51 says "LPC Clock Control Using the LPC_CLKRUN# May Not Behave As Expected" and that the implication is that "The SoC may prevent a peripheral device from successfully requesting the LPC clock".

So I would say that CLKRUN protocol should NOT be enabled instead since the CLKRUN# signal is not reliable. Or what am I missing here?

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#31 lore raw
Quoted 3 lines · Javier Martinez Canillas

CHP51 says "LPC Clock Control Using the LPC_CLKRUN# May Not Behave As Expected" and that the implication is that "The SoC may prevent a peripheral device from successfully requesting the LPC clock".

Now we are back to the beginning - the LPC_CLKRUN protocol is simply broken in BSW chipsets, and it has nothing to do with the TPM?

Intel is trying to work around that broken-ness and still preserve power management in the case where only the TPM is connected to the LPC bus.. It is questionable to me if this is even a good idea, or if Linux is the right place to implement this work around (eg something in SMM mode may be more appropriate for a chipset bug)

I think your patch is still the right improvement, if the BIOS turned the feature off, we should not turn it back on.

Jason

#3 lore raw

Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

In reply to Javier Martinez Canillas
Quoted 15 lines · Javier Martinez Canillas

The driver maps the I/O memory address to control the LPC bus CLKRUN_EN, but on the error path the memory is accessed by the .clk_enable handler after this was already unmapped. So only unmap the I/O memory region if it will not be used anymore.

Also, the correct thing to do is to cleanup the resources in the inverse order that were acquired to prevent issues like these.

 drivers/char/tpm/tpm_tis_core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis_core.c +0 -0
index c2227983ed88..3455abbb2035 100644

drivers/char/tpm/tpm_tis_core.c

Quoted 1 line
drivers/char/tpm/tpm_tis_core.c +1 -0
+++ b/drivers/char/tpm/tpm_tis_core.c

Yoiks. This patch is helping but the more I look at this the wronger everything looks..

1) tpm_chip_unregister makes chip->ops == NULL, so this sequence:

static int tpm_tis_plat_remove(struct platform_device *pdev)

	tpm_chip_unregister(chip);
	tpm_tis_remove(chip);
void tpm_tis_remove(struct tpm_chip *chip)
	if (chip->ops->clk_enable != NULL)

Will oops

2) tpm_chip_register can also NULL ops in error cases, so this sequence can oops:

       rc = tpm_chip_register(chip);
       if (rc && is_bsw())
               iounmap(priv->ilb_base_addr);

        if (chip->ops->clk_enable != NULL)
                chip->ops->clk_enable(chip, false);

3) iounmap should not be split between tpm_tis and tpm_tis_core Put it at the end of tpm_tis_remove.

4) This sequence:

+ return tpm_chip_register(chip);
+out_err:
+ tpm_tis_remove(chip);
+ return rc;

Doesn't look right. If tpm_chip_register fails then tpm_tis_remove will never be called. This was sort of OK when tpm_tis_remove didn't manage any resources, but now that it does the above needs fixing too.

The below draft fixes everything except #1. That needs a more thoughtful idea..

Jason

drivers/char/tpm/tpm_tis.c +0 -7
index d29add49b03388..09f18e2e644774 100644
@@ -275,9 +275,6 @@ static void tpm_tis_pnp_remove(struct pnp_dev *dev)
 
 	tpm_chip_unregister(chip);
 	tpm_tis_remove(chip);
-	if (is_bsw())
-		iounmap(priv->ilb_base_addr);
-
 }
 
 static struct pnp_driver tis_pnp_driver = {
@@ -328,10 +325,6 @@ static int tpm_tis_plat_remove(struct platform_device *pdev)
 
 	tpm_chip_unregister(chip);
 	tpm_tis_remove(chip);
-
-	if (is_bsw())
-		iounmap(priv->ilb_base_addr);
-
 	return 0;
 }
 
drivers/char/tpm/tpm_tis_core.c +7 -11
index c2227983ed88d4..ffda1694a6aba3 100644
@@ -727,6 +727,9 @@ void tpm_tis_remove(struct tpm_chip *chip)
 
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
+
+	if (priv->ilb_base_addr)
+		iounmap(priv->ilb_base_addr);
 }
 EXPORT_SYMBOL_GPL(tpm_tis_remove);
 
@@ -921,22 +924,15 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 		}
 	}
 
-	rc = tpm_chip_register(chip);
-	if (rc && is_bsw())
-		iounmap(priv->ilb_base_addr);
-
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
 
-	return rc;
+	rc = tpm_chip_register(chip);
+	if (rc):
+		goto out_err;
+	return 0;
 out_err:
 	tpm_tis_remove(chip);
-	if (is_bsw())
-		iounmap(priv->ilb_base_addr);
-
-	if (chip->ops->clk_enable != NULL)
-		chip->ops->clk_enable(chip, false);
-
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
#15 lore raw

Re: [PATCH 2/4] tpm: delete the TPM_TIS_CLK_ENABLE flag

In reply to Shaikh, Azhar
Quoted 7 lines · Azhar
Quoted 4 lines

This flag is only used to warn if CLKRUN_EN wasn't disabled on Braswell systems, but the only way this can happen is if the code is not correct.

So it's an unnecessary check that just makes the code harder to read.

This code was implemented as a suggestion from Jason on the previous patches.
https://www.spinics.net/lists/linux-integrity/msg00827.html

The concept was to be like ASSERT_RTNL, maybe it just needs a suitably named static inline to addrress Javier's readability concerns?

Jason

#4 lore raw

Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

In reply to Jason Gunthorpe
Quoted 47 lines · Jason Gunthorpe
Quoted 15 lines · Javier Martinez Canillas

The driver maps the I/O memory address to control the LPC bus CLKRUN_EN, but on the error path the memory is accessed by the .clk_enable handler after this was already unmapped. So only unmap the I/O memory region if it will not be used anymore.

Also, the correct thing to do is to cleanup the resources in the inverse order that were acquired to prevent issues like these.

 drivers/char/tpm/tpm_tis_core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis_core.c +0 -0
index c2227983ed88..3455abbb2035 100644

drivers/char/tpm/tpm_tis_core.c

Quoted 1 line
drivers/char/tpm/tpm_tis_core.c +1 -0
+++ b/drivers/char/tpm/tpm_tis_core.c

Yoiks. This patch is helping but the more I look at this the wronger everything looks..

1) tpm_chip_unregister makes chip->ops == NULL, so this sequence:

static int tpm_tis_plat_remove(struct platform_device *pdev)

	tpm_chip_unregister(chip);
	tpm_tis_remove(chip);
void tpm_tis_remove(struct tpm_chip *chip)
	if (chip->ops->clk_enable != NULL)

Will oops

2) tpm_chip_register can also NULL ops in error cases, so this sequence can oops:

       rc = tpm_chip_register(chip);
       if (rc && is_bsw())
               iounmap(priv->ilb_base_addr);

        if (chip->ops->clk_enable != NULL)
                chip->ops->clk_enable(chip, false);

3) iounmap should not be split between tpm_tis and tpm_tis_core Put it at the end of tpm_tis_remove.

4) This sequence:

Review

Quoted 9 lines
(quoted diff) +4 -0
+       return tpm_chip_register(chip);
+out_err:
+       tpm_tis_remove(chip);
+       return rc;
 
   Doesn't look right. If tpm_chip_register fails then
   tpm_tis_remove will never be called. This was sort of OK when
   tpm_tis_remove didn't manage any resources, but now that it does
   the above needs fixing too.

Right, I only noticed the issue this patch fixes and (wrongly) assumed the rest was correct.

Quoted 3 lines

The below draft fixes everything except #1. That needs a more thoughtful idea..

I'll just drop this patch from the series and you can fix all the issues in the error / driver removal paths. It's not a dependency anyways, I included it just because noticed the issue while reading the code.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#16 lore raw

Re: [PATCH 2/4] tpm: delete the TPM_TIS_CLK_ENABLE flag

In reply to Jason Gunthorpe
Quoted 4 lines · Jason Gunthorpe
Quoted 3 lines · Azhar
Quoted 3 lines

This flag is only used to warn if CLKRUN_EN wasn't disabled on Braswell systems, but the only way this can happen is if the code is not correct.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

So it's an unnecessary check that just makes the code harder to read.

This code was implemented as a suggestion from Jason on the previous patches.
https://www.spinics.net/lists/linux-integrity/msg00827.html

The concept was to be like ASSERT_RTNL, maybe it just needs a suitably named static inline to addrress Javier's readability concerns?

I really think is not worth it and pollutes all the tpm_tcg_{read,write} functions with those is_bsw() and flags checks. Your example is different since is a core API used by in lot of places in the kernel, but it's not the case here.

But I don't have a strong opinion either, it was Jarkko who questioned the value of the flag so I can drop this patch too if you disagree with the change. I'm mostly interested in PATCH 4/4 that's the actual fix.

Quoted 2 lines

Jason

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#32 lore raw
In reply to Jason Gunthorpe
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Javier Martinez Canillas

CHP51 says "LPC Clock Control Using the LPC_CLKRUN# May Not Behave As Expected" and that the implication is that "The SoC may prevent a peripheral device from successfully requesting the LPC clock".

Now we are back to the beginning - the LPC_CLKRUN protocol is simply broken in BSW chipsets, and it has nothing to do with the TPM?

Quoted 9 lines

Intel is trying to work around that broken-ness and still preserve power management in the case where only the TPM is connected to the LPC bus.. It is questionable to me if this is even a good idea, or if Linux is the right place to implement this work around (eg something in SMM mode may be more appropriate for a chipset bug)

I think your patch is still the right improvement, if the BIOS turned the feature off, we should not turn it back on.

Yes, the patch has merits on its own since fixes a flaw in the logic of the original CLKRUN patch. I think we should merge it and then the other issues can be fixed (or rework how the CLKRUN is managed) as follow-ups.

Quoted 2 lines

Jason

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#5 lore raw

Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

In reply to Javier Martinez Canillas
Quoted 7 lines · Javier Martinez Canillas
Quoted 3 lines

The below draft fixes everything except #1. That needs a more thoughtful idea..

I'll just drop this patch from the series and you can fix all the issues in the error / driver removal paths. It's not a dependency anyways, I included it just because noticed the issue while reading the code.

Azhar, this means it becomes your problem :)

Since the patches Jarkko has queued from you introduce oops's you need to fix them..

Jason

#6 lore raw

RE: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Quoted 21 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 10:55 AM
To: Javier Martinez Canillas <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Shaikh, Azhar <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman <[email protected]>; linux- [email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Quoted 4 lines · Javier Martinez Canillas
Quoted 3 lines

The below draft fixes everything except #1. That needs a more thoughtful idea..

Quoted 6 lines
Quoted 3 lines

I'll just drop this patch from the series and you can fix all the issues in the error / driver removal paths. It's not a dependency anyways, I included it just because noticed the issue while reading the code.

Azhar, this means it becomes your problem :)

Sure, I will fix this.
Javier you can drop this patch.

Quoted 4 lines

Since the patches Jarkko has queued from you introduce oops's you need to fix them..

Jason

Regards,
Azhar Shaikh

#34 lore raw
In reply to Javier Martinez Canillas

OK, I built a kernel based on:

- git clone of git.infradead.org/users/jjs/linux-tpmdd.git taken last night
- applying the patch in https://patchwork.kernel.org/patch/10119417/
- stock config-4.14.6-300.fc27.x86_64 + make oldconfig

The tpm modules are loaded and the keyboard is working.

Note: I won't have access to this machine over the next few weeks.

Thanks,
James.

Quoted 43 lines · Javier Martinez Canillas

Hello,

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell systems") added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

The bug was reported to the Fedora kernel [0] and the kernel bugzilla [1]. This issue and the propossed solution were discussed in this [2] thread, and the reporter (Jame Ettle) confirmed that his system works again after the fix in this series.

The patches are based on top or Jarkko Sakkinen's linux-tpmdd [3] tree.

James,

Even when there shouldn't be any functional changes, I included some other fixes / cleanups in this series so it would be great if you can test them again. I can't because I don't have access to a machine affected by this.

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/
[3]: git.infradead.org/users/jjs/linux-tpmdd.git

Best regards,
Javier

Javier Martinez Canillas (4):
tpm: fix access attempt to an already unmapped I/O memory region
tpm: delete the TPM_TIS_CLK_ENABLE flag
tpm: follow coding style for variable declaration in

    tpm_tis_core_init()

tpm: only attempt to disable the LPC CLKRUN if is already enabled

 drivers/char/tpm/tpm_tis.c      | 17 +----------------
 drivers/char/tpm/tpm_tis_core.c | 24 +++++++++++++++++-------
 drivers/char/tpm/tpm_tis_core.h |  1 -
 3 files changed, 18 insertions(+), 24 deletions(-)
#35 lore raw

Hello James,

Quoted 4 lines · James Ettle

OK, I built a kernel based on:

- git clone of git.infradead.org/users/jjs/linux-tpmdd.git taken last night
- applying the patch in https://patchwork.kernel.org/patch/10119417/

I meant linux-tpmdd + the 4 patches in this series.

Quoted 6 lines

- stock config-4.14.6-300.fc27.x86_64 + make oldconfig

The tpm modules are loaded and the keyboard is working.

Note: I won't have access to this machine over the next few weeks.

No worries, there shouldn't be any function changes anyways with the previous patch shared in the other thread and the changes are quite simple so I think it's OK.

Again thanks a lot for your help and merry Christmas / happy new year.

Quoted 3 lines

Thanks,
James.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#7 lore raw

Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

In reply to Shaikh, Azhar

Hello Azhar,

Quoted 25 lines · Azhar
Quoted 25 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 10:55 AM
To: Javier Martinez Canillas <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Shaikh, Azhar <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman <[email protected]>; linux- [email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Quoted 7 lines · Javier Martinez Canillas
Quoted 3 lines

The below draft fixes everything except #1. That needs a more thoughtful idea..

I'll just drop this patch from the series and you can fix all the issues in the error / driver removal paths. It's not a dependency anyways, I included it just because noticed the issue while reading the code.

Quoted 6 lines
Quoted 2 lines

Azhar, this means it becomes your problem :)

Sure, I will fix this.
Javier you can drop this patch.

On a second though, we should just merge $SUBJECT since is an obvious fix for one of the issues. Then you could fix the remaining bugs in error and driver removal paths.

Quoted 8 lines
Quoted 4 lines

Since the patches Jarkko has queued from you introduce oops's you need to fix them..

Jason

Regards,
Azhar Shaikh

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#8 lore raw

RE: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

HI Javier,

Quoted 47 lines

-----Original Message-----
From: [email protected] [mailto:linux-integrity- [email protected]] On Behalf Of Javier Martinez Canillas
Sent: Thursday, December 21, 2017 4:49 AM
To: Shaikh, Azhar <[email protected]>; Jason Gunthorpe <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman <[email protected]>; [email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Hello Azhar,

Quoted 11 lines · Azhar
Quoted 11 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 10:55 AM
To: Javier Martinez Canillas <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Shaikh, Azhar <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman

<[email protected]>;

Quoted 6 lines
Quoted 6 lines

linux- [email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

On Wed, Dec 20, 2017 at 07:21:31PM +0100, Javier Martinez Canillas wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

The below draft fixes everything except #1. That needs a more thoughtful idea..

I'll just drop this patch from the series and you can fix all the issues in the error / driver removal paths. It's not a dependency anyways, I included it just because noticed the issue while reading the

code.

Quoted 4 lines
Quoted 3 lines

Azhar, this means it becomes your problem :)

Quoted 7 lines
Quoted 3 lines

Sure, I will fix this.
Javier you can drop this patch.

On a second though, we should just merge $SUBJECT since is an obvious fix for one of the issues. Then you could fix the remaining bugs in error and driver removal paths.

I think you are asking to keep this patch as a part of this series, itself and not upload separately. Is this correct? Please correct me if I am wrong.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Since the patches Jarkko has queued from you introduce oops's you need to fix them..

Jason

Quoted 8 lines
Quoted 3 lines

Regards,
Azhar Shaikh

Best regards,
--
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement Red Hat

Regards,
Azhar Shaikh

#9 lore raw

Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Quoted 54 lines · Azhar

HI Javier,

Quoted 51 lines

-----Original Message-----
From: [email protected] [mailto:linux-integrity- [email protected]] On Behalf Of Javier Martinez Canillas
Sent: Thursday, December 21, 2017 4:49 AM
To: Shaikh, Azhar <[email protected]>; Jason Gunthorpe <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman <[email protected]>; [email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Hello Azhar,

Quoted 11 lines · Azhar
Quoted 11 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 10:55 AM
To: Javier Martinez Canillas <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Shaikh, Azhar <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman

<[email protected]>;

Quoted 6 lines
Quoted 6 lines

linux- [email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

On Wed, Dec 20, 2017 at 07:21:31PM +0100, Javier Martinez Canillas wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

The below draft fixes everything except #1. That needs a more thoughtful idea..

I'll just drop this patch from the series and you can fix all the issues in the error / driver removal paths. It's not a dependency anyways, I included it just because noticed the issue while reading the

code.

Quoted 7 lines
Quoted 3 lines

Azhar, this means it becomes your problem :)

Sure, I will fix this.
Javier you can drop this patch.

Quoted 6 lines
Quoted 3 lines

On a second though, we should just merge $SUBJECT since is an obvious fix for one of the issues. Then you could fix the remaining bugs in error and driver removal paths.

I think you are asking to keep this patch as a part of this series, itself and not upload separately. Is this correct? Please correct me if I am wrong.

Yes, what I meant is that we should just keep this patch in the series since it fixes one of the resource cleanup bugs. Then you can fix the other issues as a follow-up.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#10 lore raw

RE: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Quoted 78 lines

-----Original Message-----
From: Javier Martinez Canillas [mailto:[email protected]]
Sent: Thursday, December 21, 2017 7:54 AM
To: Shaikh, Azhar <[email protected]>; Jason Gunthorpe <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman <[email protected]>; [email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Quoted 11 lines · Azhar

HI Javier,

Quoted 8 lines

-----Original Message-----
From: [email protected] [mailto:linux-integrity- [email protected]] On Behalf Of Javier Martinez Canillas
Sent: Thursday, December 21, 2017 4:49 AM
To: Shaikh, Azhar <[email protected]>; Jason Gunthorpe <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Arnd Bergmann

<[email protected]>;

Quoted 2 lines
Quoted 2 lines

Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman

<[email protected]>;

Quoted 24 lines
Quoted 24 lines

[email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

Hello Azhar,

Quoted 11 lines · Azhar
Quoted 11 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: Wednesday, December 20, 2017 10:55 AM
To: Javier Martinez Canillas <[email protected]>
Cc: [email protected]; James Ettle <[email protected]>; Hans de Goede <[email protected]>; Shaikh, Azhar <[email protected]>; Arnd Bergmann <[email protected]>; Jarkko Sakkinen <[email protected]>; Peter Huewe <[email protected]>; Greg Kroah-Hartman

<[email protected]>;

Quoted 5 lines
Quoted 5 lines

linux- [email protected]
Subject: Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

On Wed, Dec 20, 2017 at 07:21:31PM +0100, Javier Martinez Canillas

Quoted 22 lines · wrote:
Quoted 21 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

The below draft fixes everything except #1. That needs a more thoughtful idea..

I'll just drop this patch from the series and you can fix all the issues in the error / driver removal paths. It's not a dependency anyways, I included it just because noticed the issue while reading the

code.

Quoted 7 lines
Quoted 3 lines

Azhar, this means it becomes your problem :)

Sure, I will fix this.
Javier you can drop this patch.

On a second though, we should just merge $SUBJECT since is an obvious fix for one of the issues. Then you could fix the remaining bugs in error and driver removal paths.

Quoted 8 lines
Quoted 1 line

I think you are asking to keep this patch as a part of this series, itself and not

upload separately. Is this correct? Please correct me if I am wrong.

Quoted 1 line

Yes, what I meant is that we should just keep this patch in the series since it fixes one of the resource cleanup bugs. Then you can fix the other issues as a follow-up.

Ok, but even the patch which I will be submitting will be doing the cleanup. So it will just split the change.

Quoted 4 lines

Best regards,
--
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement Red Hat

Regards,
Azhar Shaikh

#36 lore raw
In reply to Javier Martinez Canillas
Quoted 2 lines · Javier Martinez Canillas

I meant linux-tpmdd + the 4 patches in this series.

Javier,
I applied the four patches to the linux-tpmdd master 46dd3b29e328 this morning and it
fixed the touchpad on my affected machine.

To be clear, the patchwork patches I applied were:
10125523
10125515
10125513
10125531

I have a slightly different machine (Lenovo Yoga 11e) but found the same original CLKRUN commit doing a bisect.

#37 lore raw

Hello Jeffery,

Quoted 10 lines · Jeffery Miller
Quoted 2 lines · Javier Martinez Canillas

I meant linux-tpmdd + the 4 patches in this series.

Javier,
I applied the four patches to the linux-tpmdd master 46dd3b29e328 this morning and it
fixed the touchpad on my affected machine.

Quoted 9 lines

To be clear, the patchwork patches I applied were:
10125523
10125515
10125513
10125531

I have a slightly different machine (Lenovo Yoga 11e) but found the same original CLKRUN commit doing a bisect.

Great, thanks for testing!

Can I add your Tested-by tag then?

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
Reviewed-by Tested-by on v1 3/4
#20 lore raw

Re: [PATCH 3/4] tpm: follow coding style for variable declaration in tpm_tis_core_init()

In reply to Javier Martinez Canillas
Quoted 28 lines · Javier Martinez Canillas

The coding style says "use just one data declaration per line (no commas for multiple data declarations)" so follow this convention.

---

 drivers/char/tpm/tpm_tis_core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm_tis_core.c +3 -1
index 09da1e1adc40..534cd46fdfae 100644
@@ -798,7 +798,9 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 		      const struct tpm_tis_phy_ops *phy_ops,
 		      acpi_handle acpi_dev_handle)
 {
-	u32 vendor, intfcaps, intmask;
+	u32 vendor;
+	u32 intfcaps;
+	u32 intmask;
 	u8 rid;
 	int rc, probe;
 	struct tpm_chip *chip;
Signature · 1 line
2.14.3

/Jarkko

Reviewed-by Tested-by on v1 2/4
#18 lore raw

Re: [PATCH 2/4] tpm: delete the TPM_TIS_CLK_ENABLE flag

In reply to Javier Martinez Canillas
Quoted 7 lines · Javier Martinez Canillas

This flag is only used to warn if CLKRUN_EN wasn't disabled on Braswell systems, but the only way this can happen is if the code is not correct.

So it's an unnecessary check that just makes the code harder to read.

/Jarkko

Reviewed-by Tested-by on v1 1/4
#11 lore raw

Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

In reply to Javier Martinez Canillas
Quoted 46 lines · Javier Martinez Canillas

The driver maps the I/O memory address to control the LPC bus CLKRUN_EN, but on the error path the memory is accessed by the .clk_enable handler after this was already unmapped. So only unmap the I/O memory region if it will not be used anymore.

Also, the correct thing to do is to cleanup the resources in the inverse order that were acquired to prevent issues like these.

---

 drivers/char/tpm/tpm_tis_core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis_core.c +4 -3
index c2227983ed88..3455abbb2035 100644
@@ -923,7 +923,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 
 	rc = tpm_chip_register(chip);
 	if (rc && is_bsw())
-		iounmap(priv->ilb_base_addr);
+		goto out_err;
 
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
@@ -931,12 +931,13 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 	return rc;
 out_err:
 	tpm_tis_remove(chip);
-	if (is_bsw())
-		iounmap(priv->ilb_base_addr);
 
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
 
+	if (is_bsw())
+		iounmap(priv->ilb_base_addr);
+
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
Signature · 1 line
2.14.3

/Jarkko

Reviewed-by Tested-by on v1 4/4
#22 lore raw

Re: [PATCH 4/4] tpm: only attempt to disable the LPC CLKRUN if is already enabled

In reply to Javier Martinez Canillas
Quoted 95 lines · Javier Martinez Canillas

Commit 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell systems") added logic in the TPM TIS driver to disable the Low Pin Count CLKRUN signal during TPM transactions.

Unfortunately this breaks other devices that are attached to the LPC bus like for example PS/2 mouse and keyboards.

One flaw with the logic is that it assumes that the CLKRUN is always enabled, and so it unconditionally enables it after a TPM transaction.

But it could be that the CLKRUN# signal was already disabled in the LPC bus and so after the driver probes, CLKRUN_EN will remain enabled which may break other devices that are attached to the LPC bus but don't have support for the CLKRUN protocol.

Fixes 5e572cab92f0 ("tpm: Enable CLKRUN protocol for Braswell systems")

---

This patch fixes the bug reported for the Fedora kernel [0] and the kernel bugzilla [1]. The issue and the propossed solution were discussed in this
[2] thread.

[0]: https://bugzilla.redhat.com/show_bug.cgi?id=1498987
[1]: https://bugzilla.kernel.org/show_bug.cgi?id=197287
[2]: https://patchwork.kernel.org/patch/10119417/

 drivers/char/tpm/tpm_tis.c      |  2 +-
 drivers/char/tpm/tpm_tis_core.c | 13 +++++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis.c +1 -1
index d0ad9a89b02b..c79193f6d092 100644
@@ -314,7 +314,7 @@ static int tpm_tis_plat_remove(struct platform_device *pdev)
 	tpm_chip_unregister(chip);
 	tpm_tis_remove(chip);
 
-	if (is_bsw())
+	if (is_bsw() && priv->ilb_base_addr)
 		iounmap(priv->ilb_base_addr);
 
 	return 0;
drivers/char/tpm/tpm_tis_core.c +11 -2
index 534cd46fdfae..17fb9c600d0e 100644
@@ -742,7 +742,8 @@ static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
 	struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
 	u32 clkrun_val;
 
-	if (!IS_ENABLED(CONFIG_X86) || !is_bsw())
+	if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
+	    !data->ilb_base_addr)
 		return;
 
 	if (value) {
@@ -801,6 +802,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 	u32 vendor;
 	u32 intfcaps;
 	u32 intmask;
+	u32 clkrun_val;
 	u8 rid;
 	int rc, probe;
 	struct tpm_chip *chip;
@@ -826,6 +828,13 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 					ILB_REMAP_SIZE);
 		if (!priv->ilb_base_addr)
 			return -ENOMEM;
+
+		clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
+		/* Check if CLKRUN# is already not enabled in the LPC bus */
+		if (!(clkrun_val & LPC_CLKRUN_EN)) {
+			iounmap(priv->ilb_base_addr);
+			priv->ilb_base_addr = NULL;
+		}
 	}
 
 	if (chip->ops->clk_enable != NULL)
@@ -935,7 +944,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
 
-	if (is_bsw())
+	if (is_bsw() && priv->ilb_base_addr)
 		iounmap(priv->ilb_base_addr);
 
 	return rc;
Signature · 1 line
2.14.3

/Jarkko

#17 lore raw

Re: [PATCH 2/4] tpm: delete the TPM_TIS_CLK_ENABLE flag

In reply to Javier Martinez Canillas
Quoted 14 lines · Javier Martinez Canillas
Quoted 4 lines · Jason Gunthorpe
Quoted 3 lines · Azhar
Quoted 3 lines

This flag is only used to warn if CLKRUN_EN wasn't disabled on Braswell systems, but the only way this can happen is if the code is not correct.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

So it's an unnecessary check that just makes the code harder to read.

This code was implemented as a suggestion from Jason on the previous patches.
https://www.spinics.net/lists/linux-integrity/msg00827.html

The concept was to be like ASSERT_RTNL, maybe it just needs a suitably named static inline to addrress Javier's readability concerns?

Quoted 8 lines

I really think is not worth it and pollutes all the tpm_tcg_{read,write} functions with those is_bsw() and flags checks. Your example is different since is a core API used by in lot of places in the kernel, but it's not the case here.

But I don't have a strong opinion either, it was Jarkko who questioned the value of the flag so I can drop this patch too if you disagree with the change. I'm mostly interested in PATCH 4/4 that's the actual fix.

Not going to fight over this one. I would apply the patch but if there is strong opposition I can reconsider.

/Jarkko

#33 lore raw
In reply to Hans de Goede
Quoted 10 lines · Hans de Goede

Note I'm just reading along here, but I'm wondering if both the TPM and now also some PS/2 controllers need CLK_RUN to be disabled, why don't we just disable it once permanently and be done with it?

It seems that on machines with a PS/2 controller connected to the LPC bus the BIOS is already doing this, so I've a feeling that it not being done on devices with a TPM is a bug in the firmware there and we should just disable it everywhere (and probably find a better place then the TPM driver to do the disabling).

Quoted 3 lines

Note this is just an observation, I could be completely wrong here, but I've a feeling that just disabling CLKRUN all together is the right thing to do and that seems like an easier fix to me.

Agreed. Thanks for noting this. Right now, I would just apply TPM driver specific fixes.

/Jarkko

#12 lore raw

Re: [PATCH 1/4] tpm: fix access attempt to an already unmapped I/O memory region

In reply to Javier Martinez Canillas
Quoted 9 lines · Javier Martinez Canillas

The driver maps the I/O memory address to control the LPC bus CLKRUN_EN, but on the error path the memory is accessed by the .clk_enable handler after this was already unmapped. So only unmap the I/O memory region if it will not be used anymore.

Also, the correct thing to do is to cleanup the resources in the inverse order that were acquired to prevent issues like these.

I applied Azhar's updated patches to master and next. Shouldn't this be dropped now?

/Jarkko