blob: c09462e783c5346dfe89acff8b81db389582118b [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;
113 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700114
Jeffy Chen19e3d332017-03-03 18:24:02 +0800115 /* Wait for tpm to finish previous transaction if needed */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700116 if (car_get_var(tpm_sync_needed))
Jeffy Chen19e3d332017-03-03 18:24:02 +0800117 tpm_sync();
118 else
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700119 car_set_var(tpm_sync_needed, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700120
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800121 /* Try to wake cr50 if it is asleep. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700122 spi_claim_bus(spi_slave);
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800123 udelay(1);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700124 spi_release_bus(spi_slave);
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800125 udelay(100);
126
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700127 /*
128 * The first byte of the frame header encodes the transaction type
129 * (read or write) and transfer size (set to lentgh - 1), limited to
130 * 64 bytes.
131 */
132 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
133
134 /* The rest of the frame header is the TPM register address. */
135 for (i = 0; i < 3; i++)
136 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
137
138 /* CS assert wakes up the slave. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700139 spi_claim_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700140
141 /*
142 * The TCG TPM over SPI specification introduces the notion of SPI
143 * flow control (Section "6.4.5 Flow Control").
144 *
145 * Again, the slave (TPM device) expects each transaction to start
146 * with a 4 byte header trasmitted by master. The header indicates if
147 * the master needs to read or write a register, and the register
148 * address.
149 *
150 * If the slave needs to stall the transaction (for instance it is not
151 * ready to send the register value to the master), it sets the MOSI
152 * line to 0 during the last clock of the 4 byte header. In this case
153 * the master is supposed to start polling the SPI bus, one byte at
154 * time, until the last bit in the received byte (transferred during
155 * the last clock of the byte) is set to 1.
156 *
157 * Due to some SPI controllers' shortcomings (Rockchip comes to
158 * mind...) we trasmit the 4 byte header without checking the byte
159 * transmitted by the TPM during the transaction's last byte.
160 *
161 * We know that cr50 is guaranteed to set the flow control bit to 0
162 * during the header transfer, but real TPM2 might be fast enough not
163 * to require to stall the master, this would present an issue.
164 * crosbug.com/p/52132 has been opened to track this.
165 */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700166 spi_xfer(spi_slave, header.body, sizeof(header.body), NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700167
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800168 /*
169 * Now poll the bus until TPM removes the stall bit. Give it up to 100
170 * ms to sort it out - it could be saving stuff in nvram at some
171 * point.
172 */
173 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700174 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800175 if (stopwatch_expired(&sw)) {
176 printk(BIOS_ERR, "TPM flow control failure\n");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700177 spi_release_bus(spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800178 return 0;
179 }
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700180 spi_xfer(spi_slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700181 } while (!(byte & 1));
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800182 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700183}
184
185/*
186 * Print out the contents of a buffer, if debug is enabled. Skip registers
187 * other than FIFO, unless debug_level_ is 2.
188 */
189static void trace_dump(const char *prefix, uint32_t reg,
190 size_t bytes, const uint8_t *buffer,
191 int force)
192{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700193 static char prev_prefix CAR_GLOBAL;
194 static unsigned prev_reg CAR_GLOBAL;
195 static int current_char CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700196 const int BYTES_PER_LINE = 32;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700197 int *current_char_ptr = car_get_var_ptr(&current_char);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700198
199 if (!force) {
200 if (!debug_level_)
201 return;
202
203 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
204 return;
205 }
206
207 /*
208 * Do not print register address again if the last dump print was for
209 * that register.
210 */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700211 if ((car_get_var(prev_prefix) != *prefix) ||
212 (car_get_var(prev_reg) != reg)) {
213 car_set_var(prev_prefix, *prefix);
214 car_set_var(prev_reg, reg);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700215 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700216 *current_char_ptr = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700217 }
218
219 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
220 /*
221 * This must be a regular register address, print the 32 bit
222 * value.
223 */
224 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
225 } else {
226 int i;
227
228 /*
229 * Data read from or written to FIFO or not in 4 byte
230 * quantiites is printed byte at a time.
231 */
232 for (i = 0; i < bytes; i++) {
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700233 if (*current_char_ptr &&
234 !(*current_char_ptr % BYTES_PER_LINE)) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700235 printk(BIOS_DEBUG, "\n ");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700236 *current_char_ptr = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700237 }
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700238 (*current_char_ptr)++;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700239 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
240 }
241 }
242}
243
244/*
245 * Once transaction is initiated and the TPM indicated that it is ready to go,
246 * write the actual bytes to the register.
247 */
248static void write_bytes(const void *buffer, size_t bytes)
249{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700250 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
251 spi_xfer(spi_slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700252}
253
254/*
255 * Once transaction is initiated and the TPM indicated that it is ready to go,
256 * read the actual bytes from the register.
257 */
258static void read_bytes(void *buffer, size_t bytes)
259{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700260 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
261 spi_xfer(spi_slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700262}
263
264/*
265 * To write a register, start transaction, transfer data to the TPM, deassert
266 * CS when done.
267 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800268 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700269 */
270static int tpm2_write_reg(unsigned reg_number, const void *buffer, size_t bytes)
271{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700272 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700273 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800274 if (!start_transaction(false, bytes, reg_number))
275 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700276 write_bytes(buffer, bytes);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700277 spi_release_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700278 return 1;
279}
280
281/*
282 * To read a register, start transaction, transfer data from the TPM, deassert
283 * CS when done.
284 *
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700285 * Returns one to indicate success, zero to indicate failure. In case of
286 * failure zero out the user buffer.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700287 */
288static int tpm2_read_reg(unsigned reg_number, void *buffer, size_t bytes)
289{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700290 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800291 if (!start_transaction(true, bytes, reg_number)) {
292 memset(buffer, 0, bytes);
293 return 0;
294 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700295 read_bytes(buffer, bytes);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700296 spi_release_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700297 trace_dump("R", reg_number, bytes, buffer, 0);
298 return 1;
299}
300
301/*
302 * Status register is accessed often, wrap reading and writing it into
303 * dedicated functions.
304 */
305static int read_tpm_sts(uint32_t *status)
306{
307 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
308}
309
310static int write_tpm_sts(uint32_t status)
311{
312 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
313}
314
315/*
316 * The TPM may limit the transaction bytes count (burst count) below the 64
317 * bytes max. The current value is available as a field of the status
318 * register.
319 */
320static uint32_t get_burst_count(void)
321{
322 uint32_t status;
323
324 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700325 return (status & TPM_STS_BURST_COUNT_MASK) >> TPM_STS_BURST_COUNT_SHIFT;
326}
327
328static uint8_t tpm2_read_access_reg(void)
329{
330 uint8_t access;
331 tpm2_read_reg(TPM_ACCESS_REG, &access, sizeof(access));
332 /* We do not care about access establishment bit state. Ignore it. */
333 return access & ~TPM_ACCESS_ESTABLISHMENT;
334}
335
336static void tpm2_write_access_reg(uint8_t cmd)
337{
338 /* Writes to access register can set only 1 bit at a time. */
339 assert (!(cmd & (cmd - 1)));
340
341 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
342}
343
344static int tpm2_claim_locality(void)
345{
346 uint8_t access;
Shelley Chen85eb0312017-11-07 14:24:19 -0800347 struct stopwatch sw;
Furquan Shaikh260b2972017-04-07 13:26:01 -0700348
349 access = tpm2_read_access_reg();
350 /*
351 * If active locality is set (maybe reset line is not connected?),
352 * release the locality and try again.
353 */
354 if (access & TPM_ACCESS_ACTIVE_LOCALITY) {
355 tpm2_write_access_reg(TPM_ACCESS_ACTIVE_LOCALITY);
356 access = tpm2_read_access_reg();
357 }
358
Shelley Chen85eb0312017-11-07 14:24:19 -0800359 /*
360 * If cr50 is doing a long crypto operation, it can take up to
361 * 30 seconds to get a valid status value back
362 */
363 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
364 while (!stopwatch_expired(&sw) && access != TPM_ACCESS_VALID)
365 access = tpm2_read_access_reg();
Furquan Shaikh260b2972017-04-07 13:26:01 -0700366 if (access != TPM_ACCESS_VALID) {
367 printk(BIOS_ERR, "Invalid reset status: %#x\n", access);
368 return 0;
369 }
370
371 tpm2_write_access_reg(TPM_ACCESS_REQUEST_USE);
372 access = tpm2_read_access_reg();
373 if (access != (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
374 printk(BIOS_ERR, "Failed to claim locality 0, status: %#x\n",
375 access);
376 return 0;
377 }
378
379 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700380}
381
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700382/* Device/vendor ID values of the TPM devices this driver supports. */
383static const uint32_t supported_did_vids[] = {
384 0x00281ae0 /* H1 based Cr50 security chip. */
385};
386
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700387int tpm2_init(struct spi_slave *spi_if)
388{
389 uint32_t did_vid, status;
390 uint8_t cmd;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700391 int retries;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700392 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
393 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700394
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700395 memcpy(spi_slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700396
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800397 /*
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700398 * 150 ms should be enough to synchronize with the TPM even under the
399 * worst nested reset request conditions. In vast majority of cases
400 * there would be no wait at all.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800401 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700402 printk(BIOS_INFO, "Probing TPM: ");
403 for (retries = 15; retries > 0; retries--) {
404 int i;
405
406 /* In case of falure to read div_vid is set to zero. */
407 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
408
409 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
410 if (did_vid == supported_did_vids[i])
411 break; /* Tpm is up and ready. */
412
413 if (i < ARRAY_SIZE(supported_did_vids))
414 break;
415
416 /* TPM might be resetting, let's retry in a bit. */
417 mdelay(10);
418 printk(BIOS_INFO, ".");
419 }
420
421 if (!retries) {
422 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
423 __func__);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800424 return -1;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700425 }
426
427 printk(BIOS_INFO, " done!\n");
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700428
Furquan Shaikh260b2972017-04-07 13:26:01 -0700429 /* Claim locality 0. */
430 if (!tpm2_claim_locality())
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700431 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700432
433 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700434 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700435 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
436 status);
437 return -1;
438 }
439
440 /*
441 * Locality claimed, read the revision value and set up the tpm_info
442 * structure.
443 */
444 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700445 tpm_info->vendor_id = did_vid & 0xffff;
446 tpm_info->device_id = did_vid >> 16;
447 tpm_info->revision = cmd;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700448
449 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700450 tpm_info->vendor_id, tpm_info->device_id, tpm_info->revision);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700451
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700452 /* Let's report device FW version if available. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700453 if (tpm_info->vendor_id == 0x1ae0) {
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700454 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700455 size_t chunk_size;
456 /*
457 * let's read 50 bytes at a time; leave room for the trailing
458 * zero.
459 */
460 char vstr[51];
461
462 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700463
464 printk(BIOS_INFO, "Firmware version: ");
465
466 /*
467 * Does not really matter what's written, this just makes sure
468 * the version is reported from the beginning.
469 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700470 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700471
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700472 /* Print it out in sizeof(vstr) - 1 byte chunks. */
473 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700474 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700475 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700476 printk(BIOS_INFO, "%s", vstr);
477
478 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700479 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700480 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700481 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700482 } while (vstr[chunk_size - 1] &&
483 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700484
485 printk(BIOS_INFO, "\n");
486 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700487 return 0;
488}
489
490/*
491 * This is in seconds, certain TPM commands, like key generation, can take
492 * long time to complete.
493 *
494 * Returns one to indicate success, zero (not yet implemented) to indicate
495 * failure.
496 */
497#define MAX_STATUS_TIMEOUT 120
498static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
499{
500 uint32_t status;
501 struct stopwatch sw;
502
503 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
504 do {
505 udelay(1000);
506 if (stopwatch_expired(&sw)) {
507 printk(BIOS_ERR, "failed to get expected status %x\n",
508 status_expected);
509 return false;
510 }
511 read_tpm_sts(&status);
512 } while ((status & status_mask) != status_expected);
513
514 return 1;
515}
516
517enum fifo_transfer_direction {
518 fifo_transmit = 0,
519 fifo_receive = 1
520};
521
522/* Union allows to avoid casting away 'const' on transmit buffers. */
523union fifo_transfer_buffer {
524 uint8_t *rx_buffer;
525 const uint8_t *tx_buffer;
526};
527
528/*
529 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
530 * current burst count value.
531 */
532static void fifo_transfer(size_t transfer_size,
533 union fifo_transfer_buffer buffer,
534 enum fifo_transfer_direction direction)
535{
536 size_t transaction_size;
537 size_t burst_count;
538 size_t handled_so_far = 0;
539
540 do {
541 do {
542 /* Could be zero when TPM is busy. */
543 burst_count = get_burst_count();
544 } while (!burst_count);
545
546 transaction_size = transfer_size - handled_so_far;
547 transaction_size = MIN(transaction_size, burst_count);
548
549 /*
550 * The SPI frame header does not allow to pass more than 64
551 * bytes.
552 */
553 transaction_size = MIN(transaction_size, 64);
554
555 if (direction == fifo_receive)
556 tpm2_read_reg(TPM_DATA_FIFO_REG,
557 buffer.rx_buffer + handled_so_far,
558 transaction_size);
559 else
560 tpm2_write_reg(TPM_DATA_FIFO_REG,
561 buffer.tx_buffer + handled_so_far,
562 transaction_size);
563
564 handled_so_far += transaction_size;
565
566 } while (handled_so_far != transfer_size);
567}
568
569size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
570 void *tpm2_response, size_t max_response)
571{
572 uint32_t status;
573 uint32_t expected_status_bits;
574 size_t payload_size;
575 size_t bytes_to_go;
576 const uint8_t *cmd_body = tpm2_command;
577 uint8_t *rsp_body = tpm2_response;
578 union fifo_transfer_buffer fifo_buffer;
579 const int HEADER_SIZE = 6;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700580 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700581
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800582 /* Do not try using an uninitialized TPM. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700583 if (!tpm_info->vendor_id)
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800584 return 0;
585
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700586 /* Skip the two byte tag, read the size field. */
587 payload_size = read_be32(cmd_body + 2);
588
589 /* Sanity check. */
590 if (payload_size != command_size) {
591 printk(BIOS_ERR,
592 "Command size mismatch: encoded %zd != requested %zd\n",
593 payload_size, command_size);
594 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
595 printk(BIOS_DEBUG, "\n");
596 return 0;
597 }
598
599 /* Let the TPM know that the command is coming. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700600 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700601
602 /*
603 * Tpm commands and responses written to and read from the FIFO
604 * register (0x24) are datagrams of variable size, prepended by a 6
605 * byte header.
606 *
607 * The specification description of the state machine is a bit vague,
608 * but from experience it looks like there is no need to wait for the
609 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
610 * Just write the command into FIFO, making sure not to exceed the
611 * burst count or the maximum PDU size, whatever is smaller.
612 */
613 fifo_buffer.tx_buffer = cmd_body;
614 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
615
616 /* Now tell the TPM it can start processing the command. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700617 write_tpm_sts(TPM_STS_GO);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700618
619 /* Now wait for it to report that the response is ready. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700620 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700621 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
622 /*
623 * If timed out, which should never happen, let's at least
624 * print out the offending command.
625 */
626 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
627 printk(BIOS_DEBUG, "\n");
628 return 0;
629 }
630
631 /*
632 * The response is ready, let's read it. First we read the FIFO
633 * payload header, to see how much data to expect. The response header
634 * size is fixed to six bytes, the total payload size is stored in
635 * network order in the last four bytes.
636 */
637 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
638
639 /* Find out the total payload size, skipping the two byte tag. */
640 payload_size = read_be32(rsp_body + 2);
641
642 if (payload_size > max_response) {
643 /*
644 * TODO(vbendeb): at least drain the FIFO here or somehow let
645 * the TPM know that the response can be dropped.
646 */
647 printk(BIOS_ERR, " tpm response too long (%zd bytes)",
648 payload_size);
649 return 0;
650 }
651
652 /*
653 * Now let's read all but the last byte in the FIFO to make sure the
654 * status register is showing correct flow control bits: 'more data'
655 * until the last byte and then 'no more data' once the last byte is
656 * read.
657 */
658 bytes_to_go = payload_size - 1 - HEADER_SIZE;
659 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
660 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
661
662 /* Verify that there is still data to read. */
663 read_tpm_sts(&status);
664 if ((status & expected_status_bits) != expected_status_bits) {
665 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
666 status);
667 return 0;
668 }
669
670 /* Read the last byte of the PDU. */
671 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
672
673 /* Terminate the dump, if enabled. */
674 if (debug_level_)
675 printk(BIOS_DEBUG, "\n");
676
677 /* Verify that 'data available' is not asseretd any more. */
678 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700679 if ((status & expected_status_bits) != TPM_STS_VALID) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700680 printk(BIOS_ERR, "unexpected final status %#x\n", status);
681 return 0;
682 }
683
684 /* Move the TPM back to idle state. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700685 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700686
687 return payload_size;
688}