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