Skip to content

Commit 1436ab0

Browse files
committed
Merge branch 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull TPM updates from James Morris: "Here are the TPM updates from Jarkko for v4.14, which I've placed in their own branch (next-tpm). I ended up cherry-picking them as other changes had been made in Jarkko's branch after he sent me his original pull request. I plan on maintaining a separate branch for TPM (and other security subsystems) from now on. From Jarkko: 'Not much this time except a few fixes'" * 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: tpm: ibmvtpm: simplify crq initialization and document crq format tpm: replace msleep() with usleep_range() in TPM 1.2/2.0 generic drivers Documentation: tpm: add powered-while-suspended binding documentation tpm: tpm_crb: constify acpi_device_id. tpm: vtpm: constify vio_device_id
2 parents cd4175b + fb154e0 commit 1436ab0

File tree

8 files changed

+89
-52
lines changed

8 files changed

+89
-52
lines changed

Documentation/devicetree/bindings/security/tpm/tpm-i2c.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Required properties:
88
the firmware event log
99
- linux,sml-size : size of the memory allocated for the firmware event log
1010

11+
Optional properties:
12+
13+
- powered-while-suspended: present when the TPM is left powered on between
14+
suspend and resume (makes the suspend/resume
15+
callbacks do nothing).
16+
1117
Example (for OpenPower Systems with Nuvoton TPM 2.0 on I2C)
1218
----------------------------------------------------------
1319

drivers/char/tpm/tpm-interface.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
455455
goto out;
456456
}
457457

458-
msleep(TPM_TIMEOUT); /* CHECK */
458+
tpm_msleep(TPM_TIMEOUT);
459459
rmb();
460460
} while (time_before(jiffies, stop));
461461

@@ -970,7 +970,7 @@ int tpm_do_selftest(struct tpm_chip *chip)
970970
dev_info(
971971
&chip->dev, HW_ERR
972972
"TPM command timed out during continue self test");
973-
msleep(delay_msec);
973+
tpm_msleep(delay_msec);
974974
continue;
975975
}
976976

@@ -985,7 +985,7 @@ int tpm_do_selftest(struct tpm_chip *chip)
985985
}
986986
if (rc != TPM_WARN_DOING_SELFTEST)
987987
return rc;
988-
msleep(delay_msec);
988+
tpm_msleep(delay_msec);
989989
} while (--loops > 0);
990990

991991
return rc;
@@ -1085,7 +1085,7 @@ int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
10851085
}
10861086
} else {
10871087
do {
1088-
msleep(TPM_TIMEOUT);
1088+
tpm_msleep(TPM_TIMEOUT);
10891089
status = chip->ops->status(chip);
10901090
if ((status & mask) == mask)
10911091
return 0;
@@ -1150,7 +1150,7 @@ int tpm_pm_suspend(struct device *dev)
11501150
*/
11511151
if (rc != TPM_WARN_RETRY)
11521152
break;
1153-
msleep(TPM_TIMEOUT_RETRY);
1153+
tpm_msleep(TPM_TIMEOUT_RETRY);
11541154
}
11551155

11561156
if (rc)

drivers/char/tpm/tpm.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ enum tpm_const {
5050

5151
enum tpm_timeout {
5252
TPM_TIMEOUT = 5, /* msecs */
53-
TPM_TIMEOUT_RETRY = 100 /* msecs */
53+
TPM_TIMEOUT_RETRY = 100, /* msecs */
54+
TPM_TIMEOUT_RANGE_US = 300 /* usecs */
5455
};
5556

5657
/* TPM addresses */
@@ -527,6 +528,12 @@ int tpm_pm_resume(struct device *dev);
527528
int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
528529
wait_queue_head_t *queue, bool check_cancel);
529530

531+
static inline void tpm_msleep(unsigned int delay_msec)
532+
{
533+
usleep_range(delay_msec * 1000,
534+
(delay_msec * 1000) + TPM_TIMEOUT_RANGE_US);
535+
};
536+
530537
struct tpm_chip *tpm_chip_find_get(int chip_num);
531538
__must_check int tpm_try_get_ops(struct tpm_chip *chip);
532539
void tpm_put_ops(struct tpm_chip *chip);

drivers/char/tpm/tpm2-cmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ static int tpm2_do_selftest(struct tpm_chip *chip)
899899
if (rc != TPM2_RC_TESTING)
900900
break;
901901

902-
msleep(delay_msec);
902+
tpm_msleep(delay_msec);
903903
}
904904

905905
return rc;

drivers/char/tpm/tpm_crb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ static const struct dev_pm_ops crb_pm = {
665665
SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL)
666666
};
667667

