]> Git Repo - qemu.git/blame - hw/tpm/tpm_tis.c
qemu-common: Move qemu_isalnum() etc. to qemu/ctype.h
[qemu.git] / hw / tpm / tpm_tis.c
CommitLineData
edff8678
SB
1/*
2 * tpm_tis.c - QEMU's TPM TIS interface emulator
3 *
4 * Copyright (C) 2006,2010-2013 IBM Corporation
5 *
6 * Authors:
7 * Stefan Berger <[email protected]>
8 * David Safford <[email protected]>
9 *
10 * Xen 4 support: Andrease Niederl <[email protected]>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
14 *
15 * Implementation of the TIS interface according to specs found at
16 * http://www.trustedcomputinggroup.org. This implementation currently
9dd5c40d 17 * supports version 1.3, 21 March 2013
edff8678
SB
18 * In the developers menu choose the PC Client section then find the TIS
19 * specification.
116694c3
SB
20 *
21 * TPM TIS for TPM 2 implementation following TCG PC Client Platform
22 * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
edff8678
SB
23 */
24
0430891c 25#include "qemu/osdep.h"
732cd587 26#include "hw/isa/isa.h"
da34e65c 27#include "qapi/error.h"
023299d8 28
732cd587 29#include "hw/acpi/tpm.h"
023299d8
MAL
30#include "hw/pci/pci_ids.h"
31#include "sysemu/tpm_backend.h"
32#include "tpm_int.h"
5cf954d0 33#include "tpm_util.h"
3b97c01e 34#include "tpm_ppi.h"
fcbed221 35#include "trace.h"
732cd587
MAL
36
37#define TPM_TIS_NUM_LOCALITIES 5 /* per spec */
38#define TPM_TIS_LOCALITY_SHIFT 12
39#define TPM_TIS_NO_LOCALITY 0xff
40
41#define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES)
42
43#define TPM_TIS_BUFFER_MAX 4096
44
45typedef enum {
46 TPM_TIS_STATE_IDLE = 0,
47 TPM_TIS_STATE_READY,
48 TPM_TIS_STATE_COMPLETION,
49 TPM_TIS_STATE_EXECUTION,
50 TPM_TIS_STATE_RECEPTION,
51} TPMTISState;
52
732cd587
MAL
53/* locality data -- all fields are persisted */
54typedef struct TPMLocality {
55 TPMTISState state;
56 uint8_t access;
57 uint32_t sts;
58 uint32_t iface_id;
59 uint32_t inte;
60 uint32_t ints;
732cd587
MAL
61} TPMLocality;
62
36e86589 63typedef struct TPMState {
3d4960c7
MAL
64 ISADevice busdev;
65 MemoryRegion mmio;
66
c5496b97 67 unsigned char buffer[TPM_TIS_BUFFER_MAX];
f999d81b 68 uint16_t rw_offset;
732cd587
MAL
69
70 uint8_t active_locty;
71 uint8_t aborting_locty;
72 uint8_t next_locty;
73
74 TPMLocality loc[TPM_TIS_NUM_LOCALITIES];
75
76 qemu_irq irq;
77 uint32_t irq_num;
732cd587 78
732cd587
MAL
79 TPMBackendCmd cmd;
80
732cd587
MAL
81 TPMBackend *be_driver;
82 TPMVersion be_tpm_version;
b21e6aaf
SB
83
84 size_t be_buffer_size;
b6148757
MAL
85
86 bool ppi_enabled;
3b97c01e 87 TPMPPI ppi;
36e86589 88} TPMState;
732cd587
MAL
89
90#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
edff8678 91
4d1ba9c4 92#define DEBUG_TIS 0
edff8678 93
8db7c415
SB
94/* local prototypes */
95
96static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
97 unsigned size);
98
edff8678
SB
99/* utility functions */
100
101static uint8_t tpm_tis_locality_from_addr(hwaddr addr)
102{
103 return (uint8_t)((addr >> TPM_TIS_LOCALITY_SHIFT) & 0x7);
104}
105
e6b703f6
SB
106static void tpm_tis_show_buffer(const unsigned char *buffer,
107 size_t buffer_size, const char *string)
edff8678 108{
cd38cc51
LM
109 size_t len, i;
110 char *line_buffer, *p;
edff8678 111
e6b703f6 112 len = MIN(tpm_cmd_get_size(buffer), buffer_size);
cd38cc51
LM
113
114 /*
115 * allocate enough room for 3 chars per buffer entry plus a
116 * newline after every 16 chars and a final null terminator.
117 */
118 line_buffer = g_malloc(len * 3 + (len / 16) + 1);
119
120 for (i = 0, p = line_buffer; i < len; i++) {
edff8678 121 if (i && !(i % 16)) {
cd38cc51 122 p += sprintf(p, "\n");
edff8678 123 }
cd38cc51 124 p += sprintf(p, "%.2X ", buffer[i]);
edff8678 125 }
cd38cc51
LM
126 trace_tpm_tis_show_buffer(string, len, line_buffer);
127
128 g_free(line_buffer);
edff8678
SB
129}
130
fd859081
SB
131/*
132 * Set the given flags in the STS register by clearing the register but
116694c3
SB
133 * preserving the SELFTEST_DONE and TPM_FAMILY_MASK flags and then setting
134 * the new flags.
fd859081
SB
135 *
136 * The SELFTEST_DONE flag is acquired from the backend that determines it by
137 * peeking into TPM commands.
138 *
139 * A VM suspend/resume will preserve the flag by storing it into the VM
140 * device state, but the backend will not remember it when QEMU is started
141 * again. Therefore, we cache the flag here. Once set, it will not be unset
142 * except by a reset.
143 */
144static void tpm_tis_sts_set(TPMLocality *l, uint32_t flags)
145{
116694c3 146 l->sts &= TPM_TIS_STS_SELFTEST_DONE | TPM_TIS_STS_TPM_FAMILY_MASK;
fd859081
SB
147 l->sts |= flags;
148}
149
edff8678
SB
150/*
151 * Send a request to the TPM.
152 */
153static void tpm_tis_tpm_send(TPMState *s, uint8_t locty)
154{
cd38cc51
LM
155 if (trace_event_get_state_backends(TRACE_TPM_TIS_SHOW_BUFFER)) {
156 tpm_tis_show_buffer(s->buffer, s->be_buffer_size, "To TPM");
fcbed221 157 }
edff8678 158
edff8678 159 /*
f999d81b 160 * rw_offset serves as length indicator for length of data;
edff8678
SB
161 * it's reset when the response comes back
162 */
3d4960c7 163 s->loc[locty].state = TPM_TIS_STATE_EXECUTION;
edff8678 164
0e43b7e6
MAL
165 s->cmd = (TPMBackendCmd) {
166 .locty = locty,
c5496b97 167 .in = s->buffer,
f999d81b 168 .in_len = s->rw_offset,
c5496b97 169 .out = s->buffer,
e6b703f6 170 .out_len = s->be_buffer_size,
0e43b7e6
MAL
171 };
172
173 tpm_backend_deliver_request(s->be_driver, &s->cmd);
edff8678
SB
174}
175
176/* raise an interrupt if allowed */
177static void tpm_tis_raise_irq(TPMState *s, uint8_t locty, uint32_t irqmask)
178{
edff8678
SB
179 if (!TPM_TIS_IS_VALID_LOCTY(locty)) {
180 return;
181 }
182
3d4960c7
MAL
183 if ((s->loc[locty].inte & TPM_TIS_INT_ENABLED) &&
184 (s->loc[locty].inte & irqmask)) {
fcbed221 185 trace_tpm_tis_raise_irq(irqmask);
3d4960c7
MAL
186 qemu_irq_raise(s->irq);
187 s->loc[locty].ints |= irqmask;
edff8678
SB
188 }
189}
190
191static uint32_t tpm_tis_check_request_use_except(TPMState *s, uint8_t locty)
192{
193 uint8_t l;
194
195 for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) {
196 if (l == locty) {
197 continue;
198 }
3d4960c7 199 if ((s->loc[l].access & TPM_TIS_ACCESS_REQUEST_USE)) {
edff8678
SB
200 return 1;
201 }
202 }
203
204 return 0;
205}
206
207static void tpm_tis_new_active_locality(TPMState *s, uint8_t new_active_locty)
208{
3d4960c7 209 bool change = (s->active_locty != new_active_locty);
edff8678
SB
210 bool is_seize;
211 uint8_t mask;
212
3d4960c7 213 if (change && TPM_TIS_IS_VALID_LOCTY(s->active_locty)) {
edff8678 214 is_seize = TPM_TIS_IS_VALID_LOCTY(new_active_locty) &&
3d4960c7 215 s->loc[new_active_locty].access & TPM_TIS_ACCESS_SEIZE;
edff8678
SB
216
217 if (is_seize) {
218 mask = ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY);
219 } else {
220 mask = ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY|
221 TPM_TIS_ACCESS_REQUEST_USE);
222 }
223 /* reset flags on the old active locality */
3d4960c7 224 s->loc[s->active_locty].access &= mask;
edff8678
SB
225
226 if (is_seize) {
3d4960c7 227 s->loc[s->active_locty].access |= TPM_TIS_ACCESS_BEEN_SEIZED;
edff8678
SB
228 }
229 }
230
3d4960c7 231 s->active_locty = new_active_locty;
edff8678 232
fcbed221 233 trace_tpm_tis_new_active_locality(s->active_locty);
edff8678
SB
234
235 if (TPM_TIS_IS_VALID_LOCTY(new_active_locty)) {
236 /* set flags on the new active locality */
3d4960c7
MAL
237 s->loc[new_active_locty].access |= TPM_TIS_ACCESS_ACTIVE_LOCALITY;
238 s->loc[new_active_locty].access &= ~(TPM_TIS_ACCESS_REQUEST_USE |
edff8678
SB
239 TPM_TIS_ACCESS_SEIZE);
240 }
241
242 if (change) {
3d4960c7 243 tpm_tis_raise_irq(s, s->active_locty, TPM_TIS_INT_LOCALITY_CHANGED);
edff8678
SB
244 }
245}
246
247/* abort -- this function switches the locality */
0f5faee3 248static void tpm_tis_abort(TPMState *s)
edff8678 249{
f999d81b 250 s->rw_offset = 0;
edff8678 251
fcbed221 252 trace_tpm_tis_abort(s->next_locty);
edff8678
SB
253
254 /*
255 * Need to react differently depending on who's aborting now and
256 * which locality will become active afterwards.
257 */
3d4960c7
MAL
258 if (s->aborting_locty == s->next_locty) {
259 s->loc[s->aborting_locty].state = TPM_TIS_STATE_READY;
260 tpm_tis_sts_set(&s->loc[s->aborting_locty],
fd859081 261 TPM_TIS_STS_COMMAND_READY);
3d4960c7 262 tpm_tis_raise_irq(s, s->aborting_locty, TPM_TIS_INT_COMMAND_READY);
edff8678
SB
263 }
264
265 /* locality after abort is another one than the current one */
3d4960c7 266 tpm_tis_new_active_locality(s, s->next_locty);
edff8678 267
3d4960c7 268 s->next_locty = TPM_TIS_NO_LOCALITY;
edff8678 269 /* nobody's aborting a command anymore */
3d4960c7 270 s->aborting_locty = TPM_TIS_NO_LOCALITY;
edff8678
SB
271}
272
273/* prepare aborting current command */
274static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty)
275{
edff8678
SB
276 uint8_t busy_locty;
277
e92b63ea
SB
278 assert(TPM_TIS_IS_VALID_LOCTY(newlocty));
279
280 s->aborting_locty = locty; /* may also be TPM_TIS_NO_LOCALITY */
3d4960c7 281 s->next_locty = newlocty; /* locality after successful abort */
edff8678
SB
282
283 /*
284 * only abort a command using an interrupt if currently executing
285 * a command AND if there's a valid connection to the vTPM.
286 */
287 for (busy_locty = 0; busy_locty < TPM_TIS_NUM_LOCALITIES; busy_locty++) {
3d4960c7 288 if (s->loc[busy_locty].state == TPM_TIS_STATE_EXECUTION) {
edff8678
SB
289 /*
290 * request the backend to cancel. Some backends may not
291 * support it
292 */
8f0605cc 293 tpm_backend_cancel_cmd(s->be_driver);
edff8678
SB
294 return;
295 }
296 }
297
0f5faee3 298 tpm_tis_abort(s);
edff8678
SB
299}
300
68999059
MAL
301/*
302 * Callback from the TPM to indicate that the response was received.
303 */
6a8a2354 304static void tpm_tis_request_completed(TPMIf *ti, int ret)
edff8678 305{
68999059 306 TPMState *s = TPM(ti);
0e43b7e6 307 uint8_t locty = s->cmd.locty;
68999059
MAL
308 uint8_t l;
309
a639f961
SB
310 assert(TPM_TIS_IS_VALID_LOCTY(locty));
311
68999059
MAL
312 if (s->cmd.selftest_done) {
313 for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) {
6a50bb98 314 s->loc[l].sts |= TPM_TIS_STS_SELFTEST_DONE;
68999059
MAL
315 }
316 }
edff8678 317
6a8a2354 318 /* FIXME: report error if ret != 0 */
3d4960c7 319 tpm_tis_sts_set(&s->loc[locty],
fd859081 320 TPM_TIS_STS_VALID | TPM_TIS_STS_DATA_AVAILABLE);
3d4960c7 321 s->loc[locty].state = TPM_TIS_STATE_COMPLETION;
f999d81b 322 s->rw_offset = 0;
edff8678 323
cd38cc51
LM
324 if (trace_event_get_state_backends(TRACE_TPM_TIS_SHOW_BUFFER)) {
325 tpm_tis_show_buffer(s->buffer, s->be_buffer_size, "From TPM");
fcbed221 326 }
298d8b81 327
3d4960c7 328 if (TPM_TIS_IS_VALID_LOCTY(s->next_locty)) {
0f5faee3 329 tpm_tis_abort(s);
edff8678
SB
330 }
331
edff8678
SB
332 tpm_tis_raise_irq(s, locty,
333 TPM_TIS_INT_DATA_AVAILABLE | TPM_TIS_INT_STS_VALID);
edff8678
SB
334}
335
edff8678
SB
336/*
337 * Read a byte of response data
338 */
339static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty)
340{
edff8678
SB
341 uint32_t ret = TPM_TIS_NO_DATA_BYTE;
342 uint16_t len;
343
3d4960c7 344 if ((s->loc[locty].sts & TPM_TIS_STS_DATA_AVAILABLE)) {
c5496b97 345 len = MIN(tpm_cmd_get_size(&s->buffer),
e6b703f6 346 s->be_buffer_size);
edff8678 347
f999d81b
SB
348 ret = s->buffer[s->rw_offset++];
349 if (s->rw_offset >= len) {
edff8678 350 /* got last byte */
3d4960c7 351 tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID);
edff8678 352 tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID);
edff8678 353 }
fcbed221 354 trace_tpm_tis_data_read(ret, s->rw_offset - 1);
edff8678
SB
355 }
356
357 return ret;
358}
359
8db7c415
SB
360#ifdef DEBUG_TIS
361static void tpm_tis_dump_state(void *opaque, hwaddr addr)
362{
363 static const unsigned regs[] = {
364 TPM_TIS_REG_ACCESS,
365 TPM_TIS_REG_INT_ENABLE,
366 TPM_TIS_REG_INT_VECTOR,
367 TPM_TIS_REG_INT_STATUS,
368 TPM_TIS_REG_INTF_CAPABILITY,
369 TPM_TIS_REG_STS,
370 TPM_TIS_REG_DID_VID,
371 TPM_TIS_REG_RID,
372 0xfff};
373 int idx;
374 uint8_t locty = tpm_tis_locality_from_addr(addr);
375 hwaddr base = addr & ~0xfff;
376 TPMState *s = opaque;
8db7c415 377
fcbed221
SB
378 printf("tpm_tis: active locality : %d\n"
379 "tpm_tis: state of locality %d : %d\n"
380 "tpm_tis: register dump:\n",
381 s->active_locty,
382 locty, s->loc[locty].state);
8db7c415
SB
383
384 for (idx = 0; regs[idx] != 0xfff; idx++) {
fcbed221
SB
385 printf("tpm_tis: 0x%04x : 0x%08x\n", regs[idx],
386 (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4));
8db7c415
SB
387 }
388
fcbed221
SB
389 printf("tpm_tis: r/w offset : %d\n"
390 "tpm_tis: result buffer : ",
391 s->rw_offset);
8db7c415 392 for (idx = 0;
c5496b97 393 idx < MIN(tpm_cmd_get_size(&s->buffer), s->be_buffer_size);
8db7c415 394 idx++) {
fcbed221
SB
395 printf("%c%02x%s",
396 s->rw_offset == idx ? '>' : ' ',
397 s->buffer[idx],
398 ((idx & 0xf) == 0xf) ? "\ntpm_tis: " : "");
8db7c415 399 }
fcbed221 400 printf("\n");
8db7c415
SB
401}
402#endif
403
edff8678
SB
404/*
405 * Read a register of the TIS interface
406 * See specs pages 33-63 for description of the registers
407 */
408static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
409 unsigned size)
410{
411 TPMState *s = opaque;
edff8678
SB
412 uint16_t offset = addr & 0xffc;
413 uint8_t shift = (addr & 0x3) * 8;
414 uint32_t val = 0xffffffff;
415 uint8_t locty = tpm_tis_locality_from_addr(addr);
416 uint32_t avail;
feeb755f 417 uint8_t v;
edff8678 418
8f0605cc 419 if (tpm_backend_had_startup_error(s->be_driver)) {
6cd65969 420 return 0;
edff8678
SB
421 }
422
423 switch (offset) {
424 case TPM_TIS_REG_ACCESS:
425 /* never show the SEIZE flag even though we use it internally */
3d4960c7 426 val = s->loc[locty].access & ~TPM_TIS_ACCESS_SEIZE;
edff8678
SB
427 /* the pending flag is always calculated */
428 if (tpm_tis_check_request_use_except(s, locty)) {
429 val |= TPM_TIS_ACCESS_PENDING_REQUEST;
430 }
8f0605cc 431 val |= !tpm_backend_get_tpm_established_flag(s->be_driver);
edff8678
SB
432 break;
433 case TPM_TIS_REG_INT_ENABLE:
3d4960c7 434 val = s->loc[locty].inte;
edff8678
SB
435 break;
436 case TPM_TIS_REG_INT_VECTOR:
3d4960c7 437 val = s->irq_num;
edff8678
SB
438 break;
439 case TPM_TIS_REG_INT_STATUS:
3d4960c7 440 val = s->loc[locty].ints;
edff8678
SB
441 break;
442 case TPM_TIS_REG_INTF_CAPABILITY:
116694c3
SB
443 switch (s->be_tpm_version) {
444 case TPM_VERSION_UNSPEC:
445 val = 0;
446 break;
447 case TPM_VERSION_1_2:
448 val = TPM_TIS_CAPABILITIES_SUPPORTED1_3;
449 break;
450 case TPM_VERSION_2_0:
451 val = TPM_TIS_CAPABILITIES_SUPPORTED2_0;
452 break;
453 }
edff8678
SB
454 break;
455 case TPM_TIS_REG_STS:
3d4960c7
MAL
456 if (s->active_locty == locty) {
457 if ((s->loc[locty].sts & TPM_TIS_STS_DATA_AVAILABLE)) {
edff8678 458 val = TPM_TIS_BURST_COUNT(
c5496b97 459 MIN(tpm_cmd_get_size(&s->buffer),
e6b703f6 460 s->be_buffer_size)
f999d81b 461 - s->rw_offset) | s->loc[locty].sts;
edff8678 462 } else {
f999d81b 463 avail = s->be_buffer_size - s->rw_offset;
edff8678
SB
464 /*
465 * byte-sized reads should not return 0x00 for 0x100
466 * available bytes.
467 */
468 if (size == 1 && avail > 0xff) {
469 avail = 0xff;
470 }
3d4960c7 471 val = TPM_TIS_BURST_COUNT(avail) | s->loc[locty].sts;
edff8678
SB
472 }
473 }
474 break;
475 case TPM_TIS_REG_DATA_FIFO:
2eae8c75 476 case TPM_TIS_REG_DATA_XFIFO ... TPM_TIS_REG_DATA_XFIFO_END:
3d4960c7 477 if (s->active_locty == locty) {
feeb755f
SB
478 if (size > 4 - (addr & 0x3)) {
479 /* prevent access beyond FIFO */
480 size = 4 - (addr & 0x3);
481 }
482 val = 0;
483 shift = 0;
484 while (size > 0) {
3d4960c7 485 switch (s->loc[locty].state) {
feeb755f
SB
486 case TPM_TIS_STATE_COMPLETION:
487 v = tpm_tis_data_read(s, locty);
488 break;
489 default:
490 v = TPM_TIS_NO_DATA_BYTE;
491 break;
492 }
493 val |= (v << shift);
494 shift += 8;
495 size--;
edff8678 496 }
feeb755f 497 shift = 0; /* no more adjustments */
edff8678
SB
498 }
499 break;
116694c3 500 case TPM_TIS_REG_INTERFACE_ID:
3d4960c7 501 val = s->loc[locty].iface_id;
116694c3 502 break;
edff8678
SB
503 case TPM_TIS_REG_DID_VID:
504 val = (TPM_TIS_TPM_DID << 16) | TPM_TIS_TPM_VID;
505 break;
506 case TPM_TIS_REG_RID:
507 val = TPM_TIS_TPM_RID;
508 break;
8db7c415
SB
509#ifdef DEBUG_TIS
510 case TPM_TIS_REG_DEBUG:
511 tpm_tis_dump_state(opaque, addr);
512 break;
513#endif
edff8678
SB
514 }
515
516 if (shift) {
517 val >>= shift;
518 }
519
fcbed221 520 trace_tpm_tis_mmio_read(size, addr, val);
edff8678
SB
521
522 return val;
523}
524
525/*
526 * Write a value to a register of the TIS interface
527 * See specs pages 33-63 for description of the registers
528 */
ff2bc0c1
MAL
529static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
530 uint64_t val, unsigned size)
edff8678
SB
531{
532 TPMState *s = opaque;
feeb755f
SB
533 uint16_t off = addr & 0xffc;
534 uint8_t shift = (addr & 0x3) * 8;
edff8678
SB
535 uint8_t locty = tpm_tis_locality_from_addr(addr);
536 uint8_t active_locty, l;
537 int c, set_new_locty = 1;
538 uint16_t len;
feeb755f 539 uint32_t mask = (size == 1) ? 0xff : ((size == 2) ? 0xffff : ~0);
edff8678 540
fcbed221 541 trace_tpm_tis_mmio_write(size, addr, val);
edff8678 542
ff2bc0c1 543 if (locty == 4) {
fcbed221 544 trace_tpm_tis_mmio_write_locty4();
edff8678
SB
545 return;
546 }
547
8f0605cc 548 if (tpm_backend_had_startup_error(s->be_driver)) {
edff8678
SB
549 return;
550 }
551
feeb755f
SB
552 val &= mask;
553
554 if (shift) {
555 val <<= shift;
556 mask <<= shift;
557 }
558
559 mask ^= 0xffffffff;
560
edff8678
SB
561 switch (off) {
562 case TPM_TIS_REG_ACCESS:
563
564 if ((val & TPM_TIS_ACCESS_SEIZE)) {
565 val &= ~(TPM_TIS_ACCESS_REQUEST_USE |
566 TPM_TIS_ACCESS_ACTIVE_LOCALITY);
567 }
568
3d4960c7 569 active_locty = s->active_locty;
edff8678
SB
570
571 if ((val & TPM_TIS_ACCESS_ACTIVE_LOCALITY)) {
572 /* give up locality if currently owned */
3d4960c7 573 if (s->active_locty == locty) {
fcbed221 574 trace_tpm_tis_mmio_write_release_locty(locty);
edff8678
SB
575
576 uint8_t newlocty = TPM_TIS_NO_LOCALITY;
577 /* anybody wants the locality ? */
578 for (c = TPM_TIS_NUM_LOCALITIES - 1; c >= 0; c--) {
3d4960c7 579 if ((s->loc[c].access & TPM_TIS_ACCESS_REQUEST_USE)) {
fcbed221 580 trace_tpm_tis_mmio_write_locty_req_use(c);
edff8678
SB
581 newlocty = c;
582 break;
583 }
584 }
fcbed221 585 trace_tpm_tis_mmio_write_next_locty(newlocty);
edff8678
SB
586
587 if (TPM_TIS_IS_VALID_LOCTY(newlocty)) {
588 set_new_locty = 0;
589 tpm_tis_prep_abort(s, locty, newlocty);
590 } else {
591 active_locty = TPM_TIS_NO_LOCALITY;
592 }
593 } else {
594 /* not currently the owner; clear a pending request */
3d4960c7 595 s->loc[locty].access &= ~TPM_TIS_ACCESS_REQUEST_USE;
edff8678
SB
596 }
597 }
598
599 if ((val & TPM_TIS_ACCESS_BEEN_SEIZED)) {
3d4960c7 600 s->loc[locty].access &= ~TPM_TIS_ACCESS_BEEN_SEIZED;
edff8678
SB
601 }
602
603 if ((val & TPM_TIS_ACCESS_SEIZE)) {
604 /*
605 * allow seize if a locality is active and the requesting
606 * locality is higher than the one that's active
607 * OR
608 * allow seize for requesting locality if no locality is
609 * active
610 */
3d4960c7
MAL
611 while ((TPM_TIS_IS_VALID_LOCTY(s->active_locty) &&
612 locty > s->active_locty) ||
613 !TPM_TIS_IS_VALID_LOCTY(s->active_locty)) {
edff8678
SB
614 bool higher_seize = FALSE;
615
616 /* already a pending SEIZE ? */
3d4960c7 617 if ((s->loc[locty].access & TPM_TIS_ACCESS_SEIZE)) {
edff8678
SB
618 break;
619 }
620
621 /* check for ongoing seize by a higher locality */
622 for (l = locty + 1; l < TPM_TIS_NUM_LOCALITIES; l++) {
3d4960c7 623 if ((s->loc[l].access & TPM_TIS_ACCESS_SEIZE)) {
edff8678
SB
624 higher_seize = TRUE;
625 break;
626 }
627 }
628
629 if (higher_seize) {
630 break;
631 }
632
633 /* cancel any seize by a lower locality */
37b55d67 634 for (l = 0; l < locty; l++) {
3d4960c7 635 s->loc[l].access &= ~TPM_TIS_ACCESS_SEIZE;
edff8678
SB
636 }
637
3d4960c7 638 s->loc[locty].access |= TPM_TIS_ACCESS_SEIZE;
fcbed221
SB
639
640 trace_tpm_tis_mmio_write_locty_seized(locty, s->active_locty);
641 trace_tpm_tis_mmio_write_init_abort();
642
edff8678 643 set_new_locty = 0;
3d4960c7 644 tpm_tis_prep_abort(s, s->active_locty, locty);
edff8678
SB
645 break;
646 }
647 }
648
649 if ((val & TPM_TIS_ACCESS_REQUEST_USE)) {
3d4960c7
MAL
650 if (s->active_locty != locty) {
651 if (TPM_TIS_IS_VALID_LOCTY(s->active_locty)) {
652 s->loc[locty].access |= TPM_TIS_ACCESS_REQUEST_USE;
edff8678
SB
653 } else {
654 /* no locality active -> make this one active now */
655 active_locty = locty;
656 }
657 }
658 }
659
660 if (set_new_locty) {
661 tpm_tis_new_active_locality(s, active_locty);
662 }
663
664 break;
665 case TPM_TIS_REG_INT_ENABLE:
3d4960c7 666 if (s->active_locty != locty) {
edff8678
SB
667 break;
668 }
669
3d4960c7
MAL
670 s->loc[locty].inte &= mask;
671 s->loc[locty].inte |= (val & (TPM_TIS_INT_ENABLED |
feeb755f
SB
672 TPM_TIS_INT_POLARITY_MASK |
673 TPM_TIS_INTERRUPTS_SUPPORTED));
edff8678
SB
674 break;
675 case TPM_TIS_REG_INT_VECTOR:
676 /* hard wired -- ignore */
677 break;
678 case TPM_TIS_REG_INT_STATUS:
3d4960c7 679 if (s->active_locty != locty) {
edff8678
SB
680 break;
681 }
682
683 /* clearing of interrupt flags */
684 if (((val & TPM_TIS_INTERRUPTS_SUPPORTED)) &&
3d4960c7
MAL
685 (s->loc[locty].ints & TPM_TIS_INTERRUPTS_SUPPORTED)) {
686 s->loc[locty].ints &= ~val;
687 if (s->loc[locty].ints == 0) {
688 qemu_irq_lower(s->irq);
fcbed221 689 trace_tpm_tis_mmio_write_lowering_irq();
edff8678
SB
690 }
691 }
3d4960c7 692 s->loc[locty].ints &= ~(val & TPM_TIS_INTERRUPTS_SUPPORTED);
edff8678
SB
693 break;
694 case TPM_TIS_REG_STS:
3d4960c7 695 if (s->active_locty != locty) {
edff8678
SB
696 break;
697 }
698
116694c3
SB
699 if (s->be_tpm_version == TPM_VERSION_2_0) {
700 /* some flags that are only supported for TPM 2 */
701 if (val & TPM_TIS_STS_COMMAND_CANCEL) {
3d4960c7 702 if (s->loc[locty].state == TPM_TIS_STATE_EXECUTION) {
116694c3
SB
703 /*
704 * request the backend to cancel. Some backends may not
705 * support it
706 */
707 tpm_backend_cancel_cmd(s->be_driver);
708 }
709 }
710
711 if (val & TPM_TIS_STS_RESET_ESTABLISHMENT_BIT) {
712 if (locty == 3 || locty == 4) {
713 tpm_backend_reset_tpm_established_flag(s->be_driver, locty);
714 }
715 }
716 }
717
edff8678
SB
718 val &= (TPM_TIS_STS_COMMAND_READY | TPM_TIS_STS_TPM_GO |
719 TPM_TIS_STS_RESPONSE_RETRY);
720
721 if (val == TPM_TIS_STS_COMMAND_READY) {
3d4960c7 722 switch (s->loc[locty].state) {
edff8678
SB
723
724 case TPM_TIS_STATE_READY:
f999d81b 725 s->rw_offset = 0;
edff8678
SB
726 break;
727
728 case TPM_TIS_STATE_IDLE:
3d4960c7
MAL
729 tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_COMMAND_READY);
730 s->loc[locty].state = TPM_TIS_STATE_READY;
edff8678
SB
731 tpm_tis_raise_irq(s, locty, TPM_TIS_INT_COMMAND_READY);
732 break;
733
734 case TPM_TIS_STATE_EXECUTION:
735 case TPM_TIS_STATE_RECEPTION:
736 /* abort currently running command */
fcbed221 737 trace_tpm_tis_mmio_write_init_abort();
edff8678
SB
738 tpm_tis_prep_abort(s, locty, locty);
739 break;
740
741 case TPM_TIS_STATE_COMPLETION:
f999d81b 742 s->rw_offset = 0;
edff8678 743 /* shortcut to ready state with C/R set */
3d4960c7
MAL
744 s->loc[locty].state = TPM_TIS_STATE_READY;
745 if (!(s->loc[locty].sts & TPM_TIS_STS_COMMAND_READY)) {
746 tpm_tis_sts_set(&s->loc[locty],
fd859081 747 TPM_TIS_STS_COMMAND_READY);
edff8678
SB
748 tpm_tis_raise_irq(s, locty, TPM_TIS_INT_COMMAND_READY);
749 }
3d4960c7 750 s->loc[locty].sts &= ~(TPM_TIS_STS_DATA_AVAILABLE);
edff8678
SB
751 break;
752
753 }
754 } else if (val == TPM_TIS_STS_TPM_GO) {
3d4960c7 755 switch (s->loc[locty].state) {
edff8678 756 case TPM_TIS_STATE_RECEPTION:
3d4960c7 757 if ((s->loc[locty].sts & TPM_TIS_STS_EXPECT) == 0) {
edff8678
SB
758 tpm_tis_tpm_send(s, locty);
759 }
760 break;
761 default:
762 /* ignore */
763 break;
764 }
765 } else if (val == TPM_TIS_STS_RESPONSE_RETRY) {
3d4960c7 766 switch (s->loc[locty].state) {
edff8678 767 case TPM_TIS_STATE_COMPLETION:
f999d81b 768 s->rw_offset = 0;
3d4960c7 769 tpm_tis_sts_set(&s->loc[locty],
fd859081
SB
770 TPM_TIS_STS_VALID|
771 TPM_TIS_STS_DATA_AVAILABLE);
edff8678
SB
772 break;
773 default:
774 /* ignore */
775 break;
776 }
777 }
778 break;
779 case TPM_TIS_REG_DATA_FIFO:
2eae8c75 780 case TPM_TIS_REG_DATA_XFIFO ... TPM_TIS_REG_DATA_XFIFO_END:
edff8678 781 /* data fifo */
3d4960c7 782 if (s->active_locty != locty) {
edff8678
SB
783 break;
784 }
785
3d4960c7
MAL
786 if (s->loc[locty].state == TPM_TIS_STATE_IDLE ||
787 s->loc[locty].state == TPM_TIS_STATE_EXECUTION ||
788 s->loc[locty].state == TPM_TIS_STATE_COMPLETION) {
edff8678
SB
789 /* drop the byte */
790 } else {
fcbed221 791 trace_tpm_tis_mmio_write_data2send(val, size);
3d4960c7
MAL
792 if (s->loc[locty].state == TPM_TIS_STATE_READY) {
793 s->loc[locty].state = TPM_TIS_STATE_RECEPTION;
794 tpm_tis_sts_set(&s->loc[locty],
fd859081 795 TPM_TIS_STS_EXPECT | TPM_TIS_STS_VALID);
edff8678
SB
796 }
797
feeb755f
SB
798 val >>= shift;
799 if (size > 4 - (addr & 0x3)) {
800 /* prevent access beyond FIFO */
801 size = 4 - (addr & 0x3);
802 }
803
3d4960c7 804 while ((s->loc[locty].sts & TPM_TIS_STS_EXPECT) && size > 0) {
f999d81b
SB
805 if (s->rw_offset < s->be_buffer_size) {
806 s->buffer[s->rw_offset++] =
e6b703f6 807 (uint8_t)val;
feeb755f
SB
808 val >>= 8;
809 size--;
edff8678 810 } else {
3d4960c7 811 tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID);
edff8678
SB
812 }
813 }
814
815 /* check for complete packet */
f999d81b 816 if (s->rw_offset > 5 &&
3d4960c7 817 (s->loc[locty].sts & TPM_TIS_STS_EXPECT)) {
edff8678 818 /* we have a packet length - see if we have all of it */
3d4960c7 819 bool need_irq = !(s->loc[locty].sts & TPM_TIS_STS_VALID);
d8383d61 820
c5496b97 821 len = tpm_cmd_get_size(&s->buffer);
f999d81b 822 if (len > s->rw_offset) {
3d4960c7 823 tpm_tis_sts_set(&s->loc[locty],
fd859081 824 TPM_TIS_STS_EXPECT | TPM_TIS_STS_VALID);
edff8678
SB
825 } else {
826 /* packet complete */
3d4960c7 827 tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID);
edff8678 828 }
29b558d8 829 if (need_irq) {
edff8678
SB
830 tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID);
831 }
edff8678
SB
832 }
833 }
834 break;
116694c3
SB
835 case TPM_TIS_REG_INTERFACE_ID:
836 if (val & TPM_TIS_IFACE_ID_INT_SEL_LOCK) {
837 for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) {
3d4960c7 838 s->loc[l].iface_id |= TPM_TIS_IFACE_ID_INT_SEL_LOCK;
116694c3
SB
839 }
840 }
841 break;
edff8678
SB
842 }
843}
844
edff8678
SB
845static const MemoryRegionOps tpm_tis_memory_ops = {
846 .read = tpm_tis_mmio_read,
847 .write = tpm_tis_mmio_write,
848 .endianness = DEVICE_LITTLE_ENDIAN,
849 .valid = {
850 .min_access_size = 1,
851 .max_access_size = 4,
852 },
853};
854
5cb18b3d
SB
855/*
856 * Get the TPMVersion of the backend device being used
857 */
9af7a721 858static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
5cb18b3d 859{
9af7a721 860 TPMState *s = TPM(ti);
5cb18b3d 861
ad4aca69
SB
862 if (tpm_backend_had_startup_error(s->be_driver)) {
863 return TPM_VERSION_UNSPEC;
864 }
865
5cb18b3d
SB
866 return tpm_backend_get_tpm_version(s->be_driver);
867}
868
edff8678
SB
869/*
870 * This function is called when the machine starts, resets or due to
871 * S3 resume.
872 */
873static void tpm_tis_reset(DeviceState *dev)
874{
875 TPMState *s = TPM(dev);
edff8678
SB
876 int c;
877
116694c3 878 s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver);
1af3d63e
SB
879 s->be_buffer_size = MIN(tpm_backend_get_buffer_size(s->be_driver),
880 TPM_TIS_BUFFER_MAX);
116694c3 881
ffab1be7
MAL
882 if (s->ppi_enabled) {
883 tpm_ppi_reset(&s->ppi);
884 }
8f0605cc 885 tpm_backend_reset(s->be_driver);
edff8678 886
3d4960c7
MAL
887 s->active_locty = TPM_TIS_NO_LOCALITY;
888 s->next_locty = TPM_TIS_NO_LOCALITY;
889 s->aborting_locty = TPM_TIS_NO_LOCALITY;
edff8678
SB
890
891 for (c = 0; c < TPM_TIS_NUM_LOCALITIES; c++) {
3d4960c7 892 s->loc[c].access = TPM_TIS_ACCESS_TPM_REG_VALID_STS;
116694c3
SB
893 switch (s->be_tpm_version) {
894 case TPM_VERSION_UNSPEC:
895 break;
896 case TPM_VERSION_1_2:
3d4960c7
MAL
897 s->loc[c].sts = TPM_TIS_STS_TPM_FAMILY1_2;
898 s->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3;
116694c3
SB
899 break;
900 case TPM_VERSION_2_0:
3d4960c7
MAL
901 s->loc[c].sts = TPM_TIS_STS_TPM_FAMILY2_0;
902 s->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0;
116694c3
SB
903 break;
904 }
3d4960c7
MAL
905 s->loc[c].inte = TPM_TIS_INT_POLARITY_LOW_LEVEL;
906 s->loc[c].ints = 0;
907 s->loc[c].state = TPM_TIS_STATE_IDLE;
908
f999d81b 909 s->rw_offset = 0;
edff8678
SB
910 }
911
3bd9e161 912 tpm_backend_startup_tpm(s->be_driver, s->be_buffer_size);
edff8678
SB
913}
914
9ec08c48
SB
915/* persistent state handling */
916
917static int tpm_tis_pre_save(void *opaque)
918{
919 TPMState *s = opaque;
920 uint8_t locty = s->active_locty;
921
922 trace_tpm_tis_pre_save(locty, s->rw_offset);
923
924 if (DEBUG_TIS) {
925 tpm_tis_dump_state(opaque, 0);
926 }
927
928 /*
929 * Synchronize with backend completion.
930 */
931 tpm_backend_finish_sync(s->be_driver);
932
933 return 0;
934}
935
936static const VMStateDescription vmstate_locty = {
937 .name = "tpm-tis/locty",
938 .version_id = 0,
939 .fields = (VMStateField[]) {
940 VMSTATE_UINT32(state, TPMLocality),
941 VMSTATE_UINT32(inte, TPMLocality),
942 VMSTATE_UINT32(ints, TPMLocality),
943 VMSTATE_UINT8(access, TPMLocality),
944 VMSTATE_UINT32(sts, TPMLocality),
945 VMSTATE_UINT32(iface_id, TPMLocality),
946 VMSTATE_END_OF_LIST(),
947 }
948};
949
edff8678 950static const VMStateDescription vmstate_tpm_tis = {
9ec08c48
SB
951 .name = "tpm-tis",
952 .version_id = 0,
953 .pre_save = tpm_tis_pre_save,
954 .fields = (VMStateField[]) {
955 VMSTATE_BUFFER(buffer, TPMState),
956 VMSTATE_UINT16(rw_offset, TPMState),
957 VMSTATE_UINT8(active_locty, TPMState),
958 VMSTATE_UINT8(aborting_locty, TPMState),
959 VMSTATE_UINT8(next_locty, TPMState),
960
961 VMSTATE_STRUCT_ARRAY(loc, TPMState, TPM_TIS_NUM_LOCALITIES, 0,
962 vmstate_locty, TPMLocality),
963
964 VMSTATE_END_OF_LIST()
965 }
edff8678
SB
966};
967
968static Property tpm_tis_properties[] = {
3d4960c7 969 DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ),
c0378544 970 DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver),
b6148757 971 DEFINE_PROP_BOOL("ppi", TPMState, ppi_enabled, true),
edff8678
SB
972 DEFINE_PROP_END_OF_LIST(),
973};
974
975static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
976{
977 TPMState *s = TPM(dev);
edff8678 978
51a837e9
MAL
979 if (!tpm_find()) {
980 error_setg(errp, "at most one TPM device is permitted");
981 return;
982 }
983
edff8678 984 if (!s->be_driver) {
c0378544 985 error_setg(errp, "'tpmdev' property is required");
edff8678
SB
986 return;
987 }
3d4960c7 988 if (s->irq_num > 15) {
c87b35fa
MAL
989 error_setg(errp, "IRQ %d is outside valid range of 0 to 15",
990 s->irq_num);
edff8678
SB
991 return;
992 }
993
3d4960c7 994 isa_init_irq(&s->busdev, &s->irq, s->irq_num);
9dfd24ed
SB
995
996 memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
997 TPM_TIS_ADDR_BASE, &s->mmio);
3b97c01e
SB
998
999 if (s->ppi_enabled) {
1000 tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
1001 TPM_PPI_ADDR_BASE, OBJECT(s));
1002 }
edff8678
SB
1003}
1004
1005static void tpm_tis_initfn(Object *obj)
1006{
edff8678
SB
1007 TPMState *s = TPM(obj);
1008
853dca12
PB
1009 memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
1010 s, "tpm-tis-mmio",
edff8678 1011 TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
edff8678
SB
1012}
1013
edff8678
SB
1014static void tpm_tis_class_init(ObjectClass *klass, void *data)
1015{
1016 DeviceClass *dc = DEVICE_CLASS(klass);
05a69998 1017 TPMIfClass *tc = TPM_IF_CLASS(klass);
edff8678
SB
1018
1019 dc->realize = tpm_tis_realizefn;
1020 dc->props = tpm_tis_properties;
1021 dc->reset = tpm_tis_reset;
1022 dc->vmsd = &vmstate_tpm_tis;
191adc94 1023 tc->model = TPM_MODEL_TPM_TIS;
9af7a721 1024 tc->get_version = tpm_tis_get_tpm_version;
05a69998 1025 tc->request_completed = tpm_tis_request_completed;
edff8678
SB
1026}
1027
1028static const TypeInfo tpm_tis_info = {
1029 .name = TYPE_TPM_TIS,
1030 .parent = TYPE_ISA_DEVICE,
1031 .instance_size = sizeof(TPMState),
1032 .instance_init = tpm_tis_initfn,
edff8678 1033 .class_init = tpm_tis_class_init,
698f5daa
MAL
1034 .interfaces = (InterfaceInfo[]) {
1035 { TYPE_TPM_IF },
1036 { }
1037 }
edff8678
SB
1038};
1039
1040static void tpm_tis_register(void)
1041{
1042 type_register_static(&tpm_tis_info);
edff8678
SB
1043}
1044
1045type_init(tpm_tis_register)
This page took 0.485237 seconds and 4 git commands to generate.