blob: bdd40a8a3428cd94735fb8bb8d18c8f3b712db7c [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>
Aaron Durbin64031672018-04-21 14:45:32 -060020#include <compiler.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070021#include <commonlib/endian.h>
22#include <console/console.h>
23#include <delay.h>
24#include <endian.h>
25#include <string.h>
26#include <timer.h>
Philipp Deppenwiesed88fb362017-10-18 20:26:18 +020027#include <security/tpm/tis.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070028
29#include "tpm.h"
30
Vadim Bendebury05155c02016-06-23 12:03:18 -070031#define TPM_LOCALITY_0_SPI_BASE 0x00d40000
32
Vadim Bendeburye31d2432016-04-09 18:33:49 -070033/* Assorted TPM2 registers for interface type FIFO. */
Vadim Bendebury05155c02016-06-23 12:03:18 -070034#define TPM_ACCESS_REG (TPM_LOCALITY_0_SPI_BASE + 0)
35#define TPM_STS_REG (TPM_LOCALITY_0_SPI_BASE + 0x18)
36#define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
37#define TPM_DID_VID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf00)
38#define TPM_RID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf04)
Vadim Bendebury58826fc2016-06-23 18:17:33 -070039#define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90)
Vadim Bendeburye31d2432016-04-09 18:33:49 -070040
Shelley Chen85eb0312017-11-07 14:24:19 -080041#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
42
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070043/* SPI slave structure for TPM device. */
44static struct spi_slave g_spi_slave CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070045
46/* Cached TPM device identification. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070047static struct tpm2_info g_tpm_info CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070048
49/*
50 * TODO(vbendeb): make CONFIG_DEBUG_TPM an int to allow different level of
51 * debug traces. Right now it is either 0 or 1.
52 */
53static const int debug_level_ = CONFIG_DEBUG_TPM;
54
Vadim Bendeburye31d2432016-04-09 18:33:49 -070055/*
56 * SPI frame header for TPM transactions is 4 bytes in size, it is described
57 * in section "6.4.6 Spi Bit Protocol".
58 */
59typedef struct {
60 unsigned char body[4];
61} spi_frame_header;
62
63void tpm2_get_info(struct tpm2_info *info)
64{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070065 *info = car_get_var(g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -070066}
67
Aaron Durbin64031672018-04-21 14:45:32 -060068__weak int tis_plat_irq_status(void)
Jeffy Chen19e3d332017-03-03 18:24:02 +080069{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070070 static int warning_displayed CAR_GLOBAL;
Jeffy Chen19e3d332017-03-03 18:24:02 +080071
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070072 if (!car_get_var(warning_displayed)) {
Jeffy Chen19e3d332017-03-03 18:24:02 +080073 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 10ms to wait on Cr50!\n");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070074 car_set_var(warning_displayed, 1);
Jeffy Chen19e3d332017-03-03 18:24:02 +080075 }
76 mdelay(10);
77
78 return 1;
79}
80
81/*
Elyes HAOUAS6688f462018-08-29 17:22:44 +020082 * TPM may trigger a IRQ after finish processing previous transfer.
83 * Waiting for this IRQ to sync TPM status.
Jeffy Chen19e3d332017-03-03 18:24:02 +080084 *
85 * Returns 1 on success, 0 on failure (timeout).
86 */
87static int tpm_sync(void)
88{
89 struct stopwatch sw;
90
Furquan Shaikh260b2972017-04-07 13:26:01 -070091 stopwatch_init_msecs_expire(&sw, 10);
Jeffy Chen19e3d332017-03-03 18:24:02 +080092 while (!tis_plat_irq_status()) {
93 if (stopwatch_expired(&sw)) {
Elyes HAOUAS6688f462018-08-29 17:22:44 +020094 printk(BIOS_ERR, "Timeout wait for TPM IRQ!\n");
Jeffy Chen19e3d332017-03-03 18:24:02 +080095 return 0;
96 }
97 }
98 return 1;
99}
100
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700101/*
102 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
103 * header is sent to the TPM, the master waits til TPM is ready to continue.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800104 *
105 * Returns 1 on success, 0 on failure (TPM SPI flow control timeout.)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700106 */
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800107static int start_transaction(int read_write, size_t bytes, unsigned addr)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700108{
109 spi_frame_header header;
110 uint8_t byte;
111 int i;
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800112 struct stopwatch sw;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700113 static int tpm_sync_needed CAR_GLOBAL;
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700114 static struct stopwatch wake_up_sw CAR_GLOBAL;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700115 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700116 /*
117 * First Cr50 access in each coreboot stage where TPM is used will be
118 * prepended by a wake up pulse on the CS line.
119 */
120 int wakeup_needed = 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700121
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200122 /* Wait for TPM to finish previous transaction if needed */
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700123 if (car_get_var(tpm_sync_needed)) {
Jeffy Chen19e3d332017-03-03 18:24:02 +0800124 tpm_sync();
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700125 /*
126 * During the first invocation of this function on each stage
127 * this if () clause code does not run (as tpm_sync_needed
128 * value is zero), during all following invocations the
129 * stopwatch below is guaranteed to be started.
130 */
131 if (!stopwatch_expired(car_get_var_ptr(&wake_up_sw)))
132 wakeup_needed = 0;
133 } else {
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700134 car_set_var(tpm_sync_needed, 1);
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700135 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700136
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700137 if (wakeup_needed) {
138 /* Just in case Cr50 is asleep. */
139 spi_claim_bus(spi_slave);
140 udelay(1);
141 spi_release_bus(spi_slave);
142 udelay(100);
143 }
144
145 /*
146 * The Cr50 on H1 does not go to sleep for 1 second after any
147 * SPI slave activity, let's be conservative and limit the
148 * window to 900 ms.
149 */
150 stopwatch_init_msecs_expire(car_get_var_ptr(&wake_up_sw), 900);
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800151
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700152 /*
153 * The first byte of the frame header encodes the transaction type
154 * (read or write) and transfer size (set to lentgh - 1), limited to
155 * 64 bytes.
156 */
157 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
158
159 /* The rest of the frame header is the TPM register address. */
160 for (i = 0; i < 3; i++)
161 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
162
163 /* CS assert wakes up the slave. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700164 spi_claim_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700165
166 /*
167 * The TCG TPM over SPI specification introduces the notion of SPI
168 * flow control (Section "6.4.5 Flow Control").
169 *
170 * Again, the slave (TPM device) expects each transaction to start
171 * with a 4 byte header trasmitted by master. The header indicates if
172 * the master needs to read or write a register, and the register
173 * address.
174 *
175 * If the slave needs to stall the transaction (for instance it is not
176 * ready to send the register value to the master), it sets the MOSI
177 * line to 0 during the last clock of the 4 byte header. In this case
178 * the master is supposed to start polling the SPI bus, one byte at
179 * time, until the last bit in the received byte (transferred during
180 * the last clock of the byte) is set to 1.
181 *
182 * Due to some SPI controllers' shortcomings (Rockchip comes to
183 * mind...) we trasmit the 4 byte header without checking the byte
184 * transmitted by the TPM during the transaction's last byte.
185 *
186 * We know that cr50 is guaranteed to set the flow control bit to 0
187 * during the header transfer, but real TPM2 might be fast enough not
188 * to require to stall the master, this would present an issue.
189 * crosbug.com/p/52132 has been opened to track this.
190 */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700191 spi_xfer(spi_slave, header.body, sizeof(header.body), NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700192
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800193 /*
194 * Now poll the bus until TPM removes the stall bit. Give it up to 100
195 * ms to sort it out - it could be saving stuff in nvram at some
196 * point.
197 */
198 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700199 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800200 if (stopwatch_expired(&sw)) {
201 printk(BIOS_ERR, "TPM flow control failure\n");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700202 spi_release_bus(spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800203 return 0;
204 }
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700205 spi_xfer(spi_slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700206 } while (!(byte & 1));
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800207 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700208}
209
210/*
211 * Print out the contents of a buffer, if debug is enabled. Skip registers
212 * other than FIFO, unless debug_level_ is 2.
213 */
214static void trace_dump(const char *prefix, uint32_t reg,
215 size_t bytes, const uint8_t *buffer,
216 int force)
217{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700218 static char prev_prefix CAR_GLOBAL;
219 static unsigned prev_reg CAR_GLOBAL;
220 static int current_char CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700221 const int BYTES_PER_LINE = 32;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700222 int *current_char_ptr = car_get_var_ptr(&current_char);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700223
224 if (!force) {
225 if (!debug_level_)
226 return;
227
228 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
229 return;
230 }
231
232 /*
233 * Do not print register address again if the last dump print was for
234 * that register.
235 */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700236 if ((car_get_var(prev_prefix) != *prefix) ||
237 (car_get_var(prev_reg) != reg)) {
238 car_set_var(prev_prefix, *prefix);
239 car_set_var(prev_reg, reg);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700240 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700241 *current_char_ptr = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700242 }
243
244 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
245 /*
246 * This must be a regular register address, print the 32 bit
247 * value.
248 */
249 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
250 } else {
251 int i;
252
253 /*
254 * Data read from or written to FIFO or not in 4 byte
255 * quantiites is printed byte at a time.
256 */
257 for (i = 0; i < bytes; i++) {
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700258 if (*current_char_ptr &&
259 !(*current_char_ptr % BYTES_PER_LINE)) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700260 printk(BIOS_DEBUG, "\n ");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700261 *current_char_ptr = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700262 }
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700263 (*current_char_ptr)++;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700264 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
265 }
266 }
267}
268
269/*
270 * Once transaction is initiated and the TPM indicated that it is ready to go,
271 * write the actual bytes to the register.
272 */
273static void write_bytes(const void *buffer, size_t bytes)
274{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700275 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
276 spi_xfer(spi_slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700277}
278
279/*
280 * Once transaction is initiated and the TPM indicated that it is ready to go,
281 * read the actual bytes from the register.
282 */
283static void read_bytes(void *buffer, size_t bytes)
284{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700285 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
286 spi_xfer(spi_slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700287}
288
289/*
290 * To write a register, start transaction, transfer data to the TPM, deassert
291 * CS when done.
292 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800293 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700294 */
295static int tpm2_write_reg(unsigned reg_number, const void *buffer, size_t bytes)
296{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700297 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700298 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800299 if (!start_transaction(false, bytes, reg_number))
300 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700301 write_bytes(buffer, bytes);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700302 spi_release_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700303 return 1;
304}
305
306/*
307 * To read a register, start transaction, transfer data from the TPM, deassert
308 * CS when done.
309 *
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700310 * Returns one to indicate success, zero to indicate failure. In case of
311 * failure zero out the user buffer.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700312 */
313static int tpm2_read_reg(unsigned reg_number, void *buffer, size_t bytes)
314{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700315 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800316 if (!start_transaction(true, bytes, reg_number)) {
317 memset(buffer, 0, bytes);
318 return 0;
319 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700320 read_bytes(buffer, bytes);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700321 spi_release_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700322 trace_dump("R", reg_number, bytes, buffer, 0);
323 return 1;
324}
325
326/*
327 * Status register is accessed often, wrap reading and writing it into
328 * dedicated functions.
329 */
330static int read_tpm_sts(uint32_t *status)
331{
332 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
333}
334
335static int write_tpm_sts(uint32_t status)
336{
337 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
338}
339
340/*
341 * The TPM may limit the transaction bytes count (burst count) below the 64
342 * bytes max. The current value is available as a field of the status
343 * register.
344 */
345static uint32_t get_burst_count(void)
346{
347 uint32_t status;
348
349 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700350 return (status & TPM_STS_BURST_COUNT_MASK) >> TPM_STS_BURST_COUNT_SHIFT;
351}
352
353static uint8_t tpm2_read_access_reg(void)
354{
355 uint8_t access;
356 tpm2_read_reg(TPM_ACCESS_REG, &access, sizeof(access));
357 /* We do not care about access establishment bit state. Ignore it. */
358 return access & ~TPM_ACCESS_ESTABLISHMENT;
359}
360
361static void tpm2_write_access_reg(uint8_t cmd)
362{
363 /* Writes to access register can set only 1 bit at a time. */
364 assert (!(cmd & (cmd - 1)));
365
366 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
367}
368
369static int tpm2_claim_locality(void)
370{
371 uint8_t access;
Shelley Chen85eb0312017-11-07 14:24:19 -0800372 struct stopwatch sw;
Furquan Shaikh260b2972017-04-07 13:26:01 -0700373
Furquan Shaikh260b2972017-04-07 13:26:01 -0700374 /*
Vadim Bendebury8727e642017-11-16 21:00:41 -0800375 * Locality is released by TPM reset.
376 *
377 * If locality is taken at this point, this could be due to the fact
378 * that the TPM is performing a long operation and has not processed
379 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
380 * it releases locality when reset is processed.
Shelley Chen85eb0312017-11-07 14:24:19 -0800381 */
382 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
Vadim Bendebury8727e642017-11-16 21:00:41 -0800383 do {
Shelley Chen85eb0312017-11-07 14:24:19 -0800384 access = tpm2_read_access_reg();
Vadim Bendebury8727e642017-11-16 21:00:41 -0800385 if (access & TPM_ACCESS_ACTIVE_LOCALITY) {
386 /*
387 * Don't bombard the chip with traffic, let it keep
388 * processing the command.
389 */
390 mdelay(2);
391 continue;
392 }
Furquan Shaikh260b2972017-04-07 13:26:01 -0700393
Vadim Bendebury8727e642017-11-16 21:00:41 -0800394 /*
395 * Ok, the locality is free, TPM must be reset, let's claim
396 * it.
397 */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700398
Vadim Bendebury8727e642017-11-16 21:00:41 -0800399 tpm2_write_access_reg(TPM_ACCESS_REQUEST_USE);
400 access = tpm2_read_access_reg();
401 if (access != (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
402 break;
403 }
404
405 printk(BIOS_INFO, "TPM ready after %ld ms\n",
406 stopwatch_duration_msecs(&sw));
407
408 return 1;
409 } while (!stopwatch_expired(&sw));
410
411 printk(BIOS_ERR,
412 "Failed to claim locality 0 after %ld ms, status: %#x\n",
413 stopwatch_duration_msecs(&sw), access);
414
415 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700416}
417
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700418/* Device/vendor ID values of the TPM devices this driver supports. */
419static const uint32_t supported_did_vids[] = {
420 0x00281ae0 /* H1 based Cr50 security chip. */
421};
422
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700423int tpm2_init(struct spi_slave *spi_if)
424{
425 uint32_t did_vid, status;
426 uint8_t cmd;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700427 int retries;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700428 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
429 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700430
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700431 memcpy(spi_slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700432
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200433 /* clear any pending IRQs */
Shelley Chenf2e7b372017-12-15 15:25:08 -0800434 tis_plat_irq_status();
435
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800436 /*
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700437 * 150 ms should be enough to synchronize with the TPM even under the
438 * worst nested reset request conditions. In vast majority of cases
439 * there would be no wait at all.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800440 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700441 printk(BIOS_INFO, "Probing TPM: ");
442 for (retries = 15; retries > 0; retries--) {
443 int i;
444
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200445 /* In case of failure to read div_vid is set to zero. */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700446 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
447
448 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
449 if (did_vid == supported_did_vids[i])
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200450 break; /* TPM is up and ready. */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700451
452 if (i < ARRAY_SIZE(supported_did_vids))
453 break;
454
455 /* TPM might be resetting, let's retry in a bit. */
456 mdelay(10);
457 printk(BIOS_INFO, ".");
458 }
459
460 if (!retries) {
461 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
462 __func__);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800463 return -1;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700464 }
465
466 printk(BIOS_INFO, " done!\n");
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700467
Vadim Bendebury8727e642017-11-16 21:00:41 -0800468 if (ENV_VERSTAGE || ENV_BOOTBLOCK)
469 /*
470 * Claim locality 0, do it only during the first
471 * initialization after reset.
472 */
473 if (!tpm2_claim_locality())
474 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700475
476 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700477 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700478 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
479 status);
480 return -1;
481 }
482
483 /*
484 * Locality claimed, read the revision value and set up the tpm_info
485 * structure.
486 */
487 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700488 tpm_info->vendor_id = did_vid & 0xffff;
489 tpm_info->device_id = did_vid >> 16;
490 tpm_info->revision = cmd;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700491
492 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700493 tpm_info->vendor_id, tpm_info->device_id, tpm_info->revision);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700494
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700495 /* Let's report device FW version if available. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700496 if (tpm_info->vendor_id == 0x1ae0) {
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700497 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700498 size_t chunk_size;
499 /*
500 * let's read 50 bytes at a time; leave room for the trailing
501 * zero.
502 */
503 char vstr[51];
504
505 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700506
507 printk(BIOS_INFO, "Firmware version: ");
508
509 /*
510 * Does not really matter what's written, this just makes sure
511 * the version is reported from the beginning.
512 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700513 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700514
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700515 /* Print it out in sizeof(vstr) - 1 byte chunks. */
516 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700517 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700518 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700519 printk(BIOS_INFO, "%s", vstr);
520
521 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700522 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700523 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700524 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700525 } while (vstr[chunk_size - 1] &&
526 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700527
528 printk(BIOS_INFO, "\n");
529 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700530 return 0;
531}
532
533/*
534 * This is in seconds, certain TPM commands, like key generation, can take
535 * long time to complete.
536 *
537 * Returns one to indicate success, zero (not yet implemented) to indicate
538 * failure.
539 */
540#define MAX_STATUS_TIMEOUT 120
541static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
542{
543 uint32_t status;
544 struct stopwatch sw;
545
546 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
547 do {
548 udelay(1000);
549 if (stopwatch_expired(&sw)) {
550 printk(BIOS_ERR, "failed to get expected status %x\n",
551 status_expected);
552 return false;
553 }
554 read_tpm_sts(&status);
555 } while ((status & status_mask) != status_expected);
556
557 return 1;
558}
559
560enum fifo_transfer_direction {
561 fifo_transmit = 0,
562 fifo_receive = 1
563};
564
565/* Union allows to avoid casting away 'const' on transmit buffers. */
566union fifo_transfer_buffer {
567 uint8_t *rx_buffer;
568 const uint8_t *tx_buffer;
569};
570
571/*
572 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
573 * current burst count value.
574 */
575static void fifo_transfer(size_t transfer_size,
576 union fifo_transfer_buffer buffer,
577 enum fifo_transfer_direction direction)
578{
579 size_t transaction_size;
580 size_t burst_count;
581 size_t handled_so_far = 0;
582
583 do {
584 do {
585 /* Could be zero when TPM is busy. */
586 burst_count = get_burst_count();
587 } while (!burst_count);
588
589 transaction_size = transfer_size - handled_so_far;
590 transaction_size = MIN(transaction_size, burst_count);
591
592 /*
593 * The SPI frame header does not allow to pass more than 64
594 * bytes.
595 */
596 transaction_size = MIN(transaction_size, 64);
597
598 if (direction == fifo_receive)
599 tpm2_read_reg(TPM_DATA_FIFO_REG,
600 buffer.rx_buffer + handled_so_far,
601 transaction_size);
602 else
603 tpm2_write_reg(TPM_DATA_FIFO_REG,
604 buffer.tx_buffer + handled_so_far,
605 transaction_size);
606
607 handled_so_far += transaction_size;
608
609 } while (handled_so_far != transfer_size);
610}
611
612size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
613 void *tpm2_response, size_t max_response)
614{
615 uint32_t status;
616 uint32_t expected_status_bits;
617 size_t payload_size;
618 size_t bytes_to_go;
619 const uint8_t *cmd_body = tpm2_command;
620 uint8_t *rsp_body = tpm2_response;
621 union fifo_transfer_buffer fifo_buffer;
622 const int HEADER_SIZE = 6;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700623 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700624
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800625 /* Do not try using an uninitialized TPM. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700626 if (!tpm_info->vendor_id)
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800627 return 0;
628
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700629 /* Skip the two byte tag, read the size field. */
630 payload_size = read_be32(cmd_body + 2);
631
632 /* Sanity check. */
633 if (payload_size != command_size) {
634 printk(BIOS_ERR,
635 "Command size mismatch: encoded %zd != requested %zd\n",
636 payload_size, command_size);
637 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
638 printk(BIOS_DEBUG, "\n");
639 return 0;
640 }
641
642 /* Let the TPM know that the command is coming. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700643 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700644
645 /*
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200646 * TPM commands and responses written to and read from the FIFO
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700647 * register (0x24) are datagrams of variable size, prepended by a 6
648 * byte header.
649 *
650 * The specification description of the state machine is a bit vague,
651 * but from experience it looks like there is no need to wait for the
652 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
653 * Just write the command into FIFO, making sure not to exceed the
654 * burst count or the maximum PDU size, whatever is smaller.
655 */
656 fifo_buffer.tx_buffer = cmd_body;
657 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
658
659 /* Now tell the TPM it can start processing the command. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700660 write_tpm_sts(TPM_STS_GO);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700661
662 /* Now wait for it to report that the response is ready. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700663 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700664 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
665 /*
666 * If timed out, which should never happen, let's at least
667 * print out the offending command.
668 */
669 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
670 printk(BIOS_DEBUG, "\n");
671 return 0;
672 }
673
674 /*
675 * The response is ready, let's read it. First we read the FIFO
676 * payload header, to see how much data to expect. The response header
677 * size is fixed to six bytes, the total payload size is stored in
678 * network order in the last four bytes.
679 */
680 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
681
682 /* Find out the total payload size, skipping the two byte tag. */
683 payload_size = read_be32(rsp_body + 2);
684
685 if (payload_size > max_response) {
686 /*
687 * TODO(vbendeb): at least drain the FIFO here or somehow let
688 * the TPM know that the response can be dropped.
689 */
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200690 printk(BIOS_ERR, " TPM response too long (%zd bytes)",
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700691 payload_size);
692 return 0;
693 }
694
695 /*
696 * Now let's read all but the last byte in the FIFO to make sure the
697 * status register is showing correct flow control bits: 'more data'
698 * until the last byte and then 'no more data' once the last byte is
699 * read.
700 */
701 bytes_to_go = payload_size - 1 - HEADER_SIZE;
702 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
703 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
704
705 /* Verify that there is still data to read. */
706 read_tpm_sts(&status);
707 if ((status & expected_status_bits) != expected_status_bits) {
708 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
709 status);
710 return 0;
711 }
712
713 /* Read the last byte of the PDU. */
714 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
715
716 /* Terminate the dump, if enabled. */
717 if (debug_level_)
718 printk(BIOS_DEBUG, "\n");
719
720 /* Verify that 'data available' is not asseretd any more. */
721 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700722 if ((status & expected_status_bits) != TPM_STS_VALID) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700723 printk(BIOS_ERR, "unexpected final status %#x\n", status);
724 return 0;
725 }
726
727 /* Move the TPM back to idle state. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700728 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700729
730 return payload_size;
731}