blob: ceec0b0bc6171d81f86e1aee0f8986a721ada26c [file] [log] [blame]
Vadim Bendeburye31d2432016-04-09 18:33:49 -07001/*
2 * Copyright 2016 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 *
6 * This is a driver for a SPI interfaced TPM2 device.
7 *
8 * It assumes that the required SPI interface has been initialized before the
9 * driver is started. A 'sruct spi_slave' pointer passed at initialization is
10 * used to direct traffic to the correct SPI interface. This dirver does not
11 * provide a way to instantiate multiple TPM devices. Also, to keep things
12 * simple, the driver unconditionally uses of TPM locality zero.
13 *
14 * References to documentation are based on the TCG issued "TPM Profile (PTP)
15 * Specification Revision 00.43".
16 */
17
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070018#include <arch/early_variables.h>
Furquan Shaikh260b2972017-04-07 13:26:01 -070019#include <assert.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070020#include <commonlib/endian.h>
21#include <console/console.h>
22#include <delay.h>
23#include <endian.h>
24#include <string.h>
25#include <timer.h>
Jeffy Chen19e3d332017-03-03 18:24:02 +080026#include <tpm.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070027
28#include "tpm.h"
29
Vadim Bendebury05155c02016-06-23 12:03:18 -070030#define TPM_LOCALITY_0_SPI_BASE 0x00d40000
31
Vadim Bendeburye31d2432016-04-09 18:33:49 -070032/* Assorted TPM2 registers for interface type FIFO. */
Vadim Bendebury05155c02016-06-23 12:03:18 -070033#define TPM_ACCESS_REG (TPM_LOCALITY_0_SPI_BASE + 0)
34#define TPM_STS_REG (TPM_LOCALITY_0_SPI_BASE + 0x18)
35#define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
36#define TPM_DID_VID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf00)
37#define TPM_RID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf04)
Vadim Bendebury58826fc2016-06-23 18:17:33 -070038#define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90)
Vadim Bendeburye31d2432016-04-09 18:33:49 -070039
Shelley Chen85eb0312017-11-07 14:24:19 -080040#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
41
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070042/* SPI slave structure for TPM device. */
43static struct spi_slave g_spi_slave CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070044
45/* Cached TPM device identification. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070046static struct tpm2_info g_tpm_info CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070047
48/*
49 * TODO(vbendeb): make CONFIG_DEBUG_TPM an int to allow different level of
50 * debug traces. Right now it is either 0 or 1.
51 */
52static const int debug_level_ = CONFIG_DEBUG_TPM;
53
Vadim Bendeburye31d2432016-04-09 18:33:49 -070054/*
55 * SPI frame header for TPM transactions is 4 bytes in size, it is described
56 * in section "6.4.6 Spi Bit Protocol".
57 */
58typedef struct {
59 unsigned char body[4];
60} spi_frame_header;
61
62void tpm2_get_info(struct tpm2_info *info)
63{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070064 *info = car_get_var(g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -070065}
66
Jeffy Chen19e3d332017-03-03 18:24:02 +080067__attribute__((weak)) int tis_plat_irq_status(void)
68{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070069 static int warning_displayed CAR_GLOBAL;
Jeffy Chen19e3d332017-03-03 18:24:02 +080070
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070071 if (!car_get_var(warning_displayed)) {
Jeffy Chen19e3d332017-03-03 18:24:02 +080072 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 10ms to wait on Cr50!\n");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070073 car_set_var(warning_displayed, 1);
Jeffy Chen19e3d332017-03-03 18:24:02 +080074 }
75 mdelay(10);
76
77 return 1;
78}
79
80/*
81 * TPM may trigger a irq after finish processing previous transfer.
82 * Waiting for this irq to sync tpm status.
83 *
84 * Returns 1 on success, 0 on failure (timeout).
85 */
86static int tpm_sync(void)
87{
88 struct stopwatch sw;
89
Furquan Shaikh260b2972017-04-07 13:26:01 -070090 stopwatch_init_msecs_expire(&sw, 10);
Jeffy Chen19e3d332017-03-03 18:24:02 +080091 while (!tis_plat_irq_status()) {
92 if (stopwatch_expired(&sw)) {
93 printk(BIOS_ERR, "Timeout wait for tpm irq!\n");
94 return 0;
95 }
96 }
97 return 1;
98}
99
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700100/*
101 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
102 * header is sent to the TPM, the master waits til TPM is ready to continue.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800103 *
104 * Returns 1 on success, 0 on failure (TPM SPI flow control timeout.)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700105 */
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800106static int start_transaction(int read_write, size_t bytes, unsigned addr)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700107{
108 spi_frame_header header;
109 uint8_t byte;
110 int i;
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800111 struct stopwatch sw;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700112 static int tpm_sync_needed CAR_GLOBAL;
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700113 static struct stopwatch wake_up_sw CAR_GLOBAL;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700114 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700115 /*
116 * First Cr50 access in each coreboot stage where TPM is used will be
117 * prepended by a wake up pulse on the CS line.
118 */
119 int wakeup_needed = 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700120
Jeffy Chen19e3d332017-03-03 18:24:02 +0800121 /* Wait for tpm to finish previous transaction if needed */
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700122 if (car_get_var(tpm_sync_needed)) {
Jeffy Chen19e3d332017-03-03 18:24:02 +0800123 tpm_sync();
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700124 /*
125 * During the first invocation of this function on each stage
126 * this if () clause code does not run (as tpm_sync_needed
127 * value is zero), during all following invocations the
128 * stopwatch below is guaranteed to be started.
129 */
130 if (!stopwatch_expired(car_get_var_ptr(&wake_up_sw)))
131 wakeup_needed = 0;
132 } else {
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700133 car_set_var(tpm_sync_needed, 1);
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700134 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700135
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700136 if (wakeup_needed) {
137 /* Just in case Cr50 is asleep. */
138 spi_claim_bus(spi_slave);
139 udelay(1);
140 spi_release_bus(spi_slave);
141 udelay(100);
142 }
143
144 /*
145 * The Cr50 on H1 does not go to sleep for 1 second after any
146 * SPI slave activity, let's be conservative and limit the
147 * window to 900 ms.
148 */
149 stopwatch_init_msecs_expire(car_get_var_ptr(&wake_up_sw), 900);
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800150
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700151 /*
152 * The first byte of the frame header encodes the transaction type
153 * (read or write) and transfer size (set to lentgh - 1), limited to
154 * 64 bytes.
155 */
156 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
157
158 /* The rest of the frame header is the TPM register address. */
159 for (i = 0; i < 3; i++)
160 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
161
162 /* CS assert wakes up the slave. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700163 spi_claim_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700164
165 /*
166 * The TCG TPM over SPI specification introduces the notion of SPI
167 * flow control (Section "6.4.5 Flow Control").
168 *
169 * Again, the slave (TPM device) expects each transaction to start
170 * with a 4 byte header trasmitted by master. The header indicates if
171 * the master needs to read or write a register, and the register
172 * address.
173 *
174 * If the slave needs to stall the transaction (for instance it is not
175 * ready to send the register value to the master), it sets the MOSI
176 * line to 0 during the last clock of the 4 byte header. In this case
177 * the master is supposed to start polling the SPI bus, one byte at
178 * time, until the last bit in the received byte (transferred during
179 * the last clock of the byte) is set to 1.
180 *
181 * Due to some SPI controllers' shortcomings (Rockchip comes to
182 * mind...) we trasmit the 4 byte header without checking the byte
183 * transmitted by the TPM during the transaction's last byte.
184 *
185 * We know that cr50 is guaranteed to set the flow control bit to 0
186 * during the header transfer, but real TPM2 might be fast enough not
187 * to require to stall the master, this would present an issue.
188 * crosbug.com/p/52132 has been opened to track this.
189 */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700190 spi_xfer(spi_slave, header.body, sizeof(header.body), NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700191
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800192 /*
193 * Now poll the bus until TPM removes the stall bit. Give it up to 100
194 * ms to sort it out - it could be saving stuff in nvram at some
195 * point.
196 */
197 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700198 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800199 if (stopwatch_expired(&sw)) {
200 printk(BIOS_ERR, "TPM flow control failure\n");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700201 spi_release_bus(spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800202 return 0;
203 }
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700204 spi_xfer(spi_slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700205 } while (!(byte & 1));
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800206 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700207}
208
209/*
210 * Print out the contents of a buffer, if debug is enabled. Skip registers
211 * other than FIFO, unless debug_level_ is 2.
212 */
213static void trace_dump(const char *prefix, uint32_t reg,
214 size_t bytes, const uint8_t *buffer,
215 int force)
216{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700217 static char prev_prefix CAR_GLOBAL;
218 static unsigned prev_reg CAR_GLOBAL;
219 static int current_char CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700220 const int BYTES_PER_LINE = 32;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700221 int *current_char_ptr = car_get_var_ptr(&current_char);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700222
223 if (!force) {
224 if (!debug_level_)
225 return;
226
227 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
228 return;
229 }
230
231 /*
232 * Do not print register address again if the last dump print was for
233 * that register.
234 */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700235 if ((car_get_var(prev_prefix) != *prefix) ||
236 (car_get_var(prev_reg) != reg)) {
237 car_set_var(prev_prefix, *prefix);
238 car_set_var(prev_reg, reg);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700239 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700240 *current_char_ptr = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700241 }
242
243 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
244 /*
245 * This must be a regular register address, print the 32 bit
246 * value.
247 */
248 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
249 } else {
250 int i;
251
252 /*
253 * Data read from or written to FIFO or not in 4 byte
254 * quantiites is printed byte at a time.
255 */
256 for (i = 0; i < bytes; i++) {
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700257 if (*current_char_ptr &&
258 !(*current_char_ptr % BYTES_PER_LINE)) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700259 printk(BIOS_DEBUG, "\n ");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700260 *current_char_ptr = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700261 }
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700262 (*current_char_ptr)++;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700263 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
264 }
265 }
266}
267
268/*
269 * Once transaction is initiated and the TPM indicated that it is ready to go,
270 * write the actual bytes to the register.
271 */
272static void write_bytes(const void *buffer, size_t bytes)
273{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700274 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
275 spi_xfer(spi_slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700276}
277
278/*
279 * Once transaction is initiated and the TPM indicated that it is ready to go,
280 * read the actual bytes from the register.
281 */
282static void read_bytes(void *buffer, size_t bytes)
283{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700284 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
285 spi_xfer(spi_slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700286}
287
288/*
289 * To write a register, start transaction, transfer data to the TPM, deassert
290 * CS when done.
291 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800292 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700293 */
294static int tpm2_write_reg(unsigned reg_number, const void *buffer, size_t bytes)
295{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700296 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700297 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800298 if (!start_transaction(false, bytes, reg_number))
299 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700300 write_bytes(buffer, bytes);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700301 spi_release_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700302 return 1;
303}
304
305/*
306 * To read a register, start transaction, transfer data from the TPM, deassert
307 * CS when done.
308 *
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700309 * Returns one to indicate success, zero to indicate failure. In case of
310 * failure zero out the user buffer.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700311 */
312static int tpm2_read_reg(unsigned reg_number, void *buffer, size_t bytes)
313{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700314 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800315 if (!start_transaction(true, bytes, reg_number)) {
316 memset(buffer, 0, bytes);
317 return 0;
318 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700319 read_bytes(buffer, bytes);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700320 spi_release_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700321 trace_dump("R", reg_number, bytes, buffer, 0);
322 return 1;
323}
324
325/*
326 * Status register is accessed often, wrap reading and writing it into
327 * dedicated functions.
328 */
329static int read_tpm_sts(uint32_t *status)
330{
331 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
332}
333
334static int write_tpm_sts(uint32_t status)
335{
336 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
337}
338
339/*
340 * The TPM may limit the transaction bytes count (burst count) below the 64
341 * bytes max. The current value is available as a field of the status
342 * register.
343 */
344static uint32_t get_burst_count(void)
345{
346 uint32_t status;
347
348 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700349 return (status & TPM_STS_BURST_COUNT_MASK) >> TPM_STS_BURST_COUNT_SHIFT;
350}
351
352static uint8_t tpm2_read_access_reg(void)
353{
354 uint8_t access;
355 tpm2_read_reg(TPM_ACCESS_REG, &access, sizeof(access));
356 /* We do not care about access establishment bit state. Ignore it. */
357 return access & ~TPM_ACCESS_ESTABLISHMENT;
358}
359
360static void tpm2_write_access_reg(uint8_t cmd)
361{
362 /* Writes to access register can set only 1 bit at a time. */
363 assert (!(cmd & (cmd - 1)));
364
365 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
366}
367
368static int tpm2_claim_locality(void)
369{
370 uint8_t access;
Shelley Chen85eb0312017-11-07 14:24:19 -0800371 struct stopwatch sw;
Furquan Shaikh260b2972017-04-07 13:26:01 -0700372
Furquan Shaikh260b2972017-04-07 13:26:01 -0700373 /*
Vadim Bendebury8727e642017-11-16 21:00:41 -0800374 * Locality is released by TPM reset.
375 *
376 * If locality is taken at this point, this could be due to the fact
377 * that the TPM is performing a long operation and has not processed
378 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
379 * it releases locality when reset is processed.
Shelley Chen85eb0312017-11-07 14:24:19 -0800380 */
381 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
Vadim Bendebury8727e642017-11-16 21:00:41 -0800382 do {
Shelley Chen85eb0312017-11-07 14:24:19 -0800383 access = tpm2_read_access_reg();
Vadim Bendebury8727e642017-11-16 21:00:41 -0800384 if (access & TPM_ACCESS_ACTIVE_LOCALITY) {
385 /*
386 * Don't bombard the chip with traffic, let it keep
387 * processing the command.
388 */
389 mdelay(2);
390 continue;
391 }
Furquan Shaikh260b2972017-04-07 13:26:01 -0700392
Vadim Bendebury8727e642017-11-16 21:00:41 -0800393 /*
394 * Ok, the locality is free, TPM must be reset, let's claim
395 * it.
396 */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700397
Vadim Bendebury8727e642017-11-16 21:00:41 -0800398 tpm2_write_access_reg(TPM_ACCESS_REQUEST_USE);
399 access = tpm2_read_access_reg();
400 if (access != (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
401 break;
402 }
403
404 printk(BIOS_INFO, "TPM ready after %ld ms\n",
405 stopwatch_duration_msecs(&sw));
406
407 return 1;
408 } while (!stopwatch_expired(&sw));
409
410 printk(BIOS_ERR,
411 "Failed to claim locality 0 after %ld ms, status: %#x\n",
412 stopwatch_duration_msecs(&sw), access);
413
414 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700415}
416
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700417/* Device/vendor ID values of the TPM devices this driver supports. */
418static const uint32_t supported_did_vids[] = {
419 0x00281ae0 /* H1 based Cr50 security chip. */
420};
421
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700422int tpm2_init(struct spi_slave *spi_if)
423{
424 uint32_t did_vid, status;
425 uint8_t cmd;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700426 int retries;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700427 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
428 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700429
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700430 memcpy(spi_slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700431
Shelley Chenf2e7b372017-12-15 15:25:08 -0800432 /* clear any pending irqs */
433 tis_plat_irq_status();
434
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800435 /*
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700436 * 150 ms should be enough to synchronize with the TPM even under the
437 * worst nested reset request conditions. In vast majority of cases
438 * there would be no wait at all.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800439 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700440 printk(BIOS_INFO, "Probing TPM: ");
441 for (retries = 15; retries > 0; retries--) {
442 int i;
443
444 /* In case of falure to read div_vid is set to zero. */
445 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
446
447 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
448 if (did_vid == supported_did_vids[i])
449 break; /* Tpm is up and ready. */
450
451 if (i < ARRAY_SIZE(supported_did_vids))
452 break;
453
454 /* TPM might be resetting, let's retry in a bit. */
455 mdelay(10);
456 printk(BIOS_INFO, ".");
457 }
458
459 if (!retries) {
460 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
461 __func__);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800462 return -1;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700463 }
464
465 printk(BIOS_INFO, " done!\n");
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700466
Vadim Bendebury8727e642017-11-16 21:00:41 -0800467 if (ENV_VERSTAGE || ENV_BOOTBLOCK)
468 /*
469 * Claim locality 0, do it only during the first
470 * initialization after reset.
471 */
472 if (!tpm2_claim_locality())
473 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700474
475 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700476 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700477 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
478 status);
479 return -1;
480 }
481
482 /*
483 * Locality claimed, read the revision value and set up the tpm_info
484 * structure.
485 */
486 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700487 tpm_info->vendor_id = did_vid & 0xffff;
488 tpm_info->device_id = did_vid >> 16;
489 tpm_info->revision = cmd;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700490
491 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700492 tpm_info->vendor_id, tpm_info->device_id, tpm_info->revision);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700493
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700494 /* Let's report device FW version if available. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700495 if (tpm_info->vendor_id == 0x1ae0) {
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700496 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700497 size_t chunk_size;
498 /*
499 * let's read 50 bytes at a time; leave room for the trailing
500 * zero.
501 */
502 char vstr[51];
503
504 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700505
506 printk(BIOS_INFO, "Firmware version: ");
507
508 /*
509 * Does not really matter what's written, this just makes sure
510 * the version is reported from the beginning.
511 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700512 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700513
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700514 /* Print it out in sizeof(vstr) - 1 byte chunks. */
515 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700516 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700517 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700518 printk(BIOS_INFO, "%s", vstr);
519
520 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700521 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700522 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700523 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700524 } while (vstr[chunk_size - 1] &&
525 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700526
527 printk(BIOS_INFO, "\n");
528 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700529 return 0;
530}
531
532/*
533 * This is in seconds, certain TPM commands, like key generation, can take
534 * long time to complete.
535 *
536 * Returns one to indicate success, zero (not yet implemented) to indicate
537 * failure.
538 */
539#define MAX_STATUS_TIMEOUT 120
540static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
541{
542 uint32_t status;
543 struct stopwatch sw;
544
545 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
546 do {
547 udelay(1000);
548 if (stopwatch_expired(&sw)) {
549 printk(BIOS_ERR, "failed to get expected status %x\n",
550 status_expected);
551 return false;
552 }
553 read_tpm_sts(&status);
554 } while ((status & status_mask) != status_expected);
555
556 return 1;
557}
558
559enum fifo_transfer_direction {
560 fifo_transmit = 0,
561 fifo_receive = 1
562};
563
564/* Union allows to avoid casting away 'const' on transmit buffers. */
565union fifo_transfer_buffer {
566 uint8_t *rx_buffer;
567 const uint8_t *tx_buffer;
568};
569
570/*
571 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
572 * current burst count value.
573 */
574static void fifo_transfer(size_t transfer_size,
575 union fifo_transfer_buffer buffer,
576 enum fifo_transfer_direction direction)
577{
578 size_t transaction_size;
579 size_t burst_count;
580 size_t handled_so_far = 0;
581
582 do {
583 do {
584 /* Could be zero when TPM is busy. */
585 burst_count = get_burst_count();
586 } while (!burst_count);
587
588 transaction_size = transfer_size - handled_so_far;
589 transaction_size = MIN(transaction_size, burst_count);
590
591 /*
592 * The SPI frame header does not allow to pass more than 64
593 * bytes.
594 */
595 transaction_size = MIN(transaction_size, 64);
596
597 if (direction == fifo_receive)
598 tpm2_read_reg(TPM_DATA_FIFO_REG,
599 buffer.rx_buffer + handled_so_far,
600 transaction_size);
601 else
602 tpm2_write_reg(TPM_DATA_FIFO_REG,
603 buffer.tx_buffer + handled_so_far,
604 transaction_size);
605
606 handled_so_far += transaction_size;
607
608 } while (handled_so_far != transfer_size);
609}
610
611size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
612 void *tpm2_response, size_t max_response)
613{
614 uint32_t status;
615 uint32_t expected_status_bits;
616 size_t payload_size;
617 size_t bytes_to_go;
618 const uint8_t *cmd_body = tpm2_command;
619 uint8_t *rsp_body = tpm2_response;
620 union fifo_transfer_buffer fifo_buffer;
621 const int HEADER_SIZE = 6;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700622 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700623
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800624 /* Do not try using an uninitialized TPM. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700625 if (!tpm_info->vendor_id)
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800626 return 0;
627
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700628 /* Skip the two byte tag, read the size field. */
629 payload_size = read_be32(cmd_body + 2);
630
631 /* Sanity check. */
632 if (payload_size != command_size) {
633 printk(BIOS_ERR,
634 "Command size mismatch: encoded %zd != requested %zd\n",
635 payload_size, command_size);
636 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
637 printk(BIOS_DEBUG, "\n");
638 return 0;
639 }
640
641 /* Let the TPM know that the command is coming. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700642 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700643
644 /*
645 * Tpm commands and responses written to and read from the FIFO
646 * register (0x24) are datagrams of variable size, prepended by a 6
647 * byte header.
648 *
649 * The specification description of the state machine is a bit vague,
650 * but from experience it looks like there is no need to wait for the
651 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
652 * Just write the command into FIFO, making sure not to exceed the
653 * burst count or the maximum PDU size, whatever is smaller.
654 */
655 fifo_buffer.tx_buffer = cmd_body;
656 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
657
658 /* Now tell the TPM it can start processing the command. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700659 write_tpm_sts(TPM_STS_GO);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700660
661 /* Now wait for it to report that the response is ready. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700662 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700663 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
664 /*
665 * If timed out, which should never happen, let's at least
666 * print out the offending command.
667 */
668 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
669 printk(BIOS_DEBUG, "\n");
670 return 0;
671 }
672
673 /*
674 * The response is ready, let's read it. First we read the FIFO
675 * payload header, to see how much data to expect. The response header
676 * size is fixed to six bytes, the total payload size is stored in
677 * network order in the last four bytes.
678 */
679 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
680
681 /* Find out the total payload size, skipping the two byte tag. */
682 payload_size = read_be32(rsp_body + 2);
683
684 if (payload_size > max_response) {
685 /*
686 * TODO(vbendeb): at least drain the FIFO here or somehow let
687 * the TPM know that the response can be dropped.
688 */
689 printk(BIOS_ERR, " tpm response too long (%zd bytes)",
690 payload_size);
691 return 0;
692 }
693
694 /*
695 * Now let's read all but the last byte in the FIFO to make sure the
696 * status register is showing correct flow control bits: 'more data'
697 * until the last byte and then 'no more data' once the last byte is
698 * read.
699 */
700 bytes_to_go = payload_size - 1 - HEADER_SIZE;
701 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
702 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
703
704 /* Verify that there is still data to read. */
705 read_tpm_sts(&status);
706 if ((status & expected_status_bits) != expected_status_bits) {
707 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
708 status);
709 return 0;
710 }
711
712 /* Read the last byte of the PDU. */
713 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
714
715 /* Terminate the dump, if enabled. */
716 if (debug_level_)
717 printk(BIOS_DEBUG, "\n");
718
719 /* Verify that 'data available' is not asseretd any more. */
720 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700721 if ((status & expected_status_bits) != TPM_STS_VALID) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700722 printk(BIOS_ERR, "unexpected final status %#x\n", status);
723 return 0;
724 }
725
726 /* Move the TPM back to idle state. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700727 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700728
729 return payload_size;
730}