]> Git Repo - linux.git/commitdiff
char: tpm: cr50: Move i2c locking to request/relinquish locality ops
authorJan Dabros <[email protected]>
Thu, 10 Oct 2024 09:15:59 +0000 (09:15 +0000)
committerJarkko Sakkinen <[email protected]>
Thu, 21 Nov 2024 22:48:27 +0000 (00:48 +0200)
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 <[email protected]>
Signed-off-by: Jarkko Sakkinen <[email protected]>
drivers/char/tpm/tpm_tis_i2c_cr50.c

index eed1c296a00c4f168bbc677312adbd03aac5800d..80b0f41ffb5f2b18f05d7955032d015ff986b89e 100644 (file)
@@ -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;
 }
 
 /**
This page took 0.058784 seconds and 4 git commands to generate.