blob: 356c0fe3f764199f4039c91d1f5484a0a981e774 [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
Furquan Shaikh260b2972017-04-07 13:26:01 -0700349 /*
Vadim Bendebury8727e642017-11-16 21:00:41 -0800350 * Locality is released by TPM reset.
351 *
352 * If locality is taken at this point, this could be due to the fact
353 * that the TPM is performing a long operation and has not processed
354 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
355 * it releases locality when reset is processed.
Shelley Chen85eb0312017-11-07 14:24:19 -0800356 */
357 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
Vadim Bendebury8727e642017-11-16 21:00:41 -0800358 do {
Shelley Chen85eb0312017-11-07 14:24:19 -0800359 access = tpm2_read_access_reg();
Vadim Bendebury8727e642017-11-16 21:00:41 -0800360 if (access & TPM_ACCESS_ACTIVE_LOCALITY) {
361 /*
362 * Don't bombard the chip with traffic, let it keep
363 * processing the command.
364 */
365 mdelay(2);
366 continue;
367 }
Furquan Shaikh260b2972017-04-07 13:26:01 -0700368
Vadim Bendebury8727e642017-11-16 21:00:41 -0800369 /*
370 * Ok, the locality is free, TPM must be reset, let's claim
371 * it.
372 */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700373
Vadim Bendebury8727e642017-11-16 21:00:41 -0800374 tpm2_write_access_reg(TPM_ACCESS_REQUEST_USE);
375 access = tpm2_read_access_reg();
376 if (access != (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
377 break;
378 }
379
380 printk(BIOS_INFO, "TPM ready after %ld ms\n",
381 stopwatch_duration_msecs(&sw));
382
383 return 1;
384 } while (!stopwatch_expired(&sw));
385
386 printk(BIOS_ERR,
387 "Failed to claim locality 0 after %ld ms, status: %#x\n",
388 stopwatch_duration_msecs(&sw), access);
389
390 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700391}
392
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700393/* Device/vendor ID values of the TPM devices this driver supports. */
394static const uint32_t supported_did_vids[] = {
395 0x00281ae0 /* H1 based Cr50 security chip. */
396};
397
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700398int tpm2_init(struct spi_slave *spi_if)
399{
400 uint32_t did_vid, status;
401 uint8_t cmd;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700402 int retries;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700403 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
404 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700405
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700406 memcpy(spi_slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700407
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800408 /*
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700409 * 150 ms should be enough to synchronize with the TPM even under the
410 * worst nested reset request conditions. In vast majority of cases
411 * there would be no wait at all.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800412 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700413 printk(BIOS_INFO, "Probing TPM: ");
414 for (retries = 15; retries > 0; retries--) {
415 int i;
416
417 /* In case of falure to read div_vid is set to zero. */
418 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
419
420 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
421 if (did_vid == supported_did_vids[i])
422 break; /* Tpm is up and ready. */
423
424 if (i < ARRAY_SIZE(supported_did_vids))
425 break;
426
427 /* TPM might be resetting, let's retry in a bit. */
428 mdelay(10);
429 printk(BIOS_INFO, ".");
430 }
431
432 if (!retries) {
433 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
434 __func__);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800435 return -1;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700436 }
437
438 printk(BIOS_INFO, " done!\n");
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700439
Vadim Bendebury8727e642017-11-16 21:00:41 -0800440 if (ENV_VERSTAGE || ENV_BOOTBLOCK)
441 /*
442 * Claim locality 0, do it only during the first
443 * initialization after reset.
444 */
445 if (!tpm2_claim_locality())
446 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700447
448 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700449 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700450 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
451 status);
452 return -1;
453 }
454
455 /*
456 * Locality claimed, read the revision value and set up the tpm_info
457 * structure.
458 */
459 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700460 tpm_info->vendor_id = did_vid & 0xffff;
461 tpm_info->device_id = did_vid >> 16;
462 tpm_info->revision = cmd;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700463
464 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700465 tpm_info->vendor_id, tpm_info->device_id, tpm_info->revision);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700466
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700467 /* Let's report device FW version if available. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700468 if (tpm_info->vendor_id == 0x1ae0) {
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700469 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700470 size_t chunk_size;
471 /*
472 * let's read 50 bytes at a time; leave room for the trailing
473 * zero.
474 */
475 char vstr[51];
476
477 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700478
479 printk(BIOS_INFO, "Firmware version: ");
480
481 /*
482 * Does not really matter what's written, this just makes sure
483 * the version is reported from the beginning.
484 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700485 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700486
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700487 /* Print it out in sizeof(vstr) - 1 byte chunks. */
488 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700489 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700490 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700491 printk(BIOS_INFO, "%s", vstr);
492
493 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700494 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700495 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700496 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700497 } while (vstr[chunk_size - 1] &&
498 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700499
500 printk(BIOS_INFO, "\n");
501 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700502 return 0;
503}
504
505/*
506 * This is in seconds, certain TPM commands, like key generation, can take
507 * long time to complete.
508 *
509 * Returns one to indicate success, zero (not yet implemented) to indicate
510 * failure.
511 */
512#define MAX_STATUS_TIMEOUT 120
513static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
514{
515 uint32_t status;
516 struct stopwatch sw;
517
518 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
519 do {
520 udelay(1000);
521 if (stopwatch_expired(&sw)) {
522 printk(BIOS_ERR, "failed to get expected status %x\n",
523 status_expected);
524 return false;
525 }
526 read_tpm_sts(&status);
527 } while ((status & status_mask) != status_expected);
528
529 return 1;
530}
531
532enum fifo_transfer_direction {
533 fifo_transmit = 0,
534 fifo_receive = 1
535};
536
537/* Union allows to avoid casting away 'const' on transmit buffers. */
538union fifo_transfer_buffer {
539 uint8_t *rx_buffer;
540 const uint8_t *tx_buffer;
541};
542
543/*
544 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
545 * current burst count value.
546 */
547static void fifo_transfer(size_t transfer_size,
548 union fifo_transfer_buffer buffer,
549 enum fifo_transfer_direction direction)
550{
551 size_t transaction_size;
552 size_t burst_count;
553 size_t handled_so_far = 0;
554
555 do {
556 do {
557 /* Could be zero when TPM is busy. */
558 burst_count = get_burst_count();
559 } while (!burst_count);
560
561 transaction_size = transfer_size - handled_so_far;
562 transaction_size = MIN(transaction_size, burst_count);
563
564 /*
565 * The SPI frame header does not allow to pass more than 64
566 * bytes.
567 */
568 transaction_size = MIN(transaction_size, 64);
569
570 if (direction == fifo_receive)
571 tpm2_read_reg(TPM_DATA_FIFO_REG,
572 buffer.rx_buffer + handled_so_far,
573 transaction_size);
574 else
575 tpm2_write_reg(TPM_DATA_FIFO_REG,
576 buffer.tx_buffer + handled_so_far,
577 transaction_size);
578
579 handled_so_far += transaction_size;
580
581 } while (handled_so_far != transfer_size);
582}
583
584size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
585 void *tpm2_response, size_t max_response)
586{
587 uint32_t status;
588 uint32_t expected_status_bits;
589 size_t payload_size;
590 size_t bytes_to_go;
591 const uint8_t *cmd_body = tpm2_command;
592 uint8_t *rsp_body = tpm2_response;
593 union fifo_transfer_buffer fifo_buffer;
594 const int HEADER_SIZE = 6;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700595 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700596
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800597 /* Do not try using an uninitialized TPM. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700598 if (!tpm_info->vendor_id)
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800599 return 0;
600
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700601 /* Skip the two byte tag, read the size field. */
602 payload_size = read_be32(cmd_body + 2);
603
604 /* Sanity check. */
605 if (payload_size != command_size) {
606 printk(BIOS_ERR,
607 "Command size mismatch: encoded %zd != requested %zd\n",
608 payload_size, command_size);
609 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
610 printk(BIOS_DEBUG, "\n");
611 return 0;
612 }
613
614 /* Let the TPM know that the command is coming. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700615 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700616
617 /*
618 * Tpm commands and responses written to and read from the FIFO
619 * register (0x24) are datagrams of variable size, prepended by a 6
620 * byte header.
621 *
622 * The specification description of the state machine is a bit vague,
623 * but from experience it looks like there is no need to wait for the
624 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
625 * Just write the command into FIFO, making sure not to exceed the
626 * burst count or the maximum PDU size, whatever is smaller.
627 */
628 fifo_buffer.tx_buffer = cmd_body;
629 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
630
631 /* Now tell the TPM it can start processing the command. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700632 write_tpm_sts(TPM_STS_GO);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700633
634 /* Now wait for it to report that the response is ready. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700635 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700636 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
637 /*
638 * If timed out, which should never happen, let's at least
639 * print out the offending command.
640 */
641 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
642 printk(BIOS_DEBUG, "\n");
643 return 0;
644 }
645
646 /*
647 * The response is ready, let's read it. First we read the FIFO
648 * payload header, to see how much data to expect. The response header
649 * size is fixed to six bytes, the total payload size is stored in
650 * network order in the last four bytes.
651 */
652 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
653
654 /* Find out the total payload size, skipping the two byte tag. */
655 payload_size = read_be32(rsp_body + 2);
656
657 if (payload_size > max_response) {
658 /*
659 * TODO(vbendeb): at least drain the FIFO here or somehow let
660 * the TPM know that the response can be dropped.
661 */
662 printk(BIOS_ERR, " tpm response too long (%zd bytes)",
663 payload_size);
664 return 0;
665 }
666
667 /*
668 * Now let's read all but the last byte in the FIFO to make sure the
669 * status register is showing correct flow control bits: 'more data'
670 * until the last byte and then 'no more data' once the last byte is
671 * read.
672 */
673 bytes_to_go = payload_size - 1 - HEADER_SIZE;
674 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
675 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
676
677 /* Verify that there is still data to read. */
678 read_tpm_sts(&status);
679 if ((status & expected_status_bits) != expected_status_bits) {
680 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
681 status);
682 return 0;
683 }
684
685 /* Read the last byte of the PDU. */
686 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
687
688 /* Terminate the dump, if enabled. */
689 if (debug_level_)
690 printk(BIOS_DEBUG, "\n");
691
692 /* Verify that 'data available' is not asseretd any more. */
693 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700694 if ((status & expected_status_bits) != TPM_STS_VALID) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700695 printk(BIOS_ERR, "unexpected final status %#x\n", status);
696 return 0;
697 }
698
699 /* Move the TPM back to idle state. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700700 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700701
702 return payload_size;
703}