@samitouri / QOSamiQemu / commits / 91e29853bb

tests: Move TPM I2C bus read/write functions to common files

Move functions for reading from and writing to the Aspeed I2C device into a file so they can be reused by other functions. Reviewed-by: Arun Menon <armenon@redhat.com> Link: https://lore.kernel.org/qemu-devel/20260429121743.1346635-2-stefanb@linux.ibm.com Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>

Stefan Berger committed Apr 29, 2026 at 12:17 UTC 91e29853bb6d5503e6c652062dccb248ebf14560
4 files changed +96 -50
tests/qtest/meson.build
+1 -1
@@ -401,7 +401,7 @@ qtests = {
401 'tpm-crb-test': [io, tpmemu_files],
402 'tpm-tis-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'],
403 'tpm-tis-test': [io, tpmemu_files, 'tpm-tis-util.c'],
404 - 'tpm-tis-i2c-test': [io, tpmemu_files, 'qtest_aspeed.c'],
404 + 'tpm-tis-i2c-test': [io, tpmemu_files, 'tpm-tis-i2c-util.c', 'qtest_aspeed.c'],
405 'tpm-tis-device-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'],
406 'tpm-tis-device-test': [io, tpmemu_files, 'tpm-tis-util.c'],
407 'virtio-net-failover': test_migration_files,
tests/qtest/tpm-tis-i2c-test.c
+1 -49
@@ -20,6 +20,7 @@
20 #include "hw/pci/pci_ids.h"
21 #include "qtest_aspeed.h"
22 #include "tpm-emu.h"
23 +#include "tpm-tis-i2c-util.h"
24
25 #define DEBUG_TIS_TEST 0
26
@@ -36,58 +37,9 @@
37 #define DPRINTF_STS \
38 DPRINTF("%s: %d: sts = 0x%08x\n", __func__, __LINE__, sts)
39
39 -#define I2C_SLAVE_ADDR 0x2e
40 -#define I2C_DEV_BUS_NUM 10
41 -
40 static const uint8_t TPM_CMD[12] =
41 "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00";
42
45 -static uint32_t aspeed_bus_addr;
46 -
47 -static uint8_t cur_locty = 0xff;
48 -
49 -static void tpm_tis_i2c_set_locty(uint8_t locty)
50 -{
51 - if (cur_locty != locty) {
52 - cur_locty = locty;
53 - aspeed_i2c_writeb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR,
54 - TPM_I2C_REG_LOC_SEL, locty);
55 - }
56 -}
57 -
58 -static uint8_t tpm_tis_i2c_readb(uint8_t locty, uint8_t reg)
59 -{
60 - tpm_tis_i2c_set_locty(locty);
61 - return aspeed_i2c_readb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg);
62 -}
63 -
64 -static uint16_t tpm_tis_i2c_readw(uint8_t locty, uint8_t reg)
65 -{
66 - tpm_tis_i2c_set_locty(locty);
67 - return aspeed_i2c_readw(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg);
68 -}
69 -
70 -static uint32_t tpm_tis_i2c_readl(uint8_t locty, uint8_t reg)
71 -{
72 - tpm_tis_i2c_set_locty(locty);
73 - return aspeed_i2c_readl(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg);
74 -}
75 -
76 -static void tpm_tis_i2c_writeb(uint8_t locty, uint8_t reg, uint8_t v)
77 -{
78 - if (reg != TPM_I2C_REG_LOC_SEL) {
79 - tpm_tis_i2c_set_locty(locty);
80 - }
81 - aspeed_i2c_writeb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg, v);
82 -}
83 -
84 -static void tpm_tis_i2c_writel(uint8_t locty, uint8_t reg, uint32_t v)
85 -{
86 - if (reg != TPM_I2C_REG_LOC_SEL) {
87 - tpm_tis_i2c_set_locty(locty);
88 - }
89 - aspeed_i2c_writel(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg, v);
90 -}
43
44 static void tpm_tis_i2c_test_basic(const void *data)
45 {
tests/qtest/tpm-tis-i2c-util.c new
+64
@@ -0,0 +1,64 @@
1 +/*
2 + * SPDX-License-Identifier: GPL-2.0-or-later
3 + *
4 + * QTest utilities for TPM TIS over I2C
5 + *
6 + * Copyright (c) 2018, 2026 IBM Corporation
7 + *
8 + * Authors:
9 + * Stefan Berger <stefanb@linux.ibm.com>
10 + *
11 + */
12 +
13 +#include "qemu/osdep.h"
14 +#include "hw/acpi/tpm.h"
15 +#include "libqtest-single.h"
16 +#include "qtest_aspeed.h"
17 +#include "tpm-tis-i2c-util.h"
18 +
19 +uint32_t aspeed_bus_addr;
20 +
21 +static uint8_t cur_locty = 0xff;
22 +
23 +static void tpm_tis_i2c_set_locty(uint8_t locty)
24 +{
25 + if (cur_locty != locty) {
26 + cur_locty = locty;
27 + aspeed_i2c_writeb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR,
28 + TPM_I2C_REG_LOC_SEL, locty);
29 + }
30 +}
31 +
32 +uint8_t tpm_tis_i2c_readb(uint8_t locty, uint8_t reg)
33 +{
34 + tpm_tis_i2c_set_locty(locty);
35 + return aspeed_i2c_readb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg);
36 +}
37 +
38 +uint16_t tpm_tis_i2c_readw(uint8_t locty, uint8_t reg)
39 +{
40 + tpm_tis_i2c_set_locty(locty);
41 + return aspeed_i2c_readw(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg);
42 +}
43 +
44 +uint32_t tpm_tis_i2c_readl(uint8_t locty, uint8_t reg)
45 +{
46 + tpm_tis_i2c_set_locty(locty);
47 + return aspeed_i2c_readl(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg);
48 +}
49 +
50 +void tpm_tis_i2c_writeb(uint8_t locty, uint8_t reg, uint8_t v)
51 +{
52 + if (reg != TPM_I2C_REG_LOC_SEL) {
53 + tpm_tis_i2c_set_locty(locty);
54 + }
55 + aspeed_i2c_writeb(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg, v);
56 +}
57 +
58 +void tpm_tis_i2c_writel(uint8_t locty, uint8_t reg, uint32_t v)
59 +{
60 + if (reg != TPM_I2C_REG_LOC_SEL) {
61 + tpm_tis_i2c_set_locty(locty);
62 + }
63 + aspeed_i2c_writel(global_qtest, aspeed_bus_addr, I2C_SLAVE_ADDR, reg, v);
64 +}
tests/qtest/tpm-tis-i2c-util.h new
+30
@@ -0,0 +1,30 @@
1 +/*
2 + * SPDX-License-Identifier: GPL-2.0-or-later
3 + *
4 + * QTest TPM TIS I2C: Common test functions used for TPM I2C on Aspeed bus
5 + *
6 + * Copyright (c) 2026 IBM Corporation
7 + *
8 + * Authors:
9 + * Stefan Berger <stefanb@linux.ibm.com>
10 + *
11 + */
12 +
13 +#ifndef TESTS_TPM_TIS_I2C_UTIL_H
14 +#define TESTS_TPM_TIS_I2C_UTIL_H
15 +
16 +#include "qemu/osdep.h"
17 +
18 +extern uint32_t aspeed_bus_addr;
19 +
20 +#define I2C_SLAVE_ADDR 0x2e
21 +#define I2C_DEV_BUS_NUM 10
22 +
23 +uint8_t tpm_tis_i2c_readb(uint8_t locty, uint8_t reg);
24 +uint16_t tpm_tis_i2c_readw(uint8_t locty, uint8_t reg);
25 +uint32_t tpm_tis_i2c_readl(uint8_t locty, uint8_t reg);
26 +
27 +void tpm_tis_i2c_writeb(uint8_t locty, uint8_t reg, uint8_t v);
28 +void tpm_tis_i2c_writel(uint8_t locty, uint8_t reg, uint32_t v);
29 +
30 +#endif /* TESTS_TPM_TIS_I2C_UTIL_H */