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