blob: 58bf84226723e826149aabc313956288a2d1eebb [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
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070040/* SPI slave structure for TPM device. */
41static struct spi_slave g_spi_slave CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070042
43/* Cached TPM device identification. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070044static struct tpm2_info g_tpm_info CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070045
46/*
47 * TODO(vbendeb): make CONFIG_DEBUG_TPM an int to allow different level of
48 * debug traces. Right now it is either 0 or 1.
49 */
50static const int debug_level_ = CONFIG_DEBUG_TPM;
51
Vadim Bendeburye31d2432016-04-09 18:33:49 -070052/*
53 * SPI frame header for TPM transactions is 4 bytes in size, it is described
54 * in section "6.4.6 Spi Bit Protocol".
55 */
56typedef struct {
57 unsigned char body[4];
58} spi_frame_header;
59
60void tpm2_get_info(struct tpm2_info *info)
61{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070062 *info = car_get_var(g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -070063}
64
Jeffy Chen19e3d332017-03-03 18:24:02 +080065__attribute__((weak)) int tis_plat_irq_status(void)
66{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070067 static int warning_displayed CAR_GLOBAL;
Jeffy Chen19e3d332017-03-03 18:24:02 +080068
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070069 if (!car_get_var(warning_displayed)) {
Jeffy Chen19e3d332017-03-03 18:24:02 +080070 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 10ms to wait on Cr50!\n");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070071 car_set_var(warning_displayed, 1);
Jeffy Chen19e3d332017-03-03 18:24:02 +080072 }
73 mdelay(10);
74
75 return 1;
76}
77
78/*
79 * TPM may trigger a irq after finish processing previous transfer.
80 * Waiting for this irq to sync tpm status.
81 *
82 * Returns 1 on success, 0 on failure (timeout).
83 */
84static int tpm_sync(void)
85{
86 struct stopwatch sw;
87
Furquan Shaikh260b2972017-04-07 13:26:01 -070088 stopwatch_init_msecs_expire(&sw, 10);
Jeffy Chen19e3d332017-03-03 18:24:02 +080089 while (!tis_plat_irq_status()) {
90 if (stopwatch_expired(&sw)) {
91 printk(BIOS_ERR, "Timeout wait for tpm irq!\n");
92 return 0;
93 }
94 }
95 return 1;
96}
97
Vadim Bendeburye31d2432016-04-09 18:33:49 -070098/*
99 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
100 * header is sent to the TPM, the master waits til TPM is ready to continue.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800101 *
102 * Returns 1 on success, 0 on failure (TPM SPI flow control timeout.)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700103 */
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800104static int start_transaction(int read_write, size_t bytes, unsigned addr)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700105{
106 spi_frame_header header;
107 uint8_t byte;
108 int i;
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800109 struct stopwatch sw;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700110 static int tpm_sync_needed CAR_GLOBAL;
111 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700112
Jeffy Chen19e3d332017-03-03 18:24:02 +0800113 /* Wait for tpm to finish previous transaction if needed */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700114 if (car_get_var(tpm_sync_needed))
Jeffy Chen19e3d332017-03-03 18:24:02 +0800115 tpm_sync();
116 else
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700117 car_set_var(tpm_sync_needed, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700118
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800119 /* Try to wake cr50 if it is asleep. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700120 spi_claim_bus(spi_slave);
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800121 udelay(1);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700122 spi_release_bus(spi_slave);
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800123 udelay(100);
124
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700125 /*
126 * The first byte of the frame header encodes the transaction type
127 * (read or write) and transfer size (set to lentgh - 1), limited to
128 * 64 bytes.
129 */
130 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
131
132 /* The rest of the frame header is the TPM register address. */
133 for (i = 0; i < 3; i++)
134 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
135
136 /* CS assert wakes up the slave. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700137 spi_claim_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700138
139 /*
140 * The TCG TPM over SPI specification introduces the notion of SPI
141 * flow control (Section "6.4.5 Flow Control").
142 *
143 * Again, the slave (TPM device) expects each transaction to start
144 * with a 4 byte header trasmitted by master. The header indicates if
145 * the master needs to read or write a register, and the register
146 * address.
147 *
148 * If the slave needs to stall the transaction (for instance it is not
149 * ready to send the register value to the master), it sets the MOSI
150 * line to 0 during the last clock of the 4 byte header. In this case
151 * the master is supposed to start polling the SPI bus, one byte at
152 * time, until the last bit in the received byte (transferred during
153 * the last clock of the byte) is set to 1.
154 *
155 * Due to some SPI controllers' shortcomings (Rockchip comes to
156 * mind...) we trasmit the 4 byte header without checking the byte
157 * transmitted by the TPM during the transaction's last byte.
158 *
159 * We know that cr50 is guaranteed to set the flow control bit to 0
160 * during the header transfer, but real TPM2 might be fast enough not
161 * to require to stall the master, this would present an issue.
162 * crosbug.com/p/52132 has been opened to track this.
163 */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700164 spi_xfer(spi_slave, header.body, sizeof(header.body), NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700165
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800166 /*
167 * Now poll the bus until TPM removes the stall bit. Give it up to 100
168 * ms to sort it out - it could be saving stuff in nvram at some
169 * point.
170 */
171 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700172 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800173 if (stopwatch_expired(&sw)) {
174 printk(BIOS_ERR, "TPM flow control failure\n");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700175 spi_release_bus(spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800176 return 0;
177 }
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700178 spi_xfer(spi_slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700179 } while (!(byte & 1));
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800180 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700181}
182
183/*
184 * Print out the contents of a buffer, if debug is enabled. Skip registers
185 * other than FIFO, unless debug_level_ is 2.
186 */
187static void trace_dump(const char *prefix, uint32_t reg,
188 size_t bytes, const uint8_t *buffer,
189 int force)
190{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700191 static char prev_prefix CAR_GLOBAL;
192 static unsigned prev_reg CAR_GLOBAL;
193 static int current_char CAR_GLOBAL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700194 const int BYTES_PER_LINE = 32;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700195 int *current_char_ptr = car_get_var_ptr(&current_char);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700196
197 if (!force) {
198 if (!debug_level_)
199 return;
200
201 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
202 return;
203 }
204
205 /*
206 * Do not print register address again if the last dump print was for
207 * that register.
208 */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700209 if ((car_get_var(prev_prefix) != *prefix) ||
210 (car_get_var(prev_reg) != reg)) {
211 car_set_var(prev_prefix, *prefix);
212 car_set_var(prev_reg, reg);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700213 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700214 *current_char_ptr = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700215 }
216
217 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
218 /*
219 * This must be a regular register address, print the 32 bit
220 * value.
221 */
222 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
223 } else {
224 int i;
225
226 /*
227 * Data read from or written to FIFO or not in 4 byte
228 * quantiites is printed byte at a time.
229 */
230 for (i = 0; i < bytes; i++) {
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700231 if (*current_char_ptr &&
232 !(*current_char_ptr % BYTES_PER_LINE)) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700233 printk(BIOS_DEBUG, "\n ");
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700234 *current_char_ptr = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700235 }
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700236 (*current_char_ptr)++;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700237 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
238 }
239 }
240}
241
242/*
243 * Once transaction is initiated and the TPM indicated that it is ready to go,
244 * write the actual bytes to the register.
245 */
246static void write_bytes(const void *buffer, size_t bytes)
247{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700248 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
249 spi_xfer(spi_slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700250}
251
252/*
253 * Once transaction is initiated and the TPM indicated that it is ready to go,
254 * read the actual bytes from the register.
255 */
256static void read_bytes(void *buffer, size_t bytes)
257{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700258 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
259 spi_xfer(spi_slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700260}
261
262/*
263 * To write a register, start transaction, transfer data to the TPM, deassert
264 * CS when done.
265 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800266 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700267 */
268static int tpm2_write_reg(unsigned reg_number, const void *buffer, size_t bytes)
269{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700270 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700271 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800272 if (!start_transaction(false, bytes, reg_number))
273 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700274 write_bytes(buffer, bytes);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700275 spi_release_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700276 return 1;
277}
278
279/*
280 * To read a register, start transaction, transfer data from the TPM, deassert
281 * CS when done.
282 *
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700283 * Returns one to indicate success, zero to indicate failure. In case of
284 * failure zero out the user buffer.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700285 */
286static int tpm2_read_reg(unsigned reg_number, void *buffer, size_t bytes)
287{
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700288 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800289 if (!start_transaction(true, bytes, reg_number)) {
290 memset(buffer, 0, bytes);
291 return 0;
292 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700293 read_bytes(buffer, bytes);
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700294 spi_release_bus(spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700295 trace_dump("R", reg_number, bytes, buffer, 0);
296 return 1;
297}
298
299/*
300 * Status register is accessed often, wrap reading and writing it into
301 * dedicated functions.
302 */
303static int read_tpm_sts(uint32_t *status)
304{
305 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
306}
307
308static int write_tpm_sts(uint32_t status)
309{
310 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
311}
312
313/*
314 * The TPM may limit the transaction bytes count (burst count) below the 64
315 * bytes max. The current value is available as a field of the status
316 * register.
317 */
318static uint32_t get_burst_count(void)
319{
320 uint32_t status;
321
322 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700323 return (status & TPM_STS_BURST_COUNT_MASK) >> TPM_STS_BURST_COUNT_SHIFT;
324}
325
326static uint8_t tpm2_read_access_reg(void)
327{
328 uint8_t access;
329 tpm2_read_reg(TPM_ACCESS_REG, &access, sizeof(access));
330 /* We do not care about access establishment bit state. Ignore it. */
331 return access & ~TPM_ACCESS_ESTABLISHMENT;
332}
333
334static void tpm2_write_access_reg(uint8_t cmd)
335{
336 /* Writes to access register can set only 1 bit at a time. */
337 assert (!(cmd & (cmd - 1)));
338
339 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
340}
341
342static int tpm2_claim_locality(void)
343{
344 uint8_t access;
345
346 access = tpm2_read_access_reg();
347 /*
348 * If active locality is set (maybe reset line is not connected?),
349 * release the locality and try again.
350 */
351 if (access & TPM_ACCESS_ACTIVE_LOCALITY) {
352 tpm2_write_access_reg(TPM_ACCESS_ACTIVE_LOCALITY);
353 access = tpm2_read_access_reg();
354 }
355
356 if (access != TPM_ACCESS_VALID) {
357 printk(BIOS_ERR, "Invalid reset status: %#x\n", access);
358 return 0;
359 }
360
361 tpm2_write_access_reg(TPM_ACCESS_REQUEST_USE);
362 access = tpm2_read_access_reg();
363 if (access != (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
364 printk(BIOS_ERR, "Failed to claim locality 0, status: %#x\n",
365 access);
366 return 0;
367 }
368
369 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700370}
371
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700372/* Device/vendor ID values of the TPM devices this driver supports. */
373static const uint32_t supported_did_vids[] = {
374 0x00281ae0 /* H1 based Cr50 security chip. */
375};
376
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700377int tpm2_init(struct spi_slave *spi_if)
378{
379 uint32_t did_vid, status;
380 uint8_t cmd;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700381 int retries;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700382 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
383 struct spi_slave *spi_slave = car_get_var_ptr(&g_spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700384
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700385 memcpy(spi_slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700386
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800387 /*
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700388 * 150 ms should be enough to synchronize with the TPM even under the
389 * worst nested reset request conditions. In vast majority of cases
390 * there would be no wait at all.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800391 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700392 printk(BIOS_INFO, "Probing TPM: ");
393 for (retries = 15; retries > 0; retries--) {
394 int i;
395
396 /* In case of falure to read div_vid is set to zero. */
397 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
398
399 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
400 if (did_vid == supported_did_vids[i])
401 break; /* Tpm is up and ready. */
402
403 if (i < ARRAY_SIZE(supported_did_vids))
404 break;
405
406 /* TPM might be resetting, let's retry in a bit. */
407 mdelay(10);
408 printk(BIOS_INFO, ".");
409 }
410
411 if (!retries) {
412 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
413 __func__);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800414 return -1;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700415 }
416
417 printk(BIOS_INFO, " done!\n");
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700418
Furquan Shaikh260b2972017-04-07 13:26:01 -0700419 /* Claim locality 0. */
420 if (!tpm2_claim_locality())
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700421 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700422
423 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700424 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700425 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
426 status);
427 return -1;
428 }
429
430 /*
431 * Locality claimed, read the revision value and set up the tpm_info
432 * structure.
433 */
434 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700435 tpm_info->vendor_id = did_vid & 0xffff;
436 tpm_info->device_id = did_vid >> 16;
437 tpm_info->revision = cmd;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700438
439 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700440 tpm_info->vendor_id, tpm_info->device_id, tpm_info->revision);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700441
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700442 /* Let's report device FW version if available. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700443 if (tpm_info->vendor_id == 0x1ae0) {
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700444 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700445 size_t chunk_size;
446 /*
447 * let's read 50 bytes at a time; leave room for the trailing
448 * zero.
449 */
450 char vstr[51];
451
452 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700453
454 printk(BIOS_INFO, "Firmware version: ");
455
456 /*
457 * Does not really matter what's written, this just makes sure
458 * the version is reported from the beginning.
459 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700460 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700461
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700462 /* Print it out in sizeof(vstr) - 1 byte chunks. */
463 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700464 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700465 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700466 printk(BIOS_INFO, "%s", vstr);
467
468 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700469 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700470 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700471 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700472 } while (vstr[chunk_size - 1] &&
473 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700474
475 printk(BIOS_INFO, "\n");
476 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700477 return 0;
478}
479
480/*
481 * This is in seconds, certain TPM commands, like key generation, can take
482 * long time to complete.
483 *
484 * Returns one to indicate success, zero (not yet implemented) to indicate
485 * failure.
486 */
487#define MAX_STATUS_TIMEOUT 120
488static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
489{
490 uint32_t status;
491 struct stopwatch sw;
492
493 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
494 do {
495 udelay(1000);
496 if (stopwatch_expired(&sw)) {
497 printk(BIOS_ERR, "failed to get expected status %x\n",
498 status_expected);
499 return false;
500 }
501 read_tpm_sts(&status);
502 } while ((status & status_mask) != status_expected);
503
504 return 1;
505}
506
507enum fifo_transfer_direction {
508 fifo_transmit = 0,
509 fifo_receive = 1
510};
511
512/* Union allows to avoid casting away 'const' on transmit buffers. */
513union fifo_transfer_buffer {
514 uint8_t *rx_buffer;
515 const uint8_t *tx_buffer;
516};
517
518/*
519 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
520 * current burst count value.
521 */
522static void fifo_transfer(size_t transfer_size,
523 union fifo_transfer_buffer buffer,
524 enum fifo_transfer_direction direction)
525{
526 size_t transaction_size;
527 size_t burst_count;
528 size_t handled_so_far = 0;
529
530 do {
531 do {
532 /* Could be zero when TPM is busy. */
533 burst_count = get_burst_count();
534 } while (!burst_count);
535
536 transaction_size = transfer_size - handled_so_far;
537 transaction_size = MIN(transaction_size, burst_count);
538
539 /*
540 * The SPI frame header does not allow to pass more than 64
541 * bytes.
542 */
543 transaction_size = MIN(transaction_size, 64);
544
545 if (direction == fifo_receive)
546 tpm2_read_reg(TPM_DATA_FIFO_REG,
547 buffer.rx_buffer + handled_so_far,
548 transaction_size);
549 else
550 tpm2_write_reg(TPM_DATA_FIFO_REG,
551 buffer.tx_buffer + handled_so_far,
552 transaction_size);
553
554 handled_so_far += transaction_size;
555
556 } while (handled_so_far != transfer_size);
557}
558
559size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
560 void *tpm2_response, size_t max_response)
561{
562 uint32_t status;
563 uint32_t expected_status_bits;
564 size_t payload_size;
565 size_t bytes_to_go;
566 const uint8_t *cmd_body = tpm2_command;
567 uint8_t *rsp_body = tpm2_response;
568 union fifo_transfer_buffer fifo_buffer;
569 const int HEADER_SIZE = 6;
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700570 struct tpm2_info *tpm_info = car_get_var_ptr(&g_tpm_info);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700571
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800572 /* Do not try using an uninitialized TPM. */
Furquan Shaikhbdf86a62017-04-03 23:52:01 -0700573 if (!tpm_info->vendor_id)
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800574 return 0;
575
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700576 /* Skip the two byte tag, read the size field. */
577 payload_size = read_be32(cmd_body + 2);
578
579 /* Sanity check. */
580 if (payload_size != command_size) {
581 printk(BIOS_ERR,
582 "Command size mismatch: encoded %zd != requested %zd\n",
583 payload_size, command_size);
584 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
585 printk(BIOS_DEBUG, "\n");
586 return 0;
587 }
588
589 /* Let the TPM know that the command is coming. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700590 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700591
592 /*
593 * Tpm commands and responses written to and read from the FIFO
594 * register (0x24) are datagrams of variable size, prepended by a 6
595 * byte header.
596 *
597 * The specification description of the state machine is a bit vague,
598 * but from experience it looks like there is no need to wait for the
599 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
600 * Just write the command into FIFO, making sure not to exceed the
601 * burst count or the maximum PDU size, whatever is smaller.
602 */
603 fifo_buffer.tx_buffer = cmd_body;
604 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
605
606 /* Now tell the TPM it can start processing the command. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700607 write_tpm_sts(TPM_STS_GO);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700608
609 /* Now wait for it to report that the response is ready. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700610 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700611 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
612 /*
613 * If timed out, which should never happen, let's at least
614 * print out the offending command.
615 */
616 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
617 printk(BIOS_DEBUG, "\n");
618 return 0;
619 }
620
621 /*
622 * The response is ready, let's read it. First we read the FIFO
623 * payload header, to see how much data to expect. The response header
624 * size is fixed to six bytes, the total payload size is stored in
625 * network order in the last four bytes.
626 */
627 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
628
629 /* Find out the total payload size, skipping the two byte tag. */
630 payload_size = read_be32(rsp_body + 2);
631
632 if (payload_size > max_response) {
633 /*
634 * TODO(vbendeb): at least drain the FIFO here or somehow let
635 * the TPM know that the response can be dropped.
636 */
637 printk(BIOS_ERR, " tpm response too long (%zd bytes)",
638 payload_size);
639 return 0;
640 }
641
642 /*
643 * Now let's read all but the last byte in the FIFO to make sure the
644 * status register is showing correct flow control bits: 'more data'
645 * until the last byte and then 'no more data' once the last byte is
646 * read.
647 */
648 bytes_to_go = payload_size - 1 - HEADER_SIZE;
649 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
650 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
651
652 /* Verify that there is still data to read. */
653 read_tpm_sts(&status);
654 if ((status & expected_status_bits) != expected_status_bits) {
655 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
656 status);
657 return 0;
658 }
659
660 /* Read the last byte of the PDU. */
661 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
662
663 /* Terminate the dump, if enabled. */
664 if (debug_level_)
665 printk(BIOS_DEBUG, "\n");
666
667 /* Verify that 'data available' is not asseretd any more. */
668 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700669 if ((status & expected_status_bits) != TPM_STS_VALID) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700670 printk(BIOS_ERR, "unexpected final status %#x\n", status);
671 return 0;
672 }
673
674 /* Move the TPM back to idle state. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700675 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700676
677 return payload_size;
678}