Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * The code in this file has been heavily based on the article "Writing a TPM |
| 5 | * Device Driver" published on http://ptgmedia.pearsoncmg.com and the |
| 6 | * submission by Stefan Berger on Qemu-devel mailing list. |
| 7 | * |
| 8 | * One principal difference is that in the simplest config the other than 0 |
| 9 | * TPM localities do not get mapped by some devices (for instance, by |
| 10 | * Infineon slb9635), so this driver provides access to locality 0 only. |
| 11 | */ |
| 12 | |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 13 | #include <commonlib/helpers.h> |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 14 | #include <string.h> |
| 15 | #include <delay.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 16 | #include <device/mmio.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 17 | #include <acpi/acpi.h> |
| 18 | #include <acpi/acpigen.h> |
| 19 | #include <acpi/acpi_device.h> |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 20 | #include <device/device.h> |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 21 | #include <console/console.h> |
Philipp Deppenwiese | d88fb36 | 2017-10-18 20:26:18 +0200 | [diff] [blame] | 22 | #include <security/tpm/tis.h> |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 23 | #include <device/pnp.h> |
Patrick Rudolph | d8d8be1 | 2020-09-21 09:48:53 +0200 | [diff] [blame] | 24 | #include <drivers/tpm/tpm_ppi.h> |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 25 | #include <timer.h> |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 26 | #include "chip.h" |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 27 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 28 | #define PREFIX "lpc_tpm: " |
Patrick Rudolph | d8d8be1 | 2020-09-21 09:48:53 +0200 | [diff] [blame] | 29 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 30 | /* coreboot wrapper for TPM driver (start) */ |
| 31 | #define TPM_DEBUG(fmt, args...) \ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 32 | if (CONFIG(DEBUG_TPM)) { \ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 33 | printk(BIOS_DEBUG, PREFIX); \ |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 34 | printk(BIOS_DEBUG, fmt, ##args); \ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 35 | } |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 36 | #define TPM_DEBUG_IO_READ(reg_, val_) \ |
| 37 | TPM_DEBUG("Read reg 0x%x returns 0x%x\n", (reg_), (val_)) |
| 38 | #define TPM_DEBUG_IO_WRITE(reg_, val_) \ |
| 39 | TPM_DEBUG("Write reg 0x%x with 0x%x\n", (reg_), (val_)) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 40 | #define printf(x...) printk(BIOS_ERR, x) |
| 41 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 42 | /* coreboot wrapper for TPM driver (end) */ |
| 43 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 44 | /* the macro accepts the locality value, but only locality 0 is operational */ |
| 45 | #define TIS_REG(LOCALITY, REG) \ |
Patrick Rudolph | 56fdafb | 2020-07-01 20:07:08 +0200 | [diff] [blame] | 46 | (void *)(uintptr_t)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 47 | |
| 48 | /* hardware registers' offsets */ |
| 49 | #define TIS_REG_ACCESS 0x0 |
| 50 | #define TIS_REG_INT_ENABLE 0x8 |
| 51 | #define TIS_REG_INT_VECTOR 0xc |
| 52 | #define TIS_REG_INT_STATUS 0x10 |
| 53 | #define TIS_REG_INTF_CAPABILITY 0x14 |
| 54 | #define TIS_REG_STS 0x18 |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 55 | #define TIS_REG_BURST_COUNT 0x19 |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 56 | #define TIS_REG_DATA_FIFO 0x24 |
| 57 | #define TIS_REG_DID_VID 0xf00 |
| 58 | #define TIS_REG_RID 0xf04 |
| 59 | |
| 60 | /* Some registers' bit field definitions */ |
| 61 | #define TIS_STS_VALID (1 << 7) /* 0x80 */ |
| 62 | #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */ |
| 63 | #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */ |
| 64 | #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */ |
| 65 | #define TIS_STS_EXPECT (1 << 3) /* 0x08 */ |
| 66 | #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */ |
| 67 | |
| 68 | #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */ |
| 69 | #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */ |
| 70 | #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */ |
| 71 | #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */ |
| 72 | #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */ |
| 73 | #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */ |
| 74 | #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */ |
| 75 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 76 | /* |
| 77 | * Error value returned if a tpm register does not enter the expected state |
| 78 | * after continuous polling. No actual TPM register reading ever returns ~0, |
| 79 | * so this value is a safe error indication to be mixed with possible status |
| 80 | * register values. |
| 81 | */ |
| 82 | #define TPM_TIMEOUT_ERR (~0) |
| 83 | |
| 84 | /* Error value returned on various TPM driver errors */ |
| 85 | #define TPM_DRIVER_ERR (~0) |
| 86 | |
| 87 | /* 1 second is plenty for anything TPM does.*/ |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 88 | #define MAX_DELAY_US USECS_PER_SEC |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 89 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 90 | /* |
| 91 | * Structures defined below allow creating descriptions of TPM vendor/device |
| 92 | * ID information for run time discovery. The only device the system knows |
| 93 | * about at this time is Infineon slb9635 |
| 94 | */ |
| 95 | struct device_name { |
| 96 | u16 dev_id; |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 97 | const char *const dev_name; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | struct vendor_name { |
| 101 | u16 vendor_id; |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 102 | const char *vendor_name; |
| 103 | const struct device_name *dev_names; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 106 | static const struct device_name atmel_devices[] = { |
| 107 | {0x3204, "AT97SC3204"}, |
| 108 | {0xffff} |
| 109 | }; |
| 110 | |
Stefan Reinauer | c668af7 | 2011-10-27 21:28:25 +0000 | [diff] [blame] | 111 | static const struct device_name infineon_devices[] = { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 112 | {0x000b, "SLB9635 TT 1.2"}, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 113 | #if CONFIG(TPM2) |
Kamil Wcislo | bf5ccfd | 2017-10-12 13:12:11 +0200 | [diff] [blame] | 114 | {0x001a, "SLB9665 TT 2.0"}, |
Subrata Banik | 5b8c4a7 | 2016-11-11 09:28:45 +0530 | [diff] [blame] | 115 | {0x001b, "SLB9670 TT 2.0"}, |
| 116 | #else |
Kamil Wcislo | bf5ccfd | 2017-10-12 13:12:11 +0200 | [diff] [blame] | 117 | {0x001a, "SLB9660 TT 1.2"}, |
Wenkai Du | 641529b | 2015-05-29 10:54:27 -0700 | [diff] [blame] | 118 | {0x001b, "SLB9670 TT 1.2"}, |
Subrata Banik | 5b8c4a7 | 2016-11-11 09:28:45 +0530 | [diff] [blame] | 119 | #endif |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 120 | {0xffff} |
| 121 | }; |
| 122 | |
| 123 | static const struct device_name nuvoton_devices[] = { |
| 124 | {0x00fe, "NPCT420AA V2"}, |
| 125 | {0xffff} |
| 126 | }; |
| 127 | |
| 128 | static const struct device_name stmicro_devices[] = { |
| 129 | {0x0000, "ST33ZP24" }, |
| 130 | {0xffff} |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Tsung Ho Wu | 804a043 | 2019-06-07 15:03:49 -0700 | [diff] [blame] | 133 | static const struct device_name swtpm_devices[] = { |
| 134 | #if CONFIG(TPM2) |
| 135 | {0x0001, "SwTPM 2.0" }, |
| 136 | #endif |
| 137 | {0xffff} |
| 138 | }; |
| 139 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 140 | static const struct vendor_name vendor_names[] = { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 141 | {0x1114, "Atmel", atmel_devices}, |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 142 | {0x15d1, "Infineon", infineon_devices}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 143 | {0x1050, "Nuvoton", nuvoton_devices}, |
Tsung Ho Wu | 804a043 | 2019-06-07 15:03:49 -0700 | [diff] [blame] | 144 | {0x1014, "TPM Emulator", swtpm_devices}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 145 | {0x104a, "ST Microelectronics", stmicro_devices}, |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /* |
| 149 | * Cached vendor/device ID pair to indicate that the device has been already |
| 150 | * discovered |
| 151 | */ |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 152 | static u32 vendor_dev_id; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 153 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 154 | static inline u8 tpm_read_status(int locality) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 155 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 156 | u8 value = read8(TIS_REG(locality, TIS_REG_STS)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 157 | TPM_DEBUG_IO_READ(TIS_REG_STS, value); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 158 | return value; |
| 159 | } |
| 160 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 161 | static inline void tpm_write_status(u8 sts, int locality) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 162 | { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 163 | TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 164 | write8(TIS_REG(locality, TIS_REG_STS), sts); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 165 | } |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 166 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 167 | static inline u8 tpm_read_data(int locality) |
| 168 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 169 | u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 170 | TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value); |
| 171 | return value; |
| 172 | } |
| 173 | |
| 174 | static inline void tpm_write_data(u8 data, int locality) |
| 175 | { |
Werner Zeh | a31d6cd | 2021-10-07 07:15:38 +0200 | [diff] [blame] | 176 | TPM_DEBUG_IO_WRITE(TIS_REG_DATA_FIFO, data); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 177 | write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static inline u16 tpm_read_burst_count(int locality) |
| 181 | { |
| 182 | u16 count; |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 183 | count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT)); |
| 184 | count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8; |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 185 | TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count); |
| 186 | return count; |
| 187 | } |
| 188 | |
| 189 | static inline u8 tpm_read_access(int locality) |
| 190 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 191 | u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 192 | TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value); |
| 193 | return value; |
| 194 | } |
| 195 | |
| 196 | static inline void tpm_write_access(u8 data, int locality) |
| 197 | { |
| 198 | TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 199 | write8(TIS_REG(locality, TIS_REG_ACCESS), data); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static inline u32 tpm_read_did_vid(int locality) |
| 203 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 204 | u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 205 | TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value); |
| 206 | return value; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 209 | static inline void tpm_write_int_vector(int vector, int locality) |
| 210 | { |
| 211 | TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 212 | write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 215 | static inline u8 tpm_read_int_vector(int locality) |
| 216 | { |
| 217 | u8 value = read8(TIS_REG(locality, TIS_REG_INT_VECTOR)); |
| 218 | TPM_DEBUG_IO_READ(TIS_REG_INT_VECTOR, value); |
| 219 | return value; |
| 220 | } |
| 221 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 222 | static inline void tpm_write_int_polarity(int polarity, int locality) |
| 223 | { |
| 224 | /* Set polarity and leave all other bits at 0 */ |
| 225 | u32 value = (polarity & 0x3) << 3; |
| 226 | TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 227 | write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 230 | static inline u32 tpm_read_int_polarity(int locality) |
| 231 | { |
| 232 | /* Get polarity and leave all other bits */ |
| 233 | u32 value = read8(TIS_REG(locality, TIS_REG_INT_ENABLE)); |
| 234 | value = (value >> 3) & 0x3; |
| 235 | TPM_DEBUG_IO_READ(TIS_REG_INT_ENABLE, value); |
| 236 | return value; |
| 237 | } |
| 238 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 239 | /* |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 240 | * tis_wait_sts() |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 241 | * |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 242 | * Wait for at most a second for a status to change its state to match the |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 243 | * expected state. Normally the transition happens within microseconds. |
| 244 | * |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 245 | * @locality - locality |
| 246 | * @mask - bitmask for the bitfield(s) to watch |
| 247 | * @expected - value the field(s) are supposed to be set to |
| 248 | * |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 249 | * Returns 0 on success or TPM_TIMEOUT_ERR on timeout. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 250 | */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 251 | static int tis_wait_sts(int locality, u8 mask, u8 expected) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 252 | { |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 253 | struct stopwatch sw; |
| 254 | |
| 255 | stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
| 256 | do { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 257 | u8 value = tpm_read_status(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 258 | if ((value & mask) == expected) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 259 | return 0; |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 260 | udelay(1); |
| 261 | } while (!stopwatch_expired(&sw)); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 262 | return TPM_TIMEOUT_ERR; |
| 263 | } |
| 264 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 265 | static inline int tis_wait_ready(int locality) |
| 266 | { |
| 267 | return tis_wait_sts(locality, TIS_STS_COMMAND_READY, |
| 268 | TIS_STS_COMMAND_READY); |
| 269 | } |
| 270 | |
| 271 | static inline int tis_wait_valid(int locality) |
| 272 | { |
| 273 | return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID); |
| 274 | } |
| 275 | |
| 276 | static inline int tis_wait_valid_data(int locality) |
| 277 | { |
| 278 | const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID; |
| 279 | return tis_wait_sts(locality, has_data, has_data); |
| 280 | } |
| 281 | |
| 282 | static inline int tis_has_valid_data(int locality) |
| 283 | { |
| 284 | const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID; |
| 285 | return (tpm_read_status(locality) & has_data) == has_data; |
| 286 | } |
| 287 | |
| 288 | static inline int tis_expect_data(int locality) |
| 289 | { |
| 290 | return !!(tpm_read_status(locality) & TIS_STS_EXPECT); |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * tis_wait_access() |
| 295 | * |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 296 | * Wait for at most a second for a access to change its state to match the |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 297 | * expected state. Normally the transition happens within microseconds. |
| 298 | * |
| 299 | * @locality - locality |
| 300 | * @mask - bitmask for the bitfield(s) to watch |
| 301 | * @expected - value the field(s) are supposed to be set to |
| 302 | * |
| 303 | * Returns 0 on success or TPM_TIMEOUT_ERR on timeout. |
| 304 | */ |
| 305 | static int tis_wait_access(int locality, u8 mask, u8 expected) |
| 306 | { |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 307 | struct stopwatch sw; |
| 308 | |
| 309 | stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
| 310 | do { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 311 | u8 value = tpm_read_access(locality); |
| 312 | if ((value & mask) == expected) |
| 313 | return 0; |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 314 | udelay(1); |
| 315 | } while (!stopwatch_expired(&sw)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 316 | return TPM_TIMEOUT_ERR; |
| 317 | } |
| 318 | |
| 319 | static inline int tis_wait_dropped_access(int locality) |
| 320 | { |
| 321 | return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, 0); |
| 322 | } |
| 323 | |
| 324 | static inline int tis_wait_received_access(int locality) |
| 325 | { |
| 326 | return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, |
| 327 | TIS_ACCESS_ACTIVE_LOCALITY); |
| 328 | } |
| 329 | |
| 330 | static inline int tis_has_access(int locality) |
| 331 | { |
| 332 | return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY); |
| 333 | } |
| 334 | |
| 335 | static inline void tis_request_access(int locality) |
| 336 | { |
| 337 | tpm_write_access(TIS_ACCESS_REQUEST_USE, locality); |
| 338 | } |
| 339 | |
| 340 | static inline void tis_drop_access(int locality) |
| 341 | { |
| 342 | tpm_write_access(TIS_ACCESS_ACTIVE_LOCALITY, locality); |
| 343 | } |
| 344 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 345 | /* |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 346 | * PC Client Specific TPM Interface Specification section 11.2.12: |
| 347 | * |
| 348 | * Software must be prepared to send two writes of a "1" to command ready |
| 349 | * field: the first to indicate successful read of all the data, thus |
| 350 | * clearing the data from the ReadFIFO and freeing the TPM's resources, |
| 351 | * and the second to indicate to the TPM it is about to send a new command. |
| 352 | * |
| 353 | * In practice not all TPMs behave the same so it is necessary to be |
| 354 | * flexible when trying to set command ready. |
| 355 | * |
| 356 | * Returns 0 on success if the TPM is ready for transactions. |
| 357 | * Returns TPM_TIMEOUT_ERR if the command ready bit does not get set. |
| 358 | */ |
| 359 | static int tis_command_ready(u8 locality) |
| 360 | { |
| 361 | u32 status; |
| 362 | |
| 363 | /* 1st attempt to set command ready */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 364 | tpm_write_status(TIS_STS_COMMAND_READY, locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 365 | |
| 366 | /* Wait for response */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 367 | status = tpm_read_status(locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 368 | |
| 369 | /* Check if command ready is set yet */ |
| 370 | if (status & TIS_STS_COMMAND_READY) |
| 371 | return 0; |
| 372 | |
| 373 | /* 2nd attempt to set command ready */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 374 | tpm_write_status(TIS_STS_COMMAND_READY, locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 375 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 376 | return tis_wait_ready(locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 380 | * Probe the TPM device and try determining its manufacturer/device name. |
| 381 | * |
| 382 | * Returns 0 on success (the device is found or was found during an earlier |
| 383 | * invocation) or TPM_DRIVER_ERR if the device is not found. |
| 384 | */ |
| 385 | static u32 tis_probe(void) |
| 386 | { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 387 | const char *device_name = "unknown"; |
| 388 | const char *vendor_name = device_name; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 389 | const struct device_name *dev; |
| 390 | u32 didvid; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 391 | u16 vid, did; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 392 | int i; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 393 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 394 | if (vendor_dev_id) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 395 | return 0; /* Already probed. */ |
| 396 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 397 | didvid = tpm_read_did_vid(0); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 398 | if (!didvid || (didvid == 0xffffffff)) { |
Angel Pons | 08e8cab | 2020-06-18 15:20:37 +0200 | [diff] [blame] | 399 | printf("%s: No TPM device found\n", __func__); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 400 | return TPM_DRIVER_ERR; |
| 401 | } |
| 402 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 403 | vendor_dev_id = didvid; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 404 | |
| 405 | vid = didvid & 0xffff; |
| 406 | did = (didvid >> 16) & 0xffff; |
| 407 | for (i = 0; i < ARRAY_SIZE(vendor_names); i++) { |
| 408 | int j = 0; |
| 409 | u16 known_did; |
| 410 | if (vid == vendor_names[i].vendor_id) { |
| 411 | vendor_name = vendor_names[i].vendor_name; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 412 | } else { |
| 413 | continue; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 414 | } |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 415 | dev = &vendor_names[i].dev_names[j]; |
| 416 | while ((known_did = dev->dev_id) != 0xffff) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 417 | if (known_did == did) { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 418 | device_name = dev->dev_name; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | j++; |
Subrata | 41b08d9 | 2015-05-14 14:38:07 +0530 | [diff] [blame] | 422 | dev = &vendor_names[i].dev_names[j]; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 423 | } |
| 424 | break; |
| 425 | } |
| 426 | /* this will have to be converted into debug printout */ |
Duncan Laurie | 17ba944 | 2015-09-03 16:00:49 -0700 | [diff] [blame] | 427 | printk(BIOS_INFO, "Found TPM %s by %s\n", device_name, vendor_name); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * tis_senddata() |
| 433 | * |
| 434 | * send the passed in data to the TPM device. |
| 435 | * |
| 436 | * @data - address of the data to send, byte by byte |
| 437 | * @len - length of the data to send |
| 438 | * |
| 439 | * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does |
| 440 | * not accept the entire command). |
| 441 | */ |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 442 | static u32 tis_senddata(const u8 *const data, u32 len) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 443 | { |
| 444 | u32 offset = 0; |
| 445 | u16 burst = 0; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 446 | u8 locality = 0; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 447 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 448 | if (tis_wait_ready(locality)) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 449 | printf("%s:%d - failed to get 'command_ready' status\n", |
| 450 | __FILE__, __LINE__); |
| 451 | return TPM_DRIVER_ERR; |
| 452 | } |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 453 | burst = tpm_read_burst_count(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 454 | |
| 455 | while (1) { |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 456 | unsigned int count; |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 457 | struct stopwatch sw; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 458 | |
| 459 | /* Wait till the device is ready to accept more data. */ |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 460 | stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 461 | while (!burst) { |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 462 | if (stopwatch_expired(&sw)) { |
Werner Zeh | 66b2f20 | 2021-10-12 15:14:22 +0200 | [diff] [blame^] | 463 | printf("%s:%d failed to feed %u bytes of %u\n", |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 464 | __FILE__, __LINE__, len - offset, len); |
| 465 | return TPM_DRIVER_ERR; |
| 466 | } |
| 467 | udelay(1); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 468 | burst = tpm_read_burst_count(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 471 | /* |
| 472 | * Calculate number of bytes the TPM is ready to accept in one |
| 473 | * shot. |
| 474 | * |
| 475 | * We want to send the last byte outside of the loop (hence |
| 476 | * the -1 below) to make sure that the 'expected' status bit |
| 477 | * changes to zero exactly after the last byte is fed into the |
| 478 | * FIFO. |
| 479 | */ |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 480 | count = MIN(burst, len - offset - 1); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 481 | while (count--) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 482 | tpm_write_data(data[offset++], locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 483 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 484 | if (tis_wait_valid(locality) || !tis_expect_data(locality)) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 485 | printf("%s:%d TPM command feed overflow\n", |
| 486 | __FILE__, __LINE__); |
| 487 | return TPM_DRIVER_ERR; |
| 488 | } |
| 489 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 490 | burst = tpm_read_burst_count(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 491 | if ((offset == (len - 1)) && burst) |
| 492 | /* |
| 493 | * We need to be able to send the last byte to the |
| 494 | * device, so burst size must be nonzero before we |
| 495 | * break out. |
| 496 | */ |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | /* Send the last byte. */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 501 | tpm_write_data(data[offset++], locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 502 | |
| 503 | /* |
| 504 | * Verify that TPM does not expect any more data as part of this |
| 505 | * command. |
| 506 | */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 507 | if (tis_wait_valid(locality) || tis_expect_data(locality)) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 508 | printf("%s:%d unexpected TPM status 0x%x\n", |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 509 | __FILE__, __LINE__, tpm_read_status(locality)); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 510 | return TPM_DRIVER_ERR; |
| 511 | } |
| 512 | |
| 513 | /* OK, sitting pretty, let's start the command execution. */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 514 | tpm_write_status(TIS_STS_TPM_GO, locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * tis_readresponse() |
| 521 | * |
| 522 | * read the TPM device response after a command was issued. |
| 523 | * |
| 524 | * @buffer - address where to read the response, byte by byte. |
| 525 | * @len - pointer to the size of buffer |
| 526 | * |
| 527 | * On success stores the number of received bytes to len and returns 0. On |
| 528 | * errors (misformatted TPM data or synchronization problems) returns |
| 529 | * TPM_DRIVER_ERR. |
| 530 | */ |
| 531 | static u32 tis_readresponse(u8 *buffer, size_t *len) |
| 532 | { |
| 533 | u16 burst_count; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 534 | u32 offset = 0; |
| 535 | u8 locality = 0; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 536 | u32 expected_count = *len; |
| 537 | int max_cycles = 0; |
| 538 | |
| 539 | /* Wait for the TPM to process the command */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 540 | if (tis_wait_valid_data(locality)) { |
| 541 | printf("%s:%d failed processing command\n", __FILE__, __LINE__); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 542 | return TPM_DRIVER_ERR; |
| 543 | } |
| 544 | |
| 545 | do { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 546 | while ((burst_count = tpm_read_burst_count(locality)) == 0) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 547 | if (max_cycles++ == MAX_DELAY_US) { |
| 548 | printf("%s:%d TPM stuck on read\n", |
| 549 | __FILE__, __LINE__); |
| 550 | return TPM_DRIVER_ERR; |
| 551 | } |
| 552 | udelay(1); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | max_cycles = 0; |
| 556 | |
| 557 | while (burst_count-- && (offset < expected_count)) { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 558 | buffer[offset++] = tpm_read_data(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 559 | if (offset == 6) { |
| 560 | /* |
| 561 | * We got the first six bytes of the reply, |
| 562 | * let's figure out how many bytes to expect |
| 563 | * total - it is stored as a 4 byte number in |
| 564 | * network order, starting with offset 2 into |
| 565 | * the body of the reply. |
| 566 | */ |
| 567 | u32 real_length; |
| 568 | memcpy(&real_length, |
| 569 | buffer + 2, |
| 570 | sizeof(real_length)); |
| 571 | expected_count = be32_to_cpu(real_length); |
| 572 | |
| 573 | if ((expected_count < offset) || |
| 574 | (expected_count > *len)) { |
Werner Zeh | 66b2f20 | 2021-10-12 15:14:22 +0200 | [diff] [blame^] | 575 | printf("%s:%d bad response size %u\n", |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 576 | __FILE__, __LINE__, |
| 577 | expected_count); |
| 578 | return TPM_DRIVER_ERR; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* Wait for the next portion */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 584 | if (tis_wait_valid(locality)) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 585 | printf("%s:%d failed to read response\n", |
| 586 | __FILE__, __LINE__); |
| 587 | return TPM_DRIVER_ERR; |
| 588 | } |
| 589 | |
| 590 | if (offset == expected_count) |
| 591 | break; /* We got all we need */ |
| 592 | |
Bill XIE | a4bf0b7 | 2018-03-22 17:07:43 +0800 | [diff] [blame] | 593 | /* |
| 594 | * Certain TPMs seem to need some delay between tis_wait_valid() |
| 595 | * and tis_has_valid_data(), or some race-condition-related |
| 596 | * issue will occur. |
| 597 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 598 | if (CONFIG(TPM_RDRESP_NEED_DELAY)) |
Bill XIE | a4bf0b7 | 2018-03-22 17:07:43 +0800 | [diff] [blame] | 599 | udelay(10); |
| 600 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 601 | } while (tis_has_valid_data(locality)); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 602 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 603 | /* * Make sure we indeed read all there was. */ |
| 604 | if (tis_has_valid_data(locality)) { |
Werner Zeh | 66b2f20 | 2021-10-12 15:14:22 +0200 | [diff] [blame^] | 605 | printf("%s:%d wrong receive status: %x %u bytes left\n", |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 606 | __FILE__, __LINE__, tpm_read_status(locality), |
| 607 | tpm_read_burst_count(locality)); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 608 | return TPM_DRIVER_ERR; |
| 609 | } |
| 610 | |
| 611 | /* Tell the TPM that we are done. */ |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 612 | if (tis_command_ready(locality) == TPM_TIMEOUT_ERR) |
| 613 | return TPM_DRIVER_ERR; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 614 | |
| 615 | *len = offset; |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * tis_init() |
| 621 | * |
| 622 | * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on |
| 623 | * failure (in case device probing did not succeed). |
| 624 | */ |
| 625 | int tis_init(void) |
| 626 | { |
| 627 | if (tis_probe()) |
| 628 | return TPM_DRIVER_ERR; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * tis_open() |
| 634 | * |
| 635 | * Requests access to locality 0 for the caller. After all commands have been |
| 636 | * completed the caller is supposed to call tis_close(). |
| 637 | * |
| 638 | * Returns 0 on success, TPM_DRIVER_ERR on failure. |
| 639 | */ |
| 640 | int tis_open(void) |
| 641 | { |
| 642 | u8 locality = 0; /* we use locality zero for everything */ |
| 643 | |
| 644 | if (tis_close()) |
| 645 | return TPM_DRIVER_ERR; |
| 646 | |
| 647 | /* now request access to locality */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 648 | tis_request_access(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 649 | |
| 650 | /* did we get a lock? */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 651 | if (tis_wait_received_access(locality)) { |
Werner Zeh | 66b2f20 | 2021-10-12 15:14:22 +0200 | [diff] [blame^] | 652 | printf("%s:%d - failed to lock locality %u\n", |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 653 | __FILE__, __LINE__, locality); |
| 654 | return TPM_DRIVER_ERR; |
| 655 | } |
| 656 | |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 657 | /* Certain TPMs seem to need some delay here or they hang... */ |
| 658 | udelay(10); |
| 659 | |
| 660 | if (tis_command_ready(locality) == TPM_TIMEOUT_ERR) |
| 661 | return TPM_DRIVER_ERR; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * tis_close() |
| 668 | * |
Martin Roth | 5688979 | 2013-07-09 21:39:46 -0600 | [diff] [blame] | 669 | * terminate the current session with the TPM by releasing the locked |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 670 | * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock |
| 671 | * removal did not succeed). |
| 672 | */ |
| 673 | int tis_close(void) |
| 674 | { |
| 675 | u8 locality = 0; |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 676 | if (tis_has_access(locality)) { |
| 677 | tis_drop_access(locality); |
| 678 | if (tis_wait_dropped_access(locality)) { |
Werner Zeh | 66b2f20 | 2021-10-12 15:14:22 +0200 | [diff] [blame^] | 679 | printf("%s:%d - failed to release locality %u\n", |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 680 | __FILE__, __LINE__, locality); |
| 681 | return TPM_DRIVER_ERR; |
| 682 | } |
| 683 | } |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * tis_sendrecv() |
| 689 | * |
| 690 | * Send the requested data to the TPM and then try to get its response |
| 691 | * |
| 692 | * @sendbuf - buffer of the data to send |
| 693 | * @send_size size of the data to send |
| 694 | * @recvbuf - memory to save the response to |
| 695 | * @recv_len - pointer to the size of the response buffer |
| 696 | * |
| 697 | * Returns 0 on success (and places the number of response bytes at recv_len) |
| 698 | * or TPM_DRIVER_ERR on failure. |
| 699 | */ |
| 700 | int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, |
| 701 | uint8_t *recvbuf, size_t *recv_len) |
| 702 | { |
| 703 | if (tis_senddata(sendbuf, send_size)) { |
| 704 | printf("%s:%d failed sending data to TPM\n", |
| 705 | __FILE__, __LINE__); |
| 706 | return TPM_DRIVER_ERR; |
| 707 | } |
| 708 | |
| 709 | return tis_readresponse(recvbuf, recv_len); |
| 710 | } |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 711 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 712 | /* |
| 713 | * tis_setup_interrupt() |
| 714 | * |
| 715 | * Set up the interrupt vector and polarity for locality 0 and |
| 716 | * disable all interrupts so they are unused in firmware but can |
| 717 | * be enabled by the OS. |
| 718 | * |
| 719 | * The values used here must match what is passed in the TPM ACPI |
| 720 | * device if ACPI is used on the platform. |
| 721 | * |
| 722 | * @vector - TPM interrupt vector |
| 723 | * @polarity - TPM interrupt polarity |
| 724 | * |
| 725 | * Returns 0 on success, TPM_DRIVER_ERR on failure. |
| 726 | */ |
| 727 | static int tis_setup_interrupt(int vector, int polarity) |
| 728 | { |
| 729 | u8 locality = 0; |
| 730 | int has_access = tis_has_access(locality); |
| 731 | |
| 732 | /* Open connection and request access if not already granted */ |
| 733 | if (!has_access && tis_open() < 0) |
| 734 | return TPM_DRIVER_ERR; |
| 735 | |
| 736 | /* Set TPM interrupt vector */ |
| 737 | tpm_write_int_vector(vector, locality); |
| 738 | |
Elyes HAOUAS | 1895838 | 2018-08-07 12:23:16 +0200 | [diff] [blame] | 739 | /* Set TPM interrupt polarity and disable interrupts */ |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 740 | tpm_write_int_polarity(polarity, locality); |
| 741 | |
| 742 | /* Close connection if it was opened */ |
| 743 | if (!has_access && tis_close() < 0) |
| 744 | return TPM_DRIVER_ERR; |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static void lpc_tpm_read_resources(struct device *dev) |
| 750 | { |
| 751 | /* Static 5K memory region specified in Kconfig */ |
| 752 | mmio_resource(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS >> 10, 0x5000 >> 10); |
| 753 | } |
| 754 | |
| 755 | static void lpc_tpm_set_resources(struct device *dev) |
| 756 | { |
| 757 | tpm_config_t *config = (tpm_config_t *)dev->chip_info; |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 758 | DEVTREE_CONST struct resource *res; |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 759 | |
| 760 | for (res = dev->resource_list; res; res = res->next) { |
| 761 | if (!(res->flags & IORESOURCE_ASSIGNED)) |
| 762 | continue; |
| 763 | |
| 764 | if (res->flags & IORESOURCE_IRQ) { |
| 765 | /* Set interrupt vector */ |
| 766 | tis_setup_interrupt((int)res->base, |
| 767 | config->irq_polarity); |
| 768 | } else { |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 769 | continue; |
| 770 | } |
| 771 | |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 772 | #if !DEVTREE_EARLY |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 773 | res->flags |= IORESOURCE_STORED; |
| 774 | report_resource_stored(dev, res, " <tpm>"); |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 775 | #endif |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 779 | #if CONFIG(HAVE_ACPI_TABLES) |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 780 | static void lpc_tpm_fill_ssdt(const struct device *dev) |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 781 | { |
| 782 | const char *path = acpi_device_path(dev->bus->dev); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 783 | |
Philipp Deppenwiese | 3a1fbea | 2016-12-14 01:06:55 +0100 | [diff] [blame] | 784 | if (!path) { |
Tobias Diedrich | 36537f1 | 2017-02-10 00:28:45 +0100 | [diff] [blame] | 785 | path = "\\_SB_.PCI0.LPCB"; |
Philipp Deppenwiese | 3a1fbea | 2016-12-14 01:06:55 +0100 | [diff] [blame] | 786 | printk(BIOS_DEBUG, "Using default TPM ACPI path: '%s'\n", path); |
| 787 | } |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 788 | |
| 789 | /* Device */ |
| 790 | acpigen_write_scope(path); |
| 791 | acpigen_write_device(acpi_device_name(dev)); |
| 792 | |
Michał Żygowski | 7b28801 | 2020-03-20 15:41:44 +0100 | [diff] [blame] | 793 | if (CONFIG(TPM2)) { |
| 794 | acpigen_write_name_string("_HID", "MSFT0101"); |
| 795 | acpigen_write_name_string("_CID", "MSFT0101"); |
| 796 | } else { |
| 797 | acpigen_write_name("_HID"); |
| 798 | acpigen_emit_eisaid("PNP0C31"); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 799 | |
Michał Żygowski | 7b28801 | 2020-03-20 15:41:44 +0100 | [diff] [blame] | 800 | acpigen_write_name("_CID"); |
| 801 | acpigen_emit_eisaid("PNP0C31"); |
| 802 | } |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 803 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 804 | acpi_device_write_uid(dev); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 805 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 806 | u32 did_vid = tpm_read_did_vid(0); |
| 807 | if (did_vid > 0 && did_vid < 0xffffffff) |
| 808 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 809 | else |
| 810 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_OFF); |
| 811 | |
Kevin Cody-Little | c97b5af | 2018-05-09 14:14:59 -0400 | [diff] [blame] | 812 | u16 port = dev->path.pnp.port; |
| 813 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 814 | /* Resources */ |
| 815 | acpigen_write_name("_CRS"); |
| 816 | acpigen_write_resourcetemplate_header(); |
| 817 | acpigen_write_mem32fixed(1, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000); |
Frans Hendriks | 6cc937e | 2018-10-29 14:30:58 +0100 | [diff] [blame] | 818 | if (port) |
| 819 | acpigen_write_io16(port, port, 1, 2, 1); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 820 | |
| 821 | if (CONFIG_TPM_PIRQ) { |
| 822 | /* |
| 823 | * PIRQ: Update interrupt vector with configured PIRQ |
| 824 | * Active-Low Level-Triggered Shared |
| 825 | */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 826 | struct acpi_irq tpm_irq_a = ACPI_IRQ_LEVEL_LOW(CONFIG_TPM_PIRQ); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 827 | acpi_device_write_interrupt(&tpm_irq_a); |
| 828 | } else if (tpm_read_int_vector(0) > 0) { |
| 829 | u8 int_vec = tpm_read_int_vector(0); |
| 830 | u8 int_pol = tpm_read_int_polarity(0); |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 831 | struct acpi_irq tpm_irq = ACPI_IRQ_LEVEL_LOW(int_vec); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 832 | |
| 833 | if (int_pol & 1) |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 834 | tpm_irq.polarity = ACPI_IRQ_ACTIVE_LOW; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 835 | else |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 836 | tpm_irq.polarity = ACPI_IRQ_ACTIVE_HIGH; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 837 | |
| 838 | if (int_pol & 2) |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 839 | tpm_irq.mode = ACPI_IRQ_EDGE_TRIGGERED; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 840 | else |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 841 | tpm_irq.mode = ACPI_IRQ_LEVEL_TRIGGERED; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 842 | |
| 843 | acpi_device_write_interrupt(&tpm_irq); |
| 844 | } |
| 845 | |
Patrick Rudolph | d8d8be1 | 2020-09-21 09:48:53 +0200 | [diff] [blame] | 846 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 847 | acpigen_write_resourcetemplate_footer(); |
| 848 | |
Patrick Rudolph | d8d8be1 | 2020-09-21 09:48:53 +0200 | [diff] [blame] | 849 | if (!CONFIG(CHROMEOS)) |
| 850 | tpm_ppi_acpi_fill_ssdt(dev); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 851 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 852 | acpigen_pop_len(); /* Device */ |
| 853 | acpigen_pop_len(); /* Scope */ |
| 854 | |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 855 | #if !DEVTREE_EARLY |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 856 | printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev), |
| 857 | dev->chip_ops->name, dev_path(dev)); |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 858 | #endif |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 859 | } |
| 860 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 861 | static const char *lpc_tpm_acpi_name(const struct device *dev) |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 862 | { |
| 863 | return "TPM"; |
| 864 | } |
| 865 | #endif |
| 866 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 867 | static struct device_operations lpc_tpm_ops = { |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 868 | .read_resources = lpc_tpm_read_resources, |
| 869 | .set_resources = lpc_tpm_set_resources, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 870 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 871 | .acpi_name = lpc_tpm_acpi_name, |
| 872 | .acpi_fill_ssdt = lpc_tpm_fill_ssdt, |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 873 | #endif |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 874 | }; |
| 875 | |
| 876 | static struct pnp_info pnp_dev_info[] = { |
| 877 | { .flags = PNP_IRQ0 } |
| 878 | }; |
| 879 | |
| 880 | static void enable_dev(struct device *dev) |
| 881 | { |
Kyösti Mälkki | d2b2a18 | 2021-04-29 15:33:07 +0300 | [diff] [blame] | 882 | if (CONFIG(TPM)) |
Kyösti Mälkki | 9cc6493 | 2020-05-29 19:42:07 +0300 | [diff] [blame] | 883 | pnp_enable_devices(dev, &lpc_tpm_ops, |
| 884 | ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | struct chip_operations drivers_pc80_tpm_ops = { |
| 888 | CHIP_NAME("LPC TPM") |
| 889 | .enable_dev = enable_dev |
| 890 | }; |