2 * tpm_tis.c - QEMU's TPM TIS interface emulator
4 * Copyright (C) 2006,2010-2013 IBM Corporation
7 * Stefan Berger <stefanb@us.ibm.com>
8 * David Safford <safford@us.ibm.com>
10 * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
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.
15 * Implementation of the TIS interface according to specs found at
16 * http://www.trustedcomputinggroup.org. This implementation currently
17 * supports version 1.3, 21 March 2013
18 * In the developers menu choose the PC Client section then find the TIS
21 * TPM TIS for TPM 2 implementation following TCG PC Client Platform
22 * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
25 #include "qemu/osdep.h"
26 #include "hw/isa/isa.h"
27 #include "qapi/error.h"
29 #include "hw/acpi/tpm.h"
30 #include "hw/pci/pci_ids.h"
31 #include "sysemu/tpm_backend.h"
36 #define TPM_TIS_NUM_LOCALITIES 5 /* per spec */
37 #define TPM_TIS_LOCALITY_SHIFT 12
38 #define TPM_TIS_NO_LOCALITY 0xff
40 #define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES)
42 #define TPM_TIS_BUFFER_MAX 4096
45 TPM_TIS_STATE_IDLE
= 0,
47 TPM_TIS_STATE_COMPLETION
,
48 TPM_TIS_STATE_EXECUTION
,
49 TPM_TIS_STATE_RECEPTION
,
52 /* locality data -- all fields are persisted */
53 typedef struct TPMLocality
{
62 typedef struct TPMState
{
66 unsigned char buffer
[TPM_TIS_BUFFER_MAX
];
70 uint8_t aborting_locty
;
73 TPMLocality loc
[TPM_TIS_NUM_LOCALITIES
];
80 TPMBackend
*be_driver
;
81 TPMVersion be_tpm_version
;
83 size_t be_buffer_size
;
86 #define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
90 /* local prototypes */
92 static uint64_t tpm_tis_mmio_read(void *opaque
, hwaddr addr
,
95 /* utility functions */
97 static uint8_t tpm_tis_locality_from_addr(hwaddr addr
)
99 return (uint8_t)((addr
>> TPM_TIS_LOCALITY_SHIFT
) & 0x7);
102 static void tpm_tis_show_buffer(const unsigned char *buffer
,
103 size_t buffer_size
, const char *string
)
107 len
= MIN(tpm_cmd_get_size(buffer
), buffer_size
);
108 printf("tpm_tis: %s length = %d\n", string
, len
);
109 for (i
= 0; i
< len
; i
++) {
110 if (i
&& !(i
% 16)) {
113 printf("%.2X ", buffer
[i
]);
119 * Set the given flags in the STS register by clearing the register but
120 * preserving the SELFTEST_DONE and TPM_FAMILY_MASK flags and then setting
123 * The SELFTEST_DONE flag is acquired from the backend that determines it by
124 * peeking into TPM commands.
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
131 static void tpm_tis_sts_set(TPMLocality
*l
, uint32_t flags
)
133 l
->sts
&= TPM_TIS_STS_SELFTEST_DONE
| TPM_TIS_STS_TPM_FAMILY_MASK
;
138 * Send a request to the TPM.
140 static void tpm_tis_tpm_send(TPMState
*s
, uint8_t locty
)
143 tpm_tis_show_buffer(s
->buffer
, s
->be_buffer_size
,
148 * rw_offset serves as length indicator for length of data;
149 * it's reset when the response comes back
151 s
->loc
[locty
].state
= TPM_TIS_STATE_EXECUTION
;
153 s
->cmd
= (TPMBackendCmd
) {
156 .in_len
= s
->rw_offset
,
158 .out_len
= s
->be_buffer_size
,
161 tpm_backend_deliver_request(s
->be_driver
, &s
->cmd
);
164 /* raise an interrupt if allowed */
165 static void tpm_tis_raise_irq(TPMState
*s
, uint8_t locty
, uint32_t irqmask
)
167 if (!TPM_TIS_IS_VALID_LOCTY(locty
)) {
171 if ((s
->loc
[locty
].inte
& TPM_TIS_INT_ENABLED
) &&
172 (s
->loc
[locty
].inte
& irqmask
)) {
173 trace_tpm_tis_raise_irq(irqmask
);
174 qemu_irq_raise(s
->irq
);
175 s
->loc
[locty
].ints
|= irqmask
;
179 static uint32_t tpm_tis_check_request_use_except(TPMState
*s
, uint8_t locty
)
183 for (l
= 0; l
< TPM_TIS_NUM_LOCALITIES
; l
++) {
187 if ((s
->loc
[l
].access
& TPM_TIS_ACCESS_REQUEST_USE
)) {
195 static void tpm_tis_new_active_locality(TPMState
*s
, uint8_t new_active_locty
)
197 bool change
= (s
->active_locty
!= new_active_locty
);
201 if (change
&& TPM_TIS_IS_VALID_LOCTY(s
->active_locty
)) {
202 is_seize
= TPM_TIS_IS_VALID_LOCTY(new_active_locty
) &&
203 s
->loc
[new_active_locty
].access
& TPM_TIS_ACCESS_SEIZE
;
206 mask
= ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
208 mask
= ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
209 TPM_TIS_ACCESS_REQUEST_USE
);
211 /* reset flags on the old active locality */
212 s
->loc
[s
->active_locty
].access
&= mask
;
215 s
->loc
[s
->active_locty
].access
|= TPM_TIS_ACCESS_BEEN_SEIZED
;
219 s
->active_locty
= new_active_locty
;
221 trace_tpm_tis_new_active_locality(s
->active_locty
);
223 if (TPM_TIS_IS_VALID_LOCTY(new_active_locty
)) {
224 /* set flags on the new active locality */
225 s
->loc
[new_active_locty
].access
|= TPM_TIS_ACCESS_ACTIVE_LOCALITY
;
226 s
->loc
[new_active_locty
].access
&= ~(TPM_TIS_ACCESS_REQUEST_USE
|
227 TPM_TIS_ACCESS_SEIZE
);
231 tpm_tis_raise_irq(s
, s
->active_locty
, TPM_TIS_INT_LOCALITY_CHANGED
);
235 /* abort -- this function switches the locality */
236 static void tpm_tis_abort(TPMState
*s
)
240 trace_tpm_tis_abort(s
->next_locty
);
243 * Need to react differently depending on who's aborting now and
244 * which locality will become active afterwards.
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
],
249 TPM_TIS_STS_COMMAND_READY
);
250 tpm_tis_raise_irq(s
, s
->aborting_locty
, TPM_TIS_INT_COMMAND_READY
);
253 /* locality after abort is another one than the current one */
254 tpm_tis_new_active_locality(s
, s
->next_locty
);
256 s
->next_locty
= TPM_TIS_NO_LOCALITY
;
257 /* nobody's aborting a command anymore */
258 s
->aborting_locty
= TPM_TIS_NO_LOCALITY
;
261 /* prepare aborting current command */
262 static void tpm_tis_prep_abort(TPMState
*s
, uint8_t locty
, uint8_t newlocty
)
266 assert(TPM_TIS_IS_VALID_LOCTY(newlocty
));
268 s
->aborting_locty
= locty
; /* may also be TPM_TIS_NO_LOCALITY */
269 s
->next_locty
= newlocty
; /* locality after successful abort */
272 * only abort a command using an interrupt if currently executing
273 * a command AND if there's a valid connection to the vTPM.
275 for (busy_locty
= 0; busy_locty
< TPM_TIS_NUM_LOCALITIES
; busy_locty
++) {
276 if (s
->loc
[busy_locty
].state
== TPM_TIS_STATE_EXECUTION
) {
278 * request the backend to cancel. Some backends may not
281 tpm_backend_cancel_cmd(s
->be_driver
);
290 * Callback from the TPM to indicate that the response was received.
292 static void tpm_tis_request_completed(TPMIf
*ti
, int ret
)
294 TPMState
*s
= TPM(ti
);
295 uint8_t locty
= s
->cmd
.locty
;
298 if (s
->cmd
.selftest_done
) {
299 for (l
= 0; l
< TPM_TIS_NUM_LOCALITIES
; l
++) {
300 s
->loc
[l
].sts
|= TPM_TIS_STS_SELFTEST_DONE
;
304 /* FIXME: report error if ret != 0 */
305 tpm_tis_sts_set(&s
->loc
[locty
],
306 TPM_TIS_STS_VALID
| TPM_TIS_STS_DATA_AVAILABLE
);
307 s
->loc
[locty
].state
= TPM_TIS_STATE_COMPLETION
;
311 tpm_tis_show_buffer(s
->buffer
, s
->be_buffer_size
,
312 "tpm_tis: From TPM");
315 if (TPM_TIS_IS_VALID_LOCTY(s
->next_locty
)) {
319 tpm_tis_raise_irq(s
, locty
,
320 TPM_TIS_INT_DATA_AVAILABLE
| TPM_TIS_INT_STS_VALID
);
324 * Read a byte of response data
326 static uint32_t tpm_tis_data_read(TPMState
*s
, uint8_t locty
)
328 uint32_t ret
= TPM_TIS_NO_DATA_BYTE
;
331 if ((s
->loc
[locty
].sts
& TPM_TIS_STS_DATA_AVAILABLE
)) {
332 len
= MIN(tpm_cmd_get_size(&s
->buffer
),
335 ret
= s
->buffer
[s
->rw_offset
++];
336 if (s
->rw_offset
>= len
) {
338 tpm_tis_sts_set(&s
->loc
[locty
], TPM_TIS_STS_VALID
);
339 tpm_tis_raise_irq(s
, locty
, TPM_TIS_INT_STS_VALID
);
341 trace_tpm_tis_data_read(ret
, s
->rw_offset
- 1);
348 static void tpm_tis_dump_state(void *opaque
, hwaddr addr
)
350 static const unsigned regs
[] = {
352 TPM_TIS_REG_INT_ENABLE
,
353 TPM_TIS_REG_INT_VECTOR
,
354 TPM_TIS_REG_INT_STATUS
,
355 TPM_TIS_REG_INTF_CAPABILITY
,
361 uint8_t locty
= tpm_tis_locality_from_addr(addr
);
362 hwaddr base
= addr
& ~0xfff;
363 TPMState
*s
= opaque
;
365 printf("tpm_tis: active locality : %d\n"
366 "tpm_tis: state of locality %d : %d\n"
367 "tpm_tis: register dump:\n",
369 locty
, s
->loc
[locty
].state
);
371 for (idx
= 0; regs
[idx
] != 0xfff; idx
++) {
372 printf("tpm_tis: 0x%04x : 0x%08x\n", regs
[idx
],
373 (int)tpm_tis_mmio_read(opaque
, base
+ regs
[idx
], 4));
376 printf("tpm_tis: r/w offset : %d\n"
377 "tpm_tis: result buffer : ",
380 idx
< MIN(tpm_cmd_get_size(&s
->buffer
), s
->be_buffer_size
);
383 s
->rw_offset
== idx
? '>' : ' ',
385 ((idx
& 0xf) == 0xf) ? "\ntpm_tis: " : "");
392 * Read a register of the TIS interface
393 * See specs pages 33-63 for description of the registers
395 static uint64_t tpm_tis_mmio_read(void *opaque
, hwaddr addr
,
398 TPMState
*s
= opaque
;
399 uint16_t offset
= addr
& 0xffc;
400 uint8_t shift
= (addr
& 0x3) * 8;
401 uint32_t val
= 0xffffffff;
402 uint8_t locty
= tpm_tis_locality_from_addr(addr
);
406 if (tpm_backend_had_startup_error(s
->be_driver
)) {
411 case TPM_TIS_REG_ACCESS
:
412 /* never show the SEIZE flag even though we use it internally */
413 val
= s
->loc
[locty
].access
& ~TPM_TIS_ACCESS_SEIZE
;
414 /* the pending flag is always calculated */
415 if (tpm_tis_check_request_use_except(s
, locty
)) {
416 val
|= TPM_TIS_ACCESS_PENDING_REQUEST
;
418 val
|= !tpm_backend_get_tpm_established_flag(s
->be_driver
);
420 case TPM_TIS_REG_INT_ENABLE
:
421 val
= s
->loc
[locty
].inte
;
423 case TPM_TIS_REG_INT_VECTOR
:
426 case TPM_TIS_REG_INT_STATUS
:
427 val
= s
->loc
[locty
].ints
;
429 case TPM_TIS_REG_INTF_CAPABILITY
:
430 switch (s
->be_tpm_version
) {
431 case TPM_VERSION_UNSPEC
:
434 case TPM_VERSION_1_2
:
435 val
= TPM_TIS_CAPABILITIES_SUPPORTED1_3
;
437 case TPM_VERSION_2_0
:
438 val
= TPM_TIS_CAPABILITIES_SUPPORTED2_0
;
442 case TPM_TIS_REG_STS
:
443 if (s
->active_locty
== locty
) {
444 if ((s
->loc
[locty
].sts
& TPM_TIS_STS_DATA_AVAILABLE
)) {
445 val
= TPM_TIS_BURST_COUNT(
446 MIN(tpm_cmd_get_size(&s
->buffer
),
448 - s
->rw_offset
) | s
->loc
[locty
].sts
;
450 avail
= s
->be_buffer_size
- s
->rw_offset
;
452 * byte-sized reads should not return 0x00 for 0x100
455 if (size
== 1 && avail
> 0xff) {
458 val
= TPM_TIS_BURST_COUNT(avail
) | s
->loc
[locty
].sts
;
462 case TPM_TIS_REG_DATA_FIFO
:
463 case TPM_TIS_REG_DATA_XFIFO
... TPM_TIS_REG_DATA_XFIFO_END
:
464 if (s
->active_locty
== locty
) {
465 if (size
> 4 - (addr
& 0x3)) {
466 /* prevent access beyond FIFO */
467 size
= 4 - (addr
& 0x3);
472 switch (s
->loc
[locty
].state
) {
473 case TPM_TIS_STATE_COMPLETION
:
474 v
= tpm_tis_data_read(s
, locty
);
477 v
= TPM_TIS_NO_DATA_BYTE
;
484 shift
= 0; /* no more adjustments */
487 case TPM_TIS_REG_INTERFACE_ID
:
488 val
= s
->loc
[locty
].iface_id
;
490 case TPM_TIS_REG_DID_VID
:
491 val
= (TPM_TIS_TPM_DID
<< 16) | TPM_TIS_TPM_VID
;
493 case TPM_TIS_REG_RID
:
494 val
= TPM_TIS_TPM_RID
;
497 case TPM_TIS_REG_DEBUG
:
498 tpm_tis_dump_state(opaque
, addr
);
507 trace_tpm_tis_mmio_read(size
, addr
, val
);
513 * Write a value to a register of the TIS interface
514 * See specs pages 33-63 for description of the registers
516 static void tpm_tis_mmio_write(void *opaque
, hwaddr addr
,
517 uint64_t val
, unsigned size
)
519 TPMState
*s
= opaque
;
520 uint16_t off
= addr
& 0xffc;
521 uint8_t shift
= (addr
& 0x3) * 8;
522 uint8_t locty
= tpm_tis_locality_from_addr(addr
);
523 uint8_t active_locty
, l
;
524 int c
, set_new_locty
= 1;
526 uint32_t mask
= (size
== 1) ? 0xff : ((size
== 2) ? 0xffff : ~0);
528 trace_tpm_tis_mmio_write(size
, addr
, val
);
531 trace_tpm_tis_mmio_write_locty4();
535 if (tpm_backend_had_startup_error(s
->be_driver
)) {
549 case TPM_TIS_REG_ACCESS
:
551 if ((val
& TPM_TIS_ACCESS_SEIZE
)) {
552 val
&= ~(TPM_TIS_ACCESS_REQUEST_USE
|
553 TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
556 active_locty
= s
->active_locty
;
558 if ((val
& TPM_TIS_ACCESS_ACTIVE_LOCALITY
)) {
559 /* give up locality if currently owned */
560 if (s
->active_locty
== locty
) {
561 trace_tpm_tis_mmio_write_release_locty(locty
);
563 uint8_t newlocty
= TPM_TIS_NO_LOCALITY
;
564 /* anybody wants the locality ? */
565 for (c
= TPM_TIS_NUM_LOCALITIES
- 1; c
>= 0; c
--) {
566 if ((s
->loc
[c
].access
& TPM_TIS_ACCESS_REQUEST_USE
)) {
567 trace_tpm_tis_mmio_write_locty_req_use(c
);
572 trace_tpm_tis_mmio_write_next_locty(newlocty
);
574 if (TPM_TIS_IS_VALID_LOCTY(newlocty
)) {
576 tpm_tis_prep_abort(s
, locty
, newlocty
);
578 active_locty
= TPM_TIS_NO_LOCALITY
;
581 /* not currently the owner; clear a pending request */
582 s
->loc
[locty
].access
&= ~TPM_TIS_ACCESS_REQUEST_USE
;
586 if ((val
& TPM_TIS_ACCESS_BEEN_SEIZED
)) {
587 s
->loc
[locty
].access
&= ~TPM_TIS_ACCESS_BEEN_SEIZED
;
590 if ((val
& TPM_TIS_ACCESS_SEIZE
)) {
592 * allow seize if a locality is active and the requesting
593 * locality is higher than the one that's active
595 * allow seize for requesting locality if no locality is
598 while ((TPM_TIS_IS_VALID_LOCTY(s
->active_locty
) &&
599 locty
> s
->active_locty
) ||
600 !TPM_TIS_IS_VALID_LOCTY(s
->active_locty
)) {
601 bool higher_seize
= FALSE
;
603 /* already a pending SEIZE ? */
604 if ((s
->loc
[locty
].access
& TPM_TIS_ACCESS_SEIZE
)) {
608 /* check for ongoing seize by a higher locality */
609 for (l
= locty
+ 1; l
< TPM_TIS_NUM_LOCALITIES
; l
++) {
610 if ((s
->loc
[l
].access
& TPM_TIS_ACCESS_SEIZE
)) {
620 /* cancel any seize by a lower locality */
621 for (l
= 0; l
< locty
- 1; l
++) {
622 s
->loc
[l
].access
&= ~TPM_TIS_ACCESS_SEIZE
;
625 s
->loc
[locty
].access
|= TPM_TIS_ACCESS_SEIZE
;
627 trace_tpm_tis_mmio_write_locty_seized(locty
, s
->active_locty
);
628 trace_tpm_tis_mmio_write_init_abort();
631 tpm_tis_prep_abort(s
, s
->active_locty
, locty
);
636 if ((val
& TPM_TIS_ACCESS_REQUEST_USE
)) {
637 if (s
->active_locty
!= locty
) {
638 if (TPM_TIS_IS_VALID_LOCTY(s
->active_locty
)) {
639 s
->loc
[locty
].access
|= TPM_TIS_ACCESS_REQUEST_USE
;
641 /* no locality active -> make this one active now */
642 active_locty
= locty
;
648 tpm_tis_new_active_locality(s
, active_locty
);
652 case TPM_TIS_REG_INT_ENABLE
:
653 if (s
->active_locty
!= locty
) {
657 s
->loc
[locty
].inte
&= mask
;
658 s
->loc
[locty
].inte
|= (val
& (TPM_TIS_INT_ENABLED
|
659 TPM_TIS_INT_POLARITY_MASK
|
660 TPM_TIS_INTERRUPTS_SUPPORTED
));
662 case TPM_TIS_REG_INT_VECTOR
:
663 /* hard wired -- ignore */
665 case TPM_TIS_REG_INT_STATUS
:
666 if (s
->active_locty
!= locty
) {
670 /* clearing of interrupt flags */
671 if (((val
& TPM_TIS_INTERRUPTS_SUPPORTED
)) &&
672 (s
->loc
[locty
].ints
& TPM_TIS_INTERRUPTS_SUPPORTED
)) {
673 s
->loc
[locty
].ints
&= ~val
;
674 if (s
->loc
[locty
].ints
== 0) {
675 qemu_irq_lower(s
->irq
);
676 trace_tpm_tis_mmio_write_lowering_irq();
679 s
->loc
[locty
].ints
&= ~(val
& TPM_TIS_INTERRUPTS_SUPPORTED
);
681 case TPM_TIS_REG_STS
:
682 if (s
->active_locty
!= locty
) {
686 if (s
->be_tpm_version
== TPM_VERSION_2_0
) {
687 /* some flags that are only supported for TPM 2 */
688 if (val
& TPM_TIS_STS_COMMAND_CANCEL
) {
689 if (s
->loc
[locty
].state
== TPM_TIS_STATE_EXECUTION
) {
691 * request the backend to cancel. Some backends may not
694 tpm_backend_cancel_cmd(s
->be_driver
);
698 if (val
& TPM_TIS_STS_RESET_ESTABLISHMENT_BIT
) {
699 if (locty
== 3 || locty
== 4) {
700 tpm_backend_reset_tpm_established_flag(s
->be_driver
, locty
);
705 val
&= (TPM_TIS_STS_COMMAND_READY
| TPM_TIS_STS_TPM_GO
|
706 TPM_TIS_STS_RESPONSE_RETRY
);
708 if (val
== TPM_TIS_STS_COMMAND_READY
) {
709 switch (s
->loc
[locty
].state
) {
711 case TPM_TIS_STATE_READY
:
715 case TPM_TIS_STATE_IDLE
:
716 tpm_tis_sts_set(&s
->loc
[locty
], TPM_TIS_STS_COMMAND_READY
);
717 s
->loc
[locty
].state
= TPM_TIS_STATE_READY
;
718 tpm_tis_raise_irq(s
, locty
, TPM_TIS_INT_COMMAND_READY
);
721 case TPM_TIS_STATE_EXECUTION
:
722 case TPM_TIS_STATE_RECEPTION
:
723 /* abort currently running command */
724 trace_tpm_tis_mmio_write_init_abort();
725 tpm_tis_prep_abort(s
, locty
, locty
);
728 case TPM_TIS_STATE_COMPLETION
:
730 /* shortcut to ready state with C/R set */
731 s
->loc
[locty
].state
= TPM_TIS_STATE_READY
;
732 if (!(s
->loc
[locty
].sts
& TPM_TIS_STS_COMMAND_READY
)) {
733 tpm_tis_sts_set(&s
->loc
[locty
],
734 TPM_TIS_STS_COMMAND_READY
);
735 tpm_tis_raise_irq(s
, locty
, TPM_TIS_INT_COMMAND_READY
);
737 s
->loc
[locty
].sts
&= ~(TPM_TIS_STS_DATA_AVAILABLE
);
741 } else if (val
== TPM_TIS_STS_TPM_GO
) {
742 switch (s
->loc
[locty
].state
) {
743 case TPM_TIS_STATE_RECEPTION
:
744 if ((s
->loc
[locty
].sts
& TPM_TIS_STS_EXPECT
) == 0) {
745 tpm_tis_tpm_send(s
, locty
);
752 } else if (val
== TPM_TIS_STS_RESPONSE_RETRY
) {
753 switch (s
->loc
[locty
].state
) {
754 case TPM_TIS_STATE_COMPLETION
:
756 tpm_tis_sts_set(&s
->loc
[locty
],
758 TPM_TIS_STS_DATA_AVAILABLE
);
766 case TPM_TIS_REG_DATA_FIFO
:
767 case TPM_TIS_REG_DATA_XFIFO
... TPM_TIS_REG_DATA_XFIFO_END
:
769 if (s
->active_locty
!= locty
) {
773 if (s
->loc
[locty
].state
== TPM_TIS_STATE_IDLE
||
774 s
->loc
[locty
].state
== TPM_TIS_STATE_EXECUTION
||
775 s
->loc
[locty
].state
== TPM_TIS_STATE_COMPLETION
) {
778 trace_tpm_tis_mmio_write_data2send(val
, size
);
779 if (s
->loc
[locty
].state
== TPM_TIS_STATE_READY
) {
780 s
->loc
[locty
].state
= TPM_TIS_STATE_RECEPTION
;
781 tpm_tis_sts_set(&s
->loc
[locty
],
782 TPM_TIS_STS_EXPECT
| TPM_TIS_STS_VALID
);
786 if (size
> 4 - (addr
& 0x3)) {
787 /* prevent access beyond FIFO */
788 size
= 4 - (addr
& 0x3);
791 while ((s
->loc
[locty
].sts
& TPM_TIS_STS_EXPECT
) && size
> 0) {
792 if (s
->rw_offset
< s
->be_buffer_size
) {
793 s
->buffer
[s
->rw_offset
++] =
798 tpm_tis_sts_set(&s
->loc
[locty
], TPM_TIS_STS_VALID
);
802 /* check for complete packet */
803 if (s
->rw_offset
> 5 &&
804 (s
->loc
[locty
].sts
& TPM_TIS_STS_EXPECT
)) {
805 /* we have a packet length - see if we have all of it */
806 bool need_irq
= !(s
->loc
[locty
].sts
& TPM_TIS_STS_VALID
);
808 len
= tpm_cmd_get_size(&s
->buffer
);
809 if (len
> s
->rw_offset
) {
810 tpm_tis_sts_set(&s
->loc
[locty
],
811 TPM_TIS_STS_EXPECT
| TPM_TIS_STS_VALID
);
813 /* packet complete */
814 tpm_tis_sts_set(&s
->loc
[locty
], TPM_TIS_STS_VALID
);
817 tpm_tis_raise_irq(s
, locty
, TPM_TIS_INT_STS_VALID
);
822 case TPM_TIS_REG_INTERFACE_ID
:
823 if (val
& TPM_TIS_IFACE_ID_INT_SEL_LOCK
) {
824 for (l
= 0; l
< TPM_TIS_NUM_LOCALITIES
; l
++) {
825 s
->loc
[l
].iface_id
|= TPM_TIS_IFACE_ID_INT_SEL_LOCK
;
832 static const MemoryRegionOps tpm_tis_memory_ops
= {
833 .read
= tpm_tis_mmio_read
,
834 .write
= tpm_tis_mmio_write
,
835 .endianness
= DEVICE_LITTLE_ENDIAN
,
837 .min_access_size
= 1,
838 .max_access_size
= 4,
843 * Get the TPMVersion of the backend device being used
845 static enum TPMVersion
tpm_tis_get_tpm_version(TPMIf
*ti
)
847 TPMState
*s
= TPM(ti
);
849 if (tpm_backend_had_startup_error(s
->be_driver
)) {
850 return TPM_VERSION_UNSPEC
;
853 return tpm_backend_get_tpm_version(s
->be_driver
);
857 * This function is called when the machine starts, resets or due to
860 static void tpm_tis_reset(DeviceState
*dev
)
862 TPMState
*s
= TPM(dev
);
865 s
->be_tpm_version
= tpm_backend_get_tpm_version(s
->be_driver
);
866 s
->be_buffer_size
= MIN(tpm_backend_get_buffer_size(s
->be_driver
),
869 tpm_backend_reset(s
->be_driver
);
871 s
->active_locty
= TPM_TIS_NO_LOCALITY
;
872 s
->next_locty
= TPM_TIS_NO_LOCALITY
;
873 s
->aborting_locty
= TPM_TIS_NO_LOCALITY
;
875 for (c
= 0; c
< TPM_TIS_NUM_LOCALITIES
; c
++) {
876 s
->loc
[c
].access
= TPM_TIS_ACCESS_TPM_REG_VALID_STS
;
877 switch (s
->be_tpm_version
) {
878 case TPM_VERSION_UNSPEC
:
880 case TPM_VERSION_1_2
:
881 s
->loc
[c
].sts
= TPM_TIS_STS_TPM_FAMILY1_2
;
882 s
->loc
[c
].iface_id
= TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3
;
884 case TPM_VERSION_2_0
:
885 s
->loc
[c
].sts
= TPM_TIS_STS_TPM_FAMILY2_0
;
886 s
->loc
[c
].iface_id
= TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0
;
889 s
->loc
[c
].inte
= TPM_TIS_INT_POLARITY_LOW_LEVEL
;
891 s
->loc
[c
].state
= TPM_TIS_STATE_IDLE
;
896 tpm_backend_startup_tpm(s
->be_driver
, s
->be_buffer_size
);
899 /* persistent state handling */
901 static int tpm_tis_pre_save(void *opaque
)
903 TPMState
*s
= opaque
;
904 uint8_t locty
= s
->active_locty
;
906 trace_tpm_tis_pre_save(locty
, s
->rw_offset
);
909 tpm_tis_dump_state(opaque
, 0);
913 * Synchronize with backend completion.
915 tpm_backend_finish_sync(s
->be_driver
);
920 static const VMStateDescription vmstate_locty
= {
921 .name
= "tpm-tis/locty",
923 .fields
= (VMStateField
[]) {
924 VMSTATE_UINT32(state
, TPMLocality
),
925 VMSTATE_UINT32(inte
, TPMLocality
),
926 VMSTATE_UINT32(ints
, TPMLocality
),
927 VMSTATE_UINT8(access
, TPMLocality
),
928 VMSTATE_UINT32(sts
, TPMLocality
),
929 VMSTATE_UINT32(iface_id
, TPMLocality
),
930 VMSTATE_END_OF_LIST(),
934 static const VMStateDescription vmstate_tpm_tis
= {
937 .pre_save
= tpm_tis_pre_save
,
938 .fields
= (VMStateField
[]) {
939 VMSTATE_BUFFER(buffer
, TPMState
),
940 VMSTATE_UINT16(rw_offset
, TPMState
),
941 VMSTATE_UINT8(active_locty
, TPMState
),
942 VMSTATE_UINT8(aborting_locty
, TPMState
),
943 VMSTATE_UINT8(next_locty
, TPMState
),
945 VMSTATE_STRUCT_ARRAY(loc
, TPMState
, TPM_TIS_NUM_LOCALITIES
, 0,
946 vmstate_locty
, TPMLocality
),
948 VMSTATE_END_OF_LIST()
952 static Property tpm_tis_properties
[] = {
953 DEFINE_PROP_UINT32("irq", TPMState
, irq_num
, TPM_TIS_IRQ
),
954 DEFINE_PROP_TPMBE("tpmdev", TPMState
, be_driver
),
955 DEFINE_PROP_END_OF_LIST(),
958 static void tpm_tis_realizefn(DeviceState
*dev
, Error
**errp
)
960 TPMState
*s
= TPM(dev
);
963 error_setg(errp
, "at most one TPM device is permitted");
968 error_setg(errp
, "'tpmdev' property is required");
971 if (s
->irq_num
> 15) {
972 error_setg(errp
, "IRQ %d is outside valid range of 0 to 15",
977 isa_init_irq(&s
->busdev
, &s
->irq
, s
->irq_num
);
979 memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev
)),
980 TPM_TIS_ADDR_BASE
, &s
->mmio
);
983 static void tpm_tis_initfn(Object
*obj
)
985 TPMState
*s
= TPM(obj
);
987 memory_region_init_io(&s
->mmio
, OBJECT(s
), &tpm_tis_memory_ops
,
989 TPM_TIS_NUM_LOCALITIES
<< TPM_TIS_LOCALITY_SHIFT
);
992 static void tpm_tis_class_init(ObjectClass
*klass
, void *data
)
994 DeviceClass
*dc
= DEVICE_CLASS(klass
);
995 TPMIfClass
*tc
= TPM_IF_CLASS(klass
);
997 dc
->realize
= tpm_tis_realizefn
;
998 dc
->props
= tpm_tis_properties
;
999 dc
->reset
= tpm_tis_reset
;
1000 dc
->vmsd
= &vmstate_tpm_tis
;
1001 tc
->model
= TPM_MODEL_TPM_TIS
;
1002 tc
->get_version
= tpm_tis_get_tpm_version
;
1003 tc
->request_completed
= tpm_tis_request_completed
;
1006 static const TypeInfo tpm_tis_info
= {
1007 .name
= TYPE_TPM_TIS
,
1008 .parent
= TYPE_ISA_DEVICE
,
1009 .instance_size
= sizeof(TPMState
),
1010 .instance_init
= tpm_tis_initfn
,
1011 .class_init
= tpm_tis_class_init
,
1012 .interfaces
= (InterfaceInfo
[]) {
1018 static void tpm_tis_register(void)
1020 type_register_static(&tpm_tis_info
);
1023 type_init(tpm_tis_register
)