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