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