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> |
| 24 | #include "chip.h" |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 25 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 26 | #define PREFIX "lpc_tpm: " |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 27 | /* TCG Physical Presence Interface */ |
| 28 | #define TPM_PPI_UUID "3dddfaa6-361b-4eb4-a424-8d10089d1653" |
| 29 | /* TCG Memory Clear Interface */ |
| 30 | #define TPM_MCI_UUID "376054ed-cc13-4675-901c-4756d7f2d45d" |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 31 | /* coreboot wrapper for TPM driver (start) */ |
| 32 | #define TPM_DEBUG(fmt, args...) \ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 33 | if (CONFIG(DEBUG_TPM)) { \ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 34 | printk(BIOS_DEBUG, PREFIX); \ |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 35 | printk(BIOS_DEBUG, fmt, ##args); \ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 36 | } |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 37 | #define TPM_DEBUG_IO_READ(reg_, val_) \ |
| 38 | TPM_DEBUG("Read reg 0x%x returns 0x%x\n", (reg_), (val_)) |
| 39 | #define TPM_DEBUG_IO_WRITE(reg_, val_) \ |
| 40 | TPM_DEBUG("Write reg 0x%x with 0x%x\n", (reg_), (val_)) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 41 | #define printf(x...) printk(BIOS_ERR, x) |
| 42 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 43 | /* coreboot wrapper for TPM driver (end) */ |
| 44 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 45 | /* the macro accepts the locality value, but only locality 0 is operational */ |
| 46 | #define TIS_REG(LOCALITY, REG) \ |
Martin Roth | b9810a4 | 2017-07-23 20:00:04 -0600 | [diff] [blame] | 47 | (void *)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 48 | |
| 49 | /* hardware registers' offsets */ |
| 50 | #define TIS_REG_ACCESS 0x0 |
| 51 | #define TIS_REG_INT_ENABLE 0x8 |
| 52 | #define TIS_REG_INT_VECTOR 0xc |
| 53 | #define TIS_REG_INT_STATUS 0x10 |
| 54 | #define TIS_REG_INTF_CAPABILITY 0x14 |
| 55 | #define TIS_REG_STS 0x18 |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 56 | #define TIS_REG_BURST_COUNT 0x19 |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 57 | #define TIS_REG_DATA_FIFO 0x24 |
| 58 | #define TIS_REG_DID_VID 0xf00 |
| 59 | #define TIS_REG_RID 0xf04 |
| 60 | |
| 61 | /* Some registers' bit field definitions */ |
| 62 | #define TIS_STS_VALID (1 << 7) /* 0x80 */ |
| 63 | #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */ |
| 64 | #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */ |
| 65 | #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */ |
| 66 | #define TIS_STS_EXPECT (1 << 3) /* 0x08 */ |
| 67 | #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */ |
| 68 | |
| 69 | #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */ |
| 70 | #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */ |
| 71 | #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */ |
| 72 | #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */ |
| 73 | #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */ |
| 74 | #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */ |
| 75 | #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */ |
| 76 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 77 | /* |
| 78 | * Error value returned if a tpm register does not enter the expected state |
| 79 | * after continuous polling. No actual TPM register reading ever returns ~0, |
| 80 | * so this value is a safe error indication to be mixed with possible status |
| 81 | * register values. |
| 82 | */ |
| 83 | #define TPM_TIMEOUT_ERR (~0) |
| 84 | |
| 85 | /* Error value returned on various TPM driver errors */ |
| 86 | #define TPM_DRIVER_ERR (~0) |
| 87 | |
| 88 | /* 1 second is plenty for anything TPM does.*/ |
| 89 | #define MAX_DELAY_US (1000 * 1000) |
| 90 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 91 | /* |
| 92 | * Structures defined below allow creating descriptions of TPM vendor/device |
| 93 | * ID information for run time discovery. The only device the system knows |
| 94 | * about at this time is Infineon slb9635 |
| 95 | */ |
| 96 | struct device_name { |
| 97 | u16 dev_id; |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 98 | const char *const dev_name; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct vendor_name { |
| 102 | u16 vendor_id; |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 103 | const char *vendor_name; |
| 104 | const struct device_name *dev_names; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 107 | static const struct device_name atmel_devices[] = { |
| 108 | {0x3204, "AT97SC3204"}, |
| 109 | {0xffff} |
| 110 | }; |
| 111 | |
Stefan Reinauer | c668af7 | 2011-10-27 21:28:25 +0000 | [diff] [blame] | 112 | static const struct device_name infineon_devices[] = { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 113 | {0x000b, "SLB9635 TT 1.2"}, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 114 | #if CONFIG(TPM2) |
Kamil Wcislo | bf5ccfd | 2017-10-12 13:12:11 +0200 | [diff] [blame] | 115 | {0x001a, "SLB9665 TT 2.0"}, |
Subrata Banik | 5b8c4a7 | 2016-11-11 09:28:45 +0530 | [diff] [blame] | 116 | {0x001b, "SLB9670 TT 2.0"}, |
| 117 | #else |
Kamil Wcislo | bf5ccfd | 2017-10-12 13:12:11 +0200 | [diff] [blame] | 118 | {0x001a, "SLB9660 TT 1.2"}, |
Wenkai Du | 641529b | 2015-05-29 10:54:27 -0700 | [diff] [blame] | 119 | {0x001b, "SLB9670 TT 1.2"}, |
Subrata Banik | 5b8c4a7 | 2016-11-11 09:28:45 +0530 | [diff] [blame] | 120 | #endif |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 121 | {0xffff} |
| 122 | }; |
| 123 | |
| 124 | static const struct device_name nuvoton_devices[] = { |
| 125 | {0x00fe, "NPCT420AA V2"}, |
| 126 | {0xffff} |
| 127 | }; |
| 128 | |
| 129 | static const struct device_name stmicro_devices[] = { |
| 130 | {0x0000, "ST33ZP24" }, |
| 131 | {0xffff} |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Tsung Ho Wu | 804a043 | 2019-06-07 15:03:49 -0700 | [diff] [blame] | 134 | static const struct device_name swtpm_devices[] = { |
| 135 | #if CONFIG(TPM2) |
| 136 | {0x0001, "SwTPM 2.0" }, |
| 137 | #endif |
| 138 | {0xffff} |
| 139 | }; |
| 140 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 141 | static const struct vendor_name vendor_names[] = { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 142 | {0x1114, "Atmel", atmel_devices}, |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 143 | {0x15d1, "Infineon", infineon_devices}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 144 | {0x1050, "Nuvoton", nuvoton_devices}, |
Tsung Ho Wu | 804a043 | 2019-06-07 15:03:49 -0700 | [diff] [blame] | 145 | {0x1014, "TPM Emulator", swtpm_devices}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 146 | {0x104a, "ST Microelectronics", stmicro_devices}, |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /* |
| 150 | * Cached vendor/device ID pair to indicate that the device has been already |
| 151 | * discovered |
| 152 | */ |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 153 | static u32 vendor_dev_id; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 154 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 155 | static inline u8 tpm_read_status(int locality) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 156 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 157 | u8 value = read8(TIS_REG(locality, TIS_REG_STS)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 158 | TPM_DEBUG_IO_READ(TIS_REG_STS, value); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 159 | return value; |
| 160 | } |
| 161 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 162 | static inline void tpm_write_status(u8 sts, int locality) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 163 | { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 164 | TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 165 | write8(TIS_REG(locality, TIS_REG_STS), sts); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 166 | } |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 167 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 168 | static inline u8 tpm_read_data(int locality) |
| 169 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 170 | u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 171 | TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value); |
| 172 | return value; |
| 173 | } |
| 174 | |
| 175 | static inline void tpm_write_data(u8 data, int locality) |
| 176 | { |
| 177 | TPM_DEBUG_IO_WRITE(TIS_REG_STS, data); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 178 | write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static inline u16 tpm_read_burst_count(int locality) |
| 182 | { |
| 183 | u16 count; |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 184 | count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT)); |
| 185 | count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8; |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 186 | TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count); |
| 187 | return count; |
| 188 | } |
| 189 | |
| 190 | static inline u8 tpm_read_access(int locality) |
| 191 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 192 | u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 193 | TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value); |
| 194 | return value; |
| 195 | } |
| 196 | |
| 197 | static inline void tpm_write_access(u8 data, int locality) |
| 198 | { |
| 199 | TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 200 | write8(TIS_REG(locality, TIS_REG_ACCESS), data); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static inline u32 tpm_read_did_vid(int locality) |
| 204 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 205 | u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 206 | TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value); |
| 207 | return value; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 210 | static inline void tpm_write_int_vector(int vector, int locality) |
| 211 | { |
| 212 | TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 213 | write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 216 | static inline u8 tpm_read_int_vector(int locality) |
| 217 | { |
| 218 | u8 value = read8(TIS_REG(locality, TIS_REG_INT_VECTOR)); |
| 219 | TPM_DEBUG_IO_READ(TIS_REG_INT_VECTOR, value); |
| 220 | return value; |
| 221 | } |
| 222 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 223 | static inline void tpm_write_int_polarity(int polarity, int locality) |
| 224 | { |
| 225 | /* Set polarity and leave all other bits at 0 */ |
| 226 | u32 value = (polarity & 0x3) << 3; |
| 227 | TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 228 | write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 231 | static inline u32 tpm_read_int_polarity(int locality) |
| 232 | { |
| 233 | /* Get polarity and leave all other bits */ |
| 234 | u32 value = read8(TIS_REG(locality, TIS_REG_INT_ENABLE)); |
| 235 | value = (value >> 3) & 0x3; |
| 236 | TPM_DEBUG_IO_READ(TIS_REG_INT_ENABLE, value); |
| 237 | return value; |
| 238 | } |
| 239 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 240 | /* |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 241 | * tis_wait_sts() |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 242 | * |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 243 | * Wait for at least a second for a status to change its state to match the |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 244 | * expected state. Normally the transition happens within microseconds. |
| 245 | * |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 246 | * @locality - locality |
| 247 | * @mask - bitmask for the bitfield(s) to watch |
| 248 | * @expected - value the field(s) are supposed to be set to |
| 249 | * |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 250 | * Returns 0 on success or TPM_TIMEOUT_ERR on timeout. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 251 | */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 252 | static int tis_wait_sts(int locality, u8 mask, u8 expected) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 253 | { |
| 254 | u32 time_us = MAX_DELAY_US; |
| 255 | while (time_us > 0) { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 256 | u8 value = tpm_read_status(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 257 | if ((value & mask) == expected) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 258 | return 0; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 259 | udelay(1); /* 1 us */ |
| 260 | time_us--; |
| 261 | } |
| 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 | * |
| 296 | * Wait for at least a second for a access to change its state to match the |
| 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 | { |
| 307 | u32 time_us = MAX_DELAY_US; |
| 308 | while (time_us > 0) { |
| 309 | u8 value = tpm_read_access(locality); |
| 310 | if ((value & mask) == expected) |
| 311 | return 0; |
| 312 | udelay(1); /* 1 us */ |
| 313 | time_us--; |
| 314 | } |
| 315 | return TPM_TIMEOUT_ERR; |
| 316 | } |
| 317 | |
| 318 | static inline int tis_wait_dropped_access(int locality) |
| 319 | { |
| 320 | return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, 0); |
| 321 | } |
| 322 | |
| 323 | static inline int tis_wait_received_access(int locality) |
| 324 | { |
| 325 | return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, |
| 326 | TIS_ACCESS_ACTIVE_LOCALITY); |
| 327 | } |
| 328 | |
| 329 | static inline int tis_has_access(int locality) |
| 330 | { |
| 331 | return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY); |
| 332 | } |
| 333 | |
| 334 | static inline void tis_request_access(int locality) |
| 335 | { |
| 336 | tpm_write_access(TIS_ACCESS_REQUEST_USE, locality); |
| 337 | } |
| 338 | |
| 339 | static inline void tis_drop_access(int locality) |
| 340 | { |
| 341 | tpm_write_access(TIS_ACCESS_ACTIVE_LOCALITY, locality); |
| 342 | } |
| 343 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 344 | /* |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 345 | * PC Client Specific TPM Interface Specification section 11.2.12: |
| 346 | * |
| 347 | * Software must be prepared to send two writes of a "1" to command ready |
| 348 | * field: the first to indicate successful read of all the data, thus |
| 349 | * clearing the data from the ReadFIFO and freeing the TPM's resources, |
| 350 | * and the second to indicate to the TPM it is about to send a new command. |
| 351 | * |
| 352 | * In practice not all TPMs behave the same so it is necessary to be |
| 353 | * flexible when trying to set command ready. |
| 354 | * |
| 355 | * Returns 0 on success if the TPM is ready for transactions. |
| 356 | * Returns TPM_TIMEOUT_ERR if the command ready bit does not get set. |
| 357 | */ |
| 358 | static int tis_command_ready(u8 locality) |
| 359 | { |
| 360 | u32 status; |
| 361 | |
| 362 | /* 1st attempt to set command ready */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 363 | tpm_write_status(TIS_STS_COMMAND_READY, locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 364 | |
| 365 | /* Wait for response */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 366 | status = tpm_read_status(locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 367 | |
| 368 | /* Check if command ready is set yet */ |
| 369 | if (status & TIS_STS_COMMAND_READY) |
| 370 | return 0; |
| 371 | |
| 372 | /* 2nd attempt to set command ready */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 373 | tpm_write_status(TIS_STS_COMMAND_READY, locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 374 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 375 | return tis_wait_ready(locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 379 | * Probe the TPM device and try determining its manufacturer/device name. |
| 380 | * |
| 381 | * Returns 0 on success (the device is found or was found during an earlier |
| 382 | * invocation) or TPM_DRIVER_ERR if the device is not found. |
| 383 | */ |
| 384 | static u32 tis_probe(void) |
| 385 | { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 386 | const char *device_name = "unknown"; |
| 387 | const char *vendor_name = device_name; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 388 | const struct device_name *dev; |
| 389 | u32 didvid; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 390 | u16 vid, did; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 391 | int i; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 392 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 393 | if (vendor_dev_id) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 394 | return 0; /* Already probed. */ |
| 395 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 396 | didvid = tpm_read_did_vid(0); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 397 | if (!didvid || (didvid == 0xffffffff)) { |
Angel Pons | 08e8cab | 2020-06-18 15:20:37 +0200 | [diff] [blame] | 398 | printf("%s: No TPM device found\n", __func__); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 399 | return TPM_DRIVER_ERR; |
| 400 | } |
| 401 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 402 | vendor_dev_id = didvid; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 403 | |
| 404 | vid = didvid & 0xffff; |
| 405 | did = (didvid >> 16) & 0xffff; |
| 406 | for (i = 0; i < ARRAY_SIZE(vendor_names); i++) { |
| 407 | int j = 0; |
| 408 | u16 known_did; |
| 409 | if (vid == vendor_names[i].vendor_id) { |
| 410 | vendor_name = vendor_names[i].vendor_name; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 411 | } else { |
| 412 | continue; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 413 | } |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 414 | dev = &vendor_names[i].dev_names[j]; |
| 415 | while ((known_did = dev->dev_id) != 0xffff) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 416 | if (known_did == did) { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 417 | device_name = dev->dev_name; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | j++; |
Subrata | 41b08d9 | 2015-05-14 14:38:07 +0530 | [diff] [blame] | 421 | dev = &vendor_names[i].dev_names[j]; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 422 | } |
| 423 | break; |
| 424 | } |
| 425 | /* this will have to be converted into debug printout */ |
Duncan Laurie | 17ba944 | 2015-09-03 16:00:49 -0700 | [diff] [blame] | 426 | 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] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * tis_senddata() |
| 432 | * |
| 433 | * send the passed in data to the TPM device. |
| 434 | * |
| 435 | * @data - address of the data to send, byte by byte |
| 436 | * @len - length of the data to send |
| 437 | * |
| 438 | * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does |
| 439 | * not accept the entire command). |
| 440 | */ |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 441 | static u32 tis_senddata(const u8 *const data, u32 len) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 442 | { |
| 443 | u32 offset = 0; |
| 444 | u16 burst = 0; |
| 445 | u32 max_cycles = 0; |
| 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; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 457 | |
| 458 | /* Wait till the device is ready to accept more data. */ |
| 459 | while (!burst) { |
| 460 | if (max_cycles++ == MAX_DELAY_US) { |
| 461 | printf("%s:%d failed to feed %d bytes of %d\n", |
| 462 | __FILE__, __LINE__, len - offset, len); |
| 463 | return TPM_DRIVER_ERR; |
| 464 | } |
| 465 | udelay(1); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 466 | burst = tpm_read_burst_count(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | max_cycles = 0; |
| 470 | |
| 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)) { |
| 575 | printf("%s:%d bad response size %d\n", |
| 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)) { |
| 605 | printf("%s:%d wrong receive status: %x %d bytes left\n", |
| 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)) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 652 | printf("%s:%d - failed to lock locality %d\n", |
| 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)) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 679 | printf("%s:%d - failed to release locality %d\n", |
| 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) |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 780 | |
| 781 | static void tpm_ppi_func0_cb(void *arg) |
| 782 | { |
| 783 | /* Functions 1-8. */ |
| 784 | u8 buf[] = {0xff, 0x01}; |
| 785 | acpigen_write_return_byte_buffer(buf, 2); |
| 786 | } |
| 787 | |
| 788 | static void tpm_ppi_func1_cb(void *arg) |
| 789 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 790 | if (CONFIG(TPM2)) |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 791 | /* Interface version: 2.0 */ |
| 792 | acpigen_write_return_string("2.0"); |
| 793 | else |
| 794 | /* Interface version: 1.2 */ |
| 795 | acpigen_write_return_string("1.2"); |
| 796 | } |
| 797 | |
| 798 | static void tpm_ppi_func2_cb(void *arg) |
| 799 | { |
| 800 | /* Submit operations: drop on the floor and return success. */ |
| 801 | acpigen_write_return_byte(0); |
| 802 | } |
| 803 | |
| 804 | static void tpm_ppi_func3_cb(void *arg) |
| 805 | { |
| 806 | /* Pending operation: none. */ |
| 807 | acpigen_emit_byte(RETURN_OP); |
| 808 | acpigen_write_package(2); |
| 809 | acpigen_write_byte(0); |
| 810 | acpigen_write_byte(0); |
| 811 | acpigen_pop_len(); |
| 812 | } |
| 813 | static void tpm_ppi_func4_cb(void *arg) |
| 814 | { |
| 815 | /* Pre-OS transition method: reboot. */ |
| 816 | acpigen_write_return_byte(2); |
| 817 | } |
| 818 | static void tpm_ppi_func5_cb(void *arg) |
| 819 | { |
| 820 | /* Operation response: no operation executed. */ |
| 821 | acpigen_emit_byte(RETURN_OP); |
| 822 | acpigen_write_package(3); |
| 823 | acpigen_write_byte(0); |
| 824 | acpigen_write_byte(0); |
| 825 | acpigen_write_byte(0); |
| 826 | acpigen_pop_len(); |
| 827 | } |
| 828 | static void tpm_ppi_func6_cb(void *arg) |
| 829 | { |
| 830 | /* |
| 831 | * Set preferred user language: deprecated and must return 3 aka |
| 832 | * "not implemented". |
| 833 | */ |
| 834 | acpigen_write_return_byte(3); |
| 835 | } |
| 836 | static void tpm_ppi_func7_cb(void *arg) |
| 837 | { |
| 838 | /* Submit operations: deny. */ |
| 839 | acpigen_write_return_byte(3); |
| 840 | } |
| 841 | static void tpm_ppi_func8_cb(void *arg) |
| 842 | { |
| 843 | /* All actions are forbidden. */ |
| 844 | acpigen_write_return_byte(1); |
| 845 | } |
| 846 | static void (*tpm_ppi_callbacks[])(void *) = { |
| 847 | tpm_ppi_func0_cb, |
| 848 | tpm_ppi_func1_cb, |
| 849 | tpm_ppi_func2_cb, |
| 850 | tpm_ppi_func3_cb, |
| 851 | tpm_ppi_func4_cb, |
| 852 | tpm_ppi_func5_cb, |
| 853 | tpm_ppi_func6_cb, |
| 854 | tpm_ppi_func7_cb, |
| 855 | tpm_ppi_func8_cb, |
| 856 | }; |
| 857 | |
| 858 | static void tpm_mci_func0_cb(void *arg) |
| 859 | { |
| 860 | /* Function 1. */ |
| 861 | acpigen_write_return_singleton_buffer(0x3); |
| 862 | } |
| 863 | static void tpm_mci_func1_cb(void *arg) |
| 864 | { |
| 865 | /* Just return success. */ |
| 866 | acpigen_write_return_byte(0); |
| 867 | } |
| 868 | |
| 869 | static void (*tpm_mci_callbacks[])(void *) = { |
| 870 | tpm_mci_func0_cb, |
| 871 | tpm_mci_func1_cb, |
| 872 | }; |
| 873 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 874 | static void lpc_tpm_fill_ssdt(const struct device *dev) |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 875 | { |
| 876 | const char *path = acpi_device_path(dev->bus->dev); |
| 877 | u32 arg; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 878 | |
Philipp Deppenwiese | 3a1fbea | 2016-12-14 01:06:55 +0100 | [diff] [blame] | 879 | if (!path) { |
Tobias Diedrich | 36537f1 | 2017-02-10 00:28:45 +0100 | [diff] [blame] | 880 | path = "\\_SB_.PCI0.LPCB"; |
Philipp Deppenwiese | 3a1fbea | 2016-12-14 01:06:55 +0100 | [diff] [blame] | 881 | printk(BIOS_DEBUG, "Using default TPM ACPI path: '%s'\n", path); |
| 882 | } |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 883 | |
| 884 | /* Device */ |
| 885 | acpigen_write_scope(path); |
| 886 | acpigen_write_device(acpi_device_name(dev)); |
| 887 | |
Michał Żygowski | 7b28801 | 2020-03-20 15:41:44 +0100 | [diff] [blame] | 888 | if (CONFIG(TPM2)) { |
| 889 | acpigen_write_name_string("_HID", "MSFT0101"); |
| 890 | acpigen_write_name_string("_CID", "MSFT0101"); |
| 891 | } else { |
| 892 | acpigen_write_name("_HID"); |
| 893 | acpigen_emit_eisaid("PNP0C31"); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 894 | |
Michał Żygowski | 7b28801 | 2020-03-20 15:41:44 +0100 | [diff] [blame] | 895 | acpigen_write_name("_CID"); |
| 896 | acpigen_emit_eisaid("PNP0C31"); |
| 897 | } |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 898 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 899 | acpi_device_write_uid(dev); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 900 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 901 | u32 did_vid = tpm_read_did_vid(0); |
| 902 | if (did_vid > 0 && did_vid < 0xffffffff) |
| 903 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 904 | else |
| 905 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_OFF); |
| 906 | |
Kevin Cody-Little | c97b5af | 2018-05-09 14:14:59 -0400 | [diff] [blame] | 907 | u16 port = dev->path.pnp.port; |
| 908 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 909 | /* Resources */ |
| 910 | acpigen_write_name("_CRS"); |
| 911 | acpigen_write_resourcetemplate_header(); |
| 912 | acpigen_write_mem32fixed(1, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000); |
Frans Hendriks | 6cc937e | 2018-10-29 14:30:58 +0100 | [diff] [blame] | 913 | if (port) |
| 914 | acpigen_write_io16(port, port, 1, 2, 1); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 915 | |
| 916 | if (CONFIG_TPM_PIRQ) { |
| 917 | /* |
| 918 | * PIRQ: Update interrupt vector with configured PIRQ |
| 919 | * Active-Low Level-Triggered Shared |
| 920 | */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 921 | 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] | 922 | acpi_device_write_interrupt(&tpm_irq_a); |
| 923 | } else if (tpm_read_int_vector(0) > 0) { |
| 924 | u8 int_vec = tpm_read_int_vector(0); |
| 925 | u8 int_pol = tpm_read_int_polarity(0); |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 926 | struct acpi_irq tpm_irq = ACPI_IRQ_LEVEL_LOW(int_vec); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 927 | |
| 928 | if (int_pol & 1) |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 929 | tpm_irq.polarity = ACPI_IRQ_ACTIVE_LOW; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 930 | else |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 931 | tpm_irq.polarity = ACPI_IRQ_ACTIVE_HIGH; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 932 | |
| 933 | if (int_pol & 2) |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 934 | tpm_irq.mode = ACPI_IRQ_EDGE_TRIGGERED; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 935 | else |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 936 | tpm_irq.mode = ACPI_IRQ_LEVEL_TRIGGERED; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 937 | |
| 938 | acpi_device_write_interrupt(&tpm_irq); |
| 939 | } |
| 940 | |
| 941 | acpigen_write_resourcetemplate_footer(); |
| 942 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 943 | if (!CONFIG(CHROMEOS)) { |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 944 | /* |
| 945 | * _DSM method |
| 946 | */ |
| 947 | struct dsm_uuid ids[] = { |
| 948 | /* Physical presence interface. |
| 949 | * This is used to submit commands like "Clear TPM" to |
| 950 | * be run at next reboot provided that user confirms |
| 951 | * them. Spec allows user to cancel all commands and/or |
| 952 | * configure BIOS to reject commands. So we pretend that |
| 953 | * user did just this: cancelled everything. If user |
| 954 | * really wants to clear TPM the only option now is to |
| 955 | * do it manually in payload. |
| 956 | */ |
| 957 | DSM_UUID(TPM_PPI_UUID, &tpm_ppi_callbacks[0], |
| 958 | ARRAY_SIZE(tpm_ppi_callbacks), (void *) &arg), |
| 959 | /* Memory clearing on boot: just a dummy. */ |
| 960 | DSM_UUID(TPM_MCI_UUID, &tpm_mci_callbacks[0], |
| 961 | ARRAY_SIZE(tpm_mci_callbacks), (void *) &arg), |
| 962 | }; |
| 963 | |
| 964 | acpigen_write_dsm_uuid_arr(ids, ARRAY_SIZE(ids)); |
| 965 | } |
| 966 | acpigen_pop_len(); /* Device */ |
| 967 | acpigen_pop_len(); /* Scope */ |
| 968 | |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 969 | #if !DEVTREE_EARLY |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 970 | printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev), |
| 971 | dev->chip_ops->name, dev_path(dev)); |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 972 | #endif |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 973 | } |
| 974 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 975 | static const char *lpc_tpm_acpi_name(const struct device *dev) |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 976 | { |
| 977 | return "TPM"; |
| 978 | } |
| 979 | #endif |
| 980 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 981 | static struct device_operations lpc_tpm_ops = { |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 982 | .read_resources = lpc_tpm_read_resources, |
| 983 | .set_resources = lpc_tpm_set_resources, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 984 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 985 | .acpi_name = lpc_tpm_acpi_name, |
| 986 | .acpi_fill_ssdt = lpc_tpm_fill_ssdt, |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 987 | #endif |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 988 | }; |
| 989 | |
| 990 | static struct pnp_info pnp_dev_info[] = { |
| 991 | { .flags = PNP_IRQ0 } |
| 992 | }; |
| 993 | |
| 994 | static void enable_dev(struct device *dev) |
| 995 | { |
| 996 | pnp_enable_devices(dev, &lpc_tpm_ops, |
| 997 | ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
| 998 | } |
| 999 | |
| 1000 | struct chip_operations drivers_pc80_tpm_ops = { |
| 1001 | CHIP_NAME("LPC TPM") |
| 1002 | .enable_dev = enable_dev |
| 1003 | }; |