Skip to content

Commit 44637b0

Browse files
dabros-janjarkkojs
authored andcommitted
char: tpm: cr50: Move i2c locking to request/relinquish locality ops
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]>
1 parent 932e3a5 commit 44637b0

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

drivers/char/tpm/tpm_tis_i2c_cr50.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t
201201
};
202202
int rc;
203203

204-
i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
205-
206204
/* Prepare for completion interrupt */
207205
tpm_cr50_i2c_enable_tpm_irq(chip);
208206

@@ -221,7 +219,6 @@ static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t
221219

222220
out:
223221
tpm_cr50_i2c_disable_tpm_irq(chip);
224-
i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
225222

226223
if (rc < 0)
227224
return rc;
@@ -263,8 +260,6 @@ static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
263260
priv->buf[0] = addr;
264261
memcpy(priv->buf + 1, buffer, len);
265262

266-
i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
267-
268263
/* Prepare for completion interrupt */
269264
tpm_cr50_i2c_enable_tpm_irq(chip);
270265

@@ -278,7 +273,6 @@ static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
278273

279274
out:
280275
tpm_cr50_i2c_disable_tpm_irq(chip);
281-
i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
282276

283277
if (rc < 0)
284278
return rc;
@@ -322,20 +316,23 @@ static int tpm_cr50_check_locality(struct tpm_chip *chip, int loc)
322316
*/
323317
static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
324318
{
319+
struct i2c_client *client = to_i2c_client(chip->dev.parent);
325320
u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
326321
u8 addr = TPM_I2C_ACCESS(loc);
327322
u8 buf;
328323
int rc;
329324

330325
rc = tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf));
331326
if (rc < 0)
332-
return rc;
327+
goto unlock_out;
333328

334329
if ((buf & mask) == mask) {
335330
buf = TPM_ACCESS_ACTIVE_LOCALITY;
336331
rc = tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf));
337332
}
338333

334+
unlock_out:
335+
i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
339336
return rc;
340337
}
341338

@@ -350,16 +347,19 @@ static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
350347
*/
351348
static int tpm_cr50_request_locality(struct tpm_chip *chip, int loc)
352349
{
350+
struct i2c_client *client = to_i2c_client(chip->dev.parent);
353351
u8 buf = TPM_ACCESS_REQUEST_USE;
354352
unsigned long stop;
355353
int rc;
356354

355+
i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
356+
357357
if (tpm_cr50_check_locality(chip, loc) == loc)
358358
return loc;
359359

360360
rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
361361
if (rc < 0)
362-
return rc;
362+
goto unlock_out;
363363

364364
stop = jiffies + chip->timeout_a;
365365
do {
@@ -369,7 +369,11 @@ static int tpm_cr50_request_locality(struct tpm_chip *chip, int loc)
369369
msleep(TPM_CR50_TIMEOUT_SHORT_MS);
370370
} while (time_before(jiffies, stop));
371371

372-
return -ETIMEDOUT;
372+
rc = -ETIMEDOUT;
373+
374+
unlock_out:
375+
i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
376+
return rc;
373377
}
374378

375379
/**

0 commit comments

Comments
 (0)