2019-06-01 10:08:55 +02:00
/* SPDX-License-Identifier: GPL-2.0-only */
2016-05-19 00:35:48 +02:00
/*
* Copyright ( C ) 2005 , 2006 IBM Corporation
* Copyright ( C ) 2014 , 2015 Intel Corporation
*
* Authors :
* Leendert van Doorn < leendert @ watson . ibm . com >
* Kylene Hall < kjhall @ us . ibm . com >
*
* Maintained by : < tpmdd - devel @ lists . sourceforge . net >
*
* Device driver for TCG / TCPA TPM ( trusted platform module ) .
* Specifications at www . trustedcomputinggroup . org
*
* This device driver implements the TPM interface as defined in
* the TCG TPM Interface Spec version 1.2 , revision 1.0 .
*/
# ifndef __TPM_TIS_CORE_H__
# define __TPM_TIS_CORE_H__
# include "tpm.h"
2016-05-19 00:35:52 +02:00
enum tis_access {
TPM_ACCESS_VALID = 0x80 ,
TPM_ACCESS_ACTIVE_LOCALITY = 0x20 ,
TPM_ACCESS_REQUEST_PENDING = 0x04 ,
TPM_ACCESS_REQUEST_USE = 0x02 ,
} ;
enum tis_status {
TPM_STS_VALID = 0x80 ,
TPM_STS_COMMAND_READY = 0x40 ,
TPM_STS_GO = 0x20 ,
TPM_STS_DATA_AVAIL = 0x10 ,
TPM_STS_DATA_EXPECT = 0x08 ,
2020-09-28 11:00:12 -07:00
TPM_STS_READ_ZERO = 0x23 , /* bits that must be zero on read */
2016-05-19 00:35:52 +02:00
} ;
enum tis_int_flags {
TPM_GLOBAL_INT_ENABLE = 0x80000000 ,
TPM_INTF_BURST_COUNT_STATIC = 0x100 ,
TPM_INTF_CMD_READY_INT = 0x080 ,
TPM_INTF_INT_EDGE_FALLING = 0x040 ,
TPM_INTF_INT_EDGE_RISING = 0x020 ,
TPM_INTF_INT_LEVEL_LOW = 0x010 ,
TPM_INTF_INT_LEVEL_HIGH = 0x008 ,
TPM_INTF_LOCALITY_CHANGE_INT = 0x004 ,
TPM_INTF_STS_VALID_INT = 0x002 ,
TPM_INTF_DATA_AVAIL_INT = 0x001 ,
} ;
enum tis_defaults {
TIS_MEM_LEN = 0x5000 ,
TIS_SHORT_TIMEOUT = 750 , /* ms */
TIS_LONG_TIMEOUT = 2000 , /* 2 sec */
tpm: fix Atmel TPM crash caused by too frequent queries
The Atmel TPM 1.2 chips crash with error
`tpm_try_transmit: send(): error -62` since kernel 4.14.
It is observed from the kernel log after running `tpm_sealdata -z`.
The error thrown from the command is as follows
```
$ tpm_sealdata -z
Tspi_Key_LoadKey failed: 0x00001087 - layer=tddl,
code=0087 (135), I/O error
```
The issue was reproduced with the following Atmel TPM chip:
```
$ tpm_version
T0 TPM 1.2 Version Info:
Chip Version: 1.2.66.1
Spec Level: 2
Errata Revision: 3
TPM Vendor ID: ATML
TPM Version: 01010000
Manufacturer Info: 41544d4c
```
The root cause of the issue is due to the TPM calls to msleep()
were replaced with usleep_range() [1], which reduces
the actual timeout. Via experiments, it is observed that
the original msleep(5) actually sleeps for 15ms.
Because of a known timeout issue in Atmel TPM 1.2 chip,
the shorter timeout than 15ms can cause the error described above.
A few further changes in kernel 4.16 [2] and 4.18 [3, 4] further
reduced the timeout to less than 1ms. With experiments,
the problematic timeout in the latest kernel is the one
for `wait_for_tpm_stat`.
To fix it, the patch reverts the timeout of `wait_for_tpm_stat`
to 15ms for all Atmel TPM 1.2 chips, but leave it untouched
for Ateml TPM 2.0 chip, and chips from other vendors.
As explained above, the chosen 15ms timeout is
the actual timeout before this issue introduced,
thus the old value is used here.
Particularly, TPM_ATML_TIMEOUT_WAIT_STAT_MIN is set to 14700us,
TPM_ATML_TIMEOUT_WAIT_STAT_MIN is set to 15000us according to
the existing TPM_TIMEOUT_RANGE_US (300us).
The fixed has been tested in the system with the affected Atmel chip
with no issues observed after boot up.
References:
[1] 9f3fc7bcddcb tpm: replace msleep() with usleep_range() in TPM
1.2/2.0 generic drivers
[2] cf151a9a44d5 tpm: reduce tpm polling delay in tpm_tis_core
[3] 59f5a6b07f64 tpm: reduce poll sleep time in tpm_transmit()
[4] 424eaf910c32 tpm: reduce polling time to usecs for even finer
granularity
Fixes: 9f3fc7bcddcb ("tpm: replace msleep() with usleep_range() in TPM 1.2/2.0 generic drivers")
Link: https://patchwork.kernel.org/project/linux-integrity/patch/20200926223150.109645-1-hao.wu@rubrik.com/
Signed-off-by: Hao Wu <hao.wu@rubrik.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
2021-09-08 02:26:06 -07:00
TIS_TIMEOUT_MIN_ATML = 14700 , /* usecs */
TIS_TIMEOUT_MAX_ATML = 15000 , /* usecs */
2016-05-19 00:35:52 +02:00
} ;
/* Some timeout values are needed before it is known whether the chip is
* TPM 1.0 or TPM 2.0 .
*/
2018-03-30 18:52:36 -07:00
# define TIS_TIMEOUT_A_MAX max_t(int, TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A)
# define TIS_TIMEOUT_B_MAX max_t(int, TIS_LONG_TIMEOUT, TPM2_TIMEOUT_B)
# define TIS_TIMEOUT_C_MAX max_t(int, TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_C)
# define TIS_TIMEOUT_D_MAX max_t(int, TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_D)
2016-05-19 00:35:52 +02:00
# define TPM_ACCESS(l) (0x0000 | ((l) << 12))
# define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
# define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
# define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
# define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
# define TPM_STS(l) (0x0018 | ((l) << 12))
# define TPM_STS3(l) (0x001b | ((l) << 12))
# define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
# define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
# define TPM_RID(l) (0x0F04 | ((l) << 12))
2017-12-22 12:13:44 -08:00
# define LPC_CNTRL_OFFSET 0x84
# define LPC_CLKRUN_EN (1 << 2)
2017-12-22 12:13:43 -08:00
# define INTEL_LEGACY_BLK_BASE_ADDR 0xFED08000
# define ILB_REMAP_SIZE 0x100
2016-05-19 00:35:52 +02:00
enum tpm_tis_flags {
2023-05-30 18:41:16 +02:00
TPM_TIS_ITPM_WORKAROUND = 0 ,
TPM_TIS_INVALID_STATUS = 1 ,
TPM_TIS_DEFAULT_CANCELLATION = 2 ,
TPM_TIS_IRQ_TESTED = 3 ,
2016-05-19 00:35:52 +02:00
} ;
2016-05-19 00:35:48 +02:00
struct tpm_tis_data {
2023-07-13 21:01:08 +02:00
struct tpm_chip * chip ;
2016-05-19 00:35:48 +02:00
u16 manufacturer_id ;
2022-11-24 14:55:33 +01:00
struct mutex locality_count_mutex ;
unsigned int locality_count ;
2016-05-19 00:35:48 +02:00
int locality ;
int irq ;
2023-07-13 21:01:08 +02:00
struct work_struct free_irq_work ;
unsigned long last_unhandled_irq ;
unsigned int unhandled_irqs ;
2022-11-24 14:55:30 +01:00
unsigned int int_mask ;
2021-06-09 16:26:19 +03:00
unsigned long flags ;
2017-12-22 12:13:43 -08:00
void __iomem * ilb_base_addr ;
2017-12-22 12:13:44 -08:00
u16 clkrun_enabled ;
2016-05-19 00:35:48 +02:00
wait_queue_head_t int_queue ;
wait_queue_head_t read_queue ;
2016-05-19 00:35:49 +02:00
const struct tpm_tis_phy_ops * phy_ops ;
2018-06-29 16:13:55 +08:00
unsigned short rng_quality ;
tpm: fix Atmel TPM crash caused by too frequent queries
The Atmel TPM 1.2 chips crash with error
`tpm_try_transmit: send(): error -62` since kernel 4.14.
It is observed from the kernel log after running `tpm_sealdata -z`.
The error thrown from the command is as follows
```
$ tpm_sealdata -z
Tspi_Key_LoadKey failed: 0x00001087 - layer=tddl,
code=0087 (135), I/O error
```
The issue was reproduced with the following Atmel TPM chip:
```
$ tpm_version
T0 TPM 1.2 Version Info:
Chip Version: 1.2.66.1
Spec Level: 2
Errata Revision: 3
TPM Vendor ID: ATML
TPM Version: 01010000
Manufacturer Info: 41544d4c
```
The root cause of the issue is due to the TPM calls to msleep()
were replaced with usleep_range() [1], which reduces
the actual timeout. Via experiments, it is observed that
the original msleep(5) actually sleeps for 15ms.
Because of a known timeout issue in Atmel TPM 1.2 chip,
the shorter timeout than 15ms can cause the error described above.
A few further changes in kernel 4.16 [2] and 4.18 [3, 4] further
reduced the timeout to less than 1ms. With experiments,
the problematic timeout in the latest kernel is the one
for `wait_for_tpm_stat`.
To fix it, the patch reverts the timeout of `wait_for_tpm_stat`
to 15ms for all Atmel TPM 1.2 chips, but leave it untouched
for Ateml TPM 2.0 chip, and chips from other vendors.
As explained above, the chosen 15ms timeout is
the actual timeout before this issue introduced,
thus the old value is used here.
Particularly, TPM_ATML_TIMEOUT_WAIT_STAT_MIN is set to 14700us,
TPM_ATML_TIMEOUT_WAIT_STAT_MIN is set to 15000us according to
the existing TPM_TIMEOUT_RANGE_US (300us).
The fixed has been tested in the system with the affected Atmel chip
with no issues observed after boot up.
References:
[1] 9f3fc7bcddcb tpm: replace msleep() with usleep_range() in TPM
1.2/2.0 generic drivers
[2] cf151a9a44d5 tpm: reduce tpm polling delay in tpm_tis_core
[3] 59f5a6b07f64 tpm: reduce poll sleep time in tpm_transmit()
[4] 424eaf910c32 tpm: reduce polling time to usecs for even finer
granularity
Fixes: 9f3fc7bcddcb ("tpm: replace msleep() with usleep_range() in TPM 1.2/2.0 generic drivers")
Link: https://patchwork.kernel.org/project/linux-integrity/patch/20200926223150.109645-1-hao.wu@rubrik.com/
Signed-off-by: Hao Wu <hao.wu@rubrik.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
2021-09-08 02:26:06 -07:00
unsigned int timeout_min ; /* usecs */
unsigned int timeout_max ; /* usecs */
2016-05-19 00:35:48 +02:00
} ;
2022-03-21 10:09:24 +01:00
/*
* IO modes to indicate how many bytes should be read / written at once in the
* tpm_tis_phy_ops read_bytes / write_bytes calls . Use TPM_TIS_PHYS_8 to
* receive / transmit byte - wise , TPM_TIS_PHYS_16 for two bytes etc .
*/
enum tpm_tis_io_mode {
TPM_TIS_PHYS_8 ,
TPM_TIS_PHYS_16 ,
TPM_TIS_PHYS_32 ,
} ;
2016-05-19 00:35:49 +02:00
struct tpm_tis_phy_ops {
2022-03-21 10:09:24 +01:00
/* data is passed in little endian */
2016-05-19 00:35:49 +02:00
int ( * read_bytes ) ( struct tpm_tis_data * data , u32 addr , u16 len ,
2022-03-21 10:09:24 +01:00
u8 * result , enum tpm_tis_io_mode mode ) ;
2016-05-19 00:35:49 +02:00
int ( * write_bytes ) ( struct tpm_tis_data * data , u32 addr , u16 len ,
2022-03-21 10:09:24 +01:00
const u8 * value , enum tpm_tis_io_mode mode ) ;
2022-06-08 19:31:12 +02:00
int ( * verify_crc ) ( struct tpm_tis_data * data , size_t len ,
const u8 * value ) ;
2016-05-19 00:35:49 +02:00
} ;
static inline int tpm_tis_read_bytes ( struct tpm_tis_data * data , u32 addr ,
u16 len , u8 * result )
{
2022-03-21 10:09:24 +01:00
return data - > phy_ops - > read_bytes ( data , addr , len , result ,
TPM_TIS_PHYS_8 ) ;
2016-05-19 00:35:49 +02:00
}
static inline int tpm_tis_read8 ( struct tpm_tis_data * data , u32 addr , u8 * result )
{
2022-03-21 10:09:24 +01:00
return data - > phy_ops - > read_bytes ( data , addr , 1 , result , TPM_TIS_PHYS_8 ) ;
2016-05-19 00:35:49 +02:00
}
static inline int tpm_tis_read16 ( struct tpm_tis_data * data , u32 addr ,
u16 * result )
{
2022-03-21 10:09:24 +01:00
__le16 result_le ;
int rc ;
rc = data - > phy_ops - > read_bytes ( data , addr , sizeof ( u16 ) ,
( u8 * ) & result_le , TPM_TIS_PHYS_16 ) ;
if ( ! rc )
* result = le16_to_cpu ( result_le ) ;
return rc ;
2016-05-19 00:35:49 +02:00
}
static inline int tpm_tis_read32 ( struct tpm_tis_data * data , u32 addr ,
u32 * result )
{
2022-03-21 10:09:24 +01:00
__le32 result_le ;
int rc ;
rc = data - > phy_ops - > read_bytes ( data , addr , sizeof ( u32 ) ,
( u8 * ) & result_le , TPM_TIS_PHYS_32 ) ;
if ( ! rc )
* result = le32_to_cpu ( result_le ) ;
return rc ;
2016-05-19 00:35:49 +02:00
}
static inline int tpm_tis_write_bytes ( struct tpm_tis_data * data , u32 addr ,
2017-09-07 15:30:45 +02:00
u16 len , const u8 * value )
2016-05-19 00:35:49 +02:00
{
2022-03-21 10:09:24 +01:00
return data - > phy_ops - > write_bytes ( data , addr , len , value ,
TPM_TIS_PHYS_8 ) ;
2016-05-19 00:35:49 +02:00
}
static inline int tpm_tis_write8 ( struct tpm_tis_data * data , u32 addr , u8 value )
{
2022-03-21 10:09:24 +01:00
return data - > phy_ops - > write_bytes ( data , addr , 1 , & value ,
TPM_TIS_PHYS_8 ) ;
2016-05-19 00:35:49 +02:00
}
static inline int tpm_tis_write32 ( struct tpm_tis_data * data , u32 addr ,
u32 value )
{
2022-03-21 10:09:24 +01:00
__le32 value_le ;
int rc ;
value_le = cpu_to_le32 ( value ) ;
rc = data - > phy_ops - > write_bytes ( data , addr , sizeof ( u32 ) ,
( u8 * ) & value_le , TPM_TIS_PHYS_32 ) ;
return rc ;
2016-05-19 00:35:49 +02:00
}
2022-06-08 19:31:12 +02:00
static inline int tpm_tis_verify_crc ( struct tpm_tis_data * data , size_t len ,
const u8 * value )
{
if ( ! data - > phy_ops - > verify_crc )
return 0 ;
return data - > phy_ops - > verify_crc ( data , len , value ) ;
}
2017-12-22 12:13:43 -08:00
static inline bool is_bsw ( void )
{
# ifdef CONFIG_X86
return ( ( boot_cpu_data . x86_model = = INTEL_FAM6_ATOM_AIRMONT ) ? 1 : 0 ) ;
# else
return false ;
# endif
}
2016-05-19 00:35:52 +02:00
void tpm_tis_remove ( struct tpm_chip * chip ) ;
int tpm_tis_core_init ( struct device * dev , struct tpm_tis_data * priv , int irq ,
const struct tpm_tis_phy_ops * phy_ops ,
acpi_handle acpi_dev_handle ) ;
# ifdef CONFIG_PM_SLEEP
int tpm_tis_resume ( struct device * dev ) ;
# endif
2016-05-19 00:35:48 +02:00
# endif