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