Add support for TPM devices over I2C bus
PATCH v5 1/3 docs: Add support for TPM devices over I2C bus
In reply to Ninad PalsuleThis is a documentation change for I2C TPM device support.
Qemu already supports devices attached to ISA and sysbus. This drop adds support for the I2C bus attached TPM devices.
---
V2:
Incorporated Stephen's review comments
- Added example in the document.
---
V4:
Incorporate Cedric & Stefan's comments
- Added example for ast2600-evb
- Corrected statement about arm virtual machine.
--- docs/specs/tpm.rst | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
docs/specs/tpm.rst +32 -0
@@ -21,12 +21,16 @@ QEMU files related to TPM TIS interface: - ``hw/tpm/tpm_tis_common.c`` - ``hw/tpm/tpm_tis_isa.c`` - ``hw/tpm/tpm_tis_sysbus.c`` + - ``hw/tpm/tpm_tis_i2c.c`` - ``hw/tpm/tpm_tis.h`` Both an ISA device and a sysbus device are available. The former is used with pc/q35 machine while the latter can be instantiated in the Arm virt machine. +An I2C device support is also added which can be instantiated in the arm +based emulation machines. This device only supports the TPM 2 protocol. + CRB interface -------------
@@ -348,6 +352,34 @@ In case an Arm virt machine is emulated, use the following command line: -drive if=pflash,format=raw,file=flash0.img,readonly=on \ -drive if=pflash,format=raw,file=flash1.img +In case a ast2600-evb bmc machine is emulated and want to use TPM device +attached to I2C bus, use the following command line: + +.. code-block:: console + + qemu-system-arm -M ast2600-evb -nographic \ + -kernel arch/arm/boot/zImage \ + -dtb arch/arm/boot/dts/aspeed-ast2600-evb.dtb \ + -initrd rootfs.cpio \ + -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \ + -tpmdev emulator,id=tpm0,chardev=chrtpm \ + -device tpm-tis-i2c,tpmdev=tpm0,bus=aspeed.i2c.bus.12,address=0x2e + +In case a Rainier bmc machine is emulated and want to use TPM device +attached to I2C bus, use the following command line: + +.. code-block:: console + + qemu-system-arm -M rainier-bmc -nographic \ + -kernel ${IMAGEPATH}/fitImage-linux.bin \ + -dtb ${IMAGEPATH}/aspeed-bmc-ibm-rainier.dtb \ + -initrd ${IMAGEPATH}/obmc-phosphor-initramfs.rootfs.cpio.xz \ + -drive file=${IMAGEPATH}/obmc-phosphor-image.rootfs.wic.qcow2,if=sd,index=2\ + -net nic -net user,hostfwd=:127.0.0.1:2222-:22,hostfwd=:127.0.0.1:2443-:443\ + -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \ + -tpmdev emulator,id=tpm0,chardev=chrtpm \ + -device tpm-tis-i2c,tpmdev=tpm0,bus=aspeed.i2c.bus.12,address=0x2e + In case SeaBIOS is used as firmware, it should show the TPM menu item after entering the menu with 'ESC'.
Signature · 1 line
2.37.2
PATCH v5 2/3 tpm: Extend common APIs to support TPM TIS I2C
In reply to Ninad PalsuleQemu already supports devices attached to ISA and sysbus. This drop adds support for the I2C bus attached TPM devices.
This commit includes changes for the common code.
- Added support for the new checksum registers which are required for the I2C support. The checksum calculation is handled in the qemu common code.
- Added wrapper function for read and write data so that I2C code can call it without MMIO interface.
---
V2:
Incorporated Stephen's comments.
- Removed checksum enable and checksum get registers.
- Added checksum calculation function which can be called from i2c layer.
---
V3:
Incorporated review comments from Cedric and Stefan.
- Pass locality to the checksum calculation function and cleanup
- Moved I2C related definations in the acpi/tpm.h
---
V4:
Incorporated review comments by Stefan
- Remove the check for locality while calculating checksum
- Use bswap16 instead of cpu_ti_be16.
- Rename TPM_I2C register by dropping _TIS_ from it.
--- hw/tpm/tpm_tis.h | 3 +++ hw/tpm/tpm_tis_common.c | 28 ++++++++++++++++++++++++++++ include/hw/acpi/tpm.h | 28 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+)
hw/tpm/tpm_tis.h +3 -0
@@ -86,5 +86,8 @@ int tpm_tis_pre_save(TPMState *s);void tpm_tis_reset (TPMState *s);enum TPMVersion tpm_tis_get_tpm_version (TPMState *s);void tpm_tis_request_completed (TPMState *s,int ret); +uint32_t tpm_tis_read_data (TPMState *s,hwaddr addr,unsigned size); +void tpm_tis_write_data (TPMState *s,hwaddr addr,uint64_t val,uint32_t size); +uint16_t tpm_tis_get_checksum (TPMState *s);#endif /* TPM_TPM_TIS_H */
hw/tpm/tpm_tis_common.c +28 -0
@@ -26,6 +26,8 @@#include "hw/irq.h" #include "hw/isa/isa.h" #include "qapi/error.h" +#include "qemu/bswap.h" +#include "qemu/crc-ccitt.h" #include "qemu/module.h" #include "hw/acpi/tpm.h"
@@ -447,6 +449,23 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,return val; } +/* +* A wrapper read function so that it can be directly called without +* mmio. +*/ +uint32_t tpm_tis_read_data (TPMState *s,hwaddr addr,unsigned size) +{ +return tpm_tis_mmio_read (s, addr, size); +} + +/* +* Calculate current data buffer checksum +*/ +uint16_t tpm_tis_get_checksum (TPMState *s) +{ +return bswap16 (crc_ccitt (0 , s->buffer, s->rw_offset)); +} +/* * Write a value to a register of the TIS interface * See specs pages 33-63 for description of the registers
@@ -767,6 +786,15 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, } } +/* +* A wrapper write function so that it can be directly called without +* mmio. +*/ +void tpm_tis_write_data (TPMState *s,hwaddr addr,uint64_t val,uint32_t size) +{ +tpm_tis_mmio_write (s, addr, val, size); +} +const MemoryRegionOps tpm_tis_memory_ops = { .read = tpm_tis_mmio_read, .write = tpm_tis_mmio_write,
include/hw/acpi/tpm.h +28 -0
@@ -93,6 +93,7 @@#define TPM_TIS_CAP_DATA_TRANSFER_64B (3 << 9)#define TPM_TIS_CAP_DATA_TRANSFER_LEGACY (0 << 9)#define TPM_TIS_CAP_BURST_COUNT_DYNAMIC (0 << 8) +#define TPM_TIS_CAP_BURST_COUNT_STATIC (1 << 8)#define TPM_TIS_CAP_INTERRUPT_LOW_LEVEL (1 << 4)/* support is mandatory */ #define TPM_TIS_CAPABILITIES_SUPPORTED1_3 \ (TPM_TIS_CAP_INTERRUPT_LOW_LEVEL | \
@@ -209,6 +210,33 @@ REG32(CRB_DATA_BUFFER, 0x80)#define TPM_PPI_FUNC_ALLOWED_USR_NOT_REQ (4 << 0)#define TPM_PPI_FUNC_MASK (7 << 0) +/* TPM TIS I2C registers */ +#define TPM_I2C_REG_LOC_SEL 0x00 +#define TPM_I2C_REG_ACCESS 0x04 +#define TPM_I2C_REG_INT_ENABLE 0x08 +#define TPM_I2C_REG_INT_CAPABILITY 0x14 +#define TPM_I2C_REG_STS 0x18 +#define TPM_I2C_REG_DATA_FIFO 0x24 +#define TPM_I2C_REG_INTF_CAPABILITY 0x30 +#define TPM_I2C_REG_I2C_DEV_ADDRESS 0x38 +#define TPM_I2C_REG_DATA_CSUM_ENABLE 0x40 +#define TPM_I2C_REG_DATA_CSUM_GET 0x44 +#define TPM_I2C_REG_DID_VID 0x48 +#define TPM_I2C_REG_RID 0x4c +#define TPM_I2C_REG_UNKNOWN 0xff + +/* I2C specific interface capabilities */ +#define TPM_I2C_CAP_INTERFACE_TYPE (0x2 << 0)/* FIFO interface */ +#define TPM_I2C_CAP_INTERFACE_VER (0x0 << 4)/* TCG I2C intf 1.0 */ +#define TPM_I2C_CAP_TPM2_FAMILY (0x1 << 7)/* TPM 2.0 family. */ +#define TPM_I2C_CAP_DEV_ADDR_CHANGE (0x0 << 27)/* No dev addr chng */ +#define TPM_I2C_CAP_BURST_COUNT_STATIC (0x1 << 29)/* Burst count static */ +#define TPM_I2C_CAP_LOCALITY_CAP (0x1 << 25)/* 0-5 locality */ +#define TPM_I2C_CAP_BUS_SPEED (3 << 21)/* std and fast mode */ + +/* TPM_STS mask for read bits 31:26 must be zero */ +#define TPM_I2C_STS_READ_MASK 0x03ffffff +void tpm_build_ppi_acpi (TPMIf *tpm,Aml *dev);#endif /* CONFIG_TPM */
Signature · 1 line
2.37.2
v5 cover Add support for TPM devices over I2C bus
Hello,
In V5, I have fixed issues reported by Stefan's test.
This drop adds support for the TPM devices attached to the I2C bus. It only supports the TPM2 protocol. You need to run it with the external TPM emulator like swtpm. I have tested it with swtpm.
I have refered to the work done by [email protected] but at the core level out implementation is different.
https://github.com/theopolis/qemu/commit/2e2e57cde9e419c36af8071bb85392ad1ed70966
Based-on: $MESSAGE_ID
Ninad Palsule (3):
docs: Add support for TPM devices over I2C bus
tpm: Extend common APIs to support TPM TIS I2C
tpm: Add support for TPM device over I2C bus
docs/specs/tpm.rst | 32 +++ hw/arm/Kconfig | 1 + hw/tpm/Kconfig | 7 + hw/tpm/meson.build | 1 + hw/tpm/tpm_tis.h | 3 + hw/tpm/tpm_tis_common.c | 28 +++ hw/tpm/tpm_tis_i2c.c | 485 ++++++++++++++++++++++++++++++++++++++++ hw/tpm/trace-events | 6 + include/hw/acpi/tpm.h | 28 +++ include/sysemu/tpm.h | 3 + 10 files changed, 594 insertions(+) create mode 100644 hw/tpm/tpm_tis_i2c.c
Signature · 1 line
2.37.2
PATCH v5 3/3 tpm: Add support for TPM device over I2C bus
Qemu already supports devices attached to ISA and sysbus. This drop adds support for the I2C bus attached TPM devices. I2C model only supports TPM2 protocol.
This commit includes changes for the common code.
- Added I2C emulation model. Logic was added in the model to temporarily cache the data as I2C interface works per byte basis.
- New tpm type "tpm-tis-i2c" added for I2C support. User specify this string on command line.
Testing:
TPM I2C device modulte is tested using SWTPM (software based TPM package). The qemu used the rainier machine and it was connected to swtpm over the socket interface.
The command to start swtpm is as follows:
$ swtpm socket --tpmstate dir=/tmp/mytpm1 \
--ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
--tpm2 --log level=100
The command to start qemu is as follows:
$ qemu-system-arm -M rainier-bmc -nographic \
-kernel ${IMAGEPATH}/fitImage-linux.bin \
-dtb ${IMAGEPATH}/aspeed-bmc-ibm-rainier.dtb \
-initrd ${IMAGEPATH}/obmc-phosphor-initramfs.rootfs.cpio.xz \
-drive file=${IMAGEPATH}/obmc-phosphor-image.rootfs.wic.qcow2,if=sd,index=2 \
-net nic -net user,hostfwd=:127.0.0.1:2222-:22,hostfwd=:127.0.0.1:2443-:443 \
-chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
-tpmdev emulator,id=tpm0,chardev=chrtpm \
-device tpm-tis-i2c,tpmdev=tpm0,bus=aspeed.i2c.bus.12,address=0x2e Note: Currently you need to specify the I2C bus and device address on
command line. In future we can add a device at board level.
---
V2:
Incorporated Stephen's review comments.
- Handled checksum related register in I2C layer
- Defined I2C interface capabilities and return those instead of capabilities from TPM TIS. Add required capabilities from TIS.
- Do not cache FIFO data in the I2C layer.
- Make sure that Device address change register is not passed to I2C layer as capability indicate that it is not supported.
- Added boundary checks.
- Make sure that bits 26-31 are zeroed for the TPM_STS register on read
- Updated Kconfig files for new define.
---
V3:
- Moved processing of register TPM_I2C_LOC_SEL in the I2C. So I2C layer remembers the locality and pass it to TIS on each read/write.
- The write data is no more cached in the I2C layer so the buffer size is reduced to 16 bytes.
- Checksum registers are now managed by the I2C layer. Added new function in TIS layer to return the checksum and used that to process the request.
- Now 2-4 byte register value will be passed to TIS layer in a single write call instead of 1 byte at a time. Added functions to convert between little endian stream of bytes to single 32 bit unsigned integer. Similarly 32 bit integer to stream of bytes.
- Added restriction on device change register.
- Replace few if-else statement with switch statement for clarity.
- Log warning when unknown register is received.
- Moved all register definations to acpi/tmp.h
---
V4:
Incorporated review comments from Cedric and Stefan.
- Reduced data[] size from 16 byte to 5 bytes.
- Added register name in the mapping table which can be used for tracing.
- Removed the endian conversion functions instead used simple logic provided by Stefan.
- Rename I2C registers to reduce the length.
- Added traces for send, recv and event functions. You can turn on trace on command line by using "-trace "tpm_tis_i2c*" option.
---
V5:
Fixed issues reported by Stefan's test.
- Added mask for the INT_ENABLE register.
- Use correct TIS register for reading interrupt capability.
- Cleanup how register is converted from I2C to TIS and also saved information like tis_addr and register name in the i2cst so that we can only convert it once on i2c_send.
- Trace register number for unknown registers.
--- hw/arm/Kconfig | 1 + hw/tpm/Kconfig | 7 + hw/tpm/meson.build | 1 + hw/tpm/tpm_tis_i2c.c | 485 +++++++++++++++++++++++++++++++++++++++++++ hw/tpm/trace-events | 6 + include/sysemu/tpm.h | 3 + 6 files changed, 503 insertions(+) create mode 100644 hw/tpm/tpm_tis_i2c.c
hw/arm/Kconfig +1 -0
@@ -6,6 +6,7 @@ config ARM_VIRT imply VFIO_PLATFORM imply VFIO_XGMAC imply TPM_TIS_SYSBUS + imply TPM_TIS_I2C imply NVDIMM select ARM_GIC select ACPI
hw/tpm/Kconfig +7 -0
@@ -1,3 +1,10 @@ +config TPM_TIS_I2C + bool + depends on TPM + select TPM_BACKEND + select I2C + select TPM_TIS + config TPM_TIS_ISA bool depends on TPM && ISA_BUS
hw/tpm/meson.build +1 -0
@@ -1,6 +1,7 @@ softmmu_ss.add(when: 'CONFIG_TPM_TIS', if_true: files('tpm_tis_common.c')) softmmu_ss.add(when: 'CONFIG_TPM_TIS_ISA', if_true: files('tpm_tis_isa.c')) softmmu_ss.add(when: 'CONFIG_TPM_TIS_SYSBUS', if_true: files('tpm_tis_sysbus.c')) +softmmu_ss.add(when: 'CONFIG_TPM_TIS_I2C', if_true: files('tpm_tis_i2c.c')) softmmu_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_crb.c')) softmmu_ss.add(when: 'CONFIG_TPM_TIS', if_true: files('tpm_ppi.c')) softmmu_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_ppi.c'))
hw/tpm/tpm_tis_i2c.c new file +485 -0
485 lines not on this page · raw message
hw/tpm/trace-events +6 -0
@@ -36,3 +36,9 @@ tpm_spapr_do_crq_unknown_msg_type(uint8_t type) "Unknown message type 0x%02x" tpm_spapr_do_crq_unknown_crq(uint8_t raw1, uint8_t raw2) "unknown CRQ 0x%02x 0x%02x ..." tpm_spapr_post_load(void) "Delivering TPM response after resume" tpm_spapr_caught_response(uint32_t v) "Caught response to deliver after resume: %u bytes" + +# tpm_tis_i2c.c +tpm_tis_i2c_recv(uint8_t data) "TPM I2C read: 0x%X" +tpm_tis_i2c_send(uint8_t data) "TPM I2C write: 0x%X" +tpm_tis_i2c_event(const char *event) "TPM I2C event: %s" +tpm_tis_i2c_send_reg(const char *name, int reg) "TPM I2C write register: %s(0x%X)"
include/sysemu/tpm.h +3 -0
@@ -48,6 +48,7 @@ struct TPMIfClass {#define TYPE_TPM_TIS_SYSBUS "tpm-tis-device"#define TYPE_TPM_CRB "tpm-crb"#define TYPE_TPM_SPAPR "tpm-spapr" +#define TYPE_TPM_TIS_I2C "tpm-tis-i2c"#define TPM_IS_TIS_ISA (chr) \ object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
@@ -57,6 +58,8 @@ struct TPMIfClass { object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)#define TPM_IS_SPAPR (chr) \ object_dynamic_cast(OBJECT(chr), TYPE_TPM_SPAPR) +#define TPM_IS_TIS_I2C (chr) \ + object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_I2C)/* returns NULL unless there is exactly one TPM device */ static inline TPMIf *tpm_find (void )
Signature · 1 line
2.37.2