668-
static struct acpi_device_id crb_device_ids[] = {
668+
static const struct acpi_device_id crb_device_ids[] = {
669669
{"MSFT0101", 0},
670670
{"", 0},
671671
};

drivers/char/tpm/tpm_ibmvtpm.c

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,70 @@
3232

3333
static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm";
3434

35-
static struct vio_device_id tpm_ibmvtpm_device_table[] = {
35+
static const struct vio_device_id tpm_ibmvtpm_device_table[] = {
3636
{ "IBM,vtpm", "IBM,vtpm"},
3737
{ "", "" }
3838
};
3939
MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table);
4040

4141
/**
42+
*
43+
* ibmvtpm_send_crq_word - Send a CRQ request
44+
* @vdev: vio device struct
45+
* @w1: pre-constructed first word of tpm crq (second word is reserved)
46+
*
47+
* Return:
48+
* 0 - Success
49+
* Non-zero - Failure
50+
*/
51+
static int ibmvtpm_send_crq_word(struct vio_dev *vdev, u64 w1)
52+
{
53+
return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, 0);
54+
}
55+
56+
/**
57+
*
4258
* ibmvtpm_send_crq - Send a CRQ request
4359
*
4460
* @vdev: vio device struct
45-
* @w1: first word
46-
* @w2: second word
61+
* @valid: Valid field
62+
* @msg: Type field
63+
* @len: Length field
64+
* @data: Data field
65+
*
66+
* The ibmvtpm crq is defined as follows:
67+
*
68+
* Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
69+
* -----------------------------------------------------------------------
70+
* Word0 | Valid | Type | Length | Data
71+
* -----------------------------------------------------------------------
72+
* Word1 | Reserved
73+
* -----------------------------------------------------------------------
74+
*
75+
* Which matches the following structure (on bigendian host):
76+
*
77+
* struct ibmvtpm_crq {
78+
* u8 valid;
79+
* u8 msg;
80+
* __be16 len;
81+
* __be32 data;
82+
* __be64 reserved;
83+
* } __attribute__((packed, aligned(8)));
84+
*
85+
* However, the value is passed in a register so just compute the numeric value
86+
* to load into the register avoiding byteswap altogether. Endian only affects
87+
* memory loads and stores - registers are internally represented the same.
4788
*
4889
* Return:
49-
* 0 -Sucess
90+
* 0 (H_SUCCESS) - Success
5091
* Non-zero - Failure
5192
*/
52-
static int ibmvtpm_send_crq(struct vio_dev *vdev, u64 w1, u64 w2)
93+
static int ibmvtpm_send_crq(struct vio_dev *vdev,
94+
u8 valid, u8 msg, u16 len, u32 data)
5395
{
54-
return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, w2);
96+
u64 w1 = ((u64)valid << 56) | ((u64)msg << 48) | ((u64)len << 32) |
97+
(u64)data;
98+
return ibmvtpm_send_crq_word(vdev, w1);
5599
}
56100

57101
/**
@@ -109,8 +153,6 @@ static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
109153
static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
110154
{
111155
struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
112-
struct ibmvtpm_crq crq;
113-
__be64 *word = (__be64 *)&crq;
114156
int rc, sig;
115157

116158
if (!ibmvtpm->rtce_buf) {
@@ -137,19 +179,16 @@ static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
137179
spin_lock(&ibmvtpm->rtce_lock);
138180
ibmvtpm->res_len = 0;
139181
memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count);
140-
crq.valid = (u8)IBMVTPM_VALID_CMD;
141-
crq.msg = (u8)VTPM_TPM_COMMAND;
142-
crq.len = cpu_to_be16(count);
143-
crq.data = cpu_to_be32(ibmvtpm->rtce_dma_handle);
144182

145183
/*
146184
* set the processing flag before the Hcall, since we may get the
147185
* result (interrupt) before even being able to check rc.
148186
*/
149187
ibmvtpm->tpm_processing_cmd = true;
150188

