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