On Tue, Apr 28, 2026 at 01:07:13PM +0000, Stefan Berger wrote:
> Move functions for reading from and writing to the Aspeed I2C device into
> a file so they can be reused by other functions.
> 
> Signed-off-by: Stefan Berger <[email protected]>
> ---
>  tests/qtest/meson.build        |  2 +-
>  tests/qtest/tpm-tis-i2c-test.c | 50 +-------------------------
>  tests/qtest/tpm-tis-i2c-util.c | 64 ++++++++++++++++++++++++++++++++++
>  tests/qtest/tpm-tis-i2c-util.h | 30 ++++++++++++++++
>  4 files changed, 96 insertions(+), 50 deletions(-)
>  create mode 100644 tests/qtest/tpm-tis-i2c-util.c
>  create mode 100644 tests/qtest/tpm-tis-i2c-util.h
> 
> diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
> index b735f55fc4..823be192e7 100644
> --- a/tests/qtest/meson.build
> +++ b/tests/qtest/meson.build
> @@ -397,7 +397,7 @@ qtests = {
>    'tpm-crb-test': [io, tpmemu_files],
>    'tpm-tis-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'],
>    'tpm-tis-test': [io, tpmemu_files, 'tpm-tis-util.c'],
> -  'tpm-tis-i2c-test': [io, tpmemu_files, 'qtest_aspeed.c'],
> +  'tpm-tis-i2c-test': [io, tpmemu_files, 'tpm-tis-i2c-util.c', 
> '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'],
>    'virtio-net-failover': test_migration_files,
> diff --git a/tests/qtest/tpm-tis-i2c-test.c b/tests/qtest/tpm-tis-i2c-test.c
> index 3a1af026f2..02ddf76c2c 100644
> --- a/tests/qtest/tpm-tis-i2c-test.c
> +++ b/tests/qtest/tpm-tis-i2c-test.c
> @@ -20,6 +20,7 @@
>  #include "hw/pci/pci_ids.h"
>  #include "qtest_aspeed.h"
>  #include "tpm-emu.h"
> +#include "tpm-tis-i2c-util.h"
>  
>  #define DEBUG_TIS_TEST 0
>  
> @@ -36,58 +37,9 @@
>  #define DPRINTF_STS \
>      DPRINTF("%s: %d: sts = 0x%08x\n", __func__, __LINE__, sts)
>  
> -#define I2C_SLAVE_ADDR   0x2e
> -#define I2C_DEV_BUS_NUM  10
> -
>  static const uint8_t TPM_CMD[12] =
>      "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00";
>  
> -static uint32_t aspeed_bus_addr;
> -
> -static uint8_t cur_locty = 0xff;
> -
> -static void tpm_tis_i2c_set_locty(uint8_t locty)
> -{
> -    if (cur_locty != locty) {
> -        cur_locty = locty;
> -        aspeed_i2c_writeb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR,
> -                          TPM_I2C_REG_LOC_SEL, locty);
> -    }
> -}
> -
> -static uint8_t tpm_tis_i2c_readb(uint8_t locty, uint8_t reg)
> -{
> -    tpm_tis_i2c_set_locty(locty);
> -    return aspeed_i2c_readb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, 
> reg);
> -}
> -
> -static uint16_t tpm_tis_i2c_readw(uint8_t locty, uint8_t reg)
> -{
> -    tpm_tis_i2c_set_locty(locty);
> -    return aspeed_i2c_readw(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, 
> reg);
> -}
> -
> -static uint32_t tpm_tis_i2c_readl(uint8_t locty, uint8_t reg)
> -{
> -    tpm_tis_i2c_set_locty(locty);
> -    return aspeed_i2c_readl(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, 
> reg);
> -}
> -
> -static void tpm_tis_i2c_writeb(uint8_t locty, uint8_t reg, uint8_t v)
> -{
> -    if (reg != TPM_I2C_REG_LOC_SEL) {
> -        tpm_tis_i2c_set_locty(locty);
> -    }
> -    aspeed_i2c_writeb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg, v);
> -}
> -
> -static void tpm_tis_i2c_writel(uint8_t locty, uint8_t reg, uint32_t v)
> -{
> -    if (reg != TPM_I2C_REG_LOC_SEL) {
> -        tpm_tis_i2c_set_locty(locty);
> -    }
> -    aspeed_i2c_writel(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg, v);
> -}
>  
>  static void tpm_tis_i2c_test_basic(const void *data)
>  {
> diff --git a/tests/qtest/tpm-tis-i2c-util.c b/tests/qtest/tpm-tis-i2c-util.c
> new file mode 100644
> index 0000000000..07b1eeba69
> --- /dev/null
> +++ b/tests/qtest/tpm-tis-i2c-util.c
> @@ -0,0 +1,64 @@
> +/*
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + *
> + * QTest utilities for TPM TIS over I2C
> + *
> + * Copyright (c) 2018, 2026 IBM Corporation
> + *
> + * Authors:
> + *   Stefan Berger <[email protected]>
> + *
> + */
> +
> +#include "qemu/osdep.h"
> +#include "hw/acpi/tpm.h"
> +#include "libqtest-single.h"
> +#include "qtest_aspeed.h"
> +#include "tpm-tis-i2c-util.h"
> +
> +uint32_t aspeed_bus_addr;
> +
> +static uint8_t cur_locty = 0xff;
> +
> +static void tpm_tis_i2c_set_locty(uint8_t locty)
> +{
> +    if (cur_locty != locty) {
> +        cur_locty = locty;
> +        aspeed_i2c_writeb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR,
> +                          TPM_I2C_REG_LOC_SEL, locty);
> +    }
> +}
> +
> +uint8_t tpm_tis_i2c_readb(uint8_t locty, uint8_t reg)
> +{
> +    tpm_tis_i2c_set_locty(locty);
> +    return aspeed_i2c_readb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, 
> reg);
> +}
> +
> +uint16_t tpm_tis_i2c_readw(uint8_t locty, uint8_t reg)
> +{
> +    tpm_tis_i2c_set_locty(locty);
> +    return aspeed_i2c_readw(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, 
> reg);
> +}
> +
> +uint32_t tpm_tis_i2c_readl(uint8_t locty, uint8_t reg)
> +{
> +    tpm_tis_i2c_set_locty(locty);
> +    return aspeed_i2c_readl(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, 
> reg);
> +}
> +
> +void tpm_tis_i2c_writeb(uint8_t locty, uint8_t reg, uint8_t v)
> +{
> +    if (reg != TPM_I2C_REG_LOC_SEL) {
> +        tpm_tis_i2c_set_locty(locty);
> +    }
> +    aspeed_i2c_writeb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg, v);
> +}
> +
> +void tpm_tis_i2c_writel(uint8_t locty, uint8_t reg, uint32_t v)
> +{
> +    if (reg != TPM_I2C_REG_LOC_SEL) {
> +        tpm_tis_i2c_set_locty(locty);
> +    }
> +    aspeed_i2c_writel(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg, v);
> +}
> diff --git a/tests/qtest/tpm-tis-i2c-util.h b/tests/qtest/tpm-tis-i2c-util.h
> new file mode 100644
> index 0000000000..dfe626b43d
> --- /dev/null
> +++ b/tests/qtest/tpm-tis-i2c-util.h
> @@ -0,0 +1,30 @@
> +/*
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + *
> + * QTest TPM TIS I2C: Common test functions used for TPM I2C on Aspeed bus
> + *
> + * Copyright (c) 2026 IBM Corporation
> + *
> + * Authors:
> + *   Stefan Berger <[email protected]>
> + *
> + */
> +
> +#ifndef TESTS_TPM_TIS_I2C_UTIL_H
> +#define TESTS_TPM_TIS_I2C_UTIL_H
> +
> +#include "qemu/osdep.h"
> +
> +extern uint32_t aspeed_bus_addr;
> +
> +#define I2C_SLAVE_ADDR   0x2e
> +#define I2C_DEV_BUS_NUM  10
> +
> +uint8_t tpm_tis_i2c_readb(uint8_t locty, uint8_t reg);
> +uint16_t tpm_tis_i2c_readw(uint8_t locty, uint8_t reg);
> +uint32_t tpm_tis_i2c_readl(uint8_t locty, uint8_t reg);
> +
> +void tpm_tis_i2c_writeb(uint8_t locty, uint8_t reg, uint8_t v);
> +void tpm_tis_i2c_writel(uint8_t locty, uint8_t reg, uint32_t v);
> +
> +#endif /* TESTS_TPM_TIS_I2C_UTIL_H */
> -- 
> 2.43.0
> 
> 
Looks good to me.

Reviewed-by: Arun Menon <[email protected]>


Reply via email to