blob: 62d1bbae554f85325f8c000d7b9e115a22120c04 [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 Shaikh260b2972017-04-07 13:26:01 -070018#include <assert.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070019#include <commonlib/endian.h>
20#include <console/console.h>
21#include <delay.h>
22#include <endian.h>
23#include <string.h>
24#include <timer.h>
Philipp Deppenwiesed88fb362017-10-18 20:26:18 +020025#include <security/tpm/tis.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070026
27#include "tpm.h"
28
Vadim Bendebury05155c02016-06-23 12:03:18 -070029#define TPM_LOCALITY_0_SPI_BASE 0x00d40000
30
Vadim Bendeburye31d2432016-04-09 18:33:49 -070031/* Assorted TPM2 registers for interface type FIFO. */
Vadim Bendebury05155c02016-06-23 12:03:18 -070032#define TPM_ACCESS_REG (TPM_LOCALITY_0_SPI_BASE + 0)
33#define TPM_STS_REG (TPM_LOCALITY_0_SPI_BASE + 0x18)
34#define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
35#define TPM_DID_VID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf00)
36#define TPM_RID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf04)
Vadim Bendebury58826fc2016-06-23 18:17:33 -070037#define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90)
Vadim Bendeburye31d2432016-04-09 18:33:49 -070038
Shelley Chen85eb0312017-11-07 14:24:19 -080039#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
40
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070041/* SPI slave structure for TPM device. */
Patrick Georgic9b13592019-11-29 11:47:47 +010042static struct spi_slave spi_slave;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070043
44/* Cached TPM device identification. */
Patrick Georgic9b13592019-11-29 11:47:47 +010045static struct tpm2_info tpm_info;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070046
47/*
48 * TODO(vbendeb): make CONFIG_DEBUG_TPM an int to allow different level of
49 * debug traces. Right now it is either 0 or 1.
50 */
51static const int debug_level_ = CONFIG_DEBUG_TPM;
52
Vadim Bendeburye31d2432016-04-09 18:33:49 -070053/*
54 * SPI frame header for TPM transactions is 4 bytes in size, it is described
55 * in section "6.4.6 Spi Bit Protocol".
56 */
57typedef struct {
58 unsigned char body[4];
59} spi_frame_header;
60
61void tpm2_get_info(struct tpm2_info *info)
62{
Patrick Georgic9b13592019-11-29 11:47:47 +010063 *info = tpm_info;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070064}
65
Aaron Durbin64031672018-04-21 14:45:32 -060066__weak int tis_plat_irq_status(void)
Jeffy Chen19e3d332017-03-03 18:24:02 +080067{
Arthur Heymans0ca944b2019-11-20 19:51:06 +010068 static int warning_displayed;
Jeffy Chen19e3d332017-03-03 18:24:02 +080069
Arthur Heymans0ca944b2019-11-20 19:51:06 +010070 if (!warning_displayed) {
Jeffy Chen19e3d332017-03-03 18:24:02 +080071 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 10ms to wait on Cr50!\n");
Arthur Heymans0ca944b2019-11-20 19:51:06 +010072 warning_displayed = 1;
Jeffy Chen19e3d332017-03-03 18:24:02 +080073 }
74 mdelay(10);
75
76 return 1;
77}
78
79/*
Elyes HAOUAS6688f462018-08-29 17:22:44 +020080 * TPM may trigger a IRQ after finish processing previous transfer.
81 * Waiting for this IRQ to sync TPM status.
Jeffy Chen19e3d332017-03-03 18:24:02 +080082 *
83 * Returns 1 on success, 0 on failure (timeout).
84 */
85static int tpm_sync(void)
86{
87 struct stopwatch sw;
88
Furquan Shaikh260b2972017-04-07 13:26:01 -070089 stopwatch_init_msecs_expire(&sw, 10);
Jeffy Chen19e3d332017-03-03 18:24:02 +080090 while (!tis_plat_irq_status()) {
91 if (stopwatch_expired(&sw)) {
Elyes HAOUAS6688f462018-08-29 17:22:44 +020092 printk(BIOS_ERR, "Timeout wait for TPM IRQ!\n");
Jeffy Chen19e3d332017-03-03 18:24:02 +080093 return 0;
94 }
95 }
96 return 1;
97}
98
Vadim Bendeburye31d2432016-04-09 18:33:49 -070099/*
100 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
101 * header is sent to the TPM, the master waits til TPM is ready to continue.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800102 *
103 * Returns 1 on success, 0 on failure (TPM SPI flow control timeout.)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700104 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600105static int start_transaction(int read_write, size_t bytes, unsigned int addr)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700106{
107 spi_frame_header header;
108 uint8_t byte;
109 int i;
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800110 struct stopwatch sw;
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100111 static int tpm_sync_needed;
112 static struct stopwatch wake_up_sw;
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700113 /*
114 * First Cr50 access in each coreboot stage where TPM is used will be
115 * prepended by a wake up pulse on the CS line.
116 */
117 int wakeup_needed = 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700118
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200119 /* Wait for TPM to finish previous transaction if needed */
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100120 if (tpm_sync_needed) {
Jeffy Chen19e3d332017-03-03 18:24:02 +0800121 tpm_sync();
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700122 /*
123 * During the first invocation of this function on each stage
124 * this if () clause code does not run (as tpm_sync_needed
125 * value is zero), during all following invocations the
126 * stopwatch below is guaranteed to be started.
127 */
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100128 if (!stopwatch_expired(&wake_up_sw))
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700129 wakeup_needed = 0;
130 } else {
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100131 tpm_sync_needed = 1;
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700132 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700133
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700134 if (wakeup_needed) {
135 /* Just in case Cr50 is asleep. */
Patrick Georgic9b13592019-11-29 11:47:47 +0100136 spi_claim_bus(&spi_slave);
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700137 udelay(1);
Patrick Georgic9b13592019-11-29 11:47:47 +0100138 spi_release_bus(&spi_slave);
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700139 udelay(100);
140 }
141
142 /*
143 * The Cr50 on H1 does not go to sleep for 1 second after any
144 * SPI slave activity, let's be conservative and limit the
145 * window to 900 ms.
146 */
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100147 stopwatch_init_msecs_expire(&wake_up_sw, 900);
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800148
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700149 /*
150 * The first byte of the frame header encodes the transaction type
151 * (read or write) and transfer size (set to lentgh - 1), limited to
152 * 64 bytes.
153 */
154 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
155
156 /* The rest of the frame header is the TPM register address. */
157 for (i = 0; i < 3; i++)
158 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
159
160 /* CS assert wakes up the slave. */
Patrick Georgic9b13592019-11-29 11:47:47 +0100161 spi_claim_bus(&spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700162
163 /*
164 * The TCG TPM over SPI specification introduces the notion of SPI
165 * flow control (Section "6.4.5 Flow Control").
166 *
167 * Again, the slave (TPM device) expects each transaction to start
168 * with a 4 byte header trasmitted by master. The header indicates if
169 * the master needs to read or write a register, and the register
170 * address.
171 *
172 * If the slave needs to stall the transaction (for instance it is not
173 * ready to send the register value to the master), it sets the MOSI
174 * line to 0 during the last clock of the 4 byte header. In this case
175 * the master is supposed to start polling the SPI bus, one byte at
176 * time, until the last bit in the received byte (transferred during
177 * the last clock of the byte) is set to 1.
178 *
179 * Due to some SPI controllers' shortcomings (Rockchip comes to
180 * mind...) we trasmit the 4 byte header without checking the byte
181 * transmitted by the TPM during the transaction's last byte.
182 *
183 * We know that cr50 is guaranteed to set the flow control bit to 0
184 * during the header transfer, but real TPM2 might be fast enough not
185 * to require to stall the master, this would present an issue.
186 * crosbug.com/p/52132 has been opened to track this.
187 */
Patrick Georgic9b13592019-11-29 11:47:47 +0100188 spi_xfer(&spi_slave, header.body, sizeof(header.body), NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700189
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800190 /*
191 * Now poll the bus until TPM removes the stall bit. Give it up to 100
192 * ms to sort it out - it could be saving stuff in nvram at some
193 * point.
194 */
195 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700196 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800197 if (stopwatch_expired(&sw)) {
198 printk(BIOS_ERR, "TPM flow control failure\n");
Patrick Georgic9b13592019-11-29 11:47:47 +0100199 spi_release_bus(&spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800200 return 0;
201 }
Patrick Georgic9b13592019-11-29 11:47:47 +0100202 spi_xfer(&spi_slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700203 } while (!(byte & 1));
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800204 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700205}
206
207/*
208 * Print out the contents of a buffer, if debug is enabled. Skip registers
209 * other than FIFO, unless debug_level_ is 2.
210 */
211static void trace_dump(const char *prefix, uint32_t reg,
212 size_t bytes, const uint8_t *buffer,
213 int force)
214{
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100215 static char prev_prefix;
216 static unsigned int prev_reg;
217 static int current_char;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700218 const int BYTES_PER_LINE = 32;
219
220 if (!force) {
221 if (!debug_level_)
222 return;
223
224 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
225 return;
226 }
227
228 /*
229 * Do not print register address again if the last dump print was for
230 * that register.
231 */
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100232 if (prev_prefix != *prefix || (prev_reg != reg)) {
233 prev_prefix = *prefix;
234 prev_reg = reg;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700235 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100236 current_char = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700237 }
238
239 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
240 /*
241 * This must be a regular register address, print the 32 bit
242 * value.
243 */
244 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
245 } else {
246 int i;
247
248 /*
249 * Data read from or written to FIFO or not in 4 byte
250 * quantiites is printed byte at a time.
251 */
252 for (i = 0; i < bytes; i++) {
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100253 if (current_char &&
254 !(current_char % BYTES_PER_LINE)) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700255 printk(BIOS_DEBUG, "\n ");
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100256 current_char = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700257 }
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100258 (current_char)++;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700259 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
260 }
261 }
262}
263
264/*
265 * Once transaction is initiated and the TPM indicated that it is ready to go,
266 * write the actual bytes to the register.
267 */
268static void write_bytes(const void *buffer, size_t bytes)
269{
Patrick Georgic9b13592019-11-29 11:47:47 +0100270 spi_xfer(&spi_slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700271}
272
273/*
274 * Once transaction is initiated and the TPM indicated that it is ready to go,
275 * read the actual bytes from the register.
276 */
277static void read_bytes(void *buffer, size_t bytes)
278{
Patrick Georgic9b13592019-11-29 11:47:47 +0100279 spi_xfer(&spi_slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700280}
281
282/*
283 * To write a register, start transaction, transfer data to the TPM, deassert
284 * CS when done.
285 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800286 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700287 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600288static int tpm2_write_reg(unsigned int reg_number, const void *buffer, size_t bytes)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700289{
290 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800291 if (!start_transaction(false, bytes, reg_number))
292 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700293 write_bytes(buffer, bytes);
Patrick Georgic9b13592019-11-29 11:47:47 +0100294 spi_release_bus(&spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700295 return 1;
296}
297
298/*
299 * To read a register, start transaction, transfer data from the TPM, deassert
300 * CS when done.
301 *
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700302 * Returns one to indicate success, zero to indicate failure. In case of
303 * failure zero out the user buffer.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700304 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600305static int tpm2_read_reg(unsigned int reg_number, void *buffer, size_t bytes)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700306{
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800307 if (!start_transaction(true, bytes, reg_number)) {
308 memset(buffer, 0, bytes);
309 return 0;
310 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700311 read_bytes(buffer, bytes);
Patrick Georgic9b13592019-11-29 11:47:47 +0100312 spi_release_bus(&spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700313 trace_dump("R", reg_number, bytes, buffer, 0);
314 return 1;
315}
316
317/*
318 * Status register is accessed often, wrap reading and writing it into
319 * dedicated functions.
320 */
321static int read_tpm_sts(uint32_t *status)
322{
323 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
324}
325
326static int write_tpm_sts(uint32_t status)
327{
328 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
329}
330
331/*
332 * The TPM may limit the transaction bytes count (burst count) below the 64
333 * bytes max. The current value is available as a field of the status
334 * register.
335 */
336static uint32_t get_burst_count(void)
337{
338 uint32_t status;
339
340 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700341 return (status & TPM_STS_BURST_COUNT_MASK) >> TPM_STS_BURST_COUNT_SHIFT;
342}
343
344static uint8_t tpm2_read_access_reg(void)
345{
346 uint8_t access;
347 tpm2_read_reg(TPM_ACCESS_REG, &access, sizeof(access));
348 /* We do not care about access establishment bit state. Ignore it. */
349 return access & ~TPM_ACCESS_ESTABLISHMENT;
350}
351
352static void tpm2_write_access_reg(uint8_t cmd)
353{
354 /* Writes to access register can set only 1 bit at a time. */
355 assert (!(cmd & (cmd - 1)));
356
357 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
358}
359
360static int tpm2_claim_locality(void)
361{
362 uint8_t access;
Shelley Chen85eb0312017-11-07 14:24:19 -0800363 struct stopwatch sw;
Furquan Shaikh260b2972017-04-07 13:26:01 -0700364
Furquan Shaikh260b2972017-04-07 13:26:01 -0700365 /*
Vadim Bendebury8727e642017-11-16 21:00:41 -0800366 * Locality is released by TPM reset.
367 *
368 * If locality is taken at this point, this could be due to the fact
369 * that the TPM is performing a long operation and has not processed
370 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
371 * it releases locality when reset is processed.
Shelley Chen85eb0312017-11-07 14:24:19 -0800372 */
373 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
Vadim Bendebury8727e642017-11-16 21:00:41 -0800374 do {
Shelley Chen85eb0312017-11-07 14:24:19 -0800375 access = tpm2_read_access_reg();
Vadim Bendebury8727e642017-11-16 21:00:41 -0800376 if (access & TPM_ACCESS_ACTIVE_LOCALITY) {
377 /*
378 * Don't bombard the chip with traffic, let it keep
379 * processing the command.
380 */
381 mdelay(2);
382 continue;
383 }
Furquan Shaikh260b2972017-04-07 13:26:01 -0700384
Vadim Bendebury8727e642017-11-16 21:00:41 -0800385 /*
386 * Ok, the locality is free, TPM must be reset, let's claim
387 * it.
388 */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700389
Vadim Bendebury8727e642017-11-16 21:00:41 -0800390 tpm2_write_access_reg(TPM_ACCESS_REQUEST_USE);
391 access = tpm2_read_access_reg();
392 if (access != (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
393 break;
394 }
395
396 printk(BIOS_INFO, "TPM ready after %ld ms\n",
397 stopwatch_duration_msecs(&sw));
398
399 return 1;
400 } while (!stopwatch_expired(&sw));
401
402 printk(BIOS_ERR,
403 "Failed to claim locality 0 after %ld ms, status: %#x\n",
404 stopwatch_duration_msecs(&sw), access);
405
406 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700407}
408
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700409/* Device/vendor ID values of the TPM devices this driver supports. */
410static const uint32_t supported_did_vids[] = {
411 0x00281ae0 /* H1 based Cr50 security chip. */
412};
413
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700414int tpm2_init(struct spi_slave *spi_if)
415{
416 uint32_t did_vid, status;
417 uint8_t cmd;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700418 int retries;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700419
Patrick Georgic9b13592019-11-29 11:47:47 +0100420 memcpy(&spi_slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700421
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200422 /* clear any pending IRQs */
Shelley Chenf2e7b372017-12-15 15:25:08 -0800423 tis_plat_irq_status();
424
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800425 /*
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700426 * 150 ms should be enough to synchronize with the TPM even under the
427 * worst nested reset request conditions. In vast majority of cases
428 * there would be no wait at all.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800429 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700430 printk(BIOS_INFO, "Probing TPM: ");
431 for (retries = 15; retries > 0; retries--) {
432 int i;
433
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200434 /* In case of failure to read div_vid is set to zero. */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700435 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
436
437 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
438 if (did_vid == supported_did_vids[i])
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200439 break; /* TPM is up and ready. */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700440
441 if (i < ARRAY_SIZE(supported_did_vids))
442 break;
443
444 /* TPM might be resetting, let's retry in a bit. */
445 mdelay(10);
446 printk(BIOS_INFO, ".");
447 }
448
449 if (!retries) {
450 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
451 __func__);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800452 return -1;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700453 }
454
455 printk(BIOS_INFO, " done!\n");
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700456
Vadim Bendebury8727e642017-11-16 21:00:41 -0800457 if (ENV_VERSTAGE || ENV_BOOTBLOCK)
458 /*
459 * Claim locality 0, do it only during the first
460 * initialization after reset.
461 */
462 if (!tpm2_claim_locality())
463 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700464
465 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700466 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700467 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
468 status);
469 return -1;
470 }
471
472 /*
473 * Locality claimed, read the revision value and set up the tpm_info
474 * structure.
475 */
476 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
Patrick Georgic9b13592019-11-29 11:47:47 +0100477 tpm_info.vendor_id = did_vid & 0xffff;
478 tpm_info.device_id = did_vid >> 16;
479 tpm_info.revision = cmd;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700480
481 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
Patrick Georgic9b13592019-11-29 11:47:47 +0100482 tpm_info.vendor_id, tpm_info.device_id, tpm_info.revision);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700483
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700484 /* Let's report device FW version if available. */
Patrick Georgic9b13592019-11-29 11:47:47 +0100485 if (tpm_info.vendor_id == 0x1ae0) {
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700486 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700487 size_t chunk_size;
488 /*
489 * let's read 50 bytes at a time; leave room for the trailing
490 * zero.
491 */
492 char vstr[51];
493
494 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700495
496 printk(BIOS_INFO, "Firmware version: ");
497
498 /*
499 * Does not really matter what's written, this just makes sure
500 * the version is reported from the beginning.
501 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700502 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700503
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700504 /* Print it out in sizeof(vstr) - 1 byte chunks. */
505 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700506 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700507 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700508 printk(BIOS_INFO, "%s", vstr);
509
510 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700511 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700512 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700513 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700514 } while (vstr[chunk_size - 1] &&
515 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700516
517 printk(BIOS_INFO, "\n");
518 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700519 return 0;
520}
521
522/*
523 * This is in seconds, certain TPM commands, like key generation, can take
524 * long time to complete.
525 *
526 * Returns one to indicate success, zero (not yet implemented) to indicate
527 * failure.
528 */
529#define MAX_STATUS_TIMEOUT 120
530static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
531{
532 uint32_t status;
533 struct stopwatch sw;
534
535 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
536 do {
537 udelay(1000);
538 if (stopwatch_expired(&sw)) {
539 printk(BIOS_ERR, "failed to get expected status %x\n",
540 status_expected);
541 return false;
542 }
543 read_tpm_sts(&status);
544 } while ((status & status_mask) != status_expected);
545
546 return 1;
547}
548
549enum fifo_transfer_direction {
550 fifo_transmit = 0,
551 fifo_receive = 1
552};
553
554/* Union allows to avoid casting away 'const' on transmit buffers. */
555union fifo_transfer_buffer {
556 uint8_t *rx_buffer;
557 const uint8_t *tx_buffer;
558};
559
560/*
561 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
562 * current burst count value.
563 */
564static void fifo_transfer(size_t transfer_size,
565 union fifo_transfer_buffer buffer,
566 enum fifo_transfer_direction direction)
567{
568 size_t transaction_size;
569 size_t burst_count;
570 size_t handled_so_far = 0;
571
572 do {
573 do {
574 /* Could be zero when TPM is busy. */
575 burst_count = get_burst_count();
576 } while (!burst_count);
577
578 transaction_size = transfer_size - handled_so_far;
579 transaction_size = MIN(transaction_size, burst_count);
580
581 /*
582 * The SPI frame header does not allow to pass more than 64
583 * bytes.
584 */
585 transaction_size = MIN(transaction_size, 64);
586
587 if (direction == fifo_receive)
588 tpm2_read_reg(TPM_DATA_FIFO_REG,
589 buffer.rx_buffer + handled_so_far,
590 transaction_size);
591 else
592 tpm2_write_reg(TPM_DATA_FIFO_REG,
593 buffer.tx_buffer + handled_so_far,
594 transaction_size);
595
596 handled_so_far += transaction_size;
597
598 } while (handled_so_far != transfer_size);
599}
600
601size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
602 void *tpm2_response, size_t max_response)
603{
604 uint32_t status;
605 uint32_t expected_status_bits;
606 size_t payload_size;
607 size_t bytes_to_go;
608 const uint8_t *cmd_body = tpm2_command;
609 uint8_t *rsp_body = tpm2_response;
610 union fifo_transfer_buffer fifo_buffer;
611 const int HEADER_SIZE = 6;
612
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800613 /* Do not try using an uninitialized TPM. */
Patrick Georgic9b13592019-11-29 11:47:47 +0100614 if (!tpm_info.vendor_id)
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800615 return 0;
616
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700617 /* Skip the two byte tag, read the size field. */
618 payload_size = read_be32(cmd_body + 2);
619
620 /* Sanity check. */
621 if (payload_size != command_size) {
622 printk(BIOS_ERR,
623 "Command size mismatch: encoded %zd != requested %zd\n",
624 payload_size, command_size);
625 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
626 printk(BIOS_DEBUG, "\n");
627 return 0;
628 }
629
630 /* Let the TPM know that the command is coming. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700631 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700632
633 /*
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200634 * TPM commands and responses written to and read from the FIFO
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700635 * register (0x24) are datagrams of variable size, prepended by a 6
636 * byte header.
637 *
638 * The specification description of the state machine is a bit vague,
639 * but from experience it looks like there is no need to wait for the
640 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
641 * Just write the command into FIFO, making sure not to exceed the
642 * burst count or the maximum PDU size, whatever is smaller.
643 */
644 fifo_buffer.tx_buffer = cmd_body;
645 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
646
647 /* Now tell the TPM it can start processing the command. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700648 write_tpm_sts(TPM_STS_GO);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700649
650 /* Now wait for it to report that the response is ready. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700651 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700652 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
653 /*
654 * If timed out, which should never happen, let's at least
655 * print out the offending command.
656 */
657 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
658 printk(BIOS_DEBUG, "\n");
659 return 0;
660 }
661
662 /*
663 * The response is ready, let's read it. First we read the FIFO
664 * payload header, to see how much data to expect. The response header
665 * size is fixed to six bytes, the total payload size is stored in
666 * network order in the last four bytes.
667 */
668 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
669
670 /* Find out the total payload size, skipping the two byte tag. */
671 payload_size = read_be32(rsp_body + 2);
672
673 if (payload_size > max_response) {
674 /*
675 * TODO(vbendeb): at least drain the FIFO here or somehow let
676 * the TPM know that the response can be dropped.
677 */
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200678 printk(BIOS_ERR, " TPM response too long (%zd bytes)",
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700679 payload_size);
680 return 0;
681 }
682
683 /*
684 * Now let's read all but the last byte in the FIFO to make sure the
685 * status register is showing correct flow control bits: 'more data'
686 * until the last byte and then 'no more data' once the last byte is
687 * read.
688 */
689 bytes_to_go = payload_size - 1 - HEADER_SIZE;
690 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
691 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
692
693 /* Verify that there is still data to read. */
694 read_tpm_sts(&status);
695 if ((status & expected_status_bits) != expected_status_bits) {
696 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
697 status);
698 return 0;
699 }
700
701 /* Read the last byte of the PDU. */
702 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
703
704 /* Terminate the dump, if enabled. */
705 if (debug_level_)
706 printk(BIOS_DEBUG, "\n");
707
708 /* Verify that 'data available' is not asseretd any more. */
709 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700710 if ((status & expected_status_bits) != TPM_STS_VALID) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700711 printk(BIOS_ERR, "unexpected final status %#x\n", status);
712 return 0;
713 }
714
715 /* Move the TPM back to idle state. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700716 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700717
718 return payload_size;
719}