Skip to contentSkip to conversation

TPM TIS SPI Support

PATCH Superseded dan tan 2 participants 7 messages Nov 1, 2024 Arm/Amlogic Meson SoC support

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

dan tan

author
#0 lore raw

v3 cover TPM TIS SPI Support

*** BLURB HERE ***

Support TPM for SPI (Serial Peripheral Interface)

Revision 3 summary:
device support:
- moved variable tis_addr from TPMStateSPI struct to local
- added the VM suspend/resume support:
- added vmstate_tpm_tis_spi declaration
- added tpm_tis_spi_pre_save() function
- fixed trace formatting string

qtest:
- removed the function prototypes declaration
- fixed code format to comply with convention
- changed function names and variable names to be the same as the tpm-tis-i2c test.
- change hard coded numbers to #define's with meaningful names that are identifiable with spec documentation

Version 2 summary,
addressed the following review comments:
- break up patch into 3 separate commits;
- add more details in the commit logs;
- I added links to the TCG TPM standard documents as this device model communicates to the TPM device (hw / swtpm) via tpm_tis_commom.c which is standard compliant;
- the TPM SPI model implementation in itself is not platform specific. However, the SPI interface is via the PowerNV SPI bus master, thus it is only supported on the PowerNV platform
- change all qemu_log() calls to trace events;
- move static global variables to the TPMStateSPI struct;
- fixed code formatting (verified by scripts/checkpatch.pl);
- per requests, make the code more readable by using self- explanatory #defines and adding comments;
- added some documentation support (tpm.rst);
- beefed up the unit test exercising major supported locality functionality

dan tan (5):
tpm/tpm_tis_spi: Support TPM for SPI (Serial Peripheral Interface) tpm/tpm_tis_spi: activation for the PowerNV machines tests/qtest/tpm: add unit test to tis-spi tpm/tpm_tis_spi: Support TPM for SPI (rev 3) tests/qtest/tpm: add unit test to tis-spi (rev 3)

 docs/specs/tpm.rst                 |  15 +
 include/sysemu/tpm.h               |   3 +
 hw/tpm/tpm_tis_spi.c               | 360 +++++++++++++++
 tests/qtest/tpm-tis-spi-pnv-test.c | 710 +++++++++++++++++++++++++++++
 hw/ppc/Kconfig                     |   1 +
 hw/tpm/Kconfig                     |   6 +
 hw/tpm/meson.build                 |   1 +
 hw/tpm/trace-events                |   7 +
 tests/qtest/meson.build            |   2 +
 9 files changed, 1105 insertions(+)
 create mode 100644 hw/tpm/tpm_tis_spi.c
 create mode 100644 tests/qtest/tpm-tis-spi-pnv-test.c
Signature · 1 line
2.39.5
author
#1 lore raw

PATCH v3 1/5 tpm/tpm_tis_spi: Support TPM for SPI (Serial Peripheral Interface)

Implement support for TPM via SPI interface. The SPI bus master is provided by PowerNV SPI device which is an SSI peripheral. It can uses the tpm_emulator driver backend with the external swtpm.

Although the implementation is endian neutral, the SPI bus master provider, pnv_spi.c is only supported on the PowerNV platform, thus, is big endian specific.

---
 docs/specs/tpm.rst   |  15 ++
 include/sysemu/tpm.h |   3 +
 hw/tpm/tpm_tis_spi.c | 328 +++++++++++++++++++++++++++++++++++++++++++
 hw/tpm/Kconfig       |   6 +
 hw/tpm/meson.build   |   1 +
 hw/tpm/trace-events  |   7 +
 6 files changed, 360 insertions(+)
 create mode 100644 hw/tpm/tpm_tis_spi.c
docs/specs/tpm.rst +15 -0
index 1ad36ad709..63c3a43c7d 100644
@@ -24,6 +24,7 @@ QEMU files related to TPM TIS interface:
  - ``hw/tpm/tpm_tis_isa.c``
  - ``hw/tpm/tpm_tis_sysbus.c``
  - ``hw/tpm/tpm_tis_i2c.c``
+ - ``hw/tpm/tpm_tis_spi.c``
  - ``hw/tpm/tpm_tis.h``
 
 Both an ISA device and a sysbus device are available. The former is
@@ -33,6 +34,9 @@ Arm virt machine.
 An I2C device support is also provided which can be instantiated in the Arm
 based emulation machines. This device only supports the TPM 2 protocol.
 
+A Serial Peripheral Interface (SPI) device support has been added to the
+PowerNV emulation machines. This device only supports the TPM 2 protocol.
+
 CRB interface
 -------------
 
@@ -371,6 +375,17 @@ attached to I2C bus, use the following command line:
 
   echo tpm_tis_i2c 0x2e > /sys/bus/i2c/devices/i2c-12/new_device
 
