blob: e1fec28a416726dc079ace73db5f7543a7d9a320 [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
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800432 /*
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700433 * 150 ms should be enough to synchronize with the TPM even under the
434 * worst nested reset request conditions. In vast majority of cases
435 * there would be no wait at all.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800436 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700437 printk(BIOS_INFO, "Probing TPM: ");
438 for (retries = 15; retries > 0; retries--) {
439 int i;
440
441 /* In case of falure to read div_vid is set to zero. */
442 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
443
444 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
445 if (did_vid == supported_did_vids[i])
446 break; /* Tpm is up and ready. */
447
448 if (i < ARRAY_SIZE(supported_did_vids))
449 break;
450
451 /* TPM might be resetting, let's retry in a bit. */
452 mdelay(10);
453 printk(BIOS_INFO, ".");
454 }
455
456 if (!retries) {
457 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
458 __func__);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800459 return -1;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700460 }
461
462 printk(BIOS_INFO, " done!\n");
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700463
Vadim Bendebury8727e642017-11-16 21:00:41 -0800464 if (ENV_VERSTAGE || ENV_BOOTBLOCK)
465 /*
466 * Claim locality 0, do it only during the first
467 * initialization after reset.
468 */
469 if (!tpm2_claim_locality())
470 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700471
472 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700473 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700474 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
475 status);
476 return -1;
477 }
478
479 /*
480 * Locality claimed, read the revision value and set up the tpm_info
481 * structure.
482 */
483 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700484 tpm_info->vendor_id = did_vid & 0xffff;
485 tpm_info->device_id = did_vid >> 16;
486 tpm_info->revision = cmd;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700487
488 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700489 tpm_info->vendor_id, tpm_info->device_id, tpm_info->revision);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700490
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700491 /* Let's report device FW version if available. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700492 if (tpm_info->vendor_id == 0x1ae0) {
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700493 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700494 size_t chunk_size;
495 /*
496 * let's read 50 bytes at a time; leave room for the trailing
497 * zero.
498 */
499 char vstr[51];
500
501 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700502
503 printk(BIOS_INFO, "Firmware version: ");
504
505 /*
506 * Does not really matter what's written, this just makes sure
507 * the version is reported from the beginning.
508 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700509 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700510
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700511 /* Print it out in sizeof(vstr) - 1 byte chunks. */
512 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700513 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700514 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700515 printk(BIOS_INFO, "%s", vstr);
516
517 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700518 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700519 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700520 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700521 } while (vstr[chunk_size - 1] &&
522 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700523
524 printk(BIOS_INFO, "\n");
525 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700526 return 0;
527}
528
529/*
530 * This is in seconds, certain TPM commands, like key generation, can take
531 * long time to complete.
532 *
533 * Returns one to indicate success, zero (not yet implemented) to indicate
534 * failure.
535 */
536#define MAX_STATUS_TIMEOUT 120
537static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
538{
539 uint32_t status;
540 struct stopwatch sw;
541
542 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
543 do {
544 udelay(1000);
545 if (stopwatch_expired(&sw)) {
546 printk(BIOS_ERR, "failed to get expected status %x\n",
547 status_expected);
548 return false;
549 }
550 read_tpm_sts(&status);
551 } while ((status & status_mask) != status_expected);
552
553 return 1;
554}
555
556enum fifo_transfer_direction {
557 fifo_transmit = 0,
558 fifo_receive = 1
559};
560
561/* Union allows to avoid casting away 'const' on transmit buffers. */
562union fifo_transfer_buffer {
563 uint8_t *rx_buffer;
564 const uint8_t *tx_buffer;
565};
566
567/*
568 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
569 * current burst count value.
570 */
571static void fifo_transfer(size_t transfer_size,
572 union fifo_transfer_buffer buffer,
573 enum fifo_transfer_direction direction)
574{
575 size_t transaction_size;
576 size_t burst_count;
577 size_t handled_so_far = 0;
578
579 do {
580 do {
581 /* Could be zero when TPM is busy. */
582 burst_count = get_burst_count();
583 } while (!burst_count);
584
585 transaction_size = transfer_size - handled_so_far;
586 transaction_size = MIN(transaction_size, burst_count);
587
588 /*
589 * The SPI frame header does not allow to pass more than 64
590 * bytes.
591 */
592 transaction_size = MIN(transaction_size, 64);
593
594 if (direction == fifo_receive)
595 tpm2_read_reg(TPM_DATA_FIFO_REG,
596 buffer.rx_buffer + handled_so_far,
597 transaction_size);
598 else
599 tpm2_write_reg(TPM_DATA_FIFO_REG,
600 buffer.tx_buffer + handled_so_far,
601 transaction_size);
602
603 handled_so_far += transaction_size;
604
605 } while (handled_so_far != transfer_size);
606}
607
608size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
609 void *tpm2_response, size_t max_response)
610{
611 uint32_t status;
612 uint32_t expected_status_bits;
613 size_t payload_size;
614 size_t bytes_to_go;
615 const uint8_t *cmd_body = tpm2_command;
616 uint8_t *rsp_body = tpm2_response;
617 union fifo_transfer_buffer fifo_buffer;
618 const int HEADER_SIZE = 6;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700619 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700620
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800621 /* Do not try using an uninitialized TPM. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700622 if (!tpm_info->vendor_id)
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800623 return 0;
624
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700625 /* Skip the two byte tag, read the size field. */
626 payload_size = read_be32(cmd_body + 2);
627
628 /* Sanity check. */
629 if (payload_size != command_size) {
630 printk(BIOS_ERR,
631 "Command size mismatch: encoded %zd != requested %zd\n",
632 payload_size, command_size);
633 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
634 printk(BIOS_DEBUG, "\n");
635 return 0;
636 }
637
638 /* Let the TPM know that the command is coming. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700639 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700640
641 /*
642 * Tpm commands and responses written to and read from the FIFO
643 * register (0x24) are datagrams of variable size, prepended by a 6
644 * byte header.
645 *
646 * The specification description of the state machine is a bit vague,
647 * but from experience it looks like there is no need to wait for the
648 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
649 * Just write the command into FIFO, making sure not to exceed the
650 * burst count or the maximum PDU size, whatever is smaller.
651 */
652 fifo_buffer.tx_buffer = cmd_body;
653 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
654
655 /* Now tell the TPM it can start processing the command. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700656 write_tpm_sts(TPM_STS_GO);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700657
658 /* Now wait for it to report that the response is ready. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700659 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700660 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
661 /*
662 * If timed out, which should never happen, let's at least
663 * print out the offending command.
664 */
665 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
666 printk(BIOS_DEBUG, "\n");
667 return 0;
668 }
669
670 /*
671 * The response is ready, let's read it. First we read the FIFO
672 * payload header, to see how much data to expect. The response header
673 * size is fixed to six bytes, the total payload size is stored in
674 * network order in the last four bytes.
675 */
676 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
677
678 /* Find out the total payload size, skipping the two byte tag. */
679 payload_size = read_be32(rsp_body + 2);
680
681 if (payload_size > max_response) {
682 /*
683 * TODO(vbendeb): at least drain the FIFO here or somehow let
684 * the TPM know that the response can be dropped.
685 */
686 printk(BIOS_ERR, " tpm response too long (%zd bytes)",
687 payload_size);
688 return 0;
689 }
690
691 /*
692 * Now let's read all but the last byte in the FIFO to make sure the
693 * status register is showing correct flow control bits: 'more data'
694 * until the last byte and then 'no more data' once the last byte is
695 * read.
696 */
697 bytes_to_go = payload_size - 1 - HEADER_SIZE;
698 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
699 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
700
701 /* Verify that there is still data to read. */
702 read_tpm_sts(&status);
703 if ((status & expected_status_bits) != expected_status_bits) {
704 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
705 status);
706 return 0;
707 }
708
709 /* Read the last byte of the PDU. */
710 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
711
712 /* Terminate the dump, if enabled. */
713 if (debug_level_)
714 printk(BIOS_DEBUG, "\n");
715
716 /* Verify that 'data available' is not asseretd any more. */
717 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700718 if ((status & expected_status_bits) != TPM_STS_VALID) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700719 printk(BIOS_ERR, "unexpected final status %#x\n", status);
720 return 0;
721 }
722
723 /* Move the TPM back to idle state. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700724 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700725
726 return payload_size;
727}