151-
rc = ibmvtpm_send_crq(ibmvtpm->vdev, be64_to_cpu(word[0]),
152-
be64_to_cpu(word[1]));
189+
rc = ibmvtpm_send_crq(ibmvtpm->vdev,
190+
IBMVTPM_VALID_CMD, VTPM_TPM_COMMAND,
191+
count, ibmvtpm->rtce_dma_handle);
153192
if (rc != H_SUCCESS) {
154193
dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
155194
rc = 0;
@@ -182,15 +221,10 @@ static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
182221
*/
183222
static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
184223
{
185-
struct ibmvtpm_crq crq;
186-
u64 *buf = (u64 *) &crq;
187224
int rc;
188225

189-
crq.valid = (u8)IBMVTPM_VALID_CMD;
190-
crq.msg = (u8)VTPM_GET_RTCE_BUFFER_SIZE;
191-
192-
rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
193-
cpu_to_be64(buf[1]));
226+
rc = ibmvtpm_send_crq(ibmvtpm->vdev,
227+
IBMVTPM_VALID_CMD, VTPM_GET_RTCE_BUFFER_SIZE, 0, 0);
194228
if (rc != H_SUCCESS)
195229
dev_err(ibmvtpm->dev,
196230
"ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
@@ -210,15 +244,10 @@ static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
210244
*/
211245
static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
212246
{
213-
struct ibmvtpm_crq crq;
214-
u64 *buf = (u64 *) &crq;
215247
int rc;
216248

217-
crq.valid = (u8)IBMVTPM_VALID_CMD;
218-
crq.msg = (u8)VTPM_GET_VERSION;
219-
220-
rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
221-
cpu_to_be64(buf[1]));
249+
rc = ibmvtpm_send_crq(ibmvtpm->vdev,
250+
IBMVTPM_VALID_CMD, VTPM_GET_VERSION, 0, 0);
222251
if (rc != H_SUCCESS)
223252
dev_err(ibmvtpm->dev,
224253
"ibmvtpm_crq_get_version failed rc=%d\n", rc);
@@ -238,7 +267,7 @@ static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
238267
{
239268
int rc;
240269

241-
rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_COMP_CMD, 0);
270+
rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_COMP_CMD);
242271
if (rc != H_SUCCESS)
243272
dev_err(ibmvtpm->dev,
244273
"ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
@@ -258,7 +287,7 @@ static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
258287
{
259288
int rc;
260289

261-
rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_CMD, 0);
290+
rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_CMD);
262291
if (rc != H_SUCCESS)
263292
dev_err(ibmvtpm->dev,
264293
"ibmvtpm_crq_send_init failed rc=%d\n", rc);
@@ -340,15 +369,10 @@ static int tpm_ibmvtpm_suspend(struct device *dev)
340369
{
341370
struct tpm_chip *chip = dev_get_drvdata(dev);
342371
struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
343-
struct ibmvtpm_crq crq;
344-
u64 *buf = (u64 *) &crq;
345372
int rc = 0;
346373

347-
crq.valid = (u8)IBMVTPM_VALID_CMD;
348-
crq.msg = (u8)VTPM_PREPARE_TO_SUSPEND;
349-
350-
rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
351-
cpu_to_be64(buf[1]));
374+
rc = ibmvtpm_send_crq(ibmvtpm->vdev,
375+
IBMVTPM_VALID_CMD, VTPM_PREPARE_TO_SUSPEND, 0, 0);
352376
if (rc != H_SUCCESS)
353377
dev_err(ibmvtpm->dev,
354378
"tpm_ibmvtpm_suspend failed rc=%d\n", rc);

drivers/char/tpm/tpm_infineon.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static int wait(struct tpm_chip *chip, int wait_for_bit)
191191
/* check the status-register if wait_for_bit is set */
192192
if (status & 1 << wait_for_bit)
193193
break;
194-
msleep(TPM_MSLEEP_TIME);
194+
tpm_msleep(TPM_MSLEEP_TIME);
195195
}
196196
if (i == TPM_MAX_TRIES) { /* timeout occurs */
197197
if (wait_for_bit == STAT_XFE)
@@ -226,7 +226,7 @@ static void tpm_wtx(struct tpm_chip *chip)
226226
wait_and_send(chip, TPM_CTRL_WTX);
227227
wait_and_send(chip, 0x00);
228228
wait_and_send(chip, 0x00);
229-
msleep(TPM_WTX_MSLEEP_TIME);
229+
tpm_msleep(TPM_WTX_MSLEEP_TIME);
230230
}
231231

232232
static void tpm_wtx_abort(struct tpm_chip *chip)
@@ -237,7 +237,7 @@ static void tpm_wtx_abort(struct tpm_chip *chip)
237237
wait_and_send(chip, 0x00);
238238
wait_and_send(chip, 0x00);
239239
number_of_wtx = 0;
240-
msleep(TPM_WTX_MSLEEP_TIME);
240+
tpm_msleep(TPM_WTX_MSLEEP_TIME);
241241
}
242242

243243
static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)

drivers/char/tpm/tpm_tis_core.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int wait_startup(struct tpm_chip *chip, int l)
5151

5252
if (access & TPM_ACCESS_VALID)
5353
return 0;
54-
msleep(TPM_TIMEOUT);
54+
tpm_msleep(TPM_TIMEOUT);
5555
} while (time_before(jiffies, stop));
5656
return -1;
5757
}
@@ -117,7 +117,7 @@ static int request_locality(struct tpm_chip *chip, int l)
117117
do {
118118
if (check_locality(chip, l))
119119
return l;
120-
msleep(TPM_TIMEOUT);
120+
tpm_msleep(TPM_TIMEOUT);
121121
} while (time_before(jiffies, stop));
122122
}
123123
return -1;
@@ -164,7 +164,7 @@ static int get_burstcount(struct tpm_chip *chip)
164164
burstcnt = (value >> 8) & 0xFFFF;
165165
if (burstcnt)
166166
return burstcnt;
167-
msleep(TPM_TIMEOUT);
167+
tpm_msleep(TPM_TIMEOUT);
168168
} while (time_before(jiffies, stop));
169169
return -EBUSY;
170170
}
@@ -396,7 +396,7 @@ static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
396396
priv->irq = irq;
397397
chip->flags |= TPM_CHIP_FLAG_IRQ;
398398
if (!priv->irq_tested)
399-
msleep(1);
399+
tpm_msleep(1);
400400
if (!priv->irq_tested)
401401
disable_interrupts(chip);
402402
priv->irq_tested = true;

0 commit comments

Comments
 (0)