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> |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 35 | #include <tpm.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) \ |
| 61 | (void *)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG) |
| 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; |
| 112 | const char * const dev_name; |
| 113 | }; |
| 114 | |
| 115 | struct vendor_name { |
| 116 | u16 vendor_id; |
| 117 | const char * vendor_name; |
Stefan Reinauer | c668af7 | 2011-10-27 21:28:25 +0000 | [diff] [blame] | 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 | */ |
| 446 | static u32 tis_senddata(const u8 * const data, u32 len) |
| 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 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 598 | } while (tis_has_valid_data(locality)); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 599 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 600 | /* * Make sure we indeed read all there was. */ |
| 601 | if (tis_has_valid_data(locality)) { |
| 602 | printf("%s:%d wrong receive status: %x %d bytes left\n", |
| 603 | __FILE__, __LINE__, tpm_read_status(locality), |
| 604 | tpm_read_burst_count(locality)); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 605 | return TPM_DRIVER_ERR; |
| 606 | } |
| 607 | |
| 608 | /* Tell the TPM that we are done. */ |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 609 | if (tis_command_ready(locality) == TPM_TIMEOUT_ERR) |
| 610 | return TPM_DRIVER_ERR; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 611 | |
| 612 | *len = offset; |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * tis_init() |
| 618 | * |
| 619 | * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on |
| 620 | * failure (in case device probing did not succeed). |
| 621 | */ |
| 622 | int tis_init(void) |
| 623 | { |
| 624 | if (tis_probe()) |
| 625 | return TPM_DRIVER_ERR; |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * tis_open() |
| 631 | * |
| 632 | * Requests access to locality 0 for the caller. After all commands have been |
| 633 | * completed the caller is supposed to call tis_close(). |
| 634 | * |
| 635 | * Returns 0 on success, TPM_DRIVER_ERR on failure. |
| 636 | */ |
| 637 | int tis_open(void) |
| 638 | { |
| 639 | u8 locality = 0; /* we use locality zero for everything */ |
| 640 | |
| 641 | if (tis_close()) |
| 642 | return TPM_DRIVER_ERR; |
| 643 | |
| 644 | /* now request access to locality */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 645 | tis_request_access(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 646 | |
| 647 | /* did we get a lock? */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 648 | if (tis_wait_received_access(locality)) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 649 | printf("%s:%d - failed to lock locality %d\n", |
| 650 | __FILE__, __LINE__, locality); |
| 651 | return TPM_DRIVER_ERR; |
| 652 | } |
| 653 | |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 654 | /* Certain TPMs seem to need some delay here or they hang... */ |
| 655 | udelay(10); |
| 656 | |
| 657 | if (tis_command_ready(locality) == TPM_TIMEOUT_ERR) |
| 658 | return TPM_DRIVER_ERR; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * tis_close() |
| 665 | * |
Martin Roth | 5688979 | 2013-07-09 21:39:46 -0600 | [diff] [blame] | 666 | * terminate the current session with the TPM by releasing the locked |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 667 | * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock |
| 668 | * removal did not succeed). |
| 669 | */ |
| 670 | int tis_close(void) |
| 671 | { |
| 672 | u8 locality = 0; |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 673 | if (tis_has_access(locality)) { |
| 674 | tis_drop_access(locality); |
| 675 | if (tis_wait_dropped_access(locality)) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 676 | printf("%s:%d - failed to release locality %d\n", |
| 677 | __FILE__, __LINE__, locality); |
| 678 | return TPM_DRIVER_ERR; |
| 679 | } |
| 680 | } |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * tis_sendrecv() |
| 686 | * |
| 687 | * Send the requested data to the TPM and then try to get its response |
| 688 | * |
| 689 | * @sendbuf - buffer of the data to send |
| 690 | * @send_size size of the data to send |
| 691 | * @recvbuf - memory to save the response to |
| 692 | * @recv_len - pointer to the size of the response buffer |
| 693 | * |
| 694 | * Returns 0 on success (and places the number of response bytes at recv_len) |
| 695 | * or TPM_DRIVER_ERR on failure. |
| 696 | */ |
| 697 | int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, |
| 698 | uint8_t *recvbuf, size_t *recv_len) |
| 699 | { |
| 700 | if (tis_senddata(sendbuf, send_size)) { |
| 701 | printf("%s:%d failed sending data to TPM\n", |
| 702 | __FILE__, __LINE__); |
| 703 | return TPM_DRIVER_ERR; |
| 704 | } |
| 705 | |
| 706 | return tis_readresponse(recvbuf, recv_len); |
| 707 | } |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 708 | |
| 709 | #ifdef __RAMSTAGE__ |
| 710 | |
| 711 | /* |
| 712 | * tis_setup_interrupt() |
| 713 | * |
| 714 | * Set up the interrupt vector and polarity for locality 0 and |
| 715 | * disable all interrupts so they are unused in firmware but can |
| 716 | * be enabled by the OS. |
| 717 | * |
| 718 | * The values used here must match what is passed in the TPM ACPI |
| 719 | * device if ACPI is used on the platform. |
| 720 | * |
| 721 | * @vector - TPM interrupt vector |
| 722 | * @polarity - TPM interrupt polarity |
| 723 | * |
| 724 | * Returns 0 on success, TPM_DRIVER_ERR on failure. |
| 725 | */ |
| 726 | static int tis_setup_interrupt(int vector, int polarity) |
| 727 | { |
| 728 | u8 locality = 0; |
| 729 | int has_access = tis_has_access(locality); |
| 730 | |
| 731 | /* Open connection and request access if not already granted */ |
| 732 | if (!has_access && tis_open() < 0) |
| 733 | return TPM_DRIVER_ERR; |
| 734 | |
| 735 | /* Set TPM interrupt vector */ |
| 736 | tpm_write_int_vector(vector, locality); |
| 737 | |
| 738 | /* Set TPM interupt polarity and disable interrupts */ |
| 739 | tpm_write_int_polarity(polarity, locality); |
| 740 | |
| 741 | /* Close connection if it was opened */ |
| 742 | if (!has_access && tis_close() < 0) |
| 743 | return TPM_DRIVER_ERR; |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static void lpc_tpm_read_resources(struct device *dev) |
| 749 | { |
| 750 | /* Static 5K memory region specified in Kconfig */ |
| 751 | mmio_resource(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS >> 10, 0x5000 >> 10); |
| 752 | } |
| 753 | |
| 754 | static void lpc_tpm_set_resources(struct device *dev) |
| 755 | { |
| 756 | tpm_config_t *config = (tpm_config_t *)dev->chip_info; |
| 757 | struct resource *res; |
| 758 | |
| 759 | for (res = dev->resource_list; res; res = res->next) { |
| 760 | if (!(res->flags & IORESOURCE_ASSIGNED)) |
| 761 | continue; |
| 762 | |
| 763 | if (res->flags & IORESOURCE_IRQ) { |
| 764 | /* Set interrupt vector */ |
| 765 | tis_setup_interrupt((int)res->base, |
| 766 | config->irq_polarity); |
| 767 | } else { |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 768 | continue; |
| 769 | } |
| 770 | |
| 771 | res->flags |= IORESOURCE_STORED; |
| 772 | report_resource_stored(dev, res, " <tpm>"); |
| 773 | } |
| 774 | } |
| 775 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 776 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) |
| 777 | |
| 778 | static void tpm_ppi_func0_cb(void *arg) |
| 779 | { |
| 780 | /* Functions 1-8. */ |
| 781 | u8 buf[] = {0xff, 0x01}; |
| 782 | acpigen_write_return_byte_buffer(buf, 2); |
| 783 | } |
| 784 | |
| 785 | static void tpm_ppi_func1_cb(void *arg) |
| 786 | { |
| 787 | if (IS_ENABLED(CONFIG_TPM2)) |
| 788 | /* Interface version: 2.0 */ |
| 789 | acpigen_write_return_string("2.0"); |
| 790 | else |
| 791 | /* Interface version: 1.2 */ |
| 792 | acpigen_write_return_string("1.2"); |
| 793 | } |
| 794 | |
| 795 | static void tpm_ppi_func2_cb(void *arg) |
| 796 | { |
| 797 | /* Submit operations: drop on the floor and return success. */ |
| 798 | acpigen_write_return_byte(0); |
| 799 | } |
| 800 | |
| 801 | static void tpm_ppi_func3_cb(void *arg) |
| 802 | { |
| 803 | /* Pending operation: none. */ |
| 804 | acpigen_emit_byte(RETURN_OP); |
| 805 | acpigen_write_package(2); |
| 806 | acpigen_write_byte(0); |
| 807 | acpigen_write_byte(0); |
| 808 | acpigen_pop_len(); |
| 809 | } |
| 810 | static void tpm_ppi_func4_cb(void *arg) |
| 811 | { |
| 812 | /* Pre-OS transition method: reboot. */ |
| 813 | acpigen_write_return_byte(2); |
| 814 | } |
| 815 | static void tpm_ppi_func5_cb(void *arg) |
| 816 | { |
| 817 | /* Operation response: no operation executed. */ |
| 818 | acpigen_emit_byte(RETURN_OP); |
| 819 | acpigen_write_package(3); |
| 820 | acpigen_write_byte(0); |
| 821 | acpigen_write_byte(0); |
| 822 | acpigen_write_byte(0); |
| 823 | acpigen_pop_len(); |
| 824 | } |
| 825 | static void tpm_ppi_func6_cb(void *arg) |
| 826 | { |
| 827 | /* |
| 828 | * Set preferred user language: deprecated and must return 3 aka |
| 829 | * "not implemented". |
| 830 | */ |
| 831 | acpigen_write_return_byte(3); |
| 832 | } |
| 833 | static void tpm_ppi_func7_cb(void *arg) |
| 834 | { |
| 835 | /* Submit operations: deny. */ |
| 836 | acpigen_write_return_byte(3); |
| 837 | } |
| 838 | static void tpm_ppi_func8_cb(void *arg) |
| 839 | { |
| 840 | /* All actions are forbidden. */ |
| 841 | acpigen_write_return_byte(1); |
| 842 | } |
| 843 | static void (*tpm_ppi_callbacks[])(void *) = { |
| 844 | tpm_ppi_func0_cb, |
| 845 | tpm_ppi_func1_cb, |
| 846 | tpm_ppi_func2_cb, |
| 847 | tpm_ppi_func3_cb, |
| 848 | tpm_ppi_func4_cb, |
| 849 | tpm_ppi_func5_cb, |
| 850 | tpm_ppi_func6_cb, |
| 851 | tpm_ppi_func7_cb, |
| 852 | tpm_ppi_func8_cb, |
| 853 | }; |
| 854 | |
| 855 | static void tpm_mci_func0_cb(void *arg) |
| 856 | { |
| 857 | /* Function 1. */ |
| 858 | acpigen_write_return_singleton_buffer(0x3); |
| 859 | } |
| 860 | static void tpm_mci_func1_cb(void *arg) |
| 861 | { |
| 862 | /* Just return success. */ |
| 863 | acpigen_write_return_byte(0); |
| 864 | } |
| 865 | |
| 866 | static void (*tpm_mci_callbacks[])(void *) = { |
| 867 | tpm_mci_func0_cb, |
| 868 | tpm_mci_func1_cb, |
| 869 | }; |
| 870 | |
| 871 | static void lpc_tpm_fill_ssdt(struct device *dev) |
| 872 | { |
| 873 | const char *path = acpi_device_path(dev->bus->dev); |
| 874 | u32 arg; |
| 875 | struct opregion opreg = OPREGION("TREG", SYSTEMMEMORY, |
| 876 | CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000); |
| 877 | |
Philipp Deppenwiese | 3a1fbea | 2016-12-14 01:06:55 +0100 | [diff] [blame] | 878 | if (!path) { |
Tobias Diedrich | 36537f1 | 2017-02-10 00:28:45 +0100 | [diff] [blame] | 879 | path = "\\_SB_.PCI0.LPCB"; |
Philipp Deppenwiese | 3a1fbea | 2016-12-14 01:06:55 +0100 | [diff] [blame] | 880 | printk(BIOS_DEBUG, "Using default TPM ACPI path: '%s'\n", path); |
| 881 | } |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 882 | |
| 883 | /* Device */ |
| 884 | acpigen_write_scope(path); |
| 885 | acpigen_write_device(acpi_device_name(dev)); |
| 886 | |
| 887 | acpigen_write_name("_HID"); |
| 888 | acpigen_emit_eisaid("PNP0C31"); |
| 889 | |
| 890 | acpigen_write_name("_CID"); |
| 891 | acpigen_emit_eisaid("PNP0C31"); |
| 892 | |
| 893 | acpigen_write_name_integer("_UID", 1); |
| 894 | |
| 895 | acpigen_write_opregion(&opreg); |
| 896 | |
| 897 | struct fieldlist tpm_field_list[] = { |
| 898 | /* |
| 899 | * TPM_INT_ENABLE_0 |
| 900 | * bit 0 : dataAvailIntEnable, |
| 901 | * bit 1 : stsValidIntEnable, |
| 902 | * bit 2 : localityChangeIntEnable, |
| 903 | * bit 3:4 typePolarity. |
| 904 | */ |
| 905 | FIELDLIST_OFFSET(0x8), |
| 906 | FIELDLIST_NAMESTR("INTE", 3), |
| 907 | FIELDLIST_NAMESTR("ITPL", 2), |
| 908 | |
| 909 | /* TPM_INT_VECTOR_0 */ |
| 910 | FIELDLIST_OFFSET(0xC), |
| 911 | FIELDLIST_NAMESTR("IVEC", 4), |
| 912 | |
| 913 | /* TPM_DID_VID */ |
| 914 | FIELDLIST_OFFSET(0xf00), |
| 915 | FIELDLIST_NAMESTR("DVID", 32), |
| 916 | }; |
| 917 | |
| 918 | acpigen_write_field(opreg.name, tpm_field_list, |
| 919 | ARRAY_SIZE(tpm_field_list), |
| 920 | FIELD_BYTEACC | FIELD_NOLOCK | FIELD_PRESERVE); |
| 921 | |
| 922 | u32 did_vid = tpm_read_did_vid(0); |
| 923 | if (did_vid > 0 && did_vid < 0xffffffff) |
| 924 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 925 | else |
| 926 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_OFF); |
| 927 | |
| 928 | /* Resources */ |
| 929 | acpigen_write_name("_CRS"); |
| 930 | acpigen_write_resourcetemplate_header(); |
| 931 | acpigen_write_mem32fixed(1, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000); |
| 932 | acpigen_write_io16(0x2e, 0x2e, 1, 2, 1); |
| 933 | |
| 934 | if (CONFIG_TPM_PIRQ) { |
| 935 | /* |
| 936 | * PIRQ: Update interrupt vector with configured PIRQ |
| 937 | * Active-Low Level-Triggered Shared |
| 938 | */ |
| 939 | struct acpi_irq tpm_irq_a = IRQ_LEVEL_LOW(CONFIG_TPM_PIRQ); |
| 940 | acpi_device_write_interrupt(&tpm_irq_a); |
| 941 | } else if (tpm_read_int_vector(0) > 0) { |
| 942 | u8 int_vec = tpm_read_int_vector(0); |
| 943 | u8 int_pol = tpm_read_int_polarity(0); |
| 944 | struct acpi_irq tpm_irq = IRQ_LEVEL_LOW(int_vec); |
| 945 | |
| 946 | if (int_pol & 1) |
| 947 | tpm_irq.polarity = IRQ_ACTIVE_LOW; |
| 948 | else |
| 949 | tpm_irq.polarity = IRQ_ACTIVE_HIGH; |
| 950 | |
| 951 | if (int_pol & 2) |
| 952 | tpm_irq.mode = IRQ_EDGE_TRIGGERED; |
| 953 | else |
| 954 | tpm_irq.mode = IRQ_LEVEL_TRIGGERED; |
| 955 | |
| 956 | acpi_device_write_interrupt(&tpm_irq); |
| 957 | } |
| 958 | |
| 959 | acpigen_write_resourcetemplate_footer(); |
| 960 | |
| 961 | if (!IS_ENABLED(CONFIG_CHROMEOS)) { |
| 962 | /* |
| 963 | * _DSM method |
| 964 | */ |
| 965 | struct dsm_uuid ids[] = { |
| 966 | /* Physical presence interface. |
| 967 | * This is used to submit commands like "Clear TPM" to |
| 968 | * be run at next reboot provided that user confirms |
| 969 | * them. Spec allows user to cancel all commands and/or |
| 970 | * configure BIOS to reject commands. So we pretend that |
| 971 | * user did just this: cancelled everything. If user |
| 972 | * really wants to clear TPM the only option now is to |
| 973 | * do it manually in payload. |
| 974 | */ |
| 975 | DSM_UUID(TPM_PPI_UUID, &tpm_ppi_callbacks[0], |
| 976 | ARRAY_SIZE(tpm_ppi_callbacks), (void *) &arg), |
| 977 | /* Memory clearing on boot: just a dummy. */ |
| 978 | DSM_UUID(TPM_MCI_UUID, &tpm_mci_callbacks[0], |
| 979 | ARRAY_SIZE(tpm_mci_callbacks), (void *) &arg), |
| 980 | }; |
| 981 | |
| 982 | acpigen_write_dsm_uuid_arr(ids, ARRAY_SIZE(ids)); |
| 983 | } |
| 984 | acpigen_pop_len(); /* Device */ |
| 985 | acpigen_pop_len(); /* Scope */ |
| 986 | |
| 987 | printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev), |
| 988 | dev->chip_ops->name, dev_path(dev)); |
| 989 | } |
| 990 | |
| 991 | static const char *lpc_tpm_acpi_name(struct device *dev) |
| 992 | { |
| 993 | return "TPM"; |
| 994 | } |
| 995 | #endif |
| 996 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 997 | static struct device_operations lpc_tpm_ops = { |
| 998 | .read_resources = &lpc_tpm_read_resources, |
| 999 | .set_resources = &lpc_tpm_set_resources, |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 1000 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) |
| 1001 | .acpi_name = &lpc_tpm_acpi_name, |
| 1002 | .acpi_fill_ssdt_generator = &lpc_tpm_fill_ssdt, |
| 1003 | #endif |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 1004 | }; |
| 1005 | |
| 1006 | static struct pnp_info pnp_dev_info[] = { |
| 1007 | { .flags = PNP_IRQ0 } |
| 1008 | }; |
| 1009 | |
| 1010 | static void enable_dev(struct device *dev) |
| 1011 | { |
| 1012 | pnp_enable_devices(dev, &lpc_tpm_ops, |
| 1013 | ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
| 1014 | } |
| 1015 | |
| 1016 | struct chip_operations drivers_pc80_tpm_ops = { |
| 1017 | CHIP_NAME("LPC TPM") |
| 1018 | .enable_dev = enable_dev |
| 1019 | }; |
| 1020 | |
| 1021 | #endif /* __RAMSTAGE__ */ |