2 * tpm_tis_i2c.c - QEMU's TPM TIS I2C Device
4 * Copyright (c) 2023 IBM Corporation
7 * Ninad Palsule <ninad@linux.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 * TPM I2C implementation follows TCG TPM I2c Interface specification,
13 * Family 2.0, Level 00, Revision 1.00
15 * TPM TIS for TPM 2 implementation following TCG PC Client Platform
16 * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
20 #include "qemu/osdep.h"
21 #include "hw/i2c/i2c.h"
22 #include "hw/sysbus.h"
23 #include "hw/acpi/tpm.h"
24 #include "migration/vmstate.h"
34 /* Is locality valid */
35 #define TPM_TIS_I2C_IS_VALID_LOCTY(x) TPM_TIS_IS_VALID_LOCTY(x)
37 typedef struct TPMStateI2C
{
41 uint8_t offset
; /* offset into data[] */
42 uint8_t operation
; /* OP_SEND & OP_RECV */
43 uint8_t data
[5]; /* Data */
46 uint8_t loc_sel
; /* Current locality */
47 uint8_t csum_enable
; /* Is checksum enabled */
49 /* Derived from the above */
50 const char *reg_name
; /* Register name */
51 uint32_t tis_addr
; /* Converted tis address including locty */
54 TPMState state
; /* not a QOM object */
58 DECLARE_INSTANCE_CHECKER(TPMStateI2C
, TPM_TIS_I2C
,
62 static inline void tpm_tis_i2c_to_tis_reg(TPMStateI2C
*i2cst
, uint8_t i2c_reg
);
65 typedef struct regMap
{
66 uint8_t i2c_reg
; /* I2C register */
67 uint16_t tis_reg
; /* TIS register */
68 const char *reg_name
; /* Register name */
72 * The register values in the common code is different than the latest
73 * register numbers as per the spec hence add the conversion map
75 static const I2CRegMap tpm_tis_reg_map
[] = {
77 * These registers are sent to TIS layer. The register with UNKNOWN
78 * mapping are not sent to TIS layer and handled in I2c layer.
79 * NOTE: Adding frequently used registers at the start
81 { TPM_I2C_REG_DATA_FIFO
, TPM_TIS_REG_DATA_FIFO
, "FIFO", },
82 { TPM_I2C_REG_STS
, TPM_TIS_REG_STS
, "STS", },
83 { TPM_I2C_REG_DATA_CSUM_GET
, TPM_I2C_REG_UNKNOWN
, "CSUM_GET", },
84 { TPM_I2C_REG_LOC_SEL
, TPM_I2C_REG_UNKNOWN
, "LOC_SEL", },
85 { TPM_I2C_REG_ACCESS
, TPM_TIS_REG_ACCESS
, "ACCESS", },
86 { TPM_I2C_REG_INT_ENABLE
, TPM_TIS_REG_INT_ENABLE
, "INTR_ENABLE",},
87 { TPM_I2C_REG_INT_CAPABILITY
, TPM_I2C_REG_UNKNOWN
, "INTR_CAP", },
88 { TPM_I2C_REG_INTF_CAPABILITY
, TPM_TIS_REG_INTF_CAPABILITY
, "INTF_CAP", },
89 { TPM_I2C_REG_DID_VID
, TPM_TIS_REG_DID_VID
, "DID_VID", },
90 { TPM_I2C_REG_RID
, TPM_TIS_REG_RID
, "RID", },
91 { TPM_I2C_REG_I2C_DEV_ADDRESS
, TPM_I2C_REG_UNKNOWN
, "DEV_ADDRESS",},
92 { TPM_I2C_REG_DATA_CSUM_ENABLE
, TPM_I2C_REG_UNKNOWN
, "CSUM_ENABLE",},
95 static int tpm_tis_i2c_pre_save(void *opaque
)
97 TPMStateI2C
*i2cst
= opaque
;
99 return tpm_tis_pre_save(&i2cst
->state
);
102 static int tpm_tis_i2c_post_load(void *opaque
, int version_id
)
104 TPMStateI2C
*i2cst
= opaque
;
106 if (i2cst
->offset
>= 1) {
107 tpm_tis_i2c_to_tis_reg(i2cst
, i2cst
->data
[0]);
113 static const VMStateDescription vmstate_tpm_tis_i2c
= {
114 .name
= "tpm-tis-i2c",
116 .pre_save
= tpm_tis_i2c_pre_save
,
117 .post_load
= tpm_tis_i2c_post_load
,
118 .fields
= (VMStateField
[]) {
119 VMSTATE_BUFFER(state
.buffer
, TPMStateI2C
),
120 VMSTATE_UINT16(state
.rw_offset
, TPMStateI2C
),
121 VMSTATE_UINT8(state
.active_locty
, TPMStateI2C
),
122 VMSTATE_UINT8(state
.aborting_locty
, TPMStateI2C
),
123 VMSTATE_UINT8(state
.next_locty
, TPMStateI2C
),
125 VMSTATE_STRUCT_ARRAY(state
.loc
, TPMStateI2C
, TPM_TIS_NUM_LOCALITIES
, 0,
126 vmstate_locty
, TPMLocality
),
129 VMSTATE_UINT8(offset
, TPMStateI2C
),
130 VMSTATE_UINT8(operation
, TPMStateI2C
),
131 VMSTATE_BUFFER(data
, TPMStateI2C
),
132 VMSTATE_UINT8(loc_sel
, TPMStateI2C
),
133 VMSTATE_UINT8(csum_enable
, TPMStateI2C
),
135 VMSTATE_END_OF_LIST()
140 * Set data value. The i2cst->offset is not updated as called in
143 static void tpm_tis_i2c_set_data(TPMStateI2C
*i2cst
, uint32_t data
)
145 i2cst
->data
[1] = data
;
146 i2cst
->data
[2] = data
>> 8;
147 i2cst
->data
[3] = data
>> 16;
148 i2cst
->data
[4] = data
>> 24;
151 * Generate interface capability based on what is returned by TIS and what is
152 * expected by I2C. Save the capability in the data array overwriting the TIS
155 static uint32_t tpm_tis_i2c_interface_capability(TPMStateI2C
*i2cst
,
160 /* Now generate i2c capability */
161 i2c_cap
= (TPM_I2C_CAP_INTERFACE_TYPE
|
162 TPM_I2C_CAP_INTERFACE_VER
|
163 TPM_I2C_CAP_TPM2_FAMILY
|
164 TPM_I2C_CAP_LOCALITY_CAP
|
165 TPM_I2C_CAP_BUS_SPEED
|
166 TPM_I2C_CAP_DEV_ADDR_CHANGE
);
168 /* Now check the TIS and set some capabilities */
170 /* Static burst count set */
171 if (tis_cap
& TPM_TIS_CAP_BURST_COUNT_STATIC
) {
172 i2c_cap
|= TPM_I2C_CAP_BURST_COUNT_STATIC
;
178 /* Convert I2C register to TIS address and returns the name of the register */
179 static inline void tpm_tis_i2c_to_tis_reg(TPMStateI2C
*i2cst
, uint8_t i2c_reg
)
181 const I2CRegMap
*reg_map
;
184 i2cst
->tis_addr
= 0xffffffff;
186 /* Special case for the STS register. */
187 if (i2c_reg
>= TPM_I2C_REG_STS
&& i2c_reg
<= TPM_I2C_REG_STS
+ 3) {
188 i2c_reg
= TPM_I2C_REG_STS
;
191 for (i
= 0; i
< ARRAY_SIZE(tpm_tis_reg_map
); i
++) {
192 reg_map
= &tpm_tis_reg_map
[i
];
193 if (reg_map
->i2c_reg
== i2c_reg
) {
194 i2cst
->reg_name
= reg_map
->reg_name
;
195 i2cst
->tis_addr
= reg_map
->tis_reg
;
197 /* Include the locality in the address. */
198 assert(TPM_TIS_I2C_IS_VALID_LOCTY(i2cst
->loc_sel
));
199 i2cst
->tis_addr
+= (i2cst
->loc_sel
<< TPM_TIS_LOCALITY_SHIFT
);
205 /* Clear some fields from the structure. */
206 static inline void tpm_tis_i2c_clear_data(TPMStateI2C
*i2cst
)
208 /* Clear operation and offset */
209 i2cst
->operation
= 0;
211 i2cst
->tis_addr
= 0xffffffff;
212 i2cst
->reg_name
= NULL
;
213 memset(i2cst
->data
, 0, sizeof(i2cst
->data
));
218 /* Send data to TPM */
219 static inline void tpm_tis_i2c_tpm_send(TPMStateI2C
*i2cst
)
225 if ((i2cst
->operation
== OP_SEND
) && (i2cst
->offset
> 1)) {
227 switch (i2cst
->data
[0]) {
228 case TPM_I2C_REG_DATA_CSUM_ENABLE
:
230 * Checksum is not handled by TIS code hence we will consume the
233 i2cst
->csum_enable
= i2cst
->data
[1] & TPM_DATA_CSUM_ENABLED
;
235 case TPM_I2C_REG_DATA_FIFO
:
236 /* Handled in the main i2c_send function */
238 case TPM_I2C_REG_LOC_SEL
:
240 * This register is not handled by TIS so save the locality
243 if (TPM_TIS_I2C_IS_VALID_LOCTY(i2cst
->data
[1])) {
244 i2cst
->loc_sel
= i2cst
->data
[1];
248 /* We handle non-FIFO here */
250 /* Index 0 is a register. Convert byte stream to uint32_t */
251 data
= i2cst
->data
[1];
252 data
|= i2cst
->data
[2] << 8;
253 data
|= i2cst
->data
[3] << 16;
254 data
|= i2cst
->data
[4] << 24;
256 /* Add register specific masking */
257 switch (i2cst
->data
[0]) {
258 case TPM_I2C_REG_INT_ENABLE
:
259 data
&= TPM_I2C_INT_ENABLE_MASK
;
261 case TPM_I2C_REG_STS
... TPM_I2C_REG_STS
+ 3:
263 * STS register has 4 bytes data.
264 * As per the specs following writes must be allowed.
265 * - From base address 1 to 4 bytes are allowed.
266 * - Single byte write to first or last byte must
269 offset
= i2cst
->data
[0] - TPM_I2C_REG_STS
;
273 data
&= (TPM_I2C_STS_WRITE_MASK
>> (offset
* 8));
277 tpm_tis_write_data(&i2cst
->state
, i2cst
->tis_addr
+ offset
, data
,
282 tpm_tis_i2c_clear_data(i2cst
);
288 /* Callback from TPM to indicate that response is copied */
289 static void tpm_tis_i2c_request_completed(TPMIf
*ti
, int ret
)
291 TPMStateI2C
*i2cst
= TPM_TIS_I2C(ti
);
292 TPMState
*s
= &i2cst
->state
;
294 /* Inform the common code. */
295 tpm_tis_request_completed(s
, ret
);
298 static enum TPMVersion
tpm_tis_i2c_get_tpm_version(TPMIf
*ti
)
300 TPMStateI2C
*i2cst
= TPM_TIS_I2C(ti
);
301 TPMState
*s
= &i2cst
->state
;
303 return tpm_tis_get_tpm_version(s
);
306 static int tpm_tis_i2c_event(I2CSlave
*i2c
, enum i2c_event event
)
308 TPMStateI2C
*i2cst
= TPM_TIS_I2C(i2c
);
313 trace_tpm_tis_i2c_event("START_RECV");
316 trace_tpm_tis_i2c_event("START_SEND");
317 tpm_tis_i2c_clear_data(i2cst
);
320 trace_tpm_tis_i2c_event("FINISH");
321 if (i2cst
->operation
== OP_SEND
) {
322 tpm_tis_i2c_tpm_send(i2cst
);
324 tpm_tis_i2c_clear_data(i2cst
);
335 * If data is for FIFO then it is received from tpm_tis_common buffer
336 * otherwise it will be handled using single call to common code and
337 * cached in the local buffer.
339 static uint8_t tpm_tis_i2c_recv(I2CSlave
*i2c
)
343 TPMStateI2C
*i2cst
= TPM_TIS_I2C(i2c
);
344 TPMState
*s
= &i2cst
->state
;
345 uint16_t i2c_reg
= i2cst
->data
[0];
348 if (i2cst
->operation
== OP_RECV
) {
350 /* Do not cache FIFO data. */
351 if (i2cst
->data
[0] == TPM_I2C_REG_DATA_FIFO
) {
352 data_read
= tpm_tis_read_data(s
, i2cst
->tis_addr
, 1);
353 ret
= (data_read
& 0xff);
354 } else if (i2cst
->offset
< sizeof(i2cst
->data
)) {
355 ret
= i2cst
->data
[i2cst
->offset
++];
358 } else if ((i2cst
->operation
== OP_SEND
) && (i2cst
->offset
< 2)) {
359 /* First receive call after send */
361 i2cst
->operation
= OP_RECV
;
364 case TPM_I2C_REG_LOC_SEL
:
365 /* Location selection register is managed by i2c */
366 tpm_tis_i2c_set_data(i2cst
, i2cst
->loc_sel
);
368 case TPM_I2C_REG_DATA_FIFO
:
369 /* FIFO data is directly read from TPM TIS */
370 data_read
= tpm_tis_read_data(s
, i2cst
->tis_addr
, 1);
371 tpm_tis_i2c_set_data(i2cst
, (data_read
& 0xff));
373 case TPM_I2C_REG_DATA_CSUM_ENABLE
:
374 tpm_tis_i2c_set_data(i2cst
, i2cst
->csum_enable
);
376 case TPM_I2C_REG_INT_CAPABILITY
:
378 * Interrupt is not supported in the linux kernel hence we cannot
379 * test this model with interrupts.
381 tpm_tis_i2c_set_data(i2cst
, TPM_I2C_INT_ENABLE_MASK
);
383 case TPM_I2C_REG_DATA_CSUM_GET
:
385 * Checksum registers are not supported by common code hence
386 * call a common code to get the checksum.
388 data_read
= tpm_tis_get_checksum(s
);
390 /* Save the byte stream in data field */
391 tpm_tis_i2c_set_data(i2cst
, data_read
);
394 data_read
= tpm_tis_read_data(s
, i2cst
->tis_addr
, 4);
397 case TPM_I2C_REG_INTF_CAPABILITY
:
398 /* Prepare the capabilities as per I2C interface */
399 data_read
= tpm_tis_i2c_interface_capability(i2cst
,
402 case TPM_I2C_REG_STS
... TPM_I2C_REG_STS
+ 3:
403 offset
= i2c_reg
- TPM_I2C_REG_STS
;
405 * As per specs, STS bit 31:26 are reserved and must
408 data_read
&= TPM_I2C_STS_READ_MASK
;
410 * STS register has 4 bytes data.
411 * As per the specs following reads must be allowed.
412 * - From base address 1 to 4 bytes are allowed.
413 * - Last byte must be allowed to read as a single byte
414 * - Second and third byte must be allowed to read as two
417 data_read
>>= (offset
* 8);
421 /* Save byte stream in data[] */
422 tpm_tis_i2c_set_data(i2cst
, data_read
);
426 /* Return first byte with this call */
427 i2cst
->offset
= 1; /* keep the register value intact for debug */
428 ret
= i2cst
->data
[i2cst
->offset
++];
430 i2cst
->operation
= OP_RECV
;
433 trace_tpm_tis_i2c_recv(ret
);
439 * Send function only remembers data in the buffer and then calls
440 * TPM TIS common code during FINISH event.
442 static int tpm_tis_i2c_send(I2CSlave
*i2c
, uint8_t data
)
444 TPMStateI2C
*i2cst
= TPM_TIS_I2C(i2c
);
446 /* Reject non-supported registers. */
447 if (i2cst
->offset
== 0) {
448 /* Convert I2C register to TIS register */
449 tpm_tis_i2c_to_tis_reg(i2cst
, data
);
450 if (i2cst
->tis_addr
== 0xffffffff) {
454 trace_tpm_tis_i2c_send_reg(i2cst
->reg_name
, data
);
456 /* We do not support device address change */
457 if (data
== TPM_I2C_REG_I2C_DEV_ADDRESS
) {
458 qemu_log_mask(LOG_UNIMP
, "%s: Device address change "
459 "is not supported.\n", __func__
);
463 trace_tpm_tis_i2c_send(data
);
466 if (i2cst
->offset
< sizeof(i2cst
->data
)) {
467 i2cst
->operation
= OP_SEND
;
470 * In two cases, we save values in the local buffer.
471 * 1) The first value is always a register.
472 * 2) In case of non-FIFO multibyte registers, TIS expects full
473 * register value hence I2C layer cache the register value and send
474 * to TIS during FINISH event.
476 if ((i2cst
->offset
== 0) ||
477 (i2cst
->data
[0] != TPM_I2C_REG_DATA_FIFO
)) {
478 i2cst
->data
[i2cst
->offset
++] = data
;
481 * The TIS can process FIFO data one byte at a time hence the FIFO
482 * data is sent to TIS directly.
484 tpm_tis_write_data(&i2cst
->state
, i2cst
->tis_addr
, data
, 1);
490 /* Return non-zero to indicate NAK */
494 static Property tpm_tis_i2c_properties
[] = {
495 DEFINE_PROP_TPMBE("tpmdev", TPMStateI2C
, state
.be_driver
),
496 DEFINE_PROP_END_OF_LIST(),
499 static void tpm_tis_i2c_realizefn(DeviceState
*dev
, Error
**errp
)
501 TPMStateI2C
*i2cst
= TPM_TIS_I2C(dev
);
502 TPMState
*s
= &i2cst
->state
;
505 error_setg(errp
, "at most one TPM device is permitted");
510 * Get the backend pointer. It is not initialized propery during
511 * device_class_set_props
513 s
->be_driver
= qemu_find_tpm_be("tpm0");
516 error_setg(errp
, "'tpmdev' property is required");
521 static void tpm_tis_i2c_reset(DeviceState
*dev
)
523 TPMStateI2C
*i2cst
= TPM_TIS_I2C(dev
);
524 TPMState
*s
= &i2cst
->state
;
526 tpm_tis_i2c_clear_data(i2cst
);
528 i2cst
->csum_enable
= 0;
529 i2cst
->loc_sel
= 0x00;
531 return tpm_tis_reset(s
);
534 static void tpm_tis_i2c_class_init(ObjectClass
*klass
, void *data
)
536 DeviceClass
*dc
= DEVICE_CLASS(klass
);
537 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
538 TPMIfClass
*tc
= TPM_IF_CLASS(klass
);
540 dc
->realize
= tpm_tis_i2c_realizefn
;
541 dc
->reset
= tpm_tis_i2c_reset
;
542 dc
->vmsd
= &vmstate_tpm_tis_i2c
;
543 device_class_set_props(dc
, tpm_tis_i2c_properties
);
544 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
546 k
->event
= tpm_tis_i2c_event
;
547 k
->recv
= tpm_tis_i2c_recv
;
548 k
->send
= tpm_tis_i2c_send
;
550 tc
->model
= TPM_MODEL_TPM_TIS
;
551 tc
->request_completed
= tpm_tis_i2c_request_completed
;
552 tc
->get_version
= tpm_tis_i2c_get_tpm_version
;
555 static const TypeInfo tpm_tis_i2c_info
= {
556 .name
= TYPE_TPM_TIS_I2C
,
557 .parent
= TYPE_I2C_SLAVE
,
558 .instance_size
= sizeof(TPMStateI2C
),
559 .class_init
= tpm_tis_i2c_class_init
,
560 .interfaces
= (InterfaceInfo
[]) {
566 static void tpm_tis_i2c_register_types(void)
568 type_register_static(&tpm_tis_i2c_info
);
571 type_init(tpm_tis_i2c_register_types
)