All of lore.kernel.org
 help / color / mirror / Atom feed
From: Grzegorz Bernacki <bernacki@chromium.org>
To: jsd@semihalf.com
Cc: apronin@google.com, arnd@arndb.de, gregkh@linuxfoundation.org,
	jarkko@kernel.org, jgg@ziepe.ca, linux-integrity@vger.kernel.org,
	mw@semihalf.com, peterhuewe@gmx.de, rrangel@chromium.org,
	timvp@google.com
Subject: [PATCH V3 2/2] char: tpm: cr50: Move i2c locking to request/relinquish locality ops
Date: Thu, 10 Oct 2024 09:15:59 +0000	[thread overview]
Message-ID: <20241010091559.30866-3-bernacki@google.com> (raw)
In-Reply-To: <20241010091559.30866-1-bernacki@google.com>

From: Jan Dabros <jsd@semihalf.com>

Move i2c locking primitives to request_locality and relinquish_locality
callbacks, what effectively blocks TPM bus for the whole duration of
logical TPM operation.

With this in place, cr50-equipped TPM may be shared with external CPUs -
assuming that underneath i2c controller driver is aware of this setup
(see i2c-designware-amdpsp as an example).

Signed-off-by: Jan Dabros <jsd@semihalf.com>
---
 drivers/char/tpm/tpm_tis_i2c_cr50.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/drivers/char/tpm/tpm_tis_i2c_cr50.c b/drivers/char/tpm/tpm_tis_i2c_cr50.c
index eed1c296a00c..80b0f41ffb5f 100644
--- a/drivers/char/tpm/tpm_tis_i2c_cr50.c
+++ b/drivers/char/tpm/tpm_tis_i2c_cr50.c
@@ -201,8 +201,6 @@ static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t
 	};
 	int rc;
 
-	i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
-
 	/* Prepare for completion interrupt */
 	tpm_cr50_i2c_enable_tpm_irq(chip);
 
@@ -221,7 +219,6 @@ static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t
 
 out:
 	tpm_cr50_i2c_disable_tpm_irq(chip);
-	i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
 
 	if (rc < 0)
 		return rc;
@@ -263,8 +260,6 @@ static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
 	priv->buf[0] = addr;
 	memcpy(priv->buf + 1, buffer, len);
 
-	i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
-
 	/* Prepare for completion interrupt */
 	tpm_cr50_i2c_enable_tpm_irq(chip);
 
@@ -278,7 +273,6 @@ static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
 
 out:
 	tpm_cr50_i2c_disable_tpm_irq(chip);
-	i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
 
 	if (rc < 0)
 		return rc;
@@ -322,6 +316,7 @@ static int tpm_cr50_check_locality(struct tpm_chip *chip, int loc)
  */
 static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
 {
+	struct i2c_client *client = to_i2c_client(chip->dev.parent);
 	u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
 	u8 addr = TPM_I2C_ACCESS(loc);
 	u8 buf;
@@ -329,13 +324,15 @@ static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
 
 	rc = tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf));
 	if (rc < 0)
-		return rc;
+		goto unlock_out;
 
 	if ((buf & mask) == mask) {
 		buf = TPM_ACCESS_ACTIVE_LOCALITY;
 		rc = tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf));
 	}
 
+unlock_out:
+	i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
 	return rc;
 }
 
@@ -350,16 +347,19 @@ static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
  */
 static int tpm_cr50_request_locality(struct tpm_chip *chip, int loc)
 {
+	struct i2c_client *client = to_i2c_client(chip->dev.parent);
 	u8 buf = TPM_ACCESS_REQUEST_USE;
 	unsigned long stop;
 	int rc;
 
+	i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
+
 	if (tpm_cr50_check_locality(chip, loc) == loc)
 		return loc;
 
 	rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
 	if (rc < 0)
-		return rc;
+		goto unlock_out;
 
 	stop = jiffies + chip->timeout_a;
 	do {
@@ -369,7 +369,11 @@ static int tpm_cr50_request_locality(struct tpm_chip *chip, int loc)
 		msleep(TPM_CR50_TIMEOUT_SHORT_MS);
 	} while (time_before(jiffies, stop));
 
-	return -ETIMEDOUT;
+	rc = -ETIMEDOUT;
+
+unlock_out:
+	i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
+	return rc;
 }
 
 /**
-- 
2.47.0.rc0.187.ge670bccf7e-goog


  parent reply	other threads:[~2024-10-10  9:16 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-01  2:03 [PATCH 0/3] char: tpm: Adjust cr50_i2c locking mechanism Jan Dabros
2022-11-01  2:03 ` [PATCH 1/3] char: tpm: Protect tpm_pm_suspend with locks Jan Dabros
2022-11-01 15:53   ` Tim Van Patten
2022-11-02 21:28     ` Jan Dąbroś
2022-11-03 22:36       ` Tim Van Patten
2022-11-01  2:03 ` [PATCH 2/3] char: tpm: cr50: Use generic request/relinquish locality ops Jan Dabros
2022-11-01 16:04   ` Tim Van Patten
2022-11-02 21:42     ` Jan Dąbroś
2024-09-30 11:52     ` bernacki
2024-10-07 23:37       ` Jarkko Sakkinen
2022-11-01  2:03 ` [PATCH 3/3] char: tpm: cr50: Move i2c locking to " Jan Dabros
2024-10-09  9:42 ` [PATCH V2 0/2] char: tpm: Adjust cr50_i2c locking mechanism Grzegorz Bernacki
2024-10-09  9:42   ` [PATCH V2 1/2] char: tpm: cr50: Use generic request/relinquish locality ops Grzegorz Bernacki
2024-10-09 16:06     ` Jarkko Sakkinen
2024-10-09  9:42   ` [PATCH V2 2/2] char: tpm: cr50: Move i2c locking to " Grzegorz Bernacki
2024-10-09 16:07     ` Jarkko Sakkinen
2024-10-10  9:15 ` [PATCH V3 0/2] char: tpm: Adjust cr50_i2c locking mechanism Grzegorz Bernacki
2024-10-10  9:15   ` [PATCH V3 1/2] char: tpm: cr50: Use generic request/relinquish locality ops Grzegorz Bernacki
2024-10-10  9:15   ` Grzegorz Bernacki [this message]
2024-10-28  9:09   ` [PATCH V3 0/2] char: tpm: Adjust cr50_i2c locking mechanism Grzegorz Bernacki
2024-10-28 12:16     ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241010091559.30866-3-bernacki@google.com \
    --to=bernacki@chromium.org \
    --cc=apronin@google.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=jarkko@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=jsd@semihalf.com \
    --cc=linux-integrity@vger.kernel.org \
    --cc=mw@semihalf.com \
    --cc=peterhuewe@gmx.de \
    --cc=rrangel@chromium.org \
    --cc=timvp@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.