+In case a PowerNV machine is emulated and you want to use a TPM device
+attached to SPI bus, use the following command line (SPI bus master is
+provided by PowerNV SPI device):
+
+.. code-block:: console
+
+  qemu-system-ppc64 -m 2G -machine powernv10 -nographic \
+    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
+    -tpmdev emulator,id=tpm0,chardev=chrtpm \
+    -device tpm-tis-spi,tpmdev=tpm0,bus=pnv-spi-bus.4
+
 In case SeaBIOS is used as firmware, it should show the TPM menu item
 after entering the menu with 'ESC'.
 
include/sysemu/tpm.h +3 -0
index 1ee568b3b6..22b05110e2 100644
@@ -49,6 +49,7 @@ struct TPMIfClass {
 #define TYPE_TPM_CRB                "tpm-crb"
 #define TYPE_TPM_SPAPR              "tpm-spapr"
 #define TYPE_TPM_TIS_I2C            "tpm-tis-i2c"
+#define TYPE_TPM_TIS_SPI            "tpm-tis-spi"
 
 #define TPM_IS_TIS_ISA(chr)                         \
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
@@ -60,6 +61,8 @@ struct TPMIfClass {
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_SPAPR)
 #define TPM_IS_TIS_I2C(chr)                      \
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_I2C)
+#define TPM_IS_TIS_SPI(chr)                      \
+    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_SPI)
 
 /* returns NULL unless there is exactly one TPM device */
 static inline TPMIf *tpm_find(void)
hw/tpm/tpm_tis_spi.c new file +328 -0

328 lines not on this page · raw message

hw/tpm/Kconfig +6 -0
index a46663288c..5951c225cc 100644
@@ -5,6 +5,12 @@ config TPM_TIS_I2C
     select I2C
     select TPM_TIS
 
+config TPM_TIS_SPI
+    bool
+    depends on TPM
+    select TPM_BACKEND
+    select TPM_TIS
+
 config TPM_TIS_ISA
     bool
     depends on TPM && ISA_BUS
