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"
27 #include "hw/isa/isa.h"
28 #include "qapi/error.h"
29 #include "qemu/module.h"
31 #include "hw/acpi/tpm.h"
32 #include "hw/pci/pci_ids.h"
33 #include "hw/qdev-properties.h"
34 #include "migration/vmstate.h"
35 #include "sysemu/tpm_backend.h"
41 #define TPM_TIS_NUM_LOCALITIES 5 /* per spec */
42 #define TPM_TIS_LOCALITY_SHIFT 12
43 #define TPM_TIS_NO_LOCALITY 0xff
45 #define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES)
47 #define TPM_TIS_BUFFER_MAX 4096
50 TPM_TIS_STATE_IDLE
= 0,
52 TPM_TIS_STATE_COMPLETION
,
53 TPM_TIS_STATE_EXECUTION
,
54 TPM_TIS_STATE_RECEPTION
,
57 /* locality data -- all fields are persisted */
58 typedef struct TPMLocality
{
67 typedef struct TPMState
{
71 unsigned char buffer
[TPM_TIS_BUFFER_MAX
];
75 uint8_t aborting_locty
;
78 TPMLocality loc
[TPM_TIS_NUM_LOCALITIES
];
85 TPMBackend
*be_driver
;
86 TPMVersion be_tpm_version
;
88 size_t be_buffer_size
;
94 #define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
98 /* local prototypes */
100 static uint64_t tpm_tis_mmio_read(void *opaque
, hwaddr addr
,
103 /* utility functions */
105 static uint8_t tpm_tis_locality_from_addr(hwaddr addr
)
107 return (uint8_t)((addr
>> TPM_TIS_LOCALITY_SHIFT
) & 0x7);
112 * Set the given flags in the STS register by clearing the register but
113 * preserving the SELFTEST_DONE and TPM_FAMILY_MASK flags and then setting
116 * The SELFTEST_DONE flag is acquired from the backend that determines it by
117 * peeking into TPM commands.
119 * A VM suspend/resume will preserve the flag by storing it into the VM
120 * device state, but the backend will not remember it when QEMU is started
121 * again. Therefore, we cache the flag here. Once set, it will not be unset
124 static void tpm_tis_sts_set(TPMLocality
*l
, uint32_t flags
)
126 l
->sts
&= TPM_TIS_STS_SELFTEST_DONE
| TPM_TIS_STS_TPM_FAMILY_MASK
;
131 * Send a request to the TPM.
133 static void tpm_tis_tpm_send(TPMState
*s
, uint8_t locty
)
135 if (trace_event_get_state_backends(TRACE_TPM_UTIL_SHOW_BUFFER
)) {
136 tpm_util_show_buffer(s
->buffer
, s
->be_buffer_size
, "To TPM");
140 * rw_offset serves as length indicator for length of data;
141 * it's reset when the response comes back
143 s
->loc
[locty
].state
= TPM_TIS_STATE_EXECUTION
;
145 s
->cmd
= (TPMBackendCmd
) {
148 .in_len
= s
->rw_offset
,
150 .out_len
= s
->be_buffer_size
,
153 tpm_backend_deliver_request(s
->be_driver
, &s
->cmd
);
156 /* raise an interrupt if allowed */
157 static void tpm_tis_raise_irq(TPMState
*s
, uint8_t locty
, uint32_t irqmask
)
159 if (!TPM_TIS_IS_VALID_LOCTY(locty
)) {
163 if ((s
->loc
[locty
].inte
& TPM_TIS_INT_ENABLED
) &&
164 (s
->loc
[locty
].inte
& irqmask
)) {
165 trace_tpm_tis_raise_irq(irqmask
);
166 qemu_irq_raise(s
->irq
);
167 s
->loc
[locty
].ints
|= irqmask
;
171 static uint32_t tpm_tis_check_request_use_except(TPMState
*s
, uint8_t locty
)
175 for (l
= 0; l
< TPM_TIS_NUM_LOCALITIES
; l
++) {
179 if ((s
->loc
[l
].access
& TPM_TIS_ACCESS_REQUEST_USE
)) {
187 static void tpm_tis_new_active_locality(TPMState
*s
, uint8_t new_active_locty
)
189 bool change
= (s
->active_locty
!= new_active_locty
);
193 if (change
&& TPM_TIS_IS_VALID_LOCTY(s
->active_locty
)) {
194 is_seize
= TPM_TIS_IS_VALID_LOCTY(new_active_locty
) &&
195 s
->loc
[new_active_locty
].access
& TPM_TIS_ACCESS_SEIZE
;
198 mask
= ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
200 mask
= ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
201 TPM_TIS_ACCESS_REQUEST_USE
);
203 /* reset flags on the old active locality */
204 s
->loc
[s
->active_locty
].access
&= mask
;
207 s
->loc
[s
->active_locty
].access
|= TPM_TIS_ACCESS_BEEN_SEIZED
;
211 s
->active_locty
= new_active_locty
;
213 trace_tpm_tis_new_active_locality(s
->active_locty
);
215 if (TPM_TIS_IS_VALID_LOCTY(new_active_locty
)) {
216 /* set flags on the new active locality */
217 s
->loc
[new_active_locty
].access
|= TPM_TIS_ACCESS_ACTIVE_LOCALITY
;
218 s
->loc
[new_active_locty
].access
&= ~(TPM_TIS_ACCESS_REQUEST_USE
|
219 TPM_TIS_ACCESS_SEIZE
);
223 tpm_tis_raise_irq(s
, s
->active_locty
, TPM_TIS_INT_LOCALITY_CHANGED
);
227 /* abort -- this function switches the locality */
228 static void tpm_tis_abort(TPMState
*s
)
232 trace_tpm_tis_abort(s
->next_locty
);
235 * Need to react differently depending on who's aborting now and
236 * which locality will become active afterwards.
238 if (s
->aborting_locty
== s
->next_locty
) {
239 s
->loc
[s
->aborting_locty
].state
= TPM_TIS_STATE_READY
;
240 tpm_tis_sts_set(&s
->loc
[s
->aborting_locty
],
241 TPM_TIS_STS_COMMAND_READY
);
242 tpm_tis_raise_irq(s
, s
->aborting_locty
, TPM_TIS_INT_COMMAND_READY
);
245 /* locality after abort is another one than the current one */
246 tpm_tis_new_active_locality(s
, s
->next_locty
);
248 s
->next_locty
= TPM_TIS_NO_LOCALITY
;
249 /* nobody's aborting a command anymore */
250 s
->aborting_locty
= TPM_TIS_NO_LOCALITY
;
253 /* prepare aborting current command */
254 static void tpm_tis_prep_abort(TPMState
*s
, uint8_t locty
, uint8_t newlocty
)
258 assert(TPM_TIS_IS_VALID_LOCTY(newlocty
));
260 s
->aborting_locty
= locty
; /* may also be TPM_TIS_NO_LOCALITY */
261 s
->next_locty
= newlocty
; /* locality after successful abort */
264 * only abort a command using an interrupt if currently executing
265 * a command AND if there's a valid connection to the vTPM.
267 for (busy_locty
= 0; busy_locty
< TPM_TIS_NUM_LOCALITIES
; busy_locty
++) {
268 if (s
->loc
[busy_locty
].state
== TPM_TIS_STATE_EXECUTION
) {
270 * request the backend to cancel. Some backends may not
273 tpm_backend_cancel_cmd(s
->be_driver
);
282 * Callback from the TPM to indicate that the response was received.
284 static void tpm_tis_request_completed(TPMIf
*ti
, int ret
)
286 TPMState
*s
= TPM(ti
);
287 uint8_t locty
= s
->cmd
.locty
;
290 assert(TPM_TIS_IS_VALID_LOCTY(locty
));
292 if (s
->cmd
.selftest_done
) {
293 for (l
= 0; l
< TPM_TIS_NUM_LOCALITIES
; l
++) {
294 s
->loc
[l
].sts
|= TPM_TIS_STS_SELFTEST_DONE
;
298 /* FIXME: report error if ret != 0 */
299 tpm_tis_sts_set(&s
->loc
[locty
],
300 TPM_TIS_STS_VALID
| TPM_TIS_STS_DATA_AVAILABLE
);
301 s
->loc
[locty
].state
= TPM_TIS_STATE_COMPLETION
;
304 if (trace_event_get_state_backends(TRACE_TPM_UTIL_SHOW_BUFFER
)) {
305 tpm_util_show_buffer(s
->buffer
, s
->be_buffer_size
, "From TPM");
308 if (TPM_TIS_IS_VALID_LOCTY(s
->next_locty
)) {
312 tpm_tis_raise_irq(s
, locty
,
313 TPM_TIS_INT_DATA_AVAILABLE
| TPM_TIS_INT_STS_VALID
);
317 * Read a byte of response data
319 static uint32_t tpm_tis_data_read(TPMState
*s
, uint8_t locty
)
321 uint32_t ret
= TPM_TIS_NO_DATA_BYTE
;
324 if ((s
->loc
[locty
].sts
& TPM_TIS_STS_DATA_AVAILABLE
)) {
325 len
= MIN(tpm_cmd_get_size(&s
->buffer
),
328 ret
= s
->buffer
[s
->rw_offset
++];
329 if (s
->rw_offset
>= len
) {
331 tpm_tis_sts_set(&s
->loc
[locty
], TPM_TIS_STS_VALID
);
332 tpm_tis_raise_irq(s
, locty
, TPM_TIS_INT_STS_VALID
);
334 trace_tpm_tis_data_read(ret
, s
->rw_offset
- 1);
341 static void tpm_tis_dump_state(void *opaque
, hwaddr addr
)
343 static const unsigned regs
[] = {
345 TPM_TIS_REG_INT_ENABLE
,
346 TPM_TIS_REG_INT_VECTOR
,
347 TPM_TIS_REG_INT_STATUS
,
348 TPM_TIS_REG_INTF_CAPABILITY
,
354 uint8_t locty
= tpm_tis_locality_from_addr(addr
);
355 hwaddr base
= addr
& ~0xfff;
356 TPMState
*s
= opaque
;
358 printf("tpm_tis: active locality : %d\n"
359 "tpm_tis: state of locality %d : %d\n"
360 "tpm_tis: register dump:\n",
362 locty
, s
->loc
[locty
].state
);
364 for (idx
= 0; regs
[idx
] != 0xfff; idx
++) {
365 printf("tpm_tis: 0x%04x : 0x%08x\n", regs
[idx
],
366 (int)tpm_tis_mmio_read(opaque
, base
+ regs
[idx
], 4));
369 printf("tpm_tis: r/w offset : %d\n"
370 "tpm_tis: result buffer : ",
373 idx
< MIN(tpm_cmd_get_size(&s
->buffer
), s
->be_buffer_size
);
376 s
->rw_offset
== idx
? '>' : ' ',
378 ((idx
& 0xf) == 0xf) ? "\ntpm_tis: " : "");
385 * Read a register of the TIS interface
386 * See specs pages 33-63 for description of the registers
388 static uint64_t tpm_tis_mmio_read(void *opaque
, hwaddr addr
,
391 TPMState
*s
= opaque
;
392 uint16_t offset
= addr
& 0xffc;
393 uint8_t shift
= (addr
& 0x3) * 8;
394 uint32_t val
= 0xffffffff;
395 uint8_t locty
= tpm_tis_locality_from_addr(addr
);
399 if (tpm_backend_had_startup_error(s
->be_driver
)) {
404 case TPM_TIS_REG_ACCESS
:
405 /* never show the SEIZE flag even though we use it internally */
406 val
= s
->loc
[locty
].access
& ~TPM_TIS_ACCESS_SEIZE
;
407 /* the pending flag is always calculated */
408 if (tpm_tis_check_request_use_except(s
, locty
)) {
409 val
|= TPM_TIS_ACCESS_PENDING_REQUEST
;
411 val
|= !tpm_backend_get_tpm_established_flag(s
->be_driver
);
413 case TPM_TIS_REG_INT_ENABLE
:
414 val
= s
->loc
[locty
].inte
;
416 case TPM_TIS_REG_INT_VECTOR
:
419 case TPM_TIS_REG_INT_STATUS
:
420 val
= s
->loc
[locty
].ints
;
422 case TPM_TIS_REG_INTF_CAPABILITY
:
423 switch (s
->be_tpm_version
) {
424 case TPM_VERSION_UNSPEC
:
427 case TPM_VERSION_1_2
:
428 val
= TPM_TIS_CAPABILITIES_SUPPORTED1_3
;
430 case TPM_VERSION_2_0
:
431 val
= TPM_TIS_CAPABILITIES_SUPPORTED2_0
;
435 case TPM_TIS_REG_STS
:
436 if (s
->active_locty
== locty
) {
437 if ((s
->loc
[locty
].sts
& TPM_TIS_STS_DATA_AVAILABLE
)) {
438 val
= TPM_TIS_BURST_COUNT(
439 MIN(tpm_cmd_get_size(&s
->buffer
),
441 - s
->rw_offset
) | s
->loc
[locty
].sts
;
443 avail
= s
->be_buffer_size
- s
->rw_offset
;
445 * byte-sized reads should not return 0x00 for 0x100
448 if (size
== 1 && avail
> 0xff) {
451 val
= TPM_TIS_BURST_COUNT(avail
) | s
->loc
[locty
].sts
;
455 case TPM_TIS_REG_DATA_FIFO
:
456 case TPM_TIS_REG_DATA_XFIFO
... TPM_TIS_REG_DATA_XFIFO_END
:
457 if (s
->active_locty
== locty
) {
458 if (size
> 4 - (addr
& 0x3)) {
459 /* prevent access beyond FIFO */
460 size
= 4 - (addr
& 0x3);
465 switch (s
->loc
[locty
].state
) {
466 case TPM_TIS_STATE_COMPLETION
:
467 v
= tpm_tis_data_read(s
, locty
);
470 v
= TPM_TIS_NO_DATA_BYTE
;
477 shift
= 0; /* no more adjustments */
480 case TPM_TIS_REG_INTERFACE_ID
:
481 val
= s
->loc
[locty
].iface_id
;
483 case TPM_TIS_REG_DID_VID
:
484 val
= (TPM_TIS_TPM_DID
<< 16) | TPM_TIS_TPM_VID
;
486 case TPM_TIS_REG_RID
:
487 val
= TPM_TIS_TPM_RID
;
490 case TPM_TIS_REG_DEBUG
:
491 tpm_tis_dump_state(opaque
, addr
);
500 trace_tpm_tis_mmio_read(size
, addr
, val
);
506 * Write a value to a register of the TIS interface
507 * See specs pages 33-63 for description of the registers
509 static void tpm_tis_mmio_write(void *opaque
, hwaddr addr
,
510 uint64_t val
, unsigned size
)
512 TPMState
*s
= opaque
;
513 uint16_t off
= addr
& 0xffc;
514 uint8_t shift
= (addr
& 0x3) * 8;
515 uint8_t locty
= tpm_tis_locality_from_addr(addr
);
516 uint8_t active_locty
, l
;
517 int c
, set_new_locty
= 1;
519 uint32_t mask
= (size
== 1) ? 0xff : ((size
== 2) ? 0xffff : ~0);
521 trace_tpm_tis_mmio_write(size
, addr
, val
);
524 trace_tpm_tis_mmio_write_locty4();
528 if (tpm_backend_had_startup_error(s
->be_driver
)) {
542 case TPM_TIS_REG_ACCESS
:
544 if ((val
& TPM_TIS_ACCESS_SEIZE
)) {
545 val
&= ~(TPM_TIS_ACCESS_REQUEST_USE
|
546 TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
549 active_locty
= s
->active_locty
;
551 if ((val
& TPM_TIS_ACCESS_ACTIVE_LOCALITY
)) {
552 /* give up locality if currently owned */
553 if (s
->active_locty
== locty
) {
554 trace_tpm_tis_mmio_write_release_locty(locty
);
556 uint8_t newlocty
= TPM_TIS_NO_LOCALITY
;
557 /* anybody wants the locality ? */
558 for (c
= TPM_TIS_NUM_LOCALITIES
- 1; c
>= 0; c
--) {
559 if ((s
->loc
[c
].access
& TPM_TIS_ACCESS_REQUEST_USE
)) {
560 trace_tpm_tis_mmio_write_locty_req_use(c
);
565 trace_tpm_tis_mmio_write_next_locty(newlocty
);
567 if (TPM_TIS_IS_VALID_LOCTY(newlocty
)) {
569 tpm_tis_prep_abort(s
, locty
, newlocty
);
571 active_locty
= TPM_TIS_NO_LOCALITY
;
574 /* not currently the owner; clear a pending request */
575 s
->loc
[locty
].access
&= ~TPM_TIS_ACCESS_REQUEST_USE
;
579 if ((val
& TPM_TIS_ACCESS_BEEN_SEIZED
)) {
580 s
->loc
[locty
].access
&= ~TPM_TIS_ACCESS_BEEN_SEIZED
;
583 if ((val
& TPM_TIS_ACCESS_SEIZE
)) {
585 * allow seize if a locality is active and the requesting
586 * locality is higher than the one that's active
588 * allow seize for requesting locality if no locality is
591 while ((TPM_TIS_IS_VALID_LOCTY(s
->active_locty
) &&
592 locty
> s
->active_locty
) ||
593 !TPM_TIS_IS_VALID_LOCTY(s
->active_locty
)) {
594 bool higher_seize
= FALSE
;
596 /* already a pending SEIZE ? */
597 if ((s
->loc
[locty
].access
& TPM_TIS_ACCESS_SEIZE
)) {
601 /* check for ongoing seize by a higher locality */
602 for (l
= locty
+ 1; l
< TPM_TIS_NUM_LOCALITIES
; l
++) {
603 if ((s
->loc
[l
].access
& TPM_TIS_ACCESS_SEIZE
)) {
613 /* cancel any seize by a lower locality */
614 for (l
= 0; l
< locty
; l
++) {
615 s
->loc
[l
].access
&= ~TPM_TIS_ACCESS_SEIZE
;
618 s
->loc
[locty
].access
|= TPM_TIS_ACCESS_SEIZE
;
620 trace_tpm_tis_mmio_write_locty_seized(locty
, s
->active_locty
);
621 trace_tpm_tis_mmio_write_init_abort();
624 tpm_tis_prep_abort(s
, s
->active_locty
, locty
);
629 if ((val
& TPM_TIS_ACCESS_REQUEST_USE
)) {
630 if (s
->active_locty
!= locty
) {
631 if (TPM_TIS_IS_VALID_LOCTY(s
->active_locty
)) {
632 s
->loc
[locty
].access
|= TPM_TIS_ACCESS_REQUEST_USE
;
634 /* no locality active -> make this one active now */
635 active_locty
= locty
;
641 tpm_tis_new_active_locality(s
, active_locty
);
645 case TPM_TIS_REG_INT_ENABLE
:
646 if (s
->active_locty
!= locty
) {
650 s
->loc
[locty
].inte
&= mask
;
651 s
->loc
[locty
].inte
|= (val
& (TPM_TIS_INT_ENABLED
|
652 TPM_TIS_INT_POLARITY_MASK
|
653 TPM_TIS_INTERRUPTS_SUPPORTED
));
655 case TPM_TIS_REG_INT_VECTOR
:
656 /* hard wired -- ignore */
658 case TPM_TIS_REG_INT_STATUS
:
659 if (s
->active_locty
!= locty
) {
663 /* clearing of interrupt flags */
664 if (((val
& TPM_TIS_INTERRUPTS_SUPPORTED
)) &&
665 (s
->loc
[locty
].ints
& TPM_TIS_INTERRUPTS_SUPPORTED
)) {
666 s
->loc
[locty
].ints
&= ~val
;
667 if (s
->loc
[locty
].ints
== 0) {
668 qemu_irq_lower(s
->irq
);
669 trace_tpm_tis_mmio_write_lowering_irq();
672 s
->loc
[locty
].ints
&= ~(val
& TPM_TIS_INTERRUPTS_SUPPORTED
);
674 case TPM_TIS_REG_STS
:
675 if (s
->active_locty
!= locty
) {
679 if (s
->be_tpm_version
== TPM_VERSION_2_0
) {
680 /* some flags that are only supported for TPM 2 */
681 if (val
& TPM_TIS_STS_COMMAND_CANCEL
) {
682 if (s
->loc
[locty
].state
== TPM_TIS_STATE_EXECUTION
) {
684 * request the backend to cancel. Some backends may not
687 tpm_backend_cancel_cmd(s
->be_driver
);
691 if (val
& TPM_TIS_STS_RESET_ESTABLISHMENT_BIT
) {
692 if (locty
== 3 || locty
== 4) {
693 tpm_backend_reset_tpm_established_flag(s
->be_driver
, locty
);
698 val
&= (TPM_TIS_STS_COMMAND_READY
| TPM_TIS_STS_TPM_GO
|
699 TPM_TIS_STS_RESPONSE_RETRY
);
701 if (val
== TPM_TIS_STS_COMMAND_READY
) {
702 switch (s
->loc
[locty
].state
) {
704 case TPM_TIS_STATE_READY
:
708 case TPM_TIS_STATE_IDLE
:
709 tpm_tis_sts_set(&s
->loc
[locty
], TPM_TIS_STS_COMMAND_READY
);
710 s
->loc
[locty
].state
= TPM_TIS_STATE_READY
;
711 tpm_tis_raise_irq(s
, locty
, TPM_TIS_INT_COMMAND_READY
);
714 case TPM_TIS_STATE_EXECUTION
:
715 case TPM_TIS_STATE_RECEPTION
:
716 /* abort currently running command */
717 trace_tpm_tis_mmio_write_init_abort();
718 tpm_tis_prep_abort(s
, locty
, locty
);
721 case TPM_TIS_STATE_COMPLETION
:
723 /* shortcut to ready state with C/R set */
724 s
->loc
[locty
].state
= TPM_TIS_STATE_READY
;
725 if (!(s
->loc
[locty
].sts
& TPM_TIS_STS_COMMAND_READY
)) {
726 tpm_tis_sts_set(&s
->loc
[locty
],
727 TPM_TIS_STS_COMMAND_READY
);
728 tpm_tis_raise_irq(s
, locty
, TPM_TIS_INT_COMMAND_READY
);
730 s
->loc
[locty
].sts
&= ~(TPM_TIS_STS_DATA_AVAILABLE
);
734 } else if (val
== TPM_TIS_STS_TPM_GO
) {
735 switch (s
->loc
[locty
].state
) {
736 case TPM_TIS_STATE_RECEPTION
:
737 if ((s
->loc
[locty
].sts
& TPM_TIS_STS_EXPECT
) == 0) {
738 tpm_tis_tpm_send(s
, locty
);
745 } else if (val
== TPM_TIS_STS_RESPONSE_RETRY
) {
746 switch (s
->loc
[locty
].state
) {
747 case TPM_TIS_STATE_COMPLETION
:
749 tpm_tis_sts_set(&s
->loc
[locty
],
751 TPM_TIS_STS_DATA_AVAILABLE
);
759 case TPM_TIS_REG_DATA_FIFO
:
760 case TPM_TIS_REG_DATA_XFIFO
... TPM_TIS_REG_DATA_XFIFO_END
:
762 if (s
->active_locty
!= locty
) {
766 if (s
->loc
[locty
].state
== TPM_TIS_STATE_IDLE
||
767 s
->loc
[locty
].state
== TPM_TIS_STATE_EXECUTION
||
768 s
->loc
[locty
].state
== TPM_TIS_STATE_COMPLETION
) {
771 trace_tpm_tis_mmio_write_data2send(val
, size
);
772 if (s
->loc
[locty
].state
== TPM_TIS_STATE_READY
) {
773 s
->loc
[locty
].state
= TPM_TIS_STATE_RECEPTION
;
774 tpm_tis_sts_set(&s
->loc
[locty
],
775 TPM_TIS_STS_EXPECT
| TPM_TIS_STS_VALID
);
779 if (size
> 4 - (addr
& 0x3)) {
780 /* prevent access beyond FIFO */
781 size
= 4 - (addr
& 0x3);
784 while ((s
->loc
[locty
].sts
& TPM_TIS_STS_EXPECT
) && size
> 0) {
785 if (s
->rw_offset
< s
->be_buffer_size
) {
786 s
->buffer
[s
->rw_offset
++] =
791 tpm_tis_sts_set(&s
->loc
[locty
], TPM_TIS_STS_VALID
);
795 /* check for complete packet */
796 if (s
->rw_offset
> 5 &&
797 (s
->loc
[locty
].sts
& TPM_TIS_STS_EXPECT
)) {
798 /* we have a packet length - see if we have all of it */
799 bool need_irq
= !(s
->loc
[locty
].sts
& TPM_TIS_STS_VALID
);
801 len
= tpm_cmd_get_size(&s
->buffer
);
802 if (len
> s
->rw_offset
) {
803 tpm_tis_sts_set(&s
->loc
[locty
],
804 TPM_TIS_STS_EXPECT
| TPM_TIS_STS_VALID
);
806 /* packet complete */
807 tpm_tis_sts_set(&s
->loc
[locty
], TPM_TIS_STS_VALID
);
810 tpm_tis_raise_irq(s
, locty
, TPM_TIS_INT_STS_VALID
);
815 case TPM_TIS_REG_INTERFACE_ID
:
816 if (val
& TPM_TIS_IFACE_ID_INT_SEL_LOCK
) {
817 for (l
= 0; l
< TPM_TIS_NUM_LOCALITIES
; l
++) {
818 s
->loc
[l
].iface_id
|= TPM_TIS_IFACE_ID_INT_SEL_LOCK
;
825 static const MemoryRegionOps tpm_tis_memory_ops
= {
826 .read
= tpm_tis_mmio_read
,
827 .write
= tpm_tis_mmio_write
,
828 .endianness
= DEVICE_LITTLE_ENDIAN
,
830 .min_access_size
= 1,
831 .max_access_size
= 4,
836 * Get the TPMVersion of the backend device being used
838 static enum TPMVersion
tpm_tis_get_tpm_version(TPMIf
*ti
)
840 TPMState
*s
= TPM(ti
);
842 if (tpm_backend_had_startup_error(s
->be_driver
)) {
843 return TPM_VERSION_UNSPEC
;
846 return tpm_backend_get_tpm_version(s
->be_driver
);
850 * This function is called when the machine starts, resets or due to
853 static void tpm_tis_reset(DeviceState
*dev
)
855 TPMState
*s
= TPM(dev
);
858 s
->be_tpm_version
= tpm_backend_get_tpm_version(s
->be_driver
);
859 s
->be_buffer_size
= MIN(tpm_backend_get_buffer_size(s
->be_driver
),
862 if (s
->ppi_enabled
) {
863 tpm_ppi_reset(&s
->ppi
);
865 tpm_backend_reset(s
->be_driver
);
867 s
->active_locty
= TPM_TIS_NO_LOCALITY
;
868 s
->next_locty
= TPM_TIS_NO_LOCALITY
;
869 s
->aborting_locty
= TPM_TIS_NO_LOCALITY
;
871 for (c
= 0; c
< TPM_TIS_NUM_LOCALITIES
; c
++) {
872 s
->loc
[c
].access
= TPM_TIS_ACCESS_TPM_REG_VALID_STS
;
873 switch (s
->be_tpm_version
) {
874 case TPM_VERSION_UNSPEC
:
876 case TPM_VERSION_1_2
:
877 s
->loc
[c
].sts
= TPM_TIS_STS_TPM_FAMILY1_2
;
878 s
->loc
[c
].iface_id
= TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3
;
880 case TPM_VERSION_2_0
:
881 s
->loc
[c
].sts
= TPM_TIS_STS_TPM_FAMILY2_0
;
882 s
->loc
[c
].iface_id
= TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0
;
885 s
->loc
[c
].inte
= TPM_TIS_INT_POLARITY_LOW_LEVEL
;
887 s
->loc
[c
].state
= TPM_TIS_STATE_IDLE
;
892 if (tpm_backend_startup_tpm(s
->be_driver
, s
->be_buffer_size
) < 0) {
897 /* persistent state handling */
899 static int tpm_tis_pre_save(void *opaque
)
901 TPMState
*s
= opaque
;
902 uint8_t locty
= s
->active_locty
;
904 trace_tpm_tis_pre_save(locty
, s
->rw_offset
);
907 tpm_tis_dump_state(opaque
, 0);
911 * Synchronize with backend completion.
913 tpm_backend_finish_sync(s
->be_driver
);
918 static const VMStateDescription vmstate_locty
= {
919 .name
= "tpm-tis/locty",
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(),
932 static const VMStateDescription vmstate_tpm_tis
= {
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
),
943 VMSTATE_STRUCT_ARRAY(loc
, TPMState
, TPM_TIS_NUM_LOCALITIES
, 0,
944 vmstate_locty
, TPMLocality
),
946 VMSTATE_END_OF_LIST()
950 static Property tpm_tis_properties
[] = {
951 DEFINE_PROP_UINT32("irq", TPMState
, irq_num
, TPM_TIS_IRQ
),
952 DEFINE_PROP_TPMBE("tpmdev", TPMState
, be_driver
),
953 DEFINE_PROP_BOOL("ppi", TPMState
, ppi_enabled
, true),
954 DEFINE_PROP_END_OF_LIST(),
957 static void tpm_tis_realizefn(DeviceState
*dev
, Error
**errp
)
959 TPMState
*s
= TPM(dev
);
962 error_setg(errp
, "at most one TPM device is permitted");
967 error_setg(errp
, "'tpmdev' property is required");
970 if (s
->irq_num
> 15) {
971 error_setg(errp
, "IRQ %d is outside valid range of 0 to 15",
976 isa_init_irq(&s
->busdev
, &s
->irq
, s
->irq_num
);
978 memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev
)),
979 TPM_TIS_ADDR_BASE
, &s
->mmio
);
981 if (s
->ppi_enabled
) {
982 tpm_ppi_init(&s
->ppi
, isa_address_space(ISA_DEVICE(dev
)),
983 TPM_PPI_ADDR_BASE
, OBJECT(s
));
987 static void tpm_tis_initfn(Object
*obj
)
989 TPMState
*s
= TPM(obj
);
991 memory_region_init_io(&s
->mmio
, OBJECT(s
), &tpm_tis_memory_ops
,
993 TPM_TIS_NUM_LOCALITIES
<< TPM_TIS_LOCALITY_SHIFT
);
996 static void tpm_tis_class_init(ObjectClass
*klass
, void *data
)
998 DeviceClass
*dc
= DEVICE_CLASS(klass
);
999 TPMIfClass
*tc
= TPM_IF_CLASS(klass
);
1001 dc
->realize
= tpm_tis_realizefn
;
1002 device_class_set_props(dc
, tpm_tis_properties
);
1003 dc
->reset
= tpm_tis_reset
;
1004 dc
->vmsd
= &vmstate_tpm_tis
;
1005 tc
->model
= TPM_MODEL_TPM_TIS
;
1006 tc
->get_version
= tpm_tis_get_tpm_version
;
1007 tc
->request_completed
= tpm_tis_request_completed
;
1010 static const TypeInfo tpm_tis_info
= {
1011 .name
= TYPE_TPM_TIS
,
1012 .parent
= TYPE_ISA_DEVICE
,
1013 .instance_size
= sizeof(TPMState
),
1014 .instance_init
= tpm_tis_initfn
,
1015 .class_init
= tpm_tis_class_init
,
1016 .interfaces
= (InterfaceInfo
[]) {
1022 static void tpm_tis_register(void)
1024 type_register_static(&tpm_tis_info
);
1027 type_init(tpm_tis_register
)