1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005, 2006 IBM Corporation
4 * Copyright (C) 2014, 2015 Intel Corporation
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
15 * This device driver implements the TPM interface as defined in
16 * the TCG TPM Interface Spec version 1.2, revision 1.0.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/pnp.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/wait.h>
25 #include <linux/acpi.h>
26 #include <linux/freezer.h>
27 #include <linux/dmi.h>
29 #include "tpm_tis_core.h"
31 #define TPM_TIS_MAX_UNHANDLED_IRQS 1000
33 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
);
35 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
36 bool check_cancel
, bool *canceled
)
38 u8 status
= chip
->ops
->status(chip
);
41 if ((status
& mask
) == mask
)
43 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
50 static u8
tpm_tis_filter_sts_mask(u8 int_mask
, u8 sts_mask
)
52 if (!(int_mask
& TPM_INTF_STS_VALID_INT
))
53 sts_mask
&= ~TPM_STS_VALID
;
55 if (!(int_mask
& TPM_INTF_DATA_AVAIL_INT
))
56 sts_mask
&= ~TPM_STS_DATA_AVAIL
;
58 if (!(int_mask
& TPM_INTF_CMD_READY_INT
))
59 sts_mask
&= ~TPM_STS_COMMAND_READY
;
64 static int wait_for_tpm_stat(struct tpm_chip
*chip
, u8 mask
,
65 unsigned long timeout
, wait_queue_head_t
*queue
,
68 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
72 bool canceled
= false;
76 /* check current status */
77 status
= chip
->ops
->status(chip
);
78 if ((status
& mask
) == mask
)
81 sts_mask
= mask
& (TPM_STS_VALID
| TPM_STS_DATA_AVAIL
|
82 TPM_STS_COMMAND_READY
);
83 /* check what status changes can be handled by irqs */
84 sts_mask
= tpm_tis_filter_sts_mask(priv
->int_mask
, sts_mask
);
86 stop
= jiffies
+ timeout
;
87 /* process status changes with irq support */
91 timeout
= stop
- jiffies
;
92 if ((long)timeout
<= 0)
94 rc
= wait_event_interruptible_timeout(*queue
,
95 wait_for_tpm_stat_cond(chip
, sts_mask
, check_cancel
,
103 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
104 clear_thread_flag(TIF_SIGPENDING
);
113 if (!mask
) /* all done */
115 /* process status changes without irq support */
117 status
= chip
->ops
->status(chip
);
118 if ((status
& mask
) == mask
)
120 usleep_range(priv
->timeout_min
,
122 } while (time_before(jiffies
, stop
));
126 /* Before we attempt to access the TPM we must see that the valid bit is set.
127 * The specification says that this bit is 0 at reset and remains 0 until the
128 * 'TPM has gone through its self test and initialization and has established
129 * correct values in the other bits.'
131 static int wait_startup(struct tpm_chip
*chip
, int l
)
133 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
134 unsigned long stop
= jiffies
+ chip
->timeout_a
;
140 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
144 if (access
& TPM_ACCESS_VALID
)
146 tpm_msleep(TPM_TIMEOUT
);
147 } while (time_before(jiffies
, stop
));
151 static bool check_locality(struct tpm_chip
*chip
, int l
)
153 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
157 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
161 if ((access
& (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
162 | TPM_ACCESS_REQUEST_USE
)) ==
163 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) {
171 static int __tpm_tis_relinquish_locality(struct tpm_tis_data
*priv
, int l
)
173 tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_ACTIVE_LOCALITY
);
178 static int tpm_tis_relinquish_locality(struct tpm_chip
*chip
, int l
)
180 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
182 mutex_lock(&priv
->locality_count_mutex
);
183 priv
->locality_count
--;
184 if (priv
->locality_count
== 0)
185 __tpm_tis_relinquish_locality(priv
, l
);
186 mutex_unlock(&priv
->locality_count_mutex
);
191 static int __tpm_tis_request_locality(struct tpm_chip
*chip
, int l
)
193 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
194 unsigned long stop
, timeout
;
197 if (check_locality(chip
, l
))
200 rc
= tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_REQUEST_USE
);
204 stop
= jiffies
+ chip
->timeout_a
;
206 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
208 timeout
= stop
- jiffies
;
209 if ((long)timeout
<= 0)
211 rc
= wait_event_interruptible_timeout(priv
->int_queue
,
217 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
218 clear_thread_flag(TIF_SIGPENDING
);
222 /* wait for burstcount */
224 if (check_locality(chip
, l
))
226 tpm_msleep(TPM_TIMEOUT
);
227 } while (time_before(jiffies
, stop
));
232 static int tpm_tis_request_locality(struct tpm_chip
*chip
, int l
)
234 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
237 mutex_lock(&priv
->locality_count_mutex
);
238 if (priv
->locality_count
== 0)
239 ret
= __tpm_tis_request_locality(chip
, l
);
241 priv
->locality_count
++;
242 mutex_unlock(&priv
->locality_count_mutex
);
246 static u8
tpm_tis_status(struct tpm_chip
*chip
)
248 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
252 rc
= tpm_tis_read8(priv
, TPM_STS(priv
->locality
), &status
);
256 if (unlikely((status
& TPM_STS_READ_ZERO
) != 0)) {
257 if (!test_and_set_bit(TPM_TIS_INVALID_STATUS
, &priv
->flags
)) {
259 * If this trips, the chances are the read is
260 * returning 0xff because the locality hasn't been
261 * acquired. Usually because tpm_try_get_ops() hasn't
262 * been called before doing a TPM operation.
264 dev_err(&chip
->dev
, "invalid TPM_STS.x 0x%02x, dumping stack for forensics\n",
268 * Dump stack for forensics, as invalid TPM_STS.x could be
269 * potentially triggered by impaired tpm_try_get_ops() or
270 * tpm_find_get_ops().
281 static void tpm_tis_ready(struct tpm_chip
*chip
)
283 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
285 /* this causes the current command to be aborted */
286 tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_COMMAND_READY
);
289 static int get_burstcount(struct tpm_chip
*chip
)
291 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
296 /* wait for burstcount */
297 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
298 stop
= jiffies
+ chip
->timeout_a
;
300 stop
= jiffies
+ chip
->timeout_d
;
302 rc
= tpm_tis_read32(priv
, TPM_STS(priv
->locality
), &value
);
306 burstcnt
= (value
>> 8) & 0xFFFF;
309 usleep_range(TPM_TIMEOUT_USECS_MIN
, TPM_TIMEOUT_USECS_MAX
);
310 } while (time_before(jiffies
, stop
));
314 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
316 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
317 int size
= 0, burstcnt
, rc
;
319 while (size
< count
) {
320 rc
= wait_for_tpm_stat(chip
,
321 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
323 &priv
->read_queue
, true);
326 burstcnt
= get_burstcount(chip
);
328 dev_err(&chip
->dev
, "Unable to read burstcount\n");
331 burstcnt
= min_t(int, burstcnt
, count
- size
);
333 rc
= tpm_tis_read_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
334 burstcnt
, buf
+ size
);
343 static int tpm_tis_try_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
345 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
351 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
352 /* read first 10 bytes, including tag, paramsize, and result */
353 if (size
< TPM_HEADER_SIZE
) {
354 dev_err(&chip
->dev
, "Unable to read header\n");
358 expected
= be32_to_cpu(*(__be32
*) (buf
+ 2));
359 if (expected
> count
|| expected
< TPM_HEADER_SIZE
) {
364 rc
= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
365 expected
- TPM_HEADER_SIZE
);
371 if (size
< expected
) {
372 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
377 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
378 &priv
->int_queue
, false) < 0) {
382 status
= tpm_tis_status(chip
);
383 if (status
& TPM_STS_DATA_AVAIL
) {
384 dev_err(&chip
->dev
, "Error left over data\n");
389 rc
= tpm_tis_verify_crc(priv
, (size_t)size
, buf
);
391 dev_err(&chip
->dev
, "CRC mismatch for response.\n");
400 static int tpm_tis_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
402 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
406 if (count
< TPM_HEADER_SIZE
)
409 for (try = 0; try < TPM_RETRY
; try++) {
410 rc
= tpm_tis_try_recv(chip
, buf
, count
);
413 /* Data transfer errors, indicated by EIO, can be
414 * recovered by rereading the response.
416 tpm_tis_write8(priv
, TPM_STS(priv
->locality
),
417 TPM_STS_RESPONSE_RETRY
);
428 * If interrupts are used (signaled by an irq set in the vendor structure)
429 * tpm.c can skip polling for the data to be available as the interrupt is
432 static int tpm_tis_send_data(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
434 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
435 int rc
, status
, burstcnt
;
437 bool itpm
= test_bit(TPM_TIS_ITPM_WORKAROUND
, &priv
->flags
);
439 status
= tpm_tis_status(chip
);
440 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
442 if (wait_for_tpm_stat
443 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
444 &priv
->int_queue
, false) < 0) {
450 while (count
< len
- 1) {
451 burstcnt
= get_burstcount(chip
);
453 dev_err(&chip
->dev
, "Unable to read burstcount\n");
457 burstcnt
= min_t(int, burstcnt
, len
- count
- 1);
458 rc
= tpm_tis_write_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
459 burstcnt
, buf
+ count
);
465 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
466 &priv
->int_queue
, false) < 0) {
470 status
= tpm_tis_status(chip
);
471 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) == 0) {
477 /* write last byte */
478 rc
= tpm_tis_write8(priv
, TPM_DATA_FIFO(priv
->locality
), buf
[count
]);
482 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
483 &priv
->int_queue
, false) < 0) {
487 status
= tpm_tis_status(chip
);
488 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) != 0) {
493 rc
= tpm_tis_verify_crc(priv
, len
, buf
);
495 dev_err(&chip
->dev
, "CRC mismatch for command.\n");
506 static void __tpm_tis_disable_interrupts(struct tpm_chip
*chip
)
508 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
511 tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &int_mask
);
512 int_mask
&= ~TPM_GLOBAL_INT_ENABLE
;
513 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), int_mask
);
515 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
518 static void tpm_tis_disable_interrupts(struct tpm_chip
*chip
)
520 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
525 __tpm_tis_disable_interrupts(chip
);
527 devm_free_irq(chip
->dev
.parent
, priv
->irq
, chip
);
532 * If interrupts are used (signaled by an irq set in the vendor structure)
533 * tpm.c can skip polling for the data to be available as the interrupt is
536 static int tpm_tis_send_main(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
538 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
544 for (try = 0; try < TPM_RETRY
; try++) {
545 rc
= tpm_tis_send_data(chip
, buf
, len
);
547 /* Data transfer done successfully */
550 /* Data transfer failed, not recoverable */
555 rc
= tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_GO
);
559 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
560 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
562 dur
= tpm_calc_ordinal_duration(chip
, ordinal
);
563 if (wait_for_tpm_stat
564 (chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
, dur
,
565 &priv
->read_queue
, false) < 0) {
576 static int tpm_tis_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
579 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
581 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
) ||
582 test_bit(TPM_TIS_IRQ_TESTED
, &priv
->flags
))
583 return tpm_tis_send_main(chip
, buf
, len
);
585 /* Verify receipt of the expected IRQ */
588 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
589 rc
= tpm_tis_send_main(chip
, buf
, len
);
591 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
592 if (!test_bit(TPM_TIS_IRQ_TESTED
, &priv
->flags
))
594 if (!test_bit(TPM_TIS_IRQ_TESTED
, &priv
->flags
))
595 tpm_tis_disable_interrupts(chip
);
596 set_bit(TPM_TIS_IRQ_TESTED
, &priv
->flags
);
600 struct tis_vendor_durations_override
{
602 struct tpm1_version version
;
603 unsigned long durations
[3];
606 static const struct tis_vendor_durations_override vendor_dur_overrides
[] = {
607 /* STMicroelectronics 0x104a */
610 { (2 * 60 * HZ
), (2 * 60 * HZ
), (2 * 60 * HZ
) } },
613 static void tpm_tis_update_durations(struct tpm_chip
*chip
,
614 unsigned long *duration_cap
)
616 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
617 struct tpm1_version
*version
;
622 chip
->duration_adjusted
= false;
624 if (chip
->ops
->clk_enable
!= NULL
)
625 chip
->ops
->clk_enable(chip
, true);
627 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
629 dev_warn(&chip
->dev
, "%s: failed to read did_vid. %d\n",
634 /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */
635 rc
= tpm1_getcap(chip
, TPM_CAP_VERSION_1_2
, &cap
,
636 "attempting to determine the 1.2 version",
637 sizeof(cap
.version2
));
639 version
= &cap
.version2
.version
;
641 rc
= tpm1_getcap(chip
, TPM_CAP_VERSION_1_1
, &cap
,
642 "attempting to determine the 1.1 version",
643 sizeof(cap
.version1
));
648 version
= &cap
.version1
;
651 for (i
= 0; i
!= ARRAY_SIZE(vendor_dur_overrides
); i
++) {
652 if (vendor_dur_overrides
[i
].did_vid
!= did_vid
)
655 if ((version
->major
==
656 vendor_dur_overrides
[i
].version
.major
) &&
658 vendor_dur_overrides
[i
].version
.minor
) &&
659 (version
->rev_major
==
660 vendor_dur_overrides
[i
].version
.rev_major
) &&
661 (version
->rev_minor
==
662 vendor_dur_overrides
[i
].version
.rev_minor
)) {
665 vendor_dur_overrides
[i
].durations
,
666 sizeof(vendor_dur_overrides
[i
].durations
));
668 chip
->duration_adjusted
= true;
674 if (chip
->ops
->clk_enable
!= NULL
)
675 chip
->ops
->clk_enable(chip
, false);
678 struct tis_vendor_timeout_override
{
680 unsigned long timeout_us
[4];
683 static const struct tis_vendor_timeout_override vendor_timeout_overrides
[] = {
685 { 0x32041114, { (TIS_SHORT_TIMEOUT
*1000), (TIS_LONG_TIMEOUT
*1000),
686 (TIS_SHORT_TIMEOUT
*1000), (TIS_SHORT_TIMEOUT
*1000) } },
689 static void tpm_tis_update_timeouts(struct tpm_chip
*chip
,
690 unsigned long *timeout_cap
)
692 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
696 chip
->timeout_adjusted
= false;
698 if (chip
->ops
->clk_enable
!= NULL
)
699 chip
->ops
->clk_enable(chip
, true);
701 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
703 dev_warn(&chip
->dev
, "%s: failed to read did_vid: %d\n",
708 for (i
= 0; i
!= ARRAY_SIZE(vendor_timeout_overrides
); i
++) {
709 if (vendor_timeout_overrides
[i
].did_vid
!= did_vid
)
711 memcpy(timeout_cap
, vendor_timeout_overrides
[i
].timeout_us
,
712 sizeof(vendor_timeout_overrides
[i
].timeout_us
));
713 chip
->timeout_adjusted
= true;
717 if (chip
->ops
->clk_enable
!= NULL
)
718 chip
->ops
->clk_enable(chip
, false);
724 * Early probing for iTPM with STS_DATA_EXPECT flaw.
725 * Try sending command without itpm flag set and if that
726 * fails, repeat with itpm flag set.
728 static int probe_itpm(struct tpm_chip
*chip
)
730 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
732 static const u8 cmd_getticks
[] = {
733 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
734 0x00, 0x00, 0x00, 0xf1
736 size_t len
= sizeof(cmd_getticks
);
739 if (test_bit(TPM_TIS_ITPM_WORKAROUND
, &priv
->flags
))
742 rc
= tpm_tis_read16(priv
, TPM_DID_VID(0), &vendor
);
746 /* probe only iTPMS */
747 if (vendor
!= TPM_VID_INTEL
)
750 if (tpm_tis_request_locality(chip
, 0) != 0)
753 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
759 set_bit(TPM_TIS_ITPM_WORKAROUND
, &priv
->flags
);
761 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
763 dev_info(&chip
->dev
, "Detected an iTPM.\n");
765 clear_bit(TPM_TIS_ITPM_WORKAROUND
, &priv
->flags
);
771 tpm_tis_relinquish_locality(chip
, priv
->locality
);
776 static bool tpm_tis_req_canceled(struct tpm_chip
*chip
, u8 status
)
778 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
780 if (!test_bit(TPM_TIS_DEFAULT_CANCELLATION
, &priv
->flags
)) {
781 switch (priv
->manufacturer_id
) {
782 case TPM_VID_WINBOND
:
783 return ((status
== TPM_STS_VALID
) ||
784 (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
)));
786 return (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
));
792 return status
== TPM_STS_COMMAND_READY
;
795 static irqreturn_t
tpm_tis_revert_interrupts(struct tpm_chip
*chip
)
797 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
801 dev_warn(&chip
->dev
, FW_BUG
802 "TPM interrupt storm detected, polling instead\n");
804 vendor
= dmi_get_system_info(DMI_SYS_VENDOR
);
805 product
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
807 if (vendor
&& product
) {
809 "Consider adding the following entry to tpm_tis_dmi_table:\n");
810 dev_info(&chip
->dev
, "\tDMI_SYS_VENDOR: %s\n", vendor
);
811 dev_info(&chip
->dev
, "\tDMI_PRODUCT_VERSION: %s\n", product
);
814 if (tpm_tis_request_locality(chip
, 0) != 0)
817 __tpm_tis_disable_interrupts(chip
);
818 tpm_tis_relinquish_locality(chip
, 0);
820 schedule_work(&priv
->free_irq_work
);
825 static irqreturn_t
tpm_tis_update_unhandled_irqs(struct tpm_chip
*chip
)
827 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
828 irqreturn_t irqret
= IRQ_HANDLED
;
830 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
))
833 if (time_after(jiffies
, priv
->last_unhandled_irq
+ HZ
/10))
834 priv
->unhandled_irqs
= 1;
836 priv
->unhandled_irqs
++;
838 priv
->last_unhandled_irq
= jiffies
;
840 if (priv
->unhandled_irqs
> TPM_TIS_MAX_UNHANDLED_IRQS
)
841 irqret
= tpm_tis_revert_interrupts(chip
);
846 static irqreturn_t
tis_int_handler(int dummy
, void *dev_id
)
848 struct tpm_chip
*chip
= dev_id
;
849 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
853 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
860 set_bit(TPM_TIS_IRQ_TESTED
, &priv
->flags
);
861 if (interrupt
& TPM_INTF_DATA_AVAIL_INT
)
862 wake_up_interruptible(&priv
->read_queue
);
865 (TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_STS_VALID_INT
|
866 TPM_INTF_CMD_READY_INT
))
867 wake_up_interruptible(&priv
->int_queue
);
869 /* Clear interrupts handled with TPM_EOI */
870 tpm_tis_request_locality(chip
, 0);
871 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), interrupt
);
872 tpm_tis_relinquish_locality(chip
, 0);
876 tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
880 return tpm_tis_update_unhandled_irqs(chip
);
883 static void tpm_tis_gen_interrupt(struct tpm_chip
*chip
)
885 const char *desc
= "attempting to generate an interrupt";
890 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
892 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
893 ret
= tpm2_get_tpm_pt(chip
, 0x100, &cap2
, desc
);
895 ret
= tpm1_getcap(chip
, TPM_CAP_PROP_TIS_TIMEOUT
, &cap
, desc
, 0);
898 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
901 static void tpm_tis_free_irq_func(struct work_struct
*work
)
903 struct tpm_tis_data
*priv
= container_of(work
, typeof(*priv
), free_irq_work
);
904 struct tpm_chip
*chip
= priv
->chip
;
906 devm_free_irq(chip
->dev
.parent
, priv
->irq
, chip
);
910 /* Register the IRQ and issue a command that will cause an interrupt. If an
911 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
912 * everything and leave in polling mode. Returns 0 on success.
914 static int tpm_tis_probe_irq_single(struct tpm_chip
*chip
, u32 intmask
,
917 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
922 rc
= devm_request_threaded_irq(chip
->dev
.parent
, irq
, NULL
,
923 tis_int_handler
, IRQF_ONESHOT
| flags
,
924 dev_name(&chip
->dev
), chip
);
926 dev_info(&chip
->dev
, "Unable to request irq: %d for probe\n",
932 rc
= tpm_tis_request_locality(chip
, 0);
936 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
939 tpm_tis_relinquish_locality(chip
, priv
->locality
);
943 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), irq
);
947 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &int_status
);
951 /* Clear all existing */
952 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), int_status
);
956 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
),
957 intmask
| TPM_GLOBAL_INT_ENABLE
);
961 clear_bit(TPM_TIS_IRQ_TESTED
, &priv
->flags
);
963 /* Generate an interrupt by having the core call through to
966 tpm_tis_gen_interrupt(chip
);
969 /* tpm_tis_send will either confirm the interrupt is working or it
970 * will call disable_irq which undoes all of the above.
972 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
)) {
973 tpm_tis_write8(priv
, original_int_vec
,
974 TPM_INT_VECTOR(priv
->locality
));
978 tpm_tis_relinquish_locality(chip
, priv
->locality
);
983 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
984 * do not have ACPI/etc. We typically expect the interrupt to be declared if
987 static void tpm_tis_probe_irq(struct tpm_chip
*chip
, u32 intmask
)
989 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
993 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
998 if (!original_int_vec
) {
999 if (IS_ENABLED(CONFIG_X86
))
1000 for (i
= 3; i
<= 15; i
++)
1001 if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
1004 } else if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
1009 void tpm_tis_remove(struct tpm_chip
*chip
)
1011 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
1012 u32 reg
= TPM_INT_ENABLE(priv
->locality
);
1016 tpm_tis_clkrun_enable(chip
, true);
1018 rc
= tpm_tis_read32(priv
, reg
, &interrupt
);
1022 tpm_tis_write32(priv
, reg
, ~TPM_GLOBAL_INT_ENABLE
& interrupt
);
1023 if (priv
->free_irq_work
.func
)
1024 flush_work(&priv
->free_irq_work
);
1026 tpm_tis_clkrun_enable(chip
, false);
1028 if (priv
->ilb_base_addr
)
1029 iounmap(priv
->ilb_base_addr
);
1031 EXPORT_SYMBOL_GPL(tpm_tis_remove
);
1034 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
1035 * of a single TPM command
1036 * @chip: TPM chip to use
1037 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
1038 * 0 - Enable CLKRUN protocol
1039 * Call this function directly in tpm_tis_remove() in error or driver removal
1040 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
1042 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
)
1044 struct tpm_tis_data
*data
= dev_get_drvdata(&chip
->dev
);
1047 if (!IS_ENABLED(CONFIG_X86
) || !is_bsw() ||
1048 !data
->ilb_base_addr
)
1052 data
->clkrun_enabled
++;
1053 if (data
->clkrun_enabled
> 1)
1055 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
1057 /* Disable LPC CLKRUN# */
1058 clkrun_val
&= ~LPC_CLKRUN_EN
;
1059 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
1062 data
->clkrun_enabled
--;
1063 if (data
->clkrun_enabled
)
1066 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
1068 /* Enable LPC CLKRUN# */
1069 clkrun_val
|= LPC_CLKRUN_EN
;
1070 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
1073 #ifdef CONFIG_HAS_IOPORT
1075 * Write any random value on port 0x80 which is on LPC, to make
1076 * sure LPC clock is running before sending any TPM command.
1082 static const struct tpm_class_ops tpm_tis
= {
1083 .flags
= TPM_OPS_AUTO_STARTUP
,
1084 .status
= tpm_tis_status
,
1085 .recv
= tpm_tis_recv
,
1086 .send
= tpm_tis_send
,
1087 .cancel
= tpm_tis_ready
,
1088 .update_timeouts
= tpm_tis_update_timeouts
,
1089 .update_durations
= tpm_tis_update_durations
,
1090 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
1091 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
1092 .req_canceled
= tpm_tis_req_canceled
,
1093 .request_locality
= tpm_tis_request_locality
,
1094 .relinquish_locality
= tpm_tis_relinquish_locality
,
1095 .clk_enable
= tpm_tis_clkrun_enable
,
1098 int tpm_tis_core_init(struct device
*dev
, struct tpm_tis_data
*priv
, int irq
,
1099 const struct tpm_tis_phy_ops
*phy_ops
,
1100 acpi_handle acpi_dev_handle
)
1108 struct tpm_chip
*chip
;
1110 chip
= tpmm_chip_alloc(dev
, &tpm_tis
);
1112 return PTR_ERR(chip
);
1115 chip
->acpi_dev_handle
= acpi_dev_handle
;
1118 chip
->hwrng
.quality
= priv
->rng_quality
;
1120 /* Maximum timeouts */
1121 chip
->timeout_a
= msecs_to_jiffies(TIS_TIMEOUT_A_MAX
);
1122 chip
->timeout_b
= msecs_to_jiffies(TIS_TIMEOUT_B_MAX
);
1123 chip
->timeout_c
= msecs_to_jiffies(TIS_TIMEOUT_C_MAX
);
1124 chip
->timeout_d
= msecs_to_jiffies(TIS_TIMEOUT_D_MAX
);
1126 priv
->timeout_min
= TPM_TIMEOUT_USECS_MIN
;
1127 priv
->timeout_max
= TPM_TIMEOUT_USECS_MAX
;
1128 priv
->phy_ops
= phy_ops
;
1129 priv
->locality_count
= 0;
1130 mutex_init(&priv
->locality_count_mutex
);
1131 INIT_WORK(&priv
->free_irq_work
, tpm_tis_free_irq_func
);
1133 dev_set_drvdata(&chip
->dev
, priv
);
1135 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &vendor
);
1139 priv
->manufacturer_id
= vendor
;
1141 if (priv
->manufacturer_id
== TPM_VID_ATML
&&
1142 !(chip
->flags
& TPM_CHIP_FLAG_TPM2
)) {
1143 priv
->timeout_min
= TIS_TIMEOUT_MIN_ATML
;
1144 priv
->timeout_max
= TIS_TIMEOUT_MAX_ATML
;
1148 priv
->ilb_base_addr
= ioremap(INTEL_LEGACY_BLK_BASE_ADDR
,
1150 if (!priv
->ilb_base_addr
)
1153 clkrun_val
= ioread32(priv
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
1154 /* Check if CLKRUN# is already not enabled in the LPC bus */
1155 if (!(clkrun_val
& LPC_CLKRUN_EN
)) {
1156 iounmap(priv
->ilb_base_addr
);
1157 priv
->ilb_base_addr
= NULL
;
1161 if (chip
->ops
->clk_enable
!= NULL
)
1162 chip
->ops
->clk_enable(chip
, true);
1164 if (wait_startup(chip
, 0) != 0) {
1169 /* Take control of the TPM's interrupt hardware and shut it off */
1170 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
1174 /* Figure out the capabilities */
1175 rc
= tpm_tis_read32(priv
, TPM_INTF_CAPS(priv
->locality
), &intfcaps
);
1179 dev_dbg(dev
, "TPM interface capabilities (0x%x):\n",
1181 if (intfcaps
& TPM_INTF_BURST_COUNT_STATIC
)
1182 dev_dbg(dev
, "\tBurst Count Static\n");
1183 if (intfcaps
& TPM_INTF_CMD_READY_INT
) {
1184 intmask
|= TPM_INTF_CMD_READY_INT
;
1185 dev_dbg(dev
, "\tCommand Ready Int Support\n");
1187 if (intfcaps
& TPM_INTF_INT_EDGE_FALLING
)
1188 dev_dbg(dev
, "\tInterrupt Edge Falling\n");
1189 if (intfcaps
& TPM_INTF_INT_EDGE_RISING
)
1190 dev_dbg(dev
, "\tInterrupt Edge Rising\n");
1191 if (intfcaps
& TPM_INTF_INT_LEVEL_LOW
)
1192 dev_dbg(dev
, "\tInterrupt Level Low\n");
1193 if (intfcaps
& TPM_INTF_INT_LEVEL_HIGH
)
1194 dev_dbg(dev
, "\tInterrupt Level High\n");
1195 if (intfcaps
& TPM_INTF_LOCALITY_CHANGE_INT
) {
1196 intmask
|= TPM_INTF_LOCALITY_CHANGE_INT
;
1197 dev_dbg(dev
, "\tLocality Change Int Support\n");
1199 if (intfcaps
& TPM_INTF_STS_VALID_INT
) {
1200 intmask
|= TPM_INTF_STS_VALID_INT
;
1201 dev_dbg(dev
, "\tSts Valid Int Support\n");
1203 if (intfcaps
& TPM_INTF_DATA_AVAIL_INT
) {
1204 intmask
|= TPM_INTF_DATA_AVAIL_INT
;
1205 dev_dbg(dev
, "\tData Avail Int Support\n");
1208 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
1210 rc
= tpm_tis_request_locality(chip
, 0);
1216 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
1217 tpm_tis_relinquish_locality(chip
, 0);
1219 rc
= tpm_chip_start(chip
);
1222 rc
= tpm2_probe(chip
);
1223 tpm_chip_stop(chip
);
1227 rc
= tpm_tis_read8(priv
, TPM_RID(0), &rid
);
1231 dev_info(dev
, "%s TPM (device-id 0x%X, rev-id %d)\n",
1232 (chip
->flags
& TPM_CHIP_FLAG_TPM2
) ? "2.0" : "1.2",
1235 probe
= probe_itpm(chip
);
1241 /* INTERRUPT Setup */
1242 init_waitqueue_head(&priv
->read_queue
);
1243 init_waitqueue_head(&priv
->int_queue
);
1245 rc
= tpm_chip_bootstrap(chip
);
1251 * Before doing irq testing issue a command to the TPM in polling mode
1252 * to make sure it works. May as well use that command to set the
1253 * proper timeouts for the driver.
1256 rc
= tpm_tis_request_locality(chip
, 0);
1260 rc
= tpm_get_timeouts(chip
);
1262 tpm_tis_relinquish_locality(chip
, 0);
1265 dev_err(dev
, "Could not get TPM timeouts and durations\n");
1271 tpm_tis_probe_irq_single(chip
, intmask
, IRQF_SHARED
,
1274 tpm_tis_probe_irq(chip
, intmask
);
1276 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
1277 priv
->int_mask
= intmask
;
1279 dev_err(&chip
->dev
, FW_BUG
1280 "TPM interrupt not working, polling instead\n");
1282 rc
= tpm_tis_request_locality(chip
, 0);
1285 tpm_tis_disable_interrupts(chip
);
1286 tpm_tis_relinquish_locality(chip
, 0);
1290 rc
= tpm_chip_register(chip
);
1294 if (chip
->ops
->clk_enable
!= NULL
)
1295 chip
->ops
->clk_enable(chip
, false);
1299 if (chip
->ops
->clk_enable
!= NULL
)
1300 chip
->ops
->clk_enable(chip
, false);
1302 tpm_tis_remove(chip
);
1306 EXPORT_SYMBOL_GPL(tpm_tis_core_init
);
1308 #ifdef CONFIG_PM_SLEEP
1309 static void tpm_tis_reenable_interrupts(struct tpm_chip
*chip
)
1311 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
1316 * Re-enable interrupts that device may have lost or BIOS/firmware may
1319 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), priv
->irq
);
1321 dev_err(&chip
->dev
, "Setting IRQ failed.\n");
1325 intmask
= priv
->int_mask
| TPM_GLOBAL_INT_ENABLE
;
1326 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
1328 dev_err(&chip
->dev
, "Enabling interrupts failed.\n");
1331 int tpm_tis_resume(struct device
*dev
)
1333 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1336 ret
= tpm_chip_start(chip
);
1340 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
)
1341 tpm_tis_reenable_interrupts(chip
);
1344 * TPM 1.2 requires self-test on resume. This function actually returns
1345 * an error code but for unknown reason it isn't handled.
1347 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
))
1348 tpm1_do_selftest(chip
);
1350 tpm_chip_stop(chip
);
1352 ret
= tpm_pm_resume(dev
);
1358 EXPORT_SYMBOL_GPL(tpm_tis_resume
);
1361 MODULE_AUTHOR("Leendert van Doorn <leendert@watson.ibm.com>");
1362 MODULE_DESCRIPTION("TPM Driver");
1363 MODULE_VERSION("2.0");
1364 MODULE_LICENSE("GPL");