hw/tpm/meson.build +1 -0
index 6968e60b3f..e03cfb11b9 100644
@@ -2,6 +2,7 @@ system_ss.add(when: 'CONFIG_TPM_TIS', if_true: files('tpm_tis_common.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_ISA', if_true: files('tpm_tis_isa.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_SYSBUS', if_true: files('tpm_tis_sysbus.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_I2C', if_true: files('tpm_tis_i2c.c'))
+system_ss.add(when: 'CONFIG_TPM_TIS_SPI', if_true: files('tpm_tis_spi.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_crb.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS', if_true: files('tpm_ppi.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_ppi.c'))
hw/tpm/trace-events +7 -0
index fa882dfefe..4b39be13e2 100644
@@ -42,3 +42,10 @@ 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)"
+
+# tpm_tis_spi.c
+tpm_tis_spi_write(uint32_t addr, uint8_t val) "TPM SPI write - addr:0x%X 0x%x"
+tpm_tis_spi_read(uint32_t addr, uint8_t val) "TPM SPI read - addr:0x%X 0x%x"
+tpm_tis_spi_transfer_event(const char *event) "TPM SPI XFER event: %s"
+tpm_tis_spi_transfer_data(const char *name, uint8_t val) "TPM SPI XFER: %s = 0x%x"
+tpm_tis_spi_transfer_addr(const char *name, uint8_t val) "TPM SPI XFER: %s = 0x%08x"
Signature · 1 line
2.39.5
author
#2 lore raw

PATCH v3 2/5 tpm/tpm_tis_spi: activation for the PowerNV machines

In reply to dan tan

The addition to ppc/Kconfig is for building this into the qemu-system-ppc64 binary. The enablement requires the following command line argument:
-device tpm-tis-spi,tpmdev=tpm0,bus=pnv-spi-bus.4

---
 hw/ppc/Kconfig | 1 +
 1 file changed, 1 insertion(+)
hw/ppc/Kconfig +1 -0
index b44d91bebb..56dcf5902e 100644
@@ -39,6 +39,7 @@ config POWERNV
     select PCI_POWERNV
     select PCA9552
     select PCA9554
+    select TPM_TIS_SPI
     select SERIAL_ISA
     select SSI
     select SSI_M25P80
Signature · 1 line
2.39.5
author
#3 lore raw

PATCH v3 3/5 tests/qtest/tpm: add unit test to tis-spi

In reply to dan tan

Add qtest cases to exercise main TPM locality functionality The TPM device emulation is provided by swtpm, which is TCG TPM 2.0, and TCG TPM TIS compliant. See
https://trustedcomputinggroup.org/wp-content/uploads/TCG_PC_Client_Platform_TPM_Profile_PTP_2.0_r1.03_v22.pdf
https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientTPMInterfaceSpecification_TIS__1-3_27_03212013.pdf
The SPI registers are specific to the PowerNV platform architecture

---
 hw/tpm/tpm_tis_spi.c               |   2 +-
 tests/qtest/tpm-tis-spi-pnv-test.c | 700 +++++++++++++++++++++++++++++
 tests/qtest/meson.build            |   3 +-
 3 files changed, 703 insertions(+), 2 deletions(-)
 create mode 100644 tests/qtest/tpm-tis-spi-pnv-test.c
hw/tpm/tpm_tis_spi.c +1 -1
index 5aacae8467..f8a78a5cfb 100644
@@ -302,7 +302,7 @@ static void tpm_tis_spi_class_init(ObjectClass *klass, void *data)
     device_class_set_props(dc, tpm_tis_spi_properties);
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 
-    dc->desc = "PowerNV SPI TPM";
+    dc->desc = "SPI TPM";
 
     tc->model = TPM_MODEL_TPM_TIS;
     tc->request_completed = tpm_tis_spi_request_completed;
tests/qtest/tpm-tis-spi-pnv-test.c new file +700 -0

700 lines not on this page · raw message

tests/qtest/meson.build +2 -1
index e8be8b3942..6b1aed929e 100644
@@ -4,7 +4,6 @@ slow_qtests = {
   'bios-tables-test' : 910,
   'cdrom-test' : 610,
   'device-introspect-test' : 720,
-  'ide-test' : 120,
   'migration-test' : 480,
   'npcm7xx_pwm-test': 300,
   'npcm7xx_watchdog_timer-test': 120,
@@ -177,6 +176,7 @@ qtests_ppc64 = \
   (config_all_devices.has_key('CONFIG_PSERIES') ? ['device-plug-test'] : []) +               \
   (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-xscom-test'] : []) +                 \
   (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-spi-seeprom-test'] : []) +           \
+  (config_all_devices.has_key('CONFIG_POWERNV') ? ['tpm-tis-spi-pnv-test'] : []) +           \
   (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-host-i2c-test'] : []) +              \
   (config_all_devices.has_key('CONFIG_PSERIES') ? ['numa-test'] : []) +                      \
   (config_all_devices.has_key('CONFIG_PSERIES') ? ['rtas-test'] : []) +                      \
@@ -348,6 +348,7 @@ qtests = {
   'tpm-tis-i2c-test': [io, tpmemu_files, 'qtest_aspeed.c'],
   'tpm-tis-device-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'],
   'tpm-tis-device-test': [io, tpmemu_files, 'tpm-tis-util.c'],
+  'tpm-tis-spi-pnv-test': [io, tpmemu_files],
   'virtio-net-failover': files('migration-helpers.c'),
   'vmgenid-test': files('boot-sector.c', 'acpi-utils.c'),
   'netdev-socket': files('netdev-socket.c', '../unit/socket-helpers.c'),
Signature · 1 line
2.39.5
author
#4 lore raw

PATCH v3 4/5 tpm/tpm_tis_spi: Support TPM for SPI (rev 3)

In reply to dan tan

- moved variable tis_addr from TPMStateSPI struct to local
- added the VM suspend/resume support:
- added vmstate_tpm_tis_spi declaration
- added tpm_tis_spi_pre_save() function
- fixed trace formatting string

---
 hw/tpm/tpm_tis_spi.c | 50 ++++++++++++++++++++++++++++++++++++--------
 hw/tpm/trace-events  |  8 +++----
 2 files changed, 45 insertions(+), 13 deletions(-)
hw/tpm/tpm_tis_spi.c +41 -9
index f8a78a5cfb..4aee1c6d6c 100644
@@ -17,6 +17,7 @@
 #include "trace.h"
 #include "tpm_tis.h"
 #include "hw/ssi/ssi.h"
+#include "migration/vmstate.h"
 
 typedef struct TPMStateSPI {
     /*< private >*/
@@ -26,7 +27,6 @@ typedef struct TPMStateSPI {
     uint8_t     wait_state_cnt;  /* wait state counter */
     uint8_t     xfer_size;       /* data size of transfer */
     uint32_t    reg_addr;        /* register address of transfer */
-    uint32_t    tis_addr;        /* tis address including locty */
 
     uint8_t     spi_state;       /* READ / WRITE / IDLE */
 #define SPI_STATE_IDLE   0
@@ -46,14 +46,47 @@ typedef struct TPMStateSPI {
 
 DECLARE_INSTANCE_CHECKER(TPMStateSPI, TPM_TIS_SPI, TYPE_TPM_TIS_SPI)
 
+static int tpm_tis_spi_pre_save(void *opaque)
+{
+    TPMStateSPI *spist = opaque;
+
+    return tpm_tis_pre_save(&spist->tpm_state);
+}
+
+static const VMStateDescription vmstate_tpm_tis_spi = {
+     .name = "tpm-tis-spi",
+     .version_id = 0,
+     .pre_save  = tpm_tis_spi_pre_save,
+     .fields = (const VMStateField[]) {
+         VMSTATE_BUFFER(tpm_state.buffer, TPMStateSPI),
+         VMSTATE_UINT16(tpm_state.rw_offset, TPMStateSPI),
+         VMSTATE_UINT8(tpm_state.active_locty, TPMStateSPI),
+         VMSTATE_UINT8(tpm_state.aborting_locty, TPMStateSPI),
+         VMSTATE_UINT8(tpm_state.next_locty, TPMStateSPI),
+
+         VMSTATE_STRUCT_ARRAY(tpm_state.loc, TPMStateSPI,
+                              TPM_TIS_NUM_LOCALITIES, 0,
+                              vmstate_locty, TPMLocality),
+
+         /* spi specifics */
+         VMSTATE_UINT8(byte_offset, TPMStateSPI),
+         VMSTATE_UINT8(wait_state_cnt, TPMStateSPI),
+         VMSTATE_UINT8(xfer_size, TPMStateSPI),
+         VMSTATE_UINT32(reg_addr, TPMStateSPI),
+         VMSTATE_UINT8(spi_state, TPMStateSPI),
+         VMSTATE_BOOL(command, TPMStateSPI),
+
+         VMSTATE_END_OF_LIST()
+     }
+};
+
 static inline void tpm_tis_spi_clear_data(TPMStateSPI *spist)
 {
-    spist->spi_state = 0;
+    spist->spi_state = SPI_STATE_IDLE;
     spist->byte_offset = 0;
     spist->wait_state_cnt = 0;
     spist->xfer_size = 0;
     spist->reg_addr = 0;
-    spist->tis_addr = 0xffffffff;
 
     return;
 }
@@ -126,6 +159,7 @@ static uint32_t tpm_transfer(SSIPeripheral *ss, uint32_t tx)
     uint8_t byte;       /* reversed byte value */
     uint8_t offset = 0; /* offset of byte in payload */
     uint8_t index;      /* index of data byte in transfer */
+    uint32_t tis_addr;  /* tis address including locty */
 
     /* new transfer or not */
     if (spist->command) {   /* new transfer start */
@@ -230,11 +264,11 @@ static uint32_t tpm_transfer(SSIPeripheral *ss, uint32_t tx)
                     trace_tpm_tis_spi_transfer_event("index exceeds xfer_size");
                     return rx;
                 }
-                spist->tis_addr = spist->reg_addr + (index % 4);
+                tis_addr = spist->reg_addr + (index % 4);
                 if (spist->spi_state == SPI_STATE_WRITE) {
-                    tpm_tis_spi_write(spist, spist->tis_addr, byte);
+                    tpm_tis_spi_write(spist, tis_addr, byte);
                 } else {
-                    byte = tpm_tis_spi_read(spist, spist->tis_addr);
+                    byte = tpm_tis_spi_read(spist, tis_addr);
                     rx = rx | (byte << (24 - offset * 8));
                     trace_tpm_tis_spi_transfer_data("byte added to response",
                                                      byte);
@@ -271,9 +305,6 @@ static void tpm_realize(SSIPeripheral *dev, Error **errp)
     TPMStateSPI *spist = TPM_TIS_SPI(dev);
     TPMState *s = &spist->tpm_state;
 
-    spist->command = true;
-    spist->spi_state = SPI_STATE_IDLE;
-
     if (!tpm_find()) {
         error_setg(errp, "at most one TPM device is permitted");
         return;
@@ -303,6 +334,7 @@ static void tpm_tis_spi_class_init(ObjectClass *klass, void *data)
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 
     dc->desc = "SPI TPM";
+    dc->vmsd = &vmstate_tpm_tis_spi;
 
     tc->model = TPM_MODEL_TPM_TIS;
     tc->request_completed = tpm_tis_spi_request_completed;
hw/tpm/trace-events +4 -4
index 4b39be13e2..0324ceb6d0 100644
@@ -44,8 +44,8 @@ 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)"
 
 # tpm_tis_spi.c
-tpm_tis_spi_write(uint32_t addr, uint8_t val) "TPM SPI write - addr:0x%X 0x%x"
-tpm_tis_spi_read(uint32_t addr, uint8_t val) "TPM SPI read - addr:0x%X 0x%x"
+tpm_tis_spi_write(uint32_t addr, uint8_t val) "TPM SPI write - addr:0x%08X 0x%02x"
+tpm_tis_spi_read(uint32_t addr, uint8_t val) "TPM SPI read - addr:0x%08X 0x%02x"
 tpm_tis_spi_transfer_event(const char *event) "TPM SPI XFER event: %s"
-tpm_tis_spi_transfer_data(const char *name, uint8_t val) "TPM SPI XFER: %s = 0x%x"
-tpm_tis_spi_transfer_addr(const char *name, uint8_t val) "TPM SPI XFER: %s = 0x%08x"
+tpm_tis_spi_transfer_data(const char *name, uint8_t val) "TPM SPI XFER: %s = 0x%02x"
+tpm_tis_spi_transfer_addr(const char *name, uint32_t addr) "TPM SPI XFER: %s = 0x%08x"
Signature · 1 line
2.39.5
author
#5 lore raw

PATCH v3 5/5 tests/qtest/tpm: add unit test to tis-spi (rev 3)

In reply to dan tan

- removed the function prototypes declaration
- fixed code format to comply with convention
- changed function names and variable names to be the same as the tpm-tis-i2c test.
- change hard coded numbers to #define's with meaningful names that are identifiable with spec documentation

---
 tests/qtest/tpm-tis-spi-pnv-test.c | 220 +++++++++++++++--------------
 tests/qtest/meson.build            |   1 +
 2 files changed, 116 insertions(+), 105 deletions(-)
tests/qtest/tpm-tis-spi-pnv-test.c +115 -105
index a367564dda..2d6e1186cf 100644
@@ -19,6 +19,9 @@
 #define SPI_TPM_BASE            0xc0080
 #define SPI_SHIFT_COUNTER_N1    0x30000000
 #define SPI_SHIFT_COUNTER_N2    0x40000000
+#define SPI_RWX_OPCODE_SHIFT    56
+#define SPI_RWX_ADDR_SHIFT      32
+#define SPI_CMD_DATA_SHIFT      56
 
 #define CFG_COUNT_COMPARE_1     0x0000000200000000
 #define MM_REG_RDR_MATCH        0x00000000ff01ff00
@@ -26,6 +29,7 @@
 
 #define TPM_TIS_8BITS_MASK      0xff
 #define SPI_TPM_TIS_ADDR        0xd40000
+#define SPI_EXTEND              0x03
 #define TPM_WRITE_OP            0x0
 #define TPM_READ_OP             0x80
 
@@ -50,53 +54,11 @@ static const uint8_t TPM_CMD[12] =
 #define DPRINTF_STS \
     DPRINTF("%s: %d: sts = 0x%08x\n", __func__, __LINE__, sts)
 
-static uint64_t pnv_spi_tpm_read(const PnvChip *chip, uint32_t reg);
-static void pnv_spi_tpm_write(const PnvChip *chip, uint32_t reg, uint64_t val);
-static uint64_t spi_read_reg(const PnvChip *chip);
-static void spi_write_reg(const PnvChip *chip, uint64_t val);
-static void spi_access_start(const PnvChip *chip, bool n2, uint8_t bytes,
-                             uint8_t tpm_op, uint32_t tpm_reg);
-
-static inline void tpm_reg_writeb(const PnvChip *c,
-                                  int locl,
-                                  uint8_t reg,
-                                  uint8_t val)
-{
-    uint32_t tpm_reg_locl = SPI_TPM_TIS_ADDR | (locl << TPM_TIS_LOCALITY_SHIFT);
-
-    spi_access_start(c, false, 1, TPM_WRITE_OP, tpm_reg_locl | reg);
-    spi_write_reg(c, bswap64(val));
-}
-
-static inline uint8_t tpm_reg_readb(const PnvChip *c, int locl, uint8_t reg)
-{
-    uint32_t tpm_reg_locl = SPI_TPM_TIS_ADDR | (locl << TPM_TIS_LOCALITY_SHIFT);
-
-    spi_access_start(c, true, 1, TPM_READ_OP, tpm_reg_locl | reg);
-    return spi_read_reg(c);
-}
-
-static inline void tpm_reg_writew(const PnvChip *c,
-                                  int locl,
-                                  uint8_t reg,
-                                  uint32_t val)
-{
-    int i;
-
-    for (i = 0; i < 4; i++) {
-        tpm_reg_writeb(c, locl, reg + i, ((val >> (8 * i)) & 0xff));
-    }
-}
-
-static inline uint32_t tpm_reg_readw(const PnvChip *c, int locl, uint8_t reg)
+static uint64_t pnv_spi_tpm_read(const PnvChip *chip, uint32_t reg)
 {
-    uint32_t val = 0;
-    int i;
+    uint32_t pcba = SPI_TPM_BASE + reg;
 
-    for (i = 0; i < 4; i++) {
-        val |= tpm_reg_readb(c, locl, reg + i) << (8 * i);
-    }
-    return val;
+    return qtest_readq(global_qtest, pnv_xscom_addr(chip, pcba));
 }
 
 static void pnv_spi_tpm_write(const PnvChip *chip,
@@ -104,40 +66,8 @@ static void pnv_spi_tpm_write(const PnvChip *chip,
                               uint64_t val)
 {
     uint32_t pcba = SPI_TPM_BASE + reg;
-    qtest_writeq(global_qtest, pnv_xscom_addr(chip, pcba), val);
-}
 
-static uint64_t pnv_spi_tpm_read(const PnvChip *chip, uint32_t reg)
-{
-    uint32_t pcba = SPI_TPM_BASE + reg;
-    return qtest_readq(global_qtest, pnv_xscom_addr(chip, pcba));
-}
-
-static void spi_access_start(const PnvChip *chip,
-                             bool n2,
-                             uint8_t bytes,
-                             uint8_t tpm_op,
-                             uint32_t tpm_reg)
-{
-    uint64_t cfg_reg;
-    uint64_t reg_op;
-    uint64_t seq_op = SEQ_OP_REG_BASIC;
-
-    cfg_reg = pnv_spi_tpm_read(chip, SPI_CLK_CFG_REG);
-    if (cfg_reg != CFG_COUNT_COMPARE_1) {
-        pnv_spi_tpm_write(chip, SPI_CLK_CFG_REG, CFG_COUNT_COMPARE_1);
-    }
-    /* bytes - sequencer operation register bits 24:31 */
-    if (n2) {
-        seq_op |= SPI_SHIFT_COUNTER_N2 | (bytes << 0x18);
-    } else {
-        seq_op |= SPI_SHIFT_COUNTER_N1 | (bytes << 0x18);
-    }
-    pnv_spi_tpm_write(chip, SPI_SEQ_OP_REG, seq_op);
-    pnv_spi_tpm_write(chip, SPI_MM_REG, MM_REG_RDR_MATCH);
-    pnv_spi_tpm_write(chip, SPI_CTR_CFG_REG, (uint64_t)0);
-    reg_op = bswap64(tpm_op) | ((uint64_t)tpm_reg << 0x20);
-    pnv_spi_tpm_write(chip, SPI_XMIT_DATA_REG, reg_op);
+    qtest_writeq(global_qtest, pnv_xscom_addr(chip, pcba), val);
 }
 
 static void spi_op_complete(const PnvChip *chip)
@@ -206,6 +136,89 @@ static uint64_t spi_read_reg(const PnvChip *chip)
     return val;
 }
 
+static void spi_access_start(const PnvChip *chip,
+                             bool n2,
+                             uint8_t bytes,
+                             uint8_t tpm_op,
+                             uint32_t tpm_reg)
+{
+    uint64_t cfg_reg;
+    uint64_t reg_op;
+    uint64_t seq_op = SEQ_OP_REG_BASIC;
+
+    cfg_reg = pnv_spi_tpm_read(chip, SPI_CLK_CFG_REG);
+    if (cfg_reg != CFG_COUNT_COMPARE_1) {
+        pnv_spi_tpm_write(chip, SPI_CLK_CFG_REG, CFG_COUNT_COMPARE_1);
+    }
+    /* bytes - sequencer operation register bits 24:31 */
+    if (n2) {
+        seq_op |= SPI_SHIFT_COUNTER_N2 | (bytes << 0x18);
+    } else {
+        seq_op |= SPI_SHIFT_COUNTER_N1 | (bytes << 0x18);
+    }
+    pnv_spi_tpm_write(chip, SPI_SEQ_OP_REG, seq_op);
+    pnv_spi_tpm_write(chip, SPI_MM_REG, MM_REG_RDR_MATCH);
+    pnv_spi_tpm_write(chip, SPI_CTR_CFG_REG, (uint64_t)0);
+    reg_op = ((uint64_t)tpm_op << SPI_RWX_OPCODE_SHIFT) |
+             ((uint64_t)tpm_reg << SPI_RWX_ADDR_SHIFT);
+    pnv_spi_tpm_write(chip, SPI_XMIT_DATA_REG, reg_op);
+}
+
+static inline void tpm_reg_writeb(const PnvChip *c,
+                                  uint8_t locty,
+                                  uint8_t reg,
+                                  uint8_t val)
+{
+    uint32_t tpm_reg_locty = SPI_TPM_TIS_ADDR |
+                             (locty << TPM_TIS_LOCALITY_SHIFT);
+
+    spi_access_start(c, false, 1, TPM_WRITE_OP, tpm_reg_locty | reg);
+    spi_write_reg(c, (uint64_t) val << SPI_CMD_DATA_SHIFT);
+}
+
+static inline uint8_t tpm_reg_readb(const PnvChip *c,
+                                    uint8_t locty,
+                                    uint8_t reg)
+{
+    uint32_t tpm_reg_locty = SPI_TPM_TIS_ADDR |
+                             (locty << TPM_TIS_LOCALITY_SHIFT);
+
+    spi_access_start(c, true, 1, TPM_READ_OP, tpm_reg_locty | reg);
+    return spi_read_reg(c);
+}
+
+static inline void tpm_reg_writel(const PnvChip *c,
+                                  uint8_t locty,
+                                  uint16_t reg,
+                                  uint32_t val)
+{
+    int i;
+
+    for (i = 0; i < 4; i++) {
+        tpm_reg_writeb(c, locty, reg + i, ((val >> (8 * i)) & 0xff));
+    }
+}
+
+static inline uint32_t tpm_reg_readl(const PnvChip *c,
+                                     uint8_t locty,
+                                     uint16_t reg)
+{
+    uint32_t val = 0;
+    int i;
+
+    /* special case for SPI xmit data reg set RWX bits */
+    if (reg == TPM_TIS_REG_DID_VID) {
+        spi_access_start(c, true, 4, TPM_READ_OP | SPI_EXTEND,
+                         locty | TPM_TIS_REG_DID_VID);
+        val = bswap32(spi_read_reg(c));
+    } else {
+        for (i = 0; i < 4; i++) {
+            val |= tpm_reg_readb(c, locty, reg + i) << (8 * i);
+        }
+    }
+    return val;
+}
+
 static void tpm_set_verify_loc(const PnvChip *chip, uint8_t loc)
 {
     uint8_t access;
@@ -226,7 +239,7 @@ static void tpm_set_verify_loc(const PnvChip *chip, uint8_t loc)
     g_test_message("\tACCESS REG = 0x%x checked", access);
 
     /* test tpm status register */
-    tpm_sts = tpm_reg_readw(chip, loc, TPM_TIS_REG_STS);
+    tpm_sts = tpm_reg_readl(chip, loc, TPM_TIS_REG_STS);
     g_assert_cmpuint((tpm_sts & TPM_TIS_8BITS_MASK), ==, 0);
     g_test_message("\tTPM STATUS: 0x%x, verified", tpm_sts);
 
@@ -239,6 +252,17 @@ static void tpm_set_verify_loc(const PnvChip *chip, uint8_t loc)
     g_test_message("\tRELEASED ACCESS: 0x%x, checked", access);
 }
 
+static void test_spi_tpm_locality(const void *data)
+{
+    const PnvChip *chip = data;
+    uint8_t locality;
+
+    /* Locality 4 has special security restrictions, testing 0-3 */
+    for (locality = 0; locality < TPM_TIS_NUM_LOCALITIES - 1; locality++) {
+        tpm_set_verify_loc(chip, locality);
+    }
+}
+
 static void test_spi_tpm_basic(const void *data)
 {
     const PnvChip *chip = data;
@@ -247,11 +271,8 @@ static void test_spi_tpm_basic(const void *data)
 
     g_test_message("TPM TIS SPI interface basic tests:");
     /* vendor ID and device ID ... check against the known value*/
-    spi_access_start(chip, true, 4, 0x83,
-                     SPI_TPM_TIS_ADDR | TPM_TIS_REG_DID_VID);
-    didvid = spi_read_reg(chip);
-    g_assert_cmpint((didvid >> 16), ==, bswap16(TPM_TIS_TPM_VID));
-    g_assert_cmpint((didvid & 0xffff), ==, bswap16(TPM_TIS_TPM_DID));
+    didvid = tpm_reg_readl(chip, 0, TPM_TIS_REG_DID_VID);
+    g_assert_cmpint(didvid, ==, (1 << 16) | PCI_VENDOR_ID_IBM);
     g_test_message("\tDID_VID = 0x%x, verified", didvid);
 
     /* access register, default see TCG TIS Spec (v1.3) table-14 */
@@ -261,28 +282,17 @@ static void test_spi_tpm_basic(const void *data)
     g_test_message("\tACCESS REG = 0x%x, checked", access);
 
     /* interrupt enable register, default see TCG TIS Spec (v1.3) table-19 */
-    en_int = tpm_reg_readw(chip, 0, TPM_TIS_REG_INT_ENABLE);
+    en_int = tpm_reg_readl(chip, 0, TPM_TIS_REG_INT_ENABLE);
     g_assert_cmpuint(en_int, ==, TPM_TIS_INT_POLARITY_LOW_LEVEL);
     g_test_message("\tINT ENABLE REG: 0x%x, verified", en_int);
 
     /* status register, default see TCG TIS Spec (v1.3) table-15 */
-    tpm_sts = tpm_reg_readw(chip, 0, TPM_TIS_REG_STS);
+    tpm_sts = tpm_reg_readl(chip, 0, TPM_TIS_REG_STS);
     /* for no active locality */
     g_assert_cmpuint(tpm_sts, ==, 0xffffffff);
     g_test_message("\tTPM STATUS: 0x%x, verified", tpm_sts);
 }
 
-static void test_spi_tpm_locality(const void *data)
-{
-    const PnvChip *chip = data;
-    uint8_t locality;
-
-    /* Locality 4 has special security restrictions, testing 0-3 */
-    for (locality = 0; locality < TPM_TIS_NUM_LOCALITIES - 1; locality++) {
-        tpm_set_verify_loc(chip, locality);
-    }
-}
-
 /*
  * Test case for seizing access by a higher number locality
  */
@@ -582,7 +592,7 @@ static void test_spi_tpm_transmit_test(const void *data)
                                 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
                                 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
 
-    sts = tpm_reg_readw(chip, 0, TPM_TIS_REG_STS);
+    sts = tpm_reg_readl(chip, 0, TPM_TIS_REG_STS);
     DPRINTF_STS;
 
     g_assert_cmpint(sts & 0xff, ==, 0);
@@ -591,15 +601,15 @@ static void test_spi_tpm_transmit_test(const void *data)
     g_test_message("\t\tbcount: %x, sts: %x", bcount, sts);
     g_assert_cmpint(bcount, >=, 128);
 
-    tpm_reg_writew(chip, 0, TPM_TIS_REG_STS, TPM_TIS_STS_COMMAND_READY);
-    sts = tpm_reg_readw(chip, 0, TPM_TIS_REG_STS);
+    tpm_reg_writel(chip, 0, TPM_TIS_REG_STS, TPM_TIS_STS_COMMAND_READY);
+    sts = tpm_reg_readl(chip, 0, TPM_TIS_REG_STS);
     DPRINTF_STS;
     g_assert_cmpint(sts & 0xff, ==, TPM_TIS_STS_COMMAND_READY);
 
     /* transmit command */
     for (i = 0; i < sizeof(TPM_CMD); i++) {
         tpm_reg_writeb(chip, 0, TPM_TIS_REG_DATA_FIFO, TPM_CMD[i]);
-        sts = tpm_reg_readw(chip, 0, TPM_TIS_REG_STS);
+        sts = tpm_reg_readl(chip, 0, TPM_TIS_REG_STS);
         DPRINTF_STS;
         if (i < sizeof(TPM_CMD) - 1) {
             g_assert_cmpint(sts & 0xff, ==, TPM_TIS_STS_EXPECT |
@@ -611,17 +621,17 @@ static void test_spi_tpm_transmit_test(const void *data)
     g_test_message("\ttransmit tests, check TPM_TIS_STS_EXPECT");
 
     /* start processing */
-    tpm_reg_writew(chip, 0, TPM_TIS_REG_STS, TPM_TIS_STS_TPM_GO);
+    tpm_reg_writel(chip, 0, TPM_TIS_REG_STS, TPM_TIS_STS_TPM_GO);
 
     uint64_t end_time = g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND;
     do {
-        sts = tpm_reg_readw(chip, 0, TPM_TIS_REG_STS);
+        sts = tpm_reg_readl(chip, 0, TPM_TIS_REG_STS);
         if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) {
             break;
         }
     } while (g_get_monotonic_time() < end_time);
 
-    sts = tpm_reg_readw(chip, 0, TPM_TIS_REG_STS);
+    sts = tpm_reg_readl(chip, 0, TPM_TIS_REG_STS);
     DPRINTF_STS;
     g_assert_cmpint(sts & 0xff, == , TPM_TIS_STS_VALID |
                                      TPM_TIS_STS_DATA_AVAILABLE);
@@ -635,7 +645,7 @@ static void test_spi_tpm_transmit_test(const void *data)
 
     for (i = 0; i < sizeof(tpm_msg); i++) {
         tpm_msg[i] = tpm_reg_readb(chip, 0, TPM_TIS_REG_DATA_FIFO);
-        sts = tpm_reg_readw(chip, 0, TPM_TIS_REG_STS);
+        sts = tpm_reg_readl(chip, 0, TPM_TIS_REG_STS);
         DPRINTF_STS;
         if (sts & TPM_TIS_STS_DATA_AVAILABLE) {
             g_assert_cmpint((sts >> 8) & 0xffff, ==, --bcount);
tests/qtest/meson.build +1 -0
index 6b1aed929e..74aa9f57e0 100644
@@ -4,6 +4,7 @@ slow_qtests = {
   'bios-tables-test' : 910,
   'cdrom-test' : 610,
   'device-introspect-test' : 720,
+  'ide-test' : 120,
   'migration-test' : 480,
   'npcm7xx_pwm-test': 300,
   'npcm7xx_watchdog_timer-test': 120,
Signature · 1 line
2.39.5
#6 lore raw
In reply to dan tan
Quoted 11 lines · dan tan

*** BLURB HERE ***

Support TPM for SPI (Serial Peripheral Interface)

Revision 3 summary:
device support:
- moved variable tis_addr from TPMStateSPI struct to local
- added the VM suspend/resume support:
- added vmstate_tpm_tis_spi declaration
- added tpm_tis_spi_pre_save() function
- fixed trace formatting string

Can you please update patch 1/3 with those changes relevant for this patch and then patch 3/3 as well. I think it should be fairly simple to modify each one of them by merging 4/5 into 1/5 and 5/5 into 3/5. There's no reason that there are so many changes in 4/5 and 5/5 replacing new code.

Thanks
Stefan