qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [RFC v2 0/6] vTPM for aarch64
@ 2020-02-14 18:36 Eric Auger
  2020-02-14 18:36 ` [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA Eric Auger
                   ` (6 more replies)
  0 siblings, 7 replies; 30+ messages in thread
From: Eric Auger @ 2020-02-14 18:36 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

This series adds the capability to instantiate an MMIO TPM TIS
in ARM virt.

The existing TPM TIS code is reshuffled into a generic part,
the ISA device and the sysbus device. The last patch allows
the instantiation of the TPM TIS sysbus device in ARM virt.

The series was tested with the swtpm/libtpms emulator.
Automatic guest LUKS volume unlocking (tpm2) was successful.
EDK2 support is under development [3]. Thanks to Ard
for supporting me when setting up the test environment.

Best Regards

Eric

Testing:

mkdir /tmp/tpm
swtpm socket \
--tpm2 \
-t -d \
--tpmstate dir=/tmp/tpm \
--ctrl type=unixio,path=/tmp/swtpm-sock

qemu command line must be augmented with the following options:

-chardev socket,id=chrtpm,path=/tmp/swtpm-sock \
-tpmdev emulator,id=tpm0,chardev=chrtpm \
-device tpm-tis-device,tpmdev=tpm0 \

References:
[1] libtpms: https://github.com/stefanberger/libtpms/wiki
[2] swtpm: https://github.com/stefanberger/swtpm/wiki
[3] [PATCH 0/4] ArmVirtPkg: implement measured boot for ArmVirtQemu

This series can be found at:
https://github.com/eauger/qemu/tree/v4.2.0-tpm-rfc-v2

History:

RFC v1 -> RFC v2:
- restructure the existing code with common, ISA and sysbus part.
- both ARM and x86 integration were tested.
- acknowledgement: migration has not been tested

Eric Auger (6):
  tpm: rename TPM_TIS into TPM_TIS_ISA
  tpm: Use TPMState as a common struct
  tpm: Separate tpm_tis common functions from isa code
  tpm: Separate TPM_TIS and TPM_TIS_ISA configs
  tpm: Add the SysBus TPM TIS device
  hw/arm/virt: vTPM support

 default-configs/i386-softmmu.mak       |   2 +-
 hw/arm/Kconfig                         |   1 +
 hw/arm/sysbus-fdt.c                    |  36 +++++
 hw/arm/virt.c                          |   7 +
 hw/i386/Kconfig                        |   2 +-
 hw/i386/acpi-build.c                   |   6 +-
 hw/tpm/Kconfig                         |  12 +-
 hw/tpm/Makefile.objs                   |   4 +-
 hw/tpm/tpm_tis.h                       |  91 +++++++++++++
 hw/tpm/{tpm_tis.c => tpm_tis_common.c} | 181 ++-----------------------
 hw/tpm/tpm_tis_isa.c                   | 170 +++++++++++++++++++++++
 hw/tpm/tpm_tis_sysbus.c                | 159 ++++++++++++++++++++++
 include/sysemu/tpm.h                   |   7 +-
 tests/qtest/Makefile.include           |   4 +-
 14 files changed, 502 insertions(+), 180 deletions(-)
 create mode 100644 hw/tpm/tpm_tis.h
 rename hw/tpm/{tpm_tis.c => tpm_tis_common.c} (85%)
 create mode 100644 hw/tpm/tpm_tis_isa.c
 create mode 100644 hw/tpm/tpm_tis_sysbus.c

-- 
2.20.1



^ permalink raw reply	[flat|nested] 30+ messages in thread

* [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA
  2020-02-14 18:36 [RFC v2 0/6] vTPM for aarch64 Eric Auger
@ 2020-02-14 18:36 ` Eric Auger
  2020-02-14 18:55   ` Philippe Mathieu-Daudé
  2020-02-16 18:14   ` Stefan Berger
  2020-02-14 18:37 ` [RFC v2 2/6] tpm: Use TPMState as a common struct Eric Auger
                   ` (5 subsequent siblings)
  6 siblings, 2 replies; 30+ messages in thread
From: Eric Auger @ 2020-02-14 18:36 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

As we plan to introduce a sysbus TPM_TIS, let's rename
TPM_TIS into TPM_TIS_ISA.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 hw/i386/acpi-build.c | 6 +++---
 hw/tpm/tpm_tis.c     | 4 ++--
 include/sysemu/tpm.h | 6 +++---
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index 9c4e46fa74..26777f8828 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -2026,7 +2026,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
         }
     }
 
-    if (TPM_IS_TIS(tpm_find())) {
+    if (TPM_IS_TIS_ISA(tpm_find())) {
         aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
                    TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
     }
@@ -2197,7 +2197,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
             /* Scan all PCI buses. Generate tables to support hotplug. */
             build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
 
-            if (TPM_IS_TIS(tpm)) {
+            if (TPM_IS_TIS_ISA(tpm)) {
                 if (misc->tpm_version == TPM_VERSION_2_0) {
                     dev = aml_device("TPM");
                     aml_append(dev, aml_name_decl("_HID",
@@ -2304,7 +2304,7 @@ build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog)
         (char *)&tpm2_ptr->log_area_start_address - table_data->data;
 
     tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
-    if (TPM_IS_TIS(tpm_find())) {
+    if (TPM_IS_TIS_ISA(tpm_find())) {
         tpm2_ptr->control_area_address = cpu_to_le64(0);
         tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
     } else if (TPM_IS_CRB(tpm_find())) {
diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
index 31facb896d..c609737272 100644
--- a/hw/tpm/tpm_tis.c
+++ b/hw/tpm/tpm_tis.c
@@ -91,7 +91,7 @@ typedef struct TPMState {
     TPMPPI ppi;
 } TPMState;
 
-#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
+#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
 
 #define DEBUG_TIS 0
 
@@ -1008,7 +1008,7 @@ static void tpm_tis_class_init(ObjectClass *klass, void *data)
 }
 
 static const TypeInfo tpm_tis_info = {
-    .name = TYPE_TPM_TIS,
+    .name = TYPE_TPM_TIS_ISA,
     .parent = TYPE_ISA_DEVICE,
     .instance_size = sizeof(TPMState),
     .instance_init = tpm_tis_initfn,
diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
index 15979a3647..1691b92c28 100644
--- a/include/sysemu/tpm.h
+++ b/include/sysemu/tpm.h
@@ -43,12 +43,12 @@ typedef struct TPMIfClass {
     enum TPMVersion (*get_version)(TPMIf *obj);
 } TPMIfClass;
 
-#define TYPE_TPM_TIS                "tpm-tis"
+#define TYPE_TPM_TIS_ISA            "tpm-tis"
 #define TYPE_TPM_CRB                "tpm-crb"
 #define TYPE_TPM_SPAPR              "tpm-spapr"
 
-#define TPM_IS_TIS(chr)                             \
-    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS)
+#define TPM_IS_TIS_ISA(chr)                         \
+    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
 #define TPM_IS_CRB(chr)                             \
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
 #define TPM_IS_SPAPR(chr)                           \
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC v2 2/6] tpm: Use TPMState as a common struct
  2020-02-14 18:36 [RFC v2 0/6] vTPM for aarch64 Eric Auger
  2020-02-14 18:36 ` [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA Eric Auger
@ 2020-02-14 18:37 ` Eric Auger
  2020-02-14 19:01   ` Philippe Mathieu-Daudé
                     ` (2 more replies)
  2020-02-14 18:37 ` [RFC v2 3/6] tpm: Separate tpm_tis common functions from isa code Eric Auger
                   ` (4 subsequent siblings)
  6 siblings, 3 replies; 30+ messages in thread
From: Eric Auger @ 2020-02-14 18:37 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

As we plan to introdce a SysBus TPM TIS device, let's
make the TPMState a common struct usable by both the
ISADevice and the SysBusDevice. TPMStateISA embeds the
struct and inherits from the ISADevice.

The prototype of functions bound to be used by both
the ISA and SysBus devices is changed to take TPMState
handle.

A bunch of structs also are renamed to be specialized
for the ISA device. Besides those transformations, no
functional change is expected.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 hw/tpm/tpm_tis.c | 146 +++++++++++++++++++++++++++++------------------
 1 file changed, 91 insertions(+), 55 deletions(-)

diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
index c609737272..fc6d7ca579 100644
--- a/hw/tpm/tpm_tis.c
+++ b/hw/tpm/tpm_tis.c
@@ -65,7 +65,6 @@ typedef struct TPMLocality {
 } TPMLocality;
 
 typedef struct TPMState {
-    ISADevice busdev;
     MemoryRegion mmio;
 
     unsigned char buffer[TPM_TIS_BUFFER_MAX];
@@ -91,7 +90,15 @@ typedef struct TPMState {
     TPMPPI ppi;
 } TPMState;
 
-#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
+typedef struct TPMStateISA {
+    /*< private >*/
+    ISADevice parent_obj;
+
+    /*< public >*/
+    TPMState state; /* not a QOM object */
+} TPMStateISA;
+
+#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
 
 #define DEBUG_TIS 0
 
@@ -281,9 +288,8 @@ static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty)
 /*
  * Callback from the TPM to indicate that the response was received.
  */
-static void tpm_tis_request_completed(TPMIf *ti, int ret)
+static void tpm_tis_request_completed(TPMState *s, int ret)
 {
-    TPMState *s = TPM(ti);
     uint8_t locty = s->cmd.locty;
     uint8_t l;
 
@@ -338,7 +344,7 @@ static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty)
 }
 
 #ifdef DEBUG_TIS
-static void tpm_tis_dump_state(void *opaque, hwaddr addr)
+static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
 {
     static const unsigned regs[] = {
         TPM_TIS_REG_ACCESS,
@@ -353,7 +359,6 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr)
     int idx;
     uint8_t locty = tpm_tis_locality_from_addr(addr);
     hwaddr base = addr & ~0xfff;
-    TPMState *s = opaque;
 
     printf("tpm_tis: active locality      : %d\n"
            "tpm_tis: state of locality %d : %d\n"
@@ -363,7 +368,7 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr)
 
     for (idx = 0; regs[idx] != 0xfff; idx++) {
         printf("tpm_tis: 0x%04x : 0x%08x\n", regs[idx],
-               (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4));
+               (int)tpm_tis_mmio_read(s, base + regs[idx], 4));
     }
 
     printf("tpm_tis: r/w offset    : %d\n"
@@ -488,7 +493,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
         break;
 #ifdef DEBUG_TIS
     case TPM_TIS_REG_DEBUG:
-        tpm_tis_dump_state(opaque, addr);
+        tpm_tis_dump_state(s, addr);
         break;
 #endif
     }
@@ -835,10 +840,8 @@ static const MemoryRegionOps tpm_tis_memory_ops = {
 /*
  * Get the TPMVersion of the backend device being used
  */
-static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
+static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
 {
-    TPMState *s = TPM(ti);
-
     if (tpm_backend_had_startup_error(s->be_driver)) {
         return TPM_VERSION_UNSPEC;
     }
@@ -850,9 +853,8 @@ static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
  * This function is called when the machine starts, resets or due to
  * S3 resume.
  */
-static void tpm_tis_reset(DeviceState *dev)
+static void tpm_tis_reset(TPMState *s)
 {
-    TPMState *s = TPM(dev);
     int c;
 
     s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver);
@@ -896,15 +898,14 @@ static void tpm_tis_reset(DeviceState *dev)
 
 /* persistent state handling */
 
-static int tpm_tis_pre_save(void *opaque)
+static int tpm_tis_pre_save(TPMState *s)
 {
-    TPMState *s = opaque;
     uint8_t locty = s->active_locty;
 
     trace_tpm_tis_pre_save(locty, s->rw_offset);
 
     if (DEBUG_TIS) {
-        tpm_tis_dump_state(opaque, 0);
+        tpm_tis_dump_state(s, 0);
     }
 
     /*
@@ -929,34 +930,78 @@ static const VMStateDescription vmstate_locty = {
     }
 };
 
-static const VMStateDescription vmstate_tpm_tis = {
+/* ISA */
+
+static int tpm_tis_pre_save_isa(void *opaque)
+{
+    TPMStateISA *isadev = opaque;
+
+    return tpm_tis_pre_save(&isadev->state);
+}
+
+static const VMStateDescription vmstate_tpm_tis_isa = {
     .name = "tpm-tis",
     .version_id = 0,
-    .pre_save  = tpm_tis_pre_save,
+    .pre_save  = tpm_tis_pre_save_isa,
     .fields = (VMStateField[]) {
-        VMSTATE_BUFFER(buffer, TPMState),
-        VMSTATE_UINT16(rw_offset, TPMState),
-        VMSTATE_UINT8(active_locty, TPMState),
-        VMSTATE_UINT8(aborting_locty, TPMState),
-        VMSTATE_UINT8(next_locty, TPMState),
+        VMSTATE_BUFFER(state.buffer, TPMStateISA),
+        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
+        VMSTATE_UINT8(state.active_locty, TPMStateISA),
+        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
+        VMSTATE_UINT8(state.next_locty, TPMStateISA),
 
-        VMSTATE_STRUCT_ARRAY(loc, TPMState, TPM_TIS_NUM_LOCALITIES, 0,
+        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
                              vmstate_locty, TPMLocality),
 
         VMSTATE_END_OF_LIST()
     }
 };
 
-static Property tpm_tis_properties[] = {
-    DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ),
-    DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver),
-    DEFINE_PROP_BOOL("ppi", TPMState, ppi_enabled, true),
+static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(ti);
+    TPMState *s = &isadev->state;
+
+    tpm_tis_request_completed(s, ret);
+}
+
+static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(ti);
+    TPMState *s = &isadev->state;
+
+    return tpm_tis_get_tpm_version(s);
+}
+
+static void tpm_tis_isa_reset(DeviceState *dev)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(dev);
+    TPMState *s = &isadev->state;
+
+    return tpm_tis_reset(s);
+}
+
+static Property tpm_tis_isa_properties[] = {
+    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
+    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
+    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
+static void tpm_tis_isa_initfn(Object *obj)
 {
-    TPMState *s = TPM(dev);
+    TPMStateISA *isadev = TPM_TIS_ISA(obj);
+    TPMState *s = &isadev->state;
+
+    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
+                          s, "tpm-tis-mmio",
+                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
+}
+
+static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(dev);
+    TPMState *s = &isadev->state;
 
     if (!tpm_find()) {
         error_setg(errp, "at most one TPM device is permitted");
@@ -973,55 +1018,46 @@ static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    isa_init_irq(&s->busdev, &s->irq, s->irq_num);
+    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
 
     memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
                                 TPM_TIS_ADDR_BASE, &s->mmio);
 
     if (s->ppi_enabled) {
         tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
-                     TPM_PPI_ADDR_BASE, OBJECT(s));
+                     TPM_PPI_ADDR_BASE, OBJECT(dev));
     }
 }
 
-static void tpm_tis_initfn(Object *obj)
-{
-    TPMState *s = TPM(obj);
-
-    memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
-                          s, "tpm-tis-mmio",
-                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
-}
-
-static void tpm_tis_class_init(ObjectClass *klass, void *data)
+static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     TPMIfClass *tc = TPM_IF_CLASS(klass);
 
-    dc->realize = tpm_tis_realizefn;
-    device_class_set_props(dc, tpm_tis_properties);
-    dc->reset = tpm_tis_reset;
-    dc->vmsd  = &vmstate_tpm_tis;
+    device_class_set_props(dc, tpm_tis_isa_properties);
+    dc->vmsd  = &vmstate_tpm_tis_isa;
     tc->model = TPM_MODEL_TPM_TIS;
-    tc->get_version = tpm_tis_get_tpm_version;
-    tc->request_completed = tpm_tis_request_completed;
+    dc->realize = tpm_tis_isa_realizefn;
+    dc->reset = tpm_tis_isa_reset;
+    tc->request_completed = tpm_tis_isa_request_completed;
+    tc->get_version = tpm_tis_isa_get_tpm_version;
 }
 
-static const TypeInfo tpm_tis_info = {
+static const TypeInfo tpm_tis_isa_info = {
     .name = TYPE_TPM_TIS_ISA,
     .parent = TYPE_ISA_DEVICE,
-    .instance_size = sizeof(TPMState),
-    .instance_init = tpm_tis_initfn,
-    .class_init  = tpm_tis_class_init,
+    .instance_size = sizeof(TPMStateISA),
+    .instance_init = tpm_tis_isa_initfn,
+    .class_init  = tpm_tis_isa_class_init,
     .interfaces = (InterfaceInfo[]) {
         { TYPE_TPM_IF },
         { }
     }
 };
 
-static void tpm_tis_register(void)
+static void tpm_tis_isa_register(void)
 {
-    type_register_static(&tpm_tis_info);
+    type_register_static(&tpm_tis_isa_info);
 }
 
-type_init(tpm_tis_register)
+type_init(tpm_tis_isa_register)
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC v2 3/6] tpm: Separate tpm_tis common functions from isa code
  2020-02-14 18:36 [RFC v2 0/6] vTPM for aarch64 Eric Auger
  2020-02-14 18:36 ` [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA Eric Auger
  2020-02-14 18:37 ` [RFC v2 2/6] tpm: Use TPMState as a common struct Eric Auger
@ 2020-02-14 18:37 ` Eric Auger
  2020-02-16 18:27   ` Stefan Berger
  2020-02-14 18:37 ` [RFC v2 4/6] tpm: Separate TPM_TIS and TPM_TIS_ISA configs Eric Auger
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 30+ messages in thread
From: Eric Auger @ 2020-02-14 18:37 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

Move the device agnostic code into tpm_tis_common.c and
put the ISA device specific code into tpm_tis_isa.c

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 hw/tpm/Makefile.objs                   |   2 +-
 hw/tpm/tpm_tis.h                       |  91 +++++++++++
 hw/tpm/{tpm_tis.c => tpm_tis_common.c} | 209 ++-----------------------
 hw/tpm/tpm_tis_isa.c                   | 170 ++++++++++++++++++++
 4 files changed, 271 insertions(+), 201 deletions(-)
 create mode 100644 hw/tpm/tpm_tis.h
 rename hw/tpm/{tpm_tis.c => tpm_tis_common.c} (83%)
 create mode 100644 hw/tpm/tpm_tis_isa.c

diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
index 85eb99ae05..fcc4c2f27c 100644
--- a/hw/tpm/Makefile.objs
+++ b/hw/tpm/Makefile.objs
@@ -1,6 +1,6 @@
 common-obj-$(CONFIG_TPM) += tpm_util.o
 obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
-common-obj-$(CONFIG_TPM_TIS) += tpm_tis.o
+common-obj-$(CONFIG_TPM_TIS) += tpm_tis_isa.o tpm_tis_common.o
 common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
 common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
 common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o
diff --git a/hw/tpm/tpm_tis.h b/hw/tpm/tpm_tis.h
new file mode 100644
index 0000000000..5554989395
--- /dev/null
+++ b/hw/tpm/tpm_tis.h
@@ -0,0 +1,91 @@
+/*
+ * tpm_tis.h - QEMU's TPM TIS common header
+ *
+ * Copyright (C) 2006,2010-2013 IBM Corporation
+ *
+ * Authors:
+ *  Stefan Berger <stefanb@us.ibm.com>
+ *  David Safford <safford@us.ibm.com>
+ *
+ * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ * Implementation of the TIS interface according to specs found at
+ * http://www.trustedcomputinggroup.org. This implementation currently
+ * supports version 1.3, 21 March 2013
+ * In the developers menu choose the PC Client section then find the TIS
+ * specification.
+ *
+ * TPM TIS for TPM 2 implementation following TCG PC Client Platform
+ * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
+ */
+#ifndef TPM_TPM_TIS_H
+#define TPM_TPM_TIS_H
+
+#include "qemu/osdep.h"
+#include "sysemu/tpm_backend.h"
+#include "tpm_ppi.h"
+
+#define TPM_TIS_NUM_LOCALITIES      5     /* per spec */
+#define TPM_TIS_LOCALITY_SHIFT      12
+#define TPM_TIS_NO_LOCALITY         0xff
+
+#define TPM_TIS_IS_VALID_LOCTY(x)   ((x) < TPM_TIS_NUM_LOCALITIES)
+
+#define TPM_TIS_BUFFER_MAX          4096
+
+typedef enum {
+    TPM_TIS_STATE_IDLE = 0,
+    TPM_TIS_STATE_READY,
+    TPM_TIS_STATE_COMPLETION,
+    TPM_TIS_STATE_EXECUTION,
+    TPM_TIS_STATE_RECEPTION,
+} TPMTISState;
+
+/* locality data  -- all fields are persisted */
+typedef struct TPMLocality {
+    TPMTISState state;
+    uint8_t access;
+    uint32_t sts;
+    uint32_t iface_id;
+    uint32_t inte;
+    uint32_t ints;
+} TPMLocality;
+
+typedef struct TPMState {
+    MemoryRegion mmio;
+
+    unsigned char buffer[TPM_TIS_BUFFER_MAX];
+    uint16_t rw_offset;
+
+    uint8_t active_locty;
+    uint8_t aborting_locty;
+    uint8_t next_locty;
+
+    TPMLocality loc[TPM_TIS_NUM_LOCALITIES];
+
+    qemu_irq irq;
+    uint32_t irq_num;
+
+    TPMBackendCmd cmd;
+
+    TPMBackend *be_driver;
+    TPMVersion be_tpm_version;
+
+    size_t be_buffer_size;
+
+    bool ppi_enabled;
+    TPMPPI ppi;
+} TPMState;
+
+extern const VMStateDescription vmstate_locty;
+extern const MemoryRegionOps tpm_tis_memory_ops;
+
+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);
+
+#endif /* TPM_TPM_TIS_H */
diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis_common.c
similarity index 83%
rename from hw/tpm/tpm_tis.c
rename to hw/tpm/tpm_tis_common.c
index fc6d7ca579..9ce64d4836 100644
--- a/hw/tpm/tpm_tis.c
+++ b/hw/tpm/tpm_tis_common.c
@@ -1,5 +1,6 @@
 /*
- * tpm_tis.c - QEMU's TPM TIS interface emulator
+ * tpm_tis_common.c - QEMU's TPM TIS interface emulator
+ * device agnostic functions
  *
  * Copyright (C) 2006,2010-2013 IBM Corporation
  *
@@ -21,7 +22,6 @@
  * TPM TIS for TPM 2 implementation following TCG PC Client Platform
  * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
  */
-
 #include "qemu/osdep.h"
 #include "hw/irq.h"
 #include "hw/isa/isa.h"
@@ -38,67 +38,7 @@
 #include "tpm_ppi.h"
 #include "trace.h"
 
-#define TPM_TIS_NUM_LOCALITIES      5     /* per spec */
-#define TPM_TIS_LOCALITY_SHIFT      12
-#define TPM_TIS_NO_LOCALITY         0xff
-
-#define TPM_TIS_IS_VALID_LOCTY(x)   ((x) < TPM_TIS_NUM_LOCALITIES)
-
-#define TPM_TIS_BUFFER_MAX          4096
-
-typedef enum {
-    TPM_TIS_STATE_IDLE = 0,
-    TPM_TIS_STATE_READY,
-    TPM_TIS_STATE_COMPLETION,
-    TPM_TIS_STATE_EXECUTION,
-    TPM_TIS_STATE_RECEPTION,
-} TPMTISState;
-
-/* locality data  -- all fields are persisted */
-typedef struct TPMLocality {
-    TPMTISState state;
-    uint8_t access;
-    uint32_t sts;
-    uint32_t iface_id;
-    uint32_t inte;
-    uint32_t ints;
-} TPMLocality;
-
-typedef struct TPMState {
-    MemoryRegion mmio;
-
-    unsigned char buffer[TPM_TIS_BUFFER_MAX];
-    uint16_t rw_offset;
-
-    uint8_t active_locty;
-    uint8_t aborting_locty;
-    uint8_t next_locty;
-
-    TPMLocality loc[TPM_TIS_NUM_LOCALITIES];
-
-    qemu_irq irq;
-    uint32_t irq_num;
-
-    TPMBackendCmd cmd;
-
-    TPMBackend *be_driver;
-    TPMVersion be_tpm_version;
-
-    size_t be_buffer_size;
-
-    bool ppi_enabled;
-    TPMPPI ppi;
-} TPMState;
-
-typedef struct TPMStateISA {
-    /*< private >*/
-    ISADevice parent_obj;
-
-    /*< public >*/
-    TPMState state; /* not a QOM object */
-} TPMStateISA;
-
-#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
+#include "tpm_tis.h"
 
 #define DEBUG_TIS 0
 
@@ -288,7 +228,7 @@ static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty)
 /*
  * Callback from the TPM to indicate that the response was received.
  */
-static void tpm_tis_request_completed(TPMState *s, int ret)
+void tpm_tis_request_completed(TPMState *s, int ret)
 {
     uint8_t locty = s->cmd.locty;
     uint8_t l;
@@ -827,7 +767,7 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
     }
 }
 
-static const MemoryRegionOps tpm_tis_memory_ops = {
+const MemoryRegionOps tpm_tis_memory_ops = {
     .read = tpm_tis_mmio_read,
     .write = tpm_tis_mmio_write,
     .endianness = DEVICE_LITTLE_ENDIAN,
@@ -840,7 +780,7 @@ static const MemoryRegionOps tpm_tis_memory_ops = {
 /*
  * Get the TPMVersion of the backend device being used
  */
-static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
+enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
 {
     if (tpm_backend_had_startup_error(s->be_driver)) {
         return TPM_VERSION_UNSPEC;
@@ -853,7 +793,7 @@ static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
  * This function is called when the machine starts, resets or due to
  * S3 resume.
  */
-static void tpm_tis_reset(TPMState *s)
+void tpm_tis_reset(TPMState *s)
 {
     int c;
 
@@ -898,7 +838,7 @@ static void tpm_tis_reset(TPMState *s)
 
 /* persistent state handling */
 
-static int tpm_tis_pre_save(TPMState *s)
+int tpm_tis_pre_save(TPMState *s)
 {
     uint8_t locty = s->active_locty;
 
@@ -916,7 +856,7 @@ static int tpm_tis_pre_save(TPMState *s)
     return 0;
 }
 
-static const VMStateDescription vmstate_locty = {
+const VMStateDescription vmstate_locty = {
     .name = "tpm-tis/locty",
     .version_id = 0,
     .fields      = (VMStateField[]) {
@@ -930,134 +870,3 @@ static const VMStateDescription vmstate_locty = {
     }
 };
 
-/* ISA */
-
-static int tpm_tis_pre_save_isa(void *opaque)
-{
-    TPMStateISA *isadev = opaque;
-
-    return tpm_tis_pre_save(&isadev->state);
-}
-
-static const VMStateDescription vmstate_tpm_tis_isa = {
-    .name = "tpm-tis",
-    .version_id = 0,
-    .pre_save  = tpm_tis_pre_save_isa,
-    .fields = (VMStateField[]) {
-        VMSTATE_BUFFER(state.buffer, TPMStateISA),
-        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
-        VMSTATE_UINT8(state.active_locty, TPMStateISA),
-        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
-        VMSTATE_UINT8(state.next_locty, TPMStateISA),
-
-        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
-                             vmstate_locty, TPMLocality),
-
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
-{
-    TPMStateISA *isadev = TPM_TIS_ISA(ti);
-    TPMState *s = &isadev->state;
-
-    tpm_tis_request_completed(s, ret);
-}
-
-static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
-{
-    TPMStateISA *isadev = TPM_TIS_ISA(ti);
-    TPMState *s = &isadev->state;
-
-    return tpm_tis_get_tpm_version(s);
-}
-
-static void tpm_tis_isa_reset(DeviceState *dev)
-{
-    TPMStateISA *isadev = TPM_TIS_ISA(dev);
-    TPMState *s = &isadev->state;
-
-    return tpm_tis_reset(s);
-}
-
-static Property tpm_tis_isa_properties[] = {
-    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
-    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
-    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
-    DEFINE_PROP_END_OF_LIST(),
-};
-
-static void tpm_tis_isa_initfn(Object *obj)
-{
-    TPMStateISA *isadev = TPM_TIS_ISA(obj);
-    TPMState *s = &isadev->state;
-
-    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
-                          s, "tpm-tis-mmio",
-                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
-}
-
-static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
-{
-    TPMStateISA *isadev = TPM_TIS_ISA(dev);
-    TPMState *s = &isadev->state;
-
-    if (!tpm_find()) {
-        error_setg(errp, "at most one TPM device is permitted");
-        return;
-    }
-
-    if (!s->be_driver) {
-        error_setg(errp, "'tpmdev' property is required");
-        return;
-    }
-    if (s->irq_num > 15) {
-        error_setg(errp, "IRQ %d is outside valid range of 0 to 15",
-                   s->irq_num);
-        return;
-    }
-
-    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
-
-    memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
-                                TPM_TIS_ADDR_BASE, &s->mmio);
-
-    if (s->ppi_enabled) {
-        tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
-                     TPM_PPI_ADDR_BASE, OBJECT(dev));
-    }
-}
-
-static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(klass);
-    TPMIfClass *tc = TPM_IF_CLASS(klass);
-
-    device_class_set_props(dc, tpm_tis_isa_properties);
-    dc->vmsd  = &vmstate_tpm_tis_isa;
-    tc->model = TPM_MODEL_TPM_TIS;
-    dc->realize = tpm_tis_isa_realizefn;
-    dc->reset = tpm_tis_isa_reset;
-    tc->request_completed = tpm_tis_isa_request_completed;
-    tc->get_version = tpm_tis_isa_get_tpm_version;
-}
-
-static const TypeInfo tpm_tis_isa_info = {
-    .name = TYPE_TPM_TIS_ISA,
-    .parent = TYPE_ISA_DEVICE,
-    .instance_size = sizeof(TPMStateISA),
-    .instance_init = tpm_tis_isa_initfn,
-    .class_init  = tpm_tis_isa_class_init,
-    .interfaces = (InterfaceInfo[]) {
-        { TYPE_TPM_IF },
-        { }
-    }
-};
-
-static void tpm_tis_isa_register(void)
-{
-    type_register_static(&tpm_tis_isa_info);
-}
-
-type_init(tpm_tis_isa_register)
diff --git a/hw/tpm/tpm_tis_isa.c b/hw/tpm/tpm_tis_isa.c
new file mode 100644
index 0000000000..30ba37079d
--- /dev/null
+++ b/hw/tpm/tpm_tis_isa.c
@@ -0,0 +1,170 @@
+/*
+ * tpm_tis_isa.c - QEMU's TPM TIS ISA Device
+ *
+ * Copyright (C) 2006,2010-2013 IBM Corporation
+ *
+ * Authors:
+ *  Stefan Berger <stefanb@us.ibm.com>
+ *  David Safford <safford@us.ibm.com>
+ *
+ * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ * Implementation of the TIS interface according to specs found at
+ * http://www.trustedcomputinggroup.org. This implementation currently
+ * supports version 1.3, 21 March 2013
+ * In the developers menu choose the PC Client section then find the TIS
+ * specification.
+ *
+ * TPM TIS for TPM 2 implementation following TCG PC Client Platform
+ * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
+ */
+
+#include "qemu/osdep.h"
+#include "hw/isa/isa.h"
+#include "hw/qdev-properties.h"
+#include "migration/vmstate.h"
+#include "tpm_util.h"
+#include "tpm_tis.h"
+
+typedef struct TPMStateISA {
+    /*< private >*/
+    ISADevice parent_obj;
+
+    /*< public >*/
+    TPMState state; /* not a QOM object */
+} TPMStateISA;
+
+#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
+
+static int tpm_tis_pre_save_isa(void *opaque)
+{
+    TPMStateISA *isadev = opaque;
+
+    return tpm_tis_pre_save(&isadev->state);
+}
+
+static const VMStateDescription vmstate_tpm_tis_isa = {
+    .name = "tpm-tis",
+    .version_id = 0,
+    .pre_save  = tpm_tis_pre_save_isa,
+    .fields = (VMStateField[]) {
+        VMSTATE_BUFFER(state.buffer, TPMStateISA),
+        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
+        VMSTATE_UINT8(state.active_locty, TPMStateISA),
+        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
+        VMSTATE_UINT8(state.next_locty, TPMStateISA),
+
+        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
+                             vmstate_locty, TPMLocality),
+
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(ti);
+    TPMState *s = &isadev->state;
+
+    tpm_tis_request_completed(s, ret);
+}
+
+static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(ti);
+    TPMState *s = &isadev->state;
+
+    return tpm_tis_get_tpm_version(s);
+}
+
+static void tpm_tis_isa_reset(DeviceState *dev)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(dev);
+    TPMState *s = &isadev->state;
+
+    return tpm_tis_reset(s);
+}
+
+static Property tpm_tis_isa_properties[] = {
+    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
+    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
+    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void tpm_tis_isa_initfn(Object *obj)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(obj);
+    TPMState *s = &isadev->state;
+
+    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
+                          s, "tpm-tis-mmio",
+                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
+}
+
+static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
+{
+    TPMStateISA *isadev = TPM_TIS_ISA(dev);
+    TPMState *s = &isadev->state;
+
+    if (!tpm_find()) {
+        error_setg(errp, "at most one TPM device is permitted");
+        return;
+    }
+
+    if (!s->be_driver) {
+        error_setg(errp, "'tpmdev' property is required");
+        return;
+    }
+    if (s->irq_num > 15) {
+        error_setg(errp, "IRQ %d is outside valid range of 0 to 15",
+                   s->irq_num);
+        return;
+    }
+
+    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
+
+    memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
+                                TPM_TIS_ADDR_BASE, &s->mmio);
+
+    if (s->ppi_enabled) {
+        tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
+                     TPM_PPI_ADDR_BASE, OBJECT(dev));
+    }
+}
+
+static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    TPMIfClass *tc = TPM_IF_CLASS(klass);
+
+    device_class_set_props(dc, tpm_tis_isa_properties);
+    dc->vmsd  = &vmstate_tpm_tis_isa;
+    tc->model = TPM_MODEL_TPM_TIS;
+    dc->realize = tpm_tis_isa_realizefn;
+    dc->reset = tpm_tis_isa_reset;
+    tc->request_completed = tpm_tis_isa_request_completed;
+    tc->get_version = tpm_tis_isa_get_tpm_version;
+}
+
+static const TypeInfo tpm_tis_isa_info = {
+    .name = TYPE_TPM_TIS_ISA,
+    .parent = TYPE_ISA_DEVICE,
+    .instance_size = sizeof(TPMStateISA),
+    .instance_init = tpm_tis_isa_initfn,
+    .class_init  = tpm_tis_isa_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TPM_IF },
+        { }
+    }
+};
+
+static void tpm_tis_isa_register(void)
+{
+    type_register_static(&tpm_tis_isa_info);
+}
+
+type_init(tpm_tis_isa_register)
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC v2 4/6] tpm: Separate TPM_TIS and TPM_TIS_ISA configs
  2020-02-14 18:36 [RFC v2 0/6] vTPM for aarch64 Eric Auger
                   ` (2 preceding siblings ...)
  2020-02-14 18:37 ` [RFC v2 3/6] tpm: Separate tpm_tis common functions from isa code Eric Auger
@ 2020-02-14 18:37 ` Eric Auger
  2020-02-14 19:03   ` Philippe Mathieu-Daudé
  2020-02-14 18:37 ` [RFC v2 5/6] tpm: Add the SysBus TPM TIS device Eric Auger
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 30+ messages in thread
From: Eric Auger @ 2020-02-14 18:37 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

Let's separate the compilation of tpm_tis_common.c from
the compilation of tpm_tis_isa.c

The common part will be also compiled along with the
tpm_tis_sysbus device.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 default-configs/i386-softmmu.mak | 2 +-
 hw/i386/Kconfig                  | 2 +-
 hw/tpm/Kconfig                   | 7 ++++++-
 hw/tpm/Makefile.objs             | 3 ++-
 tests/qtest/Makefile.include     | 4 ++--
 5 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/default-configs/i386-softmmu.mak b/default-configs/i386-softmmu.mak
index 4cc64dafa2..84d1a2487c 100644
--- a/default-configs/i386-softmmu.mak
+++ b/default-configs/i386-softmmu.mak
@@ -20,7 +20,7 @@
 #CONFIG_SGA=n
 #CONFIG_TEST_DEVICES=n
 #CONFIG_TPM_CRB=n
-#CONFIG_TPM_TIS=n
+#CONFIG_TPM_TIS_ISA=n
 #CONFIG_VTD=n
 
 # Boards:
diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig
index cdc851598c..c93f32f657 100644
--- a/hw/i386/Kconfig
+++ b/hw/i386/Kconfig
@@ -20,7 +20,7 @@ config PC
     imply SGA
     imply TEST_DEVICES
     imply TPM_CRB
-    imply TPM_TIS
+    imply TPM_TIS_ISA
     imply VGA_PCI
     imply VIRTIO_VGA
     select FDC
diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
index 9e67d990e8..686f8206bb 100644
--- a/hw/tpm/Kconfig
+++ b/hw/tpm/Kconfig
@@ -2,9 +2,14 @@ config TPMDEV
     bool
     depends on TPM
 
-config TPM_TIS
+config TPM_TIS_ISA
     bool
     depends on TPM && ISA_BUS
+    select TPM_TIS
+
+config TPM_TIS
+    bool
+    depends on TPM
     select TPMDEV
 
 config TPM_CRB
diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
index fcc4c2f27c..3ef2036cca 100644
--- a/hw/tpm/Makefile.objs
+++ b/hw/tpm/Makefile.objs
@@ -1,6 +1,7 @@
 common-obj-$(CONFIG_TPM) += tpm_util.o
 obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
-common-obj-$(CONFIG_TPM_TIS) += tpm_tis_isa.o tpm_tis_common.o
+common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
+common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
 common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
 common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
 common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o
diff --git a/tests/qtest/Makefile.include b/tests/qtest/Makefile.include
index eb0f23b108..33dd3c89cc 100644
--- a/tests/qtest/Makefile.include
+++ b/tests/qtest/Makefile.include
@@ -54,8 +54,8 @@ check-qtest-i386-y += q35-test
 check-qtest-i386-y += vmgenid-test
 check-qtest-i386-$(CONFIG_TPM_CRB) += tpm-crb-swtpm-test
 check-qtest-i386-$(CONFIG_TPM_CRB) += tpm-crb-test
-check-qtest-i386-$(CONFIG_TPM_TIS) += tpm-tis-swtpm-test
-check-qtest-i386-$(CONFIG_TPM_TIS) += tpm-tis-test
+check-qtest-i386-$(CONFIG_TPM_TIS_ISA) += tpm-tis-swtpm-test
+check-qtest-i386-$(CONFIG_TPM_TIS_ISA) += tpm-tis-test
 check-qtest-i386-$(CONFIG_SLIRP) += test-netfilter
 check-qtest-i386-$(CONFIG_POSIX) += test-filter-mirror
 check-qtest-i386-$(CONFIG_RTL8139_PCI) += test-filter-redirector
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC v2 5/6] tpm: Add the SysBus TPM TIS device
  2020-02-14 18:36 [RFC v2 0/6] vTPM for aarch64 Eric Auger
                   ` (3 preceding siblings ...)
  2020-02-14 18:37 ` [RFC v2 4/6] tpm: Separate TPM_TIS and TPM_TIS_ISA configs Eric Auger
@ 2020-02-14 18:37 ` Eric Auger
  2020-02-16 18:32   ` Stefan Berger
  2020-02-14 18:37 ` [RFC v2 6/6] hw/arm/virt: vTPM support Eric Auger
  2020-02-16 16:35 ` [RFC v2 0/6] vTPM for aarch64 Ard Biesheuvel
  6 siblings, 1 reply; 30+ messages in thread
From: Eric Auger @ 2020-02-14 18:37 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

Introduce the tpm-tis-device which is a sysbus device
and is bound to be used on ARM.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 hw/tpm/Kconfig          |   5 ++
 hw/tpm/Makefile.objs    |   1 +
 hw/tpm/tpm_tis_sysbus.c | 159 ++++++++++++++++++++++++++++++++++++++++
 include/sysemu/tpm.h    |   1 +
 4 files changed, 166 insertions(+)
 create mode 100644 hw/tpm/tpm_tis_sysbus.c

diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
index 686f8206bb..4794e7fe28 100644
--- a/hw/tpm/Kconfig
+++ b/hw/tpm/Kconfig
@@ -7,6 +7,11 @@ config TPM_TIS_ISA
     depends on TPM && ISA_BUS
     select TPM_TIS
 
+config TPM_TIS_SYSBUS
+    bool
+    depends on TPM
+    select TPM_TIS
+
 config TPM_TIS
     bool
     depends on TPM
diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
index 3ef2036cca..f1ec4beb95 100644
--- a/hw/tpm/Makefile.objs
+++ b/hw/tpm/Makefile.objs
@@ -1,6 +1,7 @@
 common-obj-$(CONFIG_TPM) += tpm_util.o
 obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
 common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
+common-obj-$(CONFIG_TPM_TIS_SYSBUS) += tpm_tis_sysbus.o
 common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
 common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
 common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
diff --git a/hw/tpm/tpm_tis_sysbus.c b/hw/tpm/tpm_tis_sysbus.c
new file mode 100644
index 0000000000..18c02aed67
--- /dev/null
+++ b/hw/tpm/tpm_tis_sysbus.c
@@ -0,0 +1,159 @@
+/*
+ * tpm_tis_sysbus.c - QEMU's TPM TIS SYSBUS Device
+ *
+ * Copyright (C) 2006,2010-2013 IBM Corporation
+ *
+ * Authors:
+ *  Stefan Berger <stefanb@us.ibm.com>
+ *  David Safford <safford@us.ibm.com>
+ *
+ * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ * Implementation of the TIS interface according to specs found at
+ * http://www.trustedcomputinggroup.org. This implementation currently
+ * supports version 1.3, 21 March 2013
+ * In the developers menu choose the PC Client section then find the TIS
+ * specification.
+ *
+ * TPM TIS for TPM 2 implementation following TCG PC Client Platform
+ * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
+ */
+
+#include "qemu/osdep.h"
+#include "hw/qdev-properties.h"
+#include "migration/vmstate.h"
+#include "tpm_util.h"
+#include "hw/sysbus.h"
+#include "tpm_tis.h"
+
+typedef struct TPMStateSysBus {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    TPMState state; /* not a QOM object */
+} TPMStateSysBus;
+
+#define TPM_TIS_SYSBUS(obj) OBJECT_CHECK(TPMStateSysBus, (obj), TYPE_TPM_TIS_SYSBUS)
+
+static int tpm_tis_pre_save_sysbus(void *opaque)
+{
+    TPMStateSysBus *sbdev = opaque;
+
+    return tpm_tis_pre_save(&sbdev->state);
+}
+
+static const VMStateDescription vmstate_tpm_tis_sysbus = {
+    .name = "tpm-tis",
+    .version_id = 0,
+    .pre_save  = tpm_tis_pre_save_sysbus,
+    .fields = (VMStateField[]) {
+        VMSTATE_BUFFER(state.buffer, TPMStateSysBus),
+        VMSTATE_UINT16(state.rw_offset, TPMStateSysBus),
+        VMSTATE_UINT8(state.active_locty, TPMStateSysBus),
+        VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus),
+        VMSTATE_UINT8(state.next_locty, TPMStateSysBus),
+
+        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus, TPM_TIS_NUM_LOCALITIES,
+                             0, vmstate_locty, TPMLocality),
+
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret)
+{
+    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
+    TPMState *s = &sbdev->state;
+
+    tpm_tis_request_completed(s, ret);
+}
+
+static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti)
+{
+    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
+    TPMState *s = &sbdev->state;
+
+    return tpm_tis_get_tpm_version(s);
+}
+
+static void tpm_tis_sysbus_reset(DeviceState *dev)
+{
+    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
+    TPMState *s = &sbdev->state;
+
+    return tpm_tis_reset(s);
+}
+
+static Property tpm_tis_sysbus_properties[] = {
+    DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num, TPM_TIS_IRQ),
+    DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
+    DEFINE_PROP_BOOL("ppi", TPMStateSysBus, state.ppi_enabled, true),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void tpm_tis_sysbus_initfn(Object *obj)
+{
+    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
+    TPMState *s = &sbdev->state;
+
+    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
+                          s, "tpm-tis-mmio",
+                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
+
+    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
+    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
+}
+
+static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
+{
+    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
+    TPMState *s = &sbdev->state;
+
+    if (!tpm_find()) {
+        error_setg(errp, "at most one TPM device is permitted");
+        return;
+    }
+
+    if (!s->be_driver) {
+        error_setg(errp, "'tpmdev' property is required");
+        return;
+    }
+}
+
+static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    TPMIfClass *tc = TPM_IF_CLASS(klass);
+
+    device_class_set_props(dc, tpm_tis_sysbus_properties);
+    dc->vmsd  = &vmstate_tpm_tis_sysbus;
+    tc->model = TPM_MODEL_TPM_TIS;
+    dc->realize = tpm_tis_sysbus_realizefn;
+    dc->user_creatable = true;
+    dc->reset = tpm_tis_sysbus_reset;
+    tc->request_completed = tpm_tis_sysbus_request_completed;
+    tc->get_version = tpm_tis_sysbus_get_tpm_version;
+}
+
+static const TypeInfo tpm_tis_sysbus_info = {
+    .name = TYPE_TPM_TIS_SYSBUS,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(TPMStateSysBus),
+    .instance_init = tpm_tis_sysbus_initfn,
+    .class_init  = tpm_tis_sysbus_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TPM_IF },
+        { }
+    }
+};
+
+static void tpm_tis_sysbus_register(void)
+{
+    type_register_static(&tpm_tis_sysbus_info);
+}
+
+type_init(tpm_tis_sysbus_register)
diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
index 1691b92c28..f37851b1aa 100644
--- a/include/sysemu/tpm.h
+++ b/include/sysemu/tpm.h
@@ -44,6 +44,7 @@ typedef struct TPMIfClass {
 } TPMIfClass;
 
 #define TYPE_TPM_TIS_ISA            "tpm-tis"
+#define TYPE_TPM_TIS_SYSBUS         "tpm-tis-device"
 #define TYPE_TPM_CRB                "tpm-crb"
 #define TYPE_TPM_SPAPR              "tpm-spapr"
 
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC v2 6/6] hw/arm/virt: vTPM support
  2020-02-14 18:36 [RFC v2 0/6] vTPM for aarch64 Eric Auger
                   ` (4 preceding siblings ...)
  2020-02-14 18:37 ` [RFC v2 5/6] tpm: Add the SysBus TPM TIS device Eric Auger
@ 2020-02-14 18:37 ` Eric Auger
  2020-02-16 18:47   ` Stefan Berger
  2020-02-16 16:35 ` [RFC v2 0/6] vTPM for aarch64 Ard Biesheuvel
  6 siblings, 1 reply; 30+ messages in thread
From: Eric Auger @ 2020-02-14 18:37 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

Let the TPM TIS SYSBUS device be dynamically instantiable
in ARM virt.  A device tree node is dynamically created
(TPM via MMIO).

The TPM Physical Presence interface (PPI) is not supported.

To run with the swtmp TPM emulator, the qemu command line must
be augmented with:

        -chardev socket,id=chrtpm,path=swtpm-sock \
        -tpmdev emulator,id=tpm0,chardev=chrtpm \
        -device tpm-tis-device,tpmdev=tpm0 \

swtpm/libtpms command line example:

swtpm socket --tpm2 -t -d --tpmstate dir=/tmp/tpm \
--ctrl type=unixio,path=swtpm-sock

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 hw/arm/Kconfig      |  1 +
 hw/arm/sysbus-fdt.c | 36 ++++++++++++++++++++++++++++++++++++
 hw/arm/virt.c       |  7 +++++++
 3 files changed, 44 insertions(+)

diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
index 3d86691ae0..b6f03f7f53 100644
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -5,6 +5,7 @@ config ARM_VIRT
     imply VFIO_AMD_XGBE
     imply VFIO_PLATFORM
     imply VFIO_XGMAC
+    imply TPM_TIS_SYSBUS
     select A15MPCORE
     select ACPI
     select ARM_SMMUV3
diff --git a/hw/arm/sysbus-fdt.c b/hw/arm/sysbus-fdt.c
index 022fc97ecd..adf50444c2 100644
--- a/hw/arm/sysbus-fdt.c
+++ b/hw/arm/sysbus-fdt.c
@@ -30,6 +30,7 @@
 #include "hw/arm/sysbus-fdt.h"
 #include "qemu/error-report.h"
 #include "sysemu/device_tree.h"
+#include "sysemu/tpm.h"
 #include "hw/platform-bus.h"
 #include "hw/vfio/vfio-platform.h"
 #include "hw/vfio/vfio-calxeda-xgmac.h"
@@ -434,6 +435,40 @@ static bool vfio_platform_match(SysBusDevice *sbdev,
 #define VFIO_PLATFORM_BINDING(compat, add_fn) \
     {TYPE_VFIO_PLATFORM, (compat), (add_fn), vfio_platform_match}
 
+/*
+ * add_tpm_tis_fdt_node: Create a DT node for TPM TIS
+ *
+ * See kernel documentation:
+ * Documentation/devicetree/bindings/security/tpm/tpm_tis_mmio.txt
+ * Optional interrupt for command completion is not exposed
+ */
+static int add_tpm_tis_fdt_node(SysBusDevice *sbdev, void *opaque)
+{
+    PlatformBusFDTData *data = opaque;
+    PlatformBusDevice *pbus = data->pbus;
+    void *fdt = data->fdt;
+    const char *parent_node = data->pbus_node_name;
+    int compat_str_len;
+    char *nodename;
+    uint32_t reg_attr[2];
+    uint64_t mmio_base;
+
+    mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
+    nodename = g_strdup_printf("%s/tpm_tis@%" PRIx64, parent_node, mmio_base);
+    qemu_fdt_add_subnode(fdt, nodename);
+
+    compat_str_len = strlen("tcg,tpm-tis-mmio") + 1;
+    qemu_fdt_setprop(fdt, nodename, "compatible", "tcg,tpm-tis-mmio",
+                     compat_str_len);
+
+    reg_attr[0] = cpu_to_be32(mmio_base);
+    reg_attr[1] = cpu_to_be32(0x5000);
+    qemu_fdt_setprop(fdt, nodename, "reg", reg_attr, 2 * sizeof(uint32_t));
+
+    g_free(nodename);
+    return 0;
+}
+
 #endif /* CONFIG_LINUX */
 
 static int no_fdt_node(SysBusDevice *sbdev, void *opaque)
@@ -455,6 +490,7 @@ static const BindingEntry bindings[] = {
     TYPE_BINDING(TYPE_VFIO_CALXEDA_XGMAC, add_calxeda_midway_xgmac_fdt_node),
     TYPE_BINDING(TYPE_VFIO_AMD_XGBE, add_amd_xgbe_fdt_node),
     VFIO_PLATFORM_BINDING("amd,xgbe-seattle-v1a", add_amd_xgbe_fdt_node),
+    TYPE_BINDING(TYPE_TPM_TIS_SYSBUS, add_tpm_tis_fdt_node),
 #endif
     TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node),
     TYPE_BINDING("", NULL), /* last element */
diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index f788fe27d6..4b967e39d1 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -47,6 +47,7 @@
 #include "sysemu/numa.h"
 #include "sysemu/runstate.h"
 #include "sysemu/sysemu.h"
+#include "sysemu/tpm.h"
 #include "sysemu/kvm.h"
 #include "hw/loader.h"
 #include "exec/address-spaces.h"
@@ -2041,6 +2042,7 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
+    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
     mc->block_default_type = IF_VIRTIO;
     mc->no_cdrom = 1;
     mc->pci_allow_0_address = true;
@@ -2153,6 +2155,11 @@ type_init(machvirt_machine_init);
 
 static void virt_machine_5_0_options(MachineClass *mc)
 {
+    static GlobalProperty compat[] = {
+        { TYPE_TPM_TIS_SYSBUS, "ppi", "false" },
+    };
+
+    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
 }
 DEFINE_VIRT_MACHINE_AS_LATEST(5, 0)
 
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA
  2020-02-14 18:36 ` [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA Eric Auger
@ 2020-02-14 18:55   ` Philippe Mathieu-Daudé
  2020-02-25 10:16     ` Auger Eric
  2020-02-16 18:14   ` Stefan Berger
  1 sibling, 1 reply; 30+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-02-14 18:55 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

On 2/14/20 7:36 PM, Eric Auger wrote:
> As we plan to introduce a sysbus TPM_TIS, let's rename
> TPM_TIS into TPM_TIS_ISA.
> 
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> ---
>   hw/i386/acpi-build.c | 6 +++---
>   hw/tpm/tpm_tis.c     | 4 ++--
>   include/sysemu/tpm.h | 6 +++---
>   3 files changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
> index 9c4e46fa74..26777f8828 100644
> --- a/hw/i386/acpi-build.c
> +++ b/hw/i386/acpi-build.c
> @@ -2026,7 +2026,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
>           }
>       }
>   
> -    if (TPM_IS_TIS(tpm_find())) {
> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>           aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
>                      TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
>       }
> @@ -2197,7 +2197,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
>               /* Scan all PCI buses. Generate tables to support hotplug. */
>               build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
>   
> -            if (TPM_IS_TIS(tpm)) {
> +            if (TPM_IS_TIS_ISA(tpm)) {
>                   if (misc->tpm_version == TPM_VERSION_2_0) {
>                       dev = aml_device("TPM");
>                       aml_append(dev, aml_name_decl("_HID",
> @@ -2304,7 +2304,7 @@ build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog)
>           (char *)&tpm2_ptr->log_area_start_address - table_data->data;
>   
>       tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
> -    if (TPM_IS_TIS(tpm_find())) {
> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>           tpm2_ptr->control_area_address = cpu_to_le64(0);
>           tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
>       } else if (TPM_IS_CRB(tpm_find())) {
> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
> index 31facb896d..c609737272 100644
> --- a/hw/tpm/tpm_tis.c
> +++ b/hw/tpm/tpm_tis.c
> @@ -91,7 +91,7 @@ typedef struct TPMState {
>       TPMPPI ppi;
>   } TPMState;
>   
> -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
> +#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
>   
>   #define DEBUG_TIS 0
>   
> @@ -1008,7 +1008,7 @@ static void tpm_tis_class_init(ObjectClass *klass, void *data)
>   }
>   
>   static const TypeInfo tpm_tis_info = {
> -    .name = TYPE_TPM_TIS,
> +    .name = TYPE_TPM_TIS_ISA,
>       .parent = TYPE_ISA_DEVICE,
>       .instance_size = sizeof(TPMState),
>       .instance_init = tpm_tis_initfn,
> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
> index 15979a3647..1691b92c28 100644
> --- a/include/sysemu/tpm.h
> +++ b/include/sysemu/tpm.h
> @@ -43,12 +43,12 @@ typedef struct TPMIfClass {
>       enum TPMVersion (*get_version)(TPMIf *obj);
>   } TPMIfClass;
>   
> -#define TYPE_TPM_TIS                "tpm-tis"
> +#define TYPE_TPM_TIS_ISA            "tpm-tis"

It should be safe to rename this "tpm-tis-isa" in this patch.

Regardless:
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

>   #define TYPE_TPM_CRB                "tpm-crb"
>   #define TYPE_TPM_SPAPR              "tpm-spapr"
>   
> -#define TPM_IS_TIS(chr)                             \
> -    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS)
> +#define TPM_IS_TIS_ISA(chr)                         \
> +    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
>   #define TPM_IS_CRB(chr)                             \
>       object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
>   #define TPM_IS_SPAPR(chr)                           \
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 2/6] tpm: Use TPMState as a common struct
  2020-02-14 18:37 ` [RFC v2 2/6] tpm: Use TPMState as a common struct Eric Auger
@ 2020-02-14 19:01   ` Philippe Mathieu-Daudé
  2020-02-16 18:22   ` Stefan Berger
  2020-02-17  9:21   ` Philippe Mathieu-Daudé
  2 siblings, 0 replies; 30+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-02-14 19:01 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

On 2/14/20 7:37 PM, Eric Auger wrote:
> As we plan to introdce a SysBus TPM TIS device, let's

typo "introduce"

> make the TPMState a common struct usable by both the
> ISADevice and the SysBusDevice. TPMStateISA embeds the
> struct and inherits from the ISADevice.
> 
> The prototype of functions bound to be used by both
> the ISA and SysBus devices is changed to take TPMState
> handle.
> 
> A bunch of structs also are renamed to be specialized
> for the ISA device. Besides those transformations, no
> functional change is expected.
> 
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> ---
>   hw/tpm/tpm_tis.c | 146 +++++++++++++++++++++++++++++------------------
>   1 file changed, 91 insertions(+), 55 deletions(-)
> 
> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
> index c609737272..fc6d7ca579 100644
> --- a/hw/tpm/tpm_tis.c
> +++ b/hw/tpm/tpm_tis.c
> @@ -65,7 +65,6 @@ typedef struct TPMLocality {
>   } TPMLocality;
>   
>   typedef struct TPMState {
> -    ISADevice busdev;
>       MemoryRegion mmio;
>   
>       unsigned char buffer[TPM_TIS_BUFFER_MAX];
> @@ -91,7 +90,15 @@ typedef struct TPMState {
>       TPMPPI ppi;
>   } TPMState;
>   
> -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
> +typedef struct TPMStateISA {
> +    /*< private >*/
> +    ISADevice parent_obj;
> +
> +    /*< public >*/
> +    TPMState state; /* not a QOM object */

Maybe rename 'state' -> 'tpm'?

Anyway:
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> +} TPMStateISA;
> +
> +#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
>   
>   #define DEBUG_TIS 0
>   
> @@ -281,9 +288,8 @@ static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty)
>   /*
>    * Callback from the TPM to indicate that the response was received.
>    */
> -static void tpm_tis_request_completed(TPMIf *ti, int ret)
> +static void tpm_tis_request_completed(TPMState *s, int ret)
>   {
> -    TPMState *s = TPM(ti);
>       uint8_t locty = s->cmd.locty;
>       uint8_t l;
>   
> @@ -338,7 +344,7 @@ static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty)
>   }
>   
>   #ifdef DEBUG_TIS
> -static void tpm_tis_dump_state(void *opaque, hwaddr addr)
> +static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
>   {
>       static const unsigned regs[] = {
>           TPM_TIS_REG_ACCESS,
> @@ -353,7 +359,6 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr)
>       int idx;
>       uint8_t locty = tpm_tis_locality_from_addr(addr);
>       hwaddr base = addr & ~0xfff;
> -    TPMState *s = opaque;
>   
>       printf("tpm_tis: active locality      : %d\n"
>              "tpm_tis: state of locality %d : %d\n"
> @@ -363,7 +368,7 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr)
>   
>       for (idx = 0; regs[idx] != 0xfff; idx++) {
>           printf("tpm_tis: 0x%04x : 0x%08x\n", regs[idx],
> -               (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4));
> +               (int)tpm_tis_mmio_read(s, base + regs[idx], 4));
>       }
>   
>       printf("tpm_tis: r/w offset    : %d\n"
> @@ -488,7 +493,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
>           break;
>   #ifdef DEBUG_TIS
>       case TPM_TIS_REG_DEBUG:
> -        tpm_tis_dump_state(opaque, addr);
> +        tpm_tis_dump_state(s, addr);
>           break;
>   #endif
>       }
> @@ -835,10 +840,8 @@ static const MemoryRegionOps tpm_tis_memory_ops = {
>   /*
>    * Get the TPMVersion of the backend device being used
>    */
> -static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
> +static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
>   {
> -    TPMState *s = TPM(ti);
> -
>       if (tpm_backend_had_startup_error(s->be_driver)) {
>           return TPM_VERSION_UNSPEC;
>       }
> @@ -850,9 +853,8 @@ static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
>    * This function is called when the machine starts, resets or due to
>    * S3 resume.
>    */
> -static void tpm_tis_reset(DeviceState *dev)
> +static void tpm_tis_reset(TPMState *s)
>   {
> -    TPMState *s = TPM(dev);
>       int c;
>   
>       s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver);
> @@ -896,15 +898,14 @@ static void tpm_tis_reset(DeviceState *dev)
>   
>   /* persistent state handling */
>   
> -static int tpm_tis_pre_save(void *opaque)
> +static int tpm_tis_pre_save(TPMState *s)
>   {
> -    TPMState *s = opaque;
>       uint8_t locty = s->active_locty;
>   
>       trace_tpm_tis_pre_save(locty, s->rw_offset);
>   
>       if (DEBUG_TIS) {
> -        tpm_tis_dump_state(opaque, 0);
> +        tpm_tis_dump_state(s, 0);
>       }
>   
>       /*
> @@ -929,34 +930,78 @@ static const VMStateDescription vmstate_locty = {
>       }
>   };
>   
> -static const VMStateDescription vmstate_tpm_tis = {
> +/* ISA */
> +
> +static int tpm_tis_pre_save_isa(void *opaque)
> +{
> +    TPMStateISA *isadev = opaque;
> +
> +    return tpm_tis_pre_save(&isadev->state);
> +}
> +
> +static const VMStateDescription vmstate_tpm_tis_isa = {
>       .name = "tpm-tis",
>       .version_id = 0,
> -    .pre_save  = tpm_tis_pre_save,
> +    .pre_save  = tpm_tis_pre_save_isa,
>       .fields = (VMStateField[]) {
> -        VMSTATE_BUFFER(buffer, TPMState),
> -        VMSTATE_UINT16(rw_offset, TPMState),
> -        VMSTATE_UINT8(active_locty, TPMState),
> -        VMSTATE_UINT8(aborting_locty, TPMState),
> -        VMSTATE_UINT8(next_locty, TPMState),
> +        VMSTATE_BUFFER(state.buffer, TPMStateISA),
> +        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
> +        VMSTATE_UINT8(state.active_locty, TPMStateISA),
> +        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
> +        VMSTATE_UINT8(state.next_locty, TPMStateISA),
>   
> -        VMSTATE_STRUCT_ARRAY(loc, TPMState, TPM_TIS_NUM_LOCALITIES, 0,
> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
>                                vmstate_locty, TPMLocality),
>   
>           VMSTATE_END_OF_LIST()
>       }
>   };
>   
> -static Property tpm_tis_properties[] = {
> -    DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ),
> -    DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver),
> -    DEFINE_PROP_BOOL("ppi", TPMState, ppi_enabled, true),
> +static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> +    TPMState *s = &isadev->state;
> +
> +    tpm_tis_request_completed(s, ret);
> +}
> +
> +static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> +    TPMState *s = &isadev->state;
> +
> +    return tpm_tis_get_tpm_version(s);
> +}
> +
> +static void tpm_tis_isa_reset(DeviceState *dev)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> +    TPMState *s = &isadev->state;
> +
> +    return tpm_tis_reset(s);
> +}
> +
> +static Property tpm_tis_isa_properties[] = {
> +    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
> +    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
>       DEFINE_PROP_END_OF_LIST(),
>   };
>   
> -static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
> +static void tpm_tis_isa_initfn(Object *obj)
>   {
> -    TPMState *s = TPM(dev);
> +    TPMStateISA *isadev = TPM_TIS_ISA(obj);
> +    TPMState *s = &isadev->state;
> +
> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
> +                          s, "tpm-tis-mmio",
> +                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> +}
> +
> +static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> +    TPMState *s = &isadev->state;
>   
>       if (!tpm_find()) {
>           error_setg(errp, "at most one TPM device is permitted");
> @@ -973,55 +1018,46 @@ static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
>           return;
>       }
>   
> -    isa_init_irq(&s->busdev, &s->irq, s->irq_num);
> +    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
>   
>       memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
>                                   TPM_TIS_ADDR_BASE, &s->mmio);
>   
>       if (s->ppi_enabled) {
>           tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
> -                     TPM_PPI_ADDR_BASE, OBJECT(s));
> +                     TPM_PPI_ADDR_BASE, OBJECT(dev));
>       }
>   }
>   
> -static void tpm_tis_initfn(Object *obj)
> -{
> -    TPMState *s = TPM(obj);
> -
> -    memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
> -                          s, "tpm-tis-mmio",
> -                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> -}
> -
> -static void tpm_tis_class_init(ObjectClass *klass, void *data)
> +static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
>   {
>       DeviceClass *dc = DEVICE_CLASS(klass);
>       TPMIfClass *tc = TPM_IF_CLASS(klass);
>   
> -    dc->realize = tpm_tis_realizefn;
> -    device_class_set_props(dc, tpm_tis_properties);
> -    dc->reset = tpm_tis_reset;
> -    dc->vmsd  = &vmstate_tpm_tis;
> +    device_class_set_props(dc, tpm_tis_isa_properties);
> +    dc->vmsd  = &vmstate_tpm_tis_isa;
>       tc->model = TPM_MODEL_TPM_TIS;
> -    tc->get_version = tpm_tis_get_tpm_version;
> -    tc->request_completed = tpm_tis_request_completed;
> +    dc->realize = tpm_tis_isa_realizefn;
> +    dc->reset = tpm_tis_isa_reset;
> +    tc->request_completed = tpm_tis_isa_request_completed;
> +    tc->get_version = tpm_tis_isa_get_tpm_version;
>   }
>   
> -static const TypeInfo tpm_tis_info = {
> +static const TypeInfo tpm_tis_isa_info = {
>       .name = TYPE_TPM_TIS_ISA,
>       .parent = TYPE_ISA_DEVICE,
> -    .instance_size = sizeof(TPMState),
> -    .instance_init = tpm_tis_initfn,
> -    .class_init  = tpm_tis_class_init,
> +    .instance_size = sizeof(TPMStateISA),
> +    .instance_init = tpm_tis_isa_initfn,
> +    .class_init  = tpm_tis_isa_class_init,
>       .interfaces = (InterfaceInfo[]) {
>           { TYPE_TPM_IF },
>           { }
>       }
>   };
>   
> -static void tpm_tis_register(void)
> +static void tpm_tis_isa_register(void)
>   {
> -    type_register_static(&tpm_tis_info);
> +    type_register_static(&tpm_tis_isa_info);
>   }
>   
> -type_init(tpm_tis_register)
> +type_init(tpm_tis_isa_register)
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 4/6] tpm: Separate TPM_TIS and TPM_TIS_ISA configs
  2020-02-14 18:37 ` [RFC v2 4/6] tpm: Separate TPM_TIS and TPM_TIS_ISA configs Eric Auger
@ 2020-02-14 19:03   ` Philippe Mathieu-Daudé
  2020-02-25 10:05     ` Auger Eric
  0 siblings, 1 reply; 30+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-02-14 19:03 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

On 2/14/20 7:37 PM, Eric Auger wrote:
> Let's separate the compilation of tpm_tis_common.c from
> the compilation of tpm_tis_isa.c
> 
> The common part will be also compiled along with the
> tpm_tis_sysbus device.
> 
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> ---
>   default-configs/i386-softmmu.mak | 2 +-
>   hw/i386/Kconfig                  | 2 +-
>   hw/tpm/Kconfig                   | 7 ++++++-
>   hw/tpm/Makefile.objs             | 3 ++-
>   tests/qtest/Makefile.include     | 4 ++--
>   5 files changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/default-configs/i386-softmmu.mak b/default-configs/i386-softmmu.mak
> index 4cc64dafa2..84d1a2487c 100644
> --- a/default-configs/i386-softmmu.mak
> +++ b/default-configs/i386-softmmu.mak
> @@ -20,7 +20,7 @@
>   #CONFIG_SGA=n
>   #CONFIG_TEST_DEVICES=n
>   #CONFIG_TPM_CRB=n
> -#CONFIG_TPM_TIS=n
> +#CONFIG_TPM_TIS_ISA=n
>   #CONFIG_VTD=n
>   
>   # Boards:
> diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig
> index cdc851598c..c93f32f657 100644
> --- a/hw/i386/Kconfig
> +++ b/hw/i386/Kconfig
> @@ -20,7 +20,7 @@ config PC
>       imply SGA
>       imply TEST_DEVICES
>       imply TPM_CRB
> -    imply TPM_TIS
> +    imply TPM_TIS_ISA
>       imply VGA_PCI
>       imply VIRTIO_VGA
>       select FDC
> diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
> index 9e67d990e8..686f8206bb 100644
> --- a/hw/tpm/Kconfig
> +++ b/hw/tpm/Kconfig
> @@ -2,9 +2,14 @@ config TPMDEV
>       bool
>       depends on TPM
>   
> -config TPM_TIS
> +config TPM_TIS_ISA
>       bool
>       depends on TPM && ISA_BUS

Maybe you can relax to "depends on ISA_BUS" ...

> +    select TPM_TIS
> +
> +config TPM_TIS
> +    bool
> +    depends on TPM

... since TPM_TIS depends on TPM.

>       select TPMDEV
>   
>   config TPM_CRB
> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
> index fcc4c2f27c..3ef2036cca 100644
> --- a/hw/tpm/Makefile.objs
> +++ b/hw/tpm/Makefile.objs
> @@ -1,6 +1,7 @@
>   common-obj-$(CONFIG_TPM) += tpm_util.o
>   obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
> -common-obj-$(CONFIG_TPM_TIS) += tpm_tis_isa.o tpm_tis_common.o
> +common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
> +common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
>   common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
>   common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
>   common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o
> diff --git a/tests/qtest/Makefile.include b/tests/qtest/Makefile.include
> index eb0f23b108..33dd3c89cc 100644
> --- a/tests/qtest/Makefile.include
> +++ b/tests/qtest/Makefile.include
> @@ -54,8 +54,8 @@ check-qtest-i386-y += q35-test
>   check-qtest-i386-y += vmgenid-test
>   check-qtest-i386-$(CONFIG_TPM_CRB) += tpm-crb-swtpm-test
>   check-qtest-i386-$(CONFIG_TPM_CRB) += tpm-crb-test
> -check-qtest-i386-$(CONFIG_TPM_TIS) += tpm-tis-swtpm-test
> -check-qtest-i386-$(CONFIG_TPM_TIS) += tpm-tis-test
> +check-qtest-i386-$(CONFIG_TPM_TIS_ISA) += tpm-tis-swtpm-test
> +check-qtest-i386-$(CONFIG_TPM_TIS_ISA) += tpm-tis-test
>   check-qtest-i386-$(CONFIG_SLIRP) += test-netfilter
>   check-qtest-i386-$(CONFIG_POSIX) += test-filter-mirror
>   check-qtest-i386-$(CONFIG_RTL8139_PCI) += test-filter-redirector
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 0/6] vTPM for aarch64
  2020-02-14 18:36 [RFC v2 0/6] vTPM for aarch64 Eric Auger
                   ` (5 preceding siblings ...)
  2020-02-14 18:37 ` [RFC v2 6/6] hw/arm/virt: vTPM support Eric Auger
@ 2020-02-16 16:35 ` Ard Biesheuvel
  2020-02-17 18:03   ` Auger Eric
  6 siblings, 1 reply; 30+ messages in thread
From: Ard Biesheuvel @ 2020-02-16 16:35 UTC (permalink / raw)
  To: Eric Auger
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	QEMU Developers, qemu-arm, Marc-André Lureau, Eric Auger,
	Laszlo Ersek, Stefan Berger

On Fri, 14 Feb 2020 at 19:37, Eric Auger <eric.auger@redhat.com> wrote:
>
> This series adds the capability to instantiate an MMIO TPM TIS
> in ARM virt.
>
> The existing TPM TIS code is reshuffled into a generic part,
> the ISA device and the sysbus device. The last patch allows
> the instantiation of the TPM TIS sysbus device in ARM virt.
>
> The series was tested with the swtpm/libtpms emulator.
> Automatic guest LUKS volume unlocking (tpm2) was successful.
> EDK2 support is under development [3]. Thanks to Ard
> for supporting me when setting up the test environment.
>
> Best Regards
>
> Eric
>
> Testing:
>
> mkdir /tmp/tpm
> swtpm socket \
> --tpm2 \
> -t -d \
> --tpmstate dir=/tmp/tpm \
> --ctrl type=unixio,path=/tmp/swtpm-sock
>
> qemu command line must be augmented with the following options:
>
> -chardev socket,id=chrtpm,path=/tmp/swtpm-sock \
> -tpmdev emulator,id=tpm0,chardev=chrtpm \
> -device tpm-tis-device,tpmdev=tpm0 \
>
> References:
> [1] libtpms: https://github.com/stefanberger/libtpms/wiki
> [2] swtpm: https://github.com/stefanberger/swtpm/wiki
> [3] [PATCH 0/4] ArmVirtPkg: implement measured boot for ArmVirtQemu
>
> This series can be found at:
> https://github.com/eauger/qemu/tree/v4.2.0-tpm-rfc-v2
>
> History:
>
> RFC v1 -> RFC v2:
> - restructure the existing code with common, ISA and sysbus part.
> - both ARM and x86 integration were tested.
> - acknowledgement: migration has not been tested
>
> Eric Auger (6):
>   tpm: rename TPM_TIS into TPM_TIS_ISA
>   tpm: Use TPMState as a common struct
>   tpm: Separate tpm_tis common functions from isa code
>   tpm: Separate TPM_TIS and TPM_TIS_ISA configs
>   tpm: Add the SysBus TPM TIS device
>   hw/arm/virt: vTPM support
>

Many thanks to Eric for his work on this.

I have tested this with my EDK2 code and a Linux/arm64 VM booting in
DT mode with the tpm-tis driver enabled, and it works as expected. I
can observe the firmware taking measurements during boot, and can dump
the PCRs from Linux using tpm2_pcrlist.

Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

Note that the UEFI firmware is itself a consumer of the DT
description, so we need the DT related changes regardless of whether
the VM boots in DT or ACPI mode.



>  default-configs/i386-softmmu.mak       |   2 +-
>  hw/arm/Kconfig                         |   1 +
>  hw/arm/sysbus-fdt.c                    |  36 +++++
>  hw/arm/virt.c                          |   7 +
>  hw/i386/Kconfig                        |   2 +-
>  hw/i386/acpi-build.c                   |   6 +-
>  hw/tpm/Kconfig                         |  12 +-
>  hw/tpm/Makefile.objs                   |   4 +-
>  hw/tpm/tpm_tis.h                       |  91 +++++++++++++
>  hw/tpm/{tpm_tis.c => tpm_tis_common.c} | 181 ++-----------------------
>  hw/tpm/tpm_tis_isa.c                   | 170 +++++++++++++++++++++++
>  hw/tpm/tpm_tis_sysbus.c                | 159 ++++++++++++++++++++++
>  include/sysemu/tpm.h                   |   7 +-
>  tests/qtest/Makefile.include           |   4 +-
>  14 files changed, 502 insertions(+), 180 deletions(-)
>  create mode 100644 hw/tpm/tpm_tis.h
>  rename hw/tpm/{tpm_tis.c => tpm_tis_common.c} (85%)
>  create mode 100644 hw/tpm/tpm_tis_isa.c
>  create mode 100644 hw/tpm/tpm_tis_sysbus.c
>
> --
> 2.20.1
>


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA
  2020-02-14 18:36 ` [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA Eric Auger
  2020-02-14 18:55   ` Philippe Mathieu-Daudé
@ 2020-02-16 18:14   ` Stefan Berger
  1 sibling, 0 replies; 30+ messages in thread
From: Stefan Berger @ 2020-02-16 18:14 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

On 2/14/20 1:36 PM, Eric Auger wrote:
> As we plan to introduce a sysbus TPM_TIS, let's rename
> TPM_TIS into TPM_TIS_ISA.
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
>
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> ---
>   hw/i386/acpi-build.c | 6 +++---
>   hw/tpm/tpm_tis.c     | 4 ++--
>   include/sysemu/tpm.h | 6 +++---
>   3 files changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
> index 9c4e46fa74..26777f8828 100644
> --- a/hw/i386/acpi-build.c
> +++ b/hw/i386/acpi-build.c
> @@ -2026,7 +2026,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
>           }
>       }
>   
> -    if (TPM_IS_TIS(tpm_find())) {
> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>           aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
>                      TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
>       }
> @@ -2197,7 +2197,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
>               /* Scan all PCI buses. Generate tables to support hotplug. */
>               build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
>   
> -            if (TPM_IS_TIS(tpm)) {
> +            if (TPM_IS_TIS_ISA(tpm)) {
>                   if (misc->tpm_version == TPM_VERSION_2_0) {
>                       dev = aml_device("TPM");
>                       aml_append(dev, aml_name_decl("_HID",
> @@ -2304,7 +2304,7 @@ build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog)
>           (char *)&tpm2_ptr->log_area_start_address - table_data->data;
>   
>       tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
> -    if (TPM_IS_TIS(tpm_find())) {
> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>           tpm2_ptr->control_area_address = cpu_to_le64(0);
>           tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
>       } else if (TPM_IS_CRB(tpm_find())) {
> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
> index 31facb896d..c609737272 100644
> --- a/hw/tpm/tpm_tis.c
> +++ b/hw/tpm/tpm_tis.c
> @@ -91,7 +91,7 @@ typedef struct TPMState {
>       TPMPPI ppi;
>   } TPMState;
>   
> -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
> +#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
>   
>   #define DEBUG_TIS 0
>   
> @@ -1008,7 +1008,7 @@ static void tpm_tis_class_init(ObjectClass *klass, void *data)
>   }
>   
>   static const TypeInfo tpm_tis_info = {
> -    .name = TYPE_TPM_TIS,
> +    .name = TYPE_TPM_TIS_ISA,
>       .parent = TYPE_ISA_DEVICE,
>       .instance_size = sizeof(TPMState),
>       .instance_init = tpm_tis_initfn,
> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
> index 15979a3647..1691b92c28 100644
> --- a/include/sysemu/tpm.h
> +++ b/include/sysemu/tpm.h
> @@ -43,12 +43,12 @@ typedef struct TPMIfClass {
>       enum TPMVersion (*get_version)(TPMIf *obj);
>   } TPMIfClass;
>   
> -#define TYPE_TPM_TIS                "tpm-tis"
> +#define TYPE_TPM_TIS_ISA            "tpm-tis"
>   #define TYPE_TPM_CRB                "tpm-crb"
>   #define TYPE_TPM_SPAPR              "tpm-spapr"
>   
> -#define TPM_IS_TIS(chr)                             \
> -    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS)
> +#define TPM_IS_TIS_ISA(chr)                         \
> +    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
>   #define TPM_IS_CRB(chr)                             \
>       object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
>   #define TPM_IS_SPAPR(chr)                           \




^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 2/6] tpm: Use TPMState as a common struct
  2020-02-14 18:37 ` [RFC v2 2/6] tpm: Use TPMState as a common struct Eric Auger
  2020-02-14 19:01   ` Philippe Mathieu-Daudé
@ 2020-02-16 18:22   ` Stefan Berger
  2020-02-17  9:21   ` Philippe Mathieu-Daudé
  2 siblings, 0 replies; 30+ messages in thread
From: Stefan Berger @ 2020-02-16 18:22 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

On 2/14/20 1:37 PM, Eric Auger wrote:
> As we plan to introdce a SysBus TPM TIS device, let's
> make the TPMState a common struct usable by both the
> ISADevice and the SysBusDevice. TPMStateISA embeds the
> struct and inherits from the ISADevice.
>
> The prototype of functions bound to be used by both
> the ISA and SysBus devices is changed to take TPMState
> handle.
>
> A bunch of structs also are renamed to be specialized
> for the ISA device. Besides those transformations, no
> functional change is expected.
>
> Signed-off-by: Eric Auger <eric.auger@redhat.com>


Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>


> ---
>   hw/tpm/tpm_tis.c | 146 +++++++++++++++++++++++++++++------------------
>   1 file changed, 91 insertions(+), 55 deletions(-)
>
> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
> index c609737272..fc6d7ca579 100644
> --- a/hw/tpm/tpm_tis.c
> +++ b/hw/tpm/tpm_tis.c
> @@ -65,7 +65,6 @@ typedef struct TPMLocality {
>   } TPMLocality;
>   
>   typedef struct TPMState {
> -    ISADevice busdev;
>       MemoryRegion mmio;
>   
>       unsigned char buffer[TPM_TIS_BUFFER_MAX];
> @@ -91,7 +90,15 @@ typedef struct TPMState {
>       TPMPPI ppi;
>   } TPMState;
>   
> -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
> +typedef struct TPMStateISA {
> +    /*< private >*/
> +    ISADevice parent_obj;
> +
> +    /*< public >*/
> +    TPMState state; /* not a QOM object */
> +} TPMStateISA;
> +
> +#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
>   
>   #define DEBUG_TIS 0
>   
> @@ -281,9 +288,8 @@ static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty)
>   /*
>    * Callback from the TPM to indicate that the response was received.
>    */
> -static void tpm_tis_request_completed(TPMIf *ti, int ret)
> +static void tpm_tis_request_completed(TPMState *s, int ret)
>   {
> -    TPMState *s = TPM(ti);
>       uint8_t locty = s->cmd.locty;
>       uint8_t l;
>   
> @@ -338,7 +344,7 @@ static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty)
>   }
>   
>   #ifdef DEBUG_TIS
> -static void tpm_tis_dump_state(void *opaque, hwaddr addr)
> +static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
>   {
>       static const unsigned regs[] = {
>           TPM_TIS_REG_ACCESS,
> @@ -353,7 +359,6 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr)
>       int idx;
>       uint8_t locty = tpm_tis_locality_from_addr(addr);
>       hwaddr base = addr & ~0xfff;
> -    TPMState *s = opaque;
>   
>       printf("tpm_tis: active locality      : %d\n"
>              "tpm_tis: state of locality %d : %d\n"
> @@ -363,7 +368,7 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr)
>   
>       for (idx = 0; regs[idx] != 0xfff; idx++) {
>           printf("tpm_tis: 0x%04x : 0x%08x\n", regs[idx],
> -               (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4));
> +               (int)tpm_tis_mmio_read(s, base + regs[idx], 4));
>       }
>   
>       printf("tpm_tis: r/w offset    : %d\n"
> @@ -488,7 +493,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
>           break;
>   #ifdef DEBUG_TIS
>       case TPM_TIS_REG_DEBUG:
> -        tpm_tis_dump_state(opaque, addr);
> +        tpm_tis_dump_state(s, addr);
>           break;
>   #endif
>       }
> @@ -835,10 +840,8 @@ static const MemoryRegionOps tpm_tis_memory_ops = {
>   /*
>    * Get the TPMVersion of the backend device being used
>    */
> -static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
> +static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
>   {
> -    TPMState *s = TPM(ti);
> -
>       if (tpm_backend_had_startup_error(s->be_driver)) {
>           return TPM_VERSION_UNSPEC;
>       }
> @@ -850,9 +853,8 @@ static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
>    * This function is called when the machine starts, resets or due to
>    * S3 resume.
>    */
> -static void tpm_tis_reset(DeviceState *dev)
> +static void tpm_tis_reset(TPMState *s)
>   {
> -    TPMState *s = TPM(dev);
>       int c;
>   
>       s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver);
> @@ -896,15 +898,14 @@ static void tpm_tis_reset(DeviceState *dev)
>   
>   /* persistent state handling */
>   
> -static int tpm_tis_pre_save(void *opaque)
> +static int tpm_tis_pre_save(TPMState *s)
>   {
> -    TPMState *s = opaque;
>       uint8_t locty = s->active_locty;
>   
>       trace_tpm_tis_pre_save(locty, s->rw_offset);
>   
>       if (DEBUG_TIS) {
> -        tpm_tis_dump_state(opaque, 0);
> +        tpm_tis_dump_state(s, 0);
>       }
>   
>       /*
> @@ -929,34 +930,78 @@ static const VMStateDescription vmstate_locty = {
>       }
>   };
>   
> -static const VMStateDescription vmstate_tpm_tis = {
> +/* ISA */
> +
> +static int tpm_tis_pre_save_isa(void *opaque)
> +{
> +    TPMStateISA *isadev = opaque;
> +
> +    return tpm_tis_pre_save(&isadev->state);
> +}
> +
> +static const VMStateDescription vmstate_tpm_tis_isa = {
>       .name = "tpm-tis",
>       .version_id = 0,
> -    .pre_save  = tpm_tis_pre_save,
> +    .pre_save  = tpm_tis_pre_save_isa,
>       .fields = (VMStateField[]) {
> -        VMSTATE_BUFFER(buffer, TPMState),
> -        VMSTATE_UINT16(rw_offset, TPMState),
> -        VMSTATE_UINT8(active_locty, TPMState),
> -        VMSTATE_UINT8(aborting_locty, TPMState),
> -        VMSTATE_UINT8(next_locty, TPMState),
> +        VMSTATE_BUFFER(state.buffer, TPMStateISA),
> +        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
> +        VMSTATE_UINT8(state.active_locty, TPMStateISA),
> +        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
> +        VMSTATE_UINT8(state.next_locty, TPMStateISA),
>   
> -        VMSTATE_STRUCT_ARRAY(loc, TPMState, TPM_TIS_NUM_LOCALITIES, 0,
> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
>                                vmstate_locty, TPMLocality),
>   
>           VMSTATE_END_OF_LIST()
>       }
>   };
>   
> -static Property tpm_tis_properties[] = {
> -    DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ),
> -    DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver),
> -    DEFINE_PROP_BOOL("ppi", TPMState, ppi_enabled, true),
> +static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> +    TPMState *s = &isadev->state;
> +
> +    tpm_tis_request_completed(s, ret);
> +}
> +
> +static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> +    TPMState *s = &isadev->state;
> +
> +    return tpm_tis_get_tpm_version(s);
> +}
> +
> +static void tpm_tis_isa_reset(DeviceState *dev)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> +    TPMState *s = &isadev->state;
> +
> +    return tpm_tis_reset(s);
> +}
> +
> +static Property tpm_tis_isa_properties[] = {
> +    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
> +    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
>       DEFINE_PROP_END_OF_LIST(),
>   };
>   
> -static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
> +static void tpm_tis_isa_initfn(Object *obj)
>   {
> -    TPMState *s = TPM(dev);
> +    TPMStateISA *isadev = TPM_TIS_ISA(obj);
> +    TPMState *s = &isadev->state;
> +
> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
> +                          s, "tpm-tis-mmio",
> +                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> +}
> +
> +static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> +    TPMState *s = &isadev->state;
>   
>       if (!tpm_find()) {
>           error_setg(errp, "at most one TPM device is permitted");
> @@ -973,55 +1018,46 @@ static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
>           return;
>       }
>   
> -    isa_init_irq(&s->busdev, &s->irq, s->irq_num);
> +    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
>   
>       memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
>                                   TPM_TIS_ADDR_BASE, &s->mmio);
>   
>       if (s->ppi_enabled) {
>           tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
> -                     TPM_PPI_ADDR_BASE, OBJECT(s));
> +                     TPM_PPI_ADDR_BASE, OBJECT(dev));
>       }
>   }
>   
> -static void tpm_tis_initfn(Object *obj)
> -{
> -    TPMState *s = TPM(obj);
> -
> -    memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
> -                          s, "tpm-tis-mmio",
> -                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> -}
> -
> -static void tpm_tis_class_init(ObjectClass *klass, void *data)
> +static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
>   {
>       DeviceClass *dc = DEVICE_CLASS(klass);
>       TPMIfClass *tc = TPM_IF_CLASS(klass);
>   
> -    dc->realize = tpm_tis_realizefn;
> -    device_class_set_props(dc, tpm_tis_properties);
> -    dc->reset = tpm_tis_reset;
> -    dc->vmsd  = &vmstate_tpm_tis;
> +    device_class_set_props(dc, tpm_tis_isa_properties);
> +    dc->vmsd  = &vmstate_tpm_tis_isa;
>       tc->model = TPM_MODEL_TPM_TIS;
> -    tc->get_version = tpm_tis_get_tpm_version;
> -    tc->request_completed = tpm_tis_request_completed;
> +    dc->realize = tpm_tis_isa_realizefn;
> +    dc->reset = tpm_tis_isa_reset;
> +    tc->request_completed = tpm_tis_isa_request_completed;
> +    tc->get_version = tpm_tis_isa_get_tpm_version;
>   }
>   
> -static const TypeInfo tpm_tis_info = {
> +static const TypeInfo tpm_tis_isa_info = {
>       .name = TYPE_TPM_TIS_ISA,
>       .parent = TYPE_ISA_DEVICE,
> -    .instance_size = sizeof(TPMState),
> -    .instance_init = tpm_tis_initfn,
> -    .class_init  = tpm_tis_class_init,
> +    .instance_size = sizeof(TPMStateISA),
> +    .instance_init = tpm_tis_isa_initfn,
> +    .class_init  = tpm_tis_isa_class_init,
>       .interfaces = (InterfaceInfo[]) {
>           { TYPE_TPM_IF },
>           { }
>       }
>   };
>   
> -static void tpm_tis_register(void)
> +static void tpm_tis_isa_register(void)
>   {
> -    type_register_static(&tpm_tis_info);
> +    type_register_static(&tpm_tis_isa_info);
>   }
>   
> -type_init(tpm_tis_register)
> +type_init(tpm_tis_isa_register)




^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 3/6] tpm: Separate tpm_tis common functions from isa code
  2020-02-14 18:37 ` [RFC v2 3/6] tpm: Separate tpm_tis common functions from isa code Eric Auger
@ 2020-02-16 18:27   ` Stefan Berger
  0 siblings, 0 replies; 30+ messages in thread
From: Stefan Berger @ 2020-02-16 18:27 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

On 2/14/20 1:37 PM, Eric Auger wrote:
> Move the device agnostic code into tpm_tis_common.c and
> put the ISA device specific code into tpm_tis_isa.c
>
> Signed-off-by: Eric Auger <eric.auger@redhat.com>

Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>


> ---
>   hw/tpm/Makefile.objs                   |   2 +-
>   hw/tpm/tpm_tis.h                       |  91 +++++++++++
>   hw/tpm/{tpm_tis.c => tpm_tis_common.c} | 209 ++-----------------------
>   hw/tpm/tpm_tis_isa.c                   | 170 ++++++++++++++++++++
>   4 files changed, 271 insertions(+), 201 deletions(-)
>   create mode 100644 hw/tpm/tpm_tis.h
>   rename hw/tpm/{tpm_tis.c => tpm_tis_common.c} (83%)
>   create mode 100644 hw/tpm/tpm_tis_isa.c
>
> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
> index 85eb99ae05..fcc4c2f27c 100644
> --- a/hw/tpm/Makefile.objs
> +++ b/hw/tpm/Makefile.objs
> @@ -1,6 +1,6 @@
>   common-obj-$(CONFIG_TPM) += tpm_util.o
>   obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
> -common-obj-$(CONFIG_TPM_TIS) += tpm_tis.o
> +common-obj-$(CONFIG_TPM_TIS) += tpm_tis_isa.o tpm_tis_common.o
>   common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
>   common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
>   common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o
> diff --git a/hw/tpm/tpm_tis.h b/hw/tpm/tpm_tis.h
> new file mode 100644
> index 0000000000..5554989395
> --- /dev/null
> +++ b/hw/tpm/tpm_tis.h
> @@ -0,0 +1,91 @@
> +/*
> + * tpm_tis.h - QEMU's TPM TIS common header
> + *
> + * Copyright (C) 2006,2010-2013 IBM Corporation
> + *
> + * Authors:
> + *  Stefan Berger <stefanb@us.ibm.com>
> + *  David Safford <safford@us.ibm.com>
> + *
> + * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + *
> + * Implementation of the TIS interface according to specs found at
> + * http://www.trustedcomputinggroup.org. This implementation currently
> + * supports version 1.3, 21 March 2013
> + * In the developers menu choose the PC Client section then find the TIS
> + * specification.
> + *
> + * TPM TIS for TPM 2 implementation following TCG PC Client Platform
> + * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
> + */
> +#ifndef TPM_TPM_TIS_H
> +#define TPM_TPM_TIS_H
> +
> +#include "qemu/osdep.h"
> +#include "sysemu/tpm_backend.h"
> +#include "tpm_ppi.h"
> +
> +#define TPM_TIS_NUM_LOCALITIES      5     /* per spec */
> +#define TPM_TIS_LOCALITY_SHIFT      12
> +#define TPM_TIS_NO_LOCALITY         0xff
> +
> +#define TPM_TIS_IS_VALID_LOCTY(x)   ((x) < TPM_TIS_NUM_LOCALITIES)
> +
> +#define TPM_TIS_BUFFER_MAX          4096
> +
> +typedef enum {
> +    TPM_TIS_STATE_IDLE = 0,
> +    TPM_TIS_STATE_READY,
> +    TPM_TIS_STATE_COMPLETION,
> +    TPM_TIS_STATE_EXECUTION,
> +    TPM_TIS_STATE_RECEPTION,
> +} TPMTISState;
> +
> +/* locality data  -- all fields are persisted */
> +typedef struct TPMLocality {
> +    TPMTISState state;
> +    uint8_t access;
> +    uint32_t sts;
> +    uint32_t iface_id;
> +    uint32_t inte;
> +    uint32_t ints;
> +} TPMLocality;
> +
> +typedef struct TPMState {
> +    MemoryRegion mmio;
> +
> +    unsigned char buffer[TPM_TIS_BUFFER_MAX];
> +    uint16_t rw_offset;
> +
> +    uint8_t active_locty;
> +    uint8_t aborting_locty;
> +    uint8_t next_locty;
> +
> +    TPMLocality loc[TPM_TIS_NUM_LOCALITIES];
> +
> +    qemu_irq irq;
> +    uint32_t irq_num;
> +
> +    TPMBackendCmd cmd;
> +
> +    TPMBackend *be_driver;
> +    TPMVersion be_tpm_version;
> +
> +    size_t be_buffer_size;
> +
> +    bool ppi_enabled;
> +    TPMPPI ppi;
> +} TPMState;
> +
> +extern const VMStateDescription vmstate_locty;
> +extern const MemoryRegionOps tpm_tis_memory_ops;
> +
> +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);
> +
> +#endif /* TPM_TPM_TIS_H */
> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis_common.c
> similarity index 83%
> rename from hw/tpm/tpm_tis.c
> rename to hw/tpm/tpm_tis_common.c
> index fc6d7ca579..9ce64d4836 100644
> --- a/hw/tpm/tpm_tis.c
> +++ b/hw/tpm/tpm_tis_common.c
> @@ -1,5 +1,6 @@
>   /*
> - * tpm_tis.c - QEMU's TPM TIS interface emulator
> + * tpm_tis_common.c - QEMU's TPM TIS interface emulator
> + * device agnostic functions
>    *
>    * Copyright (C) 2006,2010-2013 IBM Corporation
>    *
> @@ -21,7 +22,6 @@
>    * TPM TIS for TPM 2 implementation following TCG PC Client Platform
>    * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
>    */
> -
>   #include "qemu/osdep.h"
>   #include "hw/irq.h"
>   #include "hw/isa/isa.h"
> @@ -38,67 +38,7 @@
>   #include "tpm_ppi.h"
>   #include "trace.h"
>   
> -#define TPM_TIS_NUM_LOCALITIES      5     /* per spec */
> -#define TPM_TIS_LOCALITY_SHIFT      12
> -#define TPM_TIS_NO_LOCALITY         0xff
> -
> -#define TPM_TIS_IS_VALID_LOCTY(x)   ((x) < TPM_TIS_NUM_LOCALITIES)
> -
> -#define TPM_TIS_BUFFER_MAX          4096
> -
> -typedef enum {
> -    TPM_TIS_STATE_IDLE = 0,
> -    TPM_TIS_STATE_READY,
> -    TPM_TIS_STATE_COMPLETION,
> -    TPM_TIS_STATE_EXECUTION,
> -    TPM_TIS_STATE_RECEPTION,
> -} TPMTISState;
> -
> -/* locality data  -- all fields are persisted */
> -typedef struct TPMLocality {
> -    TPMTISState state;
> -    uint8_t access;
> -    uint32_t sts;
> -    uint32_t iface_id;
> -    uint32_t inte;
> -    uint32_t ints;
> -} TPMLocality;
> -
> -typedef struct TPMState {
> -    MemoryRegion mmio;
> -
> -    unsigned char buffer[TPM_TIS_BUFFER_MAX];
> -    uint16_t rw_offset;
> -
> -    uint8_t active_locty;
> -    uint8_t aborting_locty;
> -    uint8_t next_locty;
> -
> -    TPMLocality loc[TPM_TIS_NUM_LOCALITIES];
> -
> -    qemu_irq irq;
> -    uint32_t irq_num;
> -
> -    TPMBackendCmd cmd;
> -
> -    TPMBackend *be_driver;
> -    TPMVersion be_tpm_version;
> -
> -    size_t be_buffer_size;
> -
> -    bool ppi_enabled;
> -    TPMPPI ppi;
> -} TPMState;
> -
> -typedef struct TPMStateISA {
> -    /*< private >*/
> -    ISADevice parent_obj;
> -
> -    /*< public >*/
> -    TPMState state; /* not a QOM object */
> -} TPMStateISA;
> -
> -#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
> +#include "tpm_tis.h"
>   
>   #define DEBUG_TIS 0
>   
> @@ -288,7 +228,7 @@ static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty)
>   /*
>    * Callback from the TPM to indicate that the response was received.
>    */
> -static void tpm_tis_request_completed(TPMState *s, int ret)
> +void tpm_tis_request_completed(TPMState *s, int ret)
>   {
>       uint8_t locty = s->cmd.locty;
>       uint8_t l;
> @@ -827,7 +767,7 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
>       }
>   }
>   
> -static const MemoryRegionOps tpm_tis_memory_ops = {
> +const MemoryRegionOps tpm_tis_memory_ops = {
>       .read = tpm_tis_mmio_read,
>       .write = tpm_tis_mmio_write,
>       .endianness = DEVICE_LITTLE_ENDIAN,
> @@ -840,7 +780,7 @@ static const MemoryRegionOps tpm_tis_memory_ops = {
>   /*
>    * Get the TPMVersion of the backend device being used
>    */
> -static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
> +enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
>   {
>       if (tpm_backend_had_startup_error(s->be_driver)) {
>           return TPM_VERSION_UNSPEC;
> @@ -853,7 +793,7 @@ static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
>    * This function is called when the machine starts, resets or due to
>    * S3 resume.
>    */
> -static void tpm_tis_reset(TPMState *s)
> +void tpm_tis_reset(TPMState *s)
>   {
>       int c;
>   
> @@ -898,7 +838,7 @@ static void tpm_tis_reset(TPMState *s)
>   
>   /* persistent state handling */
>   
> -static int tpm_tis_pre_save(TPMState *s)
> +int tpm_tis_pre_save(TPMState *s)
>   {
>       uint8_t locty = s->active_locty;
>   
> @@ -916,7 +856,7 @@ static int tpm_tis_pre_save(TPMState *s)
>       return 0;
>   }
>   
> -static const VMStateDescription vmstate_locty = {
> +const VMStateDescription vmstate_locty = {
>       .name = "tpm-tis/locty",
>       .version_id = 0,
>       .fields      = (VMStateField[]) {
> @@ -930,134 +870,3 @@ static const VMStateDescription vmstate_locty = {
>       }
>   };
>   
> -/* ISA */
> -
> -static int tpm_tis_pre_save_isa(void *opaque)
> -{
> -    TPMStateISA *isadev = opaque;
> -
> -    return tpm_tis_pre_save(&isadev->state);
> -}
> -
> -static const VMStateDescription vmstate_tpm_tis_isa = {
> -    .name = "tpm-tis",
> -    .version_id = 0,
> -    .pre_save  = tpm_tis_pre_save_isa,
> -    .fields = (VMStateField[]) {
> -        VMSTATE_BUFFER(state.buffer, TPMStateISA),
> -        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
> -        VMSTATE_UINT8(state.active_locty, TPMStateISA),
> -        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
> -        VMSTATE_UINT8(state.next_locty, TPMStateISA),
> -
> -        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
> -                             vmstate_locty, TPMLocality),
> -
> -        VMSTATE_END_OF_LIST()
> -    }
> -};
> -
> -static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
> -{
> -    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> -    TPMState *s = &isadev->state;
> -
> -    tpm_tis_request_completed(s, ret);
> -}
> -
> -static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
> -{
> -    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> -    TPMState *s = &isadev->state;
> -
> -    return tpm_tis_get_tpm_version(s);
> -}
> -
> -static void tpm_tis_isa_reset(DeviceState *dev)
> -{
> -    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> -    TPMState *s = &isadev->state;
> -
> -    return tpm_tis_reset(s);
> -}
> -
> -static Property tpm_tis_isa_properties[] = {
> -    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
> -    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
> -    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
> -    DEFINE_PROP_END_OF_LIST(),
> -};
> -
> -static void tpm_tis_isa_initfn(Object *obj)
> -{
> -    TPMStateISA *isadev = TPM_TIS_ISA(obj);
> -    TPMState *s = &isadev->state;
> -
> -    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
> -                          s, "tpm-tis-mmio",
> -                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> -}
> -
> -static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
> -{
> -    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> -    TPMState *s = &isadev->state;
> -
> -    if (!tpm_find()) {
> -        error_setg(errp, "at most one TPM device is permitted");
> -        return;
> -    }
> -
> -    if (!s->be_driver) {
> -        error_setg(errp, "'tpmdev' property is required");
> -        return;
> -    }
> -    if (s->irq_num > 15) {
> -        error_setg(errp, "IRQ %d is outside valid range of 0 to 15",
> -                   s->irq_num);
> -        return;
> -    }
> -
> -    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
> -
> -    memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
> -                                TPM_TIS_ADDR_BASE, &s->mmio);
> -
> -    if (s->ppi_enabled) {
> -        tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
> -                     TPM_PPI_ADDR_BASE, OBJECT(dev));
> -    }
> -}
> -
> -static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
> -{
> -    DeviceClass *dc = DEVICE_CLASS(klass);
> -    TPMIfClass *tc = TPM_IF_CLASS(klass);
> -
> -    device_class_set_props(dc, tpm_tis_isa_properties);
> -    dc->vmsd  = &vmstate_tpm_tis_isa;
> -    tc->model = TPM_MODEL_TPM_TIS;
> -    dc->realize = tpm_tis_isa_realizefn;
> -    dc->reset = tpm_tis_isa_reset;
> -    tc->request_completed = tpm_tis_isa_request_completed;
> -    tc->get_version = tpm_tis_isa_get_tpm_version;
> -}
> -
> -static const TypeInfo tpm_tis_isa_info = {
> -    .name = TYPE_TPM_TIS_ISA,
> -    .parent = TYPE_ISA_DEVICE,
> -    .instance_size = sizeof(TPMStateISA),
> -    .instance_init = tpm_tis_isa_initfn,
> -    .class_init  = tpm_tis_isa_class_init,
> -    .interfaces = (InterfaceInfo[]) {
> -        { TYPE_TPM_IF },
> -        { }
> -    }
> -};
> -
> -static void tpm_tis_isa_register(void)
> -{
> -    type_register_static(&tpm_tis_isa_info);
> -}
> -
> -type_init(tpm_tis_isa_register)
> diff --git a/hw/tpm/tpm_tis_isa.c b/hw/tpm/tpm_tis_isa.c
> new file mode 100644
> index 0000000000..30ba37079d
> --- /dev/null
> +++ b/hw/tpm/tpm_tis_isa.c
> @@ -0,0 +1,170 @@
> +/*
> + * tpm_tis_isa.c - QEMU's TPM TIS ISA Device
> + *
> + * Copyright (C) 2006,2010-2013 IBM Corporation
> + *
> + * Authors:
> + *  Stefan Berger <stefanb@us.ibm.com>
> + *  David Safford <safford@us.ibm.com>
> + *
> + * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + *
> + * Implementation of the TIS interface according to specs found at
> + * http://www.trustedcomputinggroup.org. This implementation currently
> + * supports version 1.3, 21 March 2013
> + * In the developers menu choose the PC Client section then find the TIS
> + * specification.
> + *
> + * TPM TIS for TPM 2 implementation following TCG PC Client Platform
> + * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
> + */
> +
> +#include "qemu/osdep.h"
> +#include "hw/isa/isa.h"
> +#include "hw/qdev-properties.h"
> +#include "migration/vmstate.h"
> +#include "tpm_util.h"
> +#include "tpm_tis.h"
> +
> +typedef struct TPMStateISA {
> +    /*< private >*/
> +    ISADevice parent_obj;
> +
> +    /*< public >*/
> +    TPMState state; /* not a QOM object */
> +} TPMStateISA;
> +
> +#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
> +
> +static int tpm_tis_pre_save_isa(void *opaque)
> +{
> +    TPMStateISA *isadev = opaque;
> +
> +    return tpm_tis_pre_save(&isadev->state);
> +}
> +
> +static const VMStateDescription vmstate_tpm_tis_isa = {
> +    .name = "tpm-tis",
> +    .version_id = 0,
> +    .pre_save  = tpm_tis_pre_save_isa,
> +    .fields = (VMStateField[]) {
> +        VMSTATE_BUFFER(state.buffer, TPMStateISA),
> +        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
> +        VMSTATE_UINT8(state.active_locty, TPMStateISA),
> +        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
> +        VMSTATE_UINT8(state.next_locty, TPMStateISA),
> +
> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
> +                             vmstate_locty, TPMLocality),
> +
> +        VMSTATE_END_OF_LIST()
> +    }
> +};
> +
> +static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> +    TPMState *s = &isadev->state;
> +
> +    tpm_tis_request_completed(s, ret);
> +}
> +
> +static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> +    TPMState *s = &isadev->state;
> +
> +    return tpm_tis_get_tpm_version(s);
> +}
> +
> +static void tpm_tis_isa_reset(DeviceState *dev)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> +    TPMState *s = &isadev->state;
> +
> +    return tpm_tis_reset(s);
> +}
> +
> +static Property tpm_tis_isa_properties[] = {
> +    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
> +    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
> +    DEFINE_PROP_END_OF_LIST(),
> +};
> +
> +static void tpm_tis_isa_initfn(Object *obj)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(obj);
> +    TPMState *s = &isadev->state;
> +
> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
> +                          s, "tpm-tis-mmio",
> +                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> +}
> +
> +static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> +    TPMState *s = &isadev->state;
> +
> +    if (!tpm_find()) {
> +        error_setg(errp, "at most one TPM device is permitted");
> +        return;
> +    }
> +
> +    if (!s->be_driver) {
> +        error_setg(errp, "'tpmdev' property is required");
> +        return;
> +    }
> +    if (s->irq_num > 15) {
> +        error_setg(errp, "IRQ %d is outside valid range of 0 to 15",
> +                   s->irq_num);
> +        return;
> +    }
> +
> +    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
> +
> +    memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
> +                                TPM_TIS_ADDR_BASE, &s->mmio);
> +
> +    if (s->ppi_enabled) {
> +        tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
> +                     TPM_PPI_ADDR_BASE, OBJECT(dev));
> +    }
> +}
> +
> +static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
> +{
> +    DeviceClass *dc = DEVICE_CLASS(klass);
> +    TPMIfClass *tc = TPM_IF_CLASS(klass);
> +
> +    device_class_set_props(dc, tpm_tis_isa_properties);
> +    dc->vmsd  = &vmstate_tpm_tis_isa;
> +    tc->model = TPM_MODEL_TPM_TIS;
> +    dc->realize = tpm_tis_isa_realizefn;
> +    dc->reset = tpm_tis_isa_reset;
> +    tc->request_completed = tpm_tis_isa_request_completed;
> +    tc->get_version = tpm_tis_isa_get_tpm_version;
> +}
> +
> +static const TypeInfo tpm_tis_isa_info = {
> +    .name = TYPE_TPM_TIS_ISA,
> +    .parent = TYPE_ISA_DEVICE,
> +    .instance_size = sizeof(TPMStateISA),
> +    .instance_init = tpm_tis_isa_initfn,
> +    .class_init  = tpm_tis_isa_class_init,
> +    .interfaces = (InterfaceInfo[]) {
> +        { TYPE_TPM_IF },
> +        { }
> +    }
> +};
> +
> +static void tpm_tis_isa_register(void)
> +{
> +    type_register_static(&tpm_tis_isa_info);
> +}
> +
> +type_init(tpm_tis_isa_register)




^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 5/6] tpm: Add the SysBus TPM TIS device
  2020-02-14 18:37 ` [RFC v2 5/6] tpm: Add the SysBus TPM TIS device Eric Auger
@ 2020-02-16 18:32   ` Stefan Berger
  2020-02-17 18:13     ` Auger Eric
  0 siblings, 1 reply; 30+ messages in thread
From: Stefan Berger @ 2020-02-16 18:32 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

On 2/14/20 1:37 PM, Eric Auger wrote:
> Introduce the tpm-tis-device which is a sysbus device
> and is bound to be used on ARM.
>
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> ---
>   hw/tpm/Kconfig          |   5 ++
>   hw/tpm/Makefile.objs    |   1 +
>   hw/tpm/tpm_tis_sysbus.c | 159 ++++++++++++++++++++++++++++++++++++++++
>   include/sysemu/tpm.h    |   1 +
>   4 files changed, 166 insertions(+)
>   create mode 100644 hw/tpm/tpm_tis_sysbus.c
>
> diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
> index 686f8206bb..4794e7fe28 100644
> --- a/hw/tpm/Kconfig
> +++ b/hw/tpm/Kconfig
> @@ -7,6 +7,11 @@ config TPM_TIS_ISA
>       depends on TPM && ISA_BUS
>       select TPM_TIS
>   
> +config TPM_TIS_SYSBUS
> +    bool
> +    depends on TPM
> +    select TPM_TIS
> +
>   config TPM_TIS
>       bool
>       depends on TPM
> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
> index 3ef2036cca..f1ec4beb95 100644
> --- a/hw/tpm/Makefile.objs
> +++ b/hw/tpm/Makefile.objs
> @@ -1,6 +1,7 @@
>   common-obj-$(CONFIG_TPM) += tpm_util.o
>   obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
>   common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
> +common-obj-$(CONFIG_TPM_TIS_SYSBUS) += tpm_tis_sysbus.o
>   common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
>   common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
>   common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
> diff --git a/hw/tpm/tpm_tis_sysbus.c b/hw/tpm/tpm_tis_sysbus.c
> new file mode 100644
> index 0000000000..18c02aed67
> --- /dev/null
> +++ b/hw/tpm/tpm_tis_sysbus.c
> @@ -0,0 +1,159 @@
> +/*
> + * tpm_tis_sysbus.c - QEMU's TPM TIS SYSBUS Device
> + *
> + * Copyright (C) 2006,2010-2013 IBM Corporation
> + *
> + * Authors:
> + *  Stefan Berger <stefanb@us.ibm.com>
> + *  David Safford <safford@us.ibm.com>
> + *
> + * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + *
> + * Implementation of the TIS interface according to specs found at
> + * http://www.trustedcomputinggroup.org. This implementation currently
> + * supports version 1.3, 21 March 2013
> + * In the developers menu choose the PC Client section then find the TIS
> + * specification.
> + *
> + * TPM TIS for TPM 2 implementation following TCG PC Client Platform
> + * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
> + */
> +
> +#include "qemu/osdep.h"
> +#include "hw/qdev-properties.h"
> +#include "migration/vmstate.h"
> +#include "tpm_util.h"
> +#include "hw/sysbus.h"
> +#include "tpm_tis.h"
> +
> +typedef struct TPMStateSysBus {
> +    /*< private >*/
> +    SysBusDevice parent_obj;
> +
> +    /*< public >*/
> +    TPMState state; /* not a QOM object */
> +} TPMStateSysBus;
> +
> +#define TPM_TIS_SYSBUS(obj) OBJECT_CHECK(TPMStateSysBus, (obj), TYPE_TPM_TIS_SYSBUS)
> +
> +static int tpm_tis_pre_save_sysbus(void *opaque)
> +{
> +    TPMStateSysBus *sbdev = opaque;
> +
> +    return tpm_tis_pre_save(&sbdev->state);
> +}
> +
> +static const VMStateDescription vmstate_tpm_tis_sysbus = {
> +    .name = "tpm-tis",
> +    .version_id = 0,
> +    .pre_save  = tpm_tis_pre_save_sysbus,
> +    .fields = (VMStateField[]) {
> +        VMSTATE_BUFFER(state.buffer, TPMStateSysBus),
> +        VMSTATE_UINT16(state.rw_offset, TPMStateSysBus),
> +        VMSTATE_UINT8(state.active_locty, TPMStateSysBus),
> +        VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus),
> +        VMSTATE_UINT8(state.next_locty, TPMStateSysBus),
> +
> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus, TPM_TIS_NUM_LOCALITIES,
> +                             0, vmstate_locty, TPMLocality),
> +
> +        VMSTATE_END_OF_LIST()
> +    }
> +};
> +
> +static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret)
> +{
> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
> +    TPMState *s = &sbdev->state;
> +
> +    tpm_tis_request_completed(s, ret);
> +}
> +
> +static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti)
> +{
> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
> +    TPMState *s = &sbdev->state;
> +
> +    return tpm_tis_get_tpm_version(s);
> +}
> +
> +static void tpm_tis_sysbus_reset(DeviceState *dev)
> +{
> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
> +    TPMState *s = &sbdev->state;
> +
> +    return tpm_tis_reset(s);
> +}
> +
> +static Property tpm_tis_sysbus_properties[] = {
> +    DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num, TPM_TIS_IRQ),
> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
> +    DEFINE_PROP_BOOL("ppi", TPMStateSysBus, state.ppi_enabled, true),
> +    DEFINE_PROP_END_OF_LIST(),
> +};
> +
> +static void tpm_tis_sysbus_initfn(Object *obj)
> +{
> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
> +    TPMState *s = &sbdev->state;
> +
> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
> +                          s, "tpm-tis-mmio",
> +                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> +
> +    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
> +    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
> +}
> +
> +static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
> +{
> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
> +    TPMState *s = &sbdev->state;
> +
> +    if (!tpm_find()) {
> +        error_setg(errp, "at most one TPM device is permitted");
> +        return;
> +    }
> +
> +    if (!s->be_driver) {
> +        error_setg(errp, "'tpmdev' property is required");
> +        return;
> +    }
> +}
> +
> +static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
> +{
> +    DeviceClass *dc = DEVICE_CLASS(klass);
> +    TPMIfClass *tc = TPM_IF_CLASS(klass);
> +
> +    device_class_set_props(dc, tpm_tis_sysbus_properties);
> +    dc->vmsd  = &vmstate_tpm_tis_sysbus;
> +    tc->model = TPM_MODEL_TPM_TIS;
> +    dc->realize = tpm_tis_sysbus_realizefn;
> +    dc->user_creatable = true;
> +    dc->reset = tpm_tis_sysbus_reset;
> +    tc->request_completed = tpm_tis_sysbus_request_completed;
> +    tc->get_version = tpm_tis_sysbus_get_tpm_version;
> +}
> +
> +static const TypeInfo tpm_tis_sysbus_info = {
> +    .name = TYPE_TPM_TIS_SYSBUS,
> +    .parent = TYPE_SYS_BUS_DEVICE,
> +    .instance_size = sizeof(TPMStateSysBus),
> +    .instance_init = tpm_tis_sysbus_initfn,
> +    .class_init  = tpm_tis_sysbus_class_init,
> +    .interfaces = (InterfaceInfo[]) {
> +        { TYPE_TPM_IF },
> +        { }
> +    }
> +};
> +
> +static void tpm_tis_sysbus_register(void)
> +{
> +    type_register_static(&tpm_tis_sysbus_info);
> +}
> +
> +type_init(tpm_tis_sysbus_register)
> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
> index 1691b92c28..f37851b1aa 100644
> --- a/include/sysemu/tpm.h
> +++ b/include/sysemu/tpm.h
> @@ -44,6 +44,7 @@ typedef struct TPMIfClass {
>   } TPMIfClass;
>   
>   #define TYPE_TPM_TIS_ISA            "tpm-tis"
> +#define TYPE_TPM_TIS_SYSBUS         "tpm-tis-device"


hm, replace the rather generic 'device' with 'sysbus'?


Otherwise looks really good.


Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>


>   #define TYPE_TPM_CRB                "tpm-crb"
>   #define TYPE_TPM_SPAPR              "tpm-spapr"
>   




^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 6/6] hw/arm/virt: vTPM support
  2020-02-14 18:37 ` [RFC v2 6/6] hw/arm/virt: vTPM support Eric Auger
@ 2020-02-16 18:47   ` Stefan Berger
  2020-02-17 18:26     ` Auger Eric
  0 siblings, 1 reply; 30+ messages in thread
From: Stefan Berger @ 2020-02-16 18:47 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

On 2/14/20 1:37 PM, Eric Auger wrote:
> Let the TPM TIS SYSBUS device be dynamically instantiable
> in ARM virt.  A device tree node is dynamically created
> (TPM via MMIO).
>
> The TPM Physical Presence interface (PPI) is not supported.
>
> To run with the swtmp TPM emulator, the qemu command line must
> be augmented with:
>
>          -chardev socket,id=chrtpm,path=swtpm-sock \
>          -tpmdev emulator,id=tpm0,chardev=chrtpm \
>          -device tpm-tis-device,tpmdev=tpm0 \
>
> swtpm/libtpms command line example:
>
> swtpm socket --tpm2 -t -d --tpmstate dir=/tmp/tpm \
> --ctrl type=unixio,path=swtpm-sock


Can you also extend docs/spec/tpm.rst for the arm case? And the test 
cases should probably also cover the arm sysbus device.


>
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> ---
>   hw/arm/Kconfig      |  1 +
>   hw/arm/sysbus-fdt.c | 36 ++++++++++++++++++++++++++++++++++++
>   hw/arm/virt.c       |  7 +++++++
>   3 files changed, 44 insertions(+)
>
> diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
> index 3d86691ae0..b6f03f7f53 100644
> --- a/hw/arm/Kconfig
> +++ b/hw/arm/Kconfig
> @@ -5,6 +5,7 @@ config ARM_VIRT
>       imply VFIO_AMD_XGBE
>       imply VFIO_PLATFORM
>       imply VFIO_XGMAC
> +    imply TPM_TIS_SYSBUS
>       select A15MPCORE
>       select ACPI
>       select ARM_SMMUV3
> diff --git a/hw/arm/sysbus-fdt.c b/hw/arm/sysbus-fdt.c
> index 022fc97ecd..adf50444c2 100644
> --- a/hw/arm/sysbus-fdt.c
> +++ b/hw/arm/sysbus-fdt.c
> @@ -30,6 +30,7 @@
>   #include "hw/arm/sysbus-fdt.h"
>   #include "qemu/error-report.h"
>   #include "sysemu/device_tree.h"
> +#include "sysemu/tpm.h"
>   #include "hw/platform-bus.h"
>   #include "hw/vfio/vfio-platform.h"
>   #include "hw/vfio/vfio-calxeda-xgmac.h"
> @@ -434,6 +435,40 @@ static bool vfio_platform_match(SysBusDevice *sbdev,
>   #define VFIO_PLATFORM_BINDING(compat, add_fn) \
>       {TYPE_VFIO_PLATFORM, (compat), (add_fn), vfio_platform_match}
>   
> +/*
> + * add_tpm_tis_fdt_node: Create a DT node for TPM TIS
> + *
> + * See kernel documentation:
> + * Documentation/devicetree/bindings/security/tpm/tpm_tis_mmio.txt
> + * Optional interrupt for command completion is not exposed
> + */
> +static int add_tpm_tis_fdt_node(SysBusDevice *sbdev, void *opaque)
> +{
> +    PlatformBusFDTData *data = opaque;
> +    PlatformBusDevice *pbus = data->pbus;
> +    void *fdt = data->fdt;
> +    const char *parent_node = data->pbus_node_name;
> +    int compat_str_len;
> +    char *nodename;
> +    uint32_t reg_attr[2];
> +    uint64_t mmio_base;
> +
> +    mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);

I suppose any conditional creation of this device tree entry is covered 
with the TYPE_BINDING below, meaning no device tree is created if the 
device wasn't added.


> +    nodename = g_strdup_printf("%s/tpm_tis@%" PRIx64, parent_node, mmio_base);
> +    qemu_fdt_add_subnode(fdt, nodename);
> +
> +    compat_str_len = strlen("tcg,tpm-tis-mmio") + 1;
> +    qemu_fdt_setprop(fdt, nodename, "compatible", "tcg,tpm-tis-mmio",
> +                     compat_str_len);

You probably can use qemu_fdt_setprop_string()?


> +
> +    reg_attr[0] = cpu_to_be32(mmio_base);
> +    reg_attr[1] = cpu_to_be32(0x5000);
> +    qemu_fdt_setprop(fdt, nodename, "reg", reg_attr, 2 * sizeof(uint32_t));
> +
> +    g_free(nodename);
> +    return 0;
> +}
> +
>   #endif /* CONFIG_LINUX */
>   
>   static int no_fdt_node(SysBusDevice *sbdev, void *opaque)
> @@ -455,6 +490,7 @@ static const BindingEntry bindings[] = {
>       TYPE_BINDING(TYPE_VFIO_CALXEDA_XGMAC, add_calxeda_midway_xgmac_fdt_node),
>       TYPE_BINDING(TYPE_VFIO_AMD_XGBE, add_amd_xgbe_fdt_node),
>       VFIO_PLATFORM_BINDING("amd,xgbe-seattle-v1a", add_amd_xgbe_fdt_node),
> +    TYPE_BINDING(TYPE_TPM_TIS_SYSBUS, add_tpm_tis_fdt_node),
>   #endif
>       TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node),
>       TYPE_BINDING("", NULL), /* last element */
> diff --git a/hw/arm/virt.c b/hw/arm/virt.c
> index f788fe27d6..4b967e39d1 100644
> --- a/hw/arm/virt.c
> +++ b/hw/arm/virt.c
> @@ -47,6 +47,7 @@
>   #include "sysemu/numa.h"
>   #include "sysemu/runstate.h"
>   #include "sysemu/sysemu.h"
> +#include "sysemu/tpm.h"
>   #include "sysemu/kvm.h"
>   #include "hw/loader.h"
>   #include "exec/address-spaces.h"
> @@ -2041,6 +2042,7 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)
>       machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
>       machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
>       machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
> +    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
>       mc->block_default_type = IF_VIRTIO;
>       mc->no_cdrom = 1;
>       mc->pci_allow_0_address = true;
> @@ -2153,6 +2155,11 @@ type_init(machvirt_machine_init);
>   
>   static void virt_machine_5_0_options(MachineClass *mc)
>   {
> +    static GlobalProperty compat[] = {
> +        { TYPE_TPM_TIS_SYSBUS, "ppi", "false" },
> +    };
> +
> +    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
>   }
>   DEFINE_VIRT_MACHINE_AS_LATEST(5, 0)
>   




^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 2/6] tpm: Use TPMState as a common struct
  2020-02-14 18:37 ` [RFC v2 2/6] tpm: Use TPMState as a common struct Eric Auger
  2020-02-14 19:01   ` Philippe Mathieu-Daudé
  2020-02-16 18:22   ` Stefan Berger
@ 2020-02-17  9:21   ` Philippe Mathieu-Daudé
  2020-02-17 18:01     ` Auger Eric
  2 siblings, 1 reply; 30+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-02-17  9:21 UTC (permalink / raw)
  To: Eric Auger, eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

Hi Eric,

On 2/14/20 7:37 PM, Eric Auger wrote:
> As we plan to introdce a SysBus TPM TIS device, let's
> make the TPMState a common struct usable by both the
> ISADevice and the SysBusDevice. TPMStateISA embeds the
> struct and inherits from the ISADevice.
> 
> The prototype of functions bound to be used by both
> the ISA and SysBus devices is changed to take TPMState
> handle.
> 
> A bunch of structs also are renamed to be specialized
> for the ISA device. Besides those transformations, no
> functional change is expected.
> 
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> ---
>   hw/tpm/tpm_tis.c | 146 +++++++++++++++++++++++++++++------------------
>   1 file changed, 91 insertions(+), 55 deletions(-)
> 
> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
> index c609737272..fc6d7ca579 100644
> --- a/hw/tpm/tpm_tis.c
> +++ b/hw/tpm/tpm_tis.c
> @@ -65,7 +65,6 @@ typedef struct TPMLocality {
>   } TPMLocality;
>   
>   typedef struct TPMState {
> -    ISADevice busdev;
>       MemoryRegion mmio;
>   
>       unsigned char buffer[TPM_TIS_BUFFER_MAX];
> @@ -91,7 +90,15 @@ typedef struct TPMState {
>       TPMPPI ppi;
>   } TPMState;
>   
> -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
> +typedef struct TPMStateISA {
> +    /*< private >*/
> +    ISADevice parent_obj;
> +
> +    /*< public >*/
> +    TPMState state; /* not a QOM object */
> +} TPMStateISA;
> +
> +#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
>   
>   #define DEBUG_TIS 0
>   
> @@ -281,9 +288,8 @@ static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty)
>   /*
>    * Callback from the TPM to indicate that the response was received.
>    */
> -static void tpm_tis_request_completed(TPMIf *ti, int ret)
> +static void tpm_tis_request_completed(TPMState *s, int ret)
>   {
> -    TPMState *s = TPM(ti);
>       uint8_t locty = s->cmd.locty;
>       uint8_t l;
>   
> @@ -338,7 +344,7 @@ static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty)
>   }
>   
>   #ifdef DEBUG_TIS
> -static void tpm_tis_dump_state(void *opaque, hwaddr addr)
> +static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
>   {
>       static const unsigned regs[] = {
>           TPM_TIS_REG_ACCESS,
> @@ -353,7 +359,6 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr)
>       int idx;
>       uint8_t locty = tpm_tis_locality_from_addr(addr);
>       hwaddr base = addr & ~0xfff;
> -    TPMState *s = opaque;
>   
>       printf("tpm_tis: active locality      : %d\n"
>              "tpm_tis: state of locality %d : %d\n"
> @@ -363,7 +368,7 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr)
>   
>       for (idx = 0; regs[idx] != 0xfff; idx++) {
>           printf("tpm_tis: 0x%04x : 0x%08x\n", regs[idx],
> -               (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4));
> +               (int)tpm_tis_mmio_read(s, base + regs[idx], 4));
>       }
>   
>       printf("tpm_tis: r/w offset    : %d\n"
> @@ -488,7 +493,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
>           break;
>   #ifdef DEBUG_TIS
>       case TPM_TIS_REG_DEBUG:
> -        tpm_tis_dump_state(opaque, addr);
> +        tpm_tis_dump_state(s, addr);
>           break;
>   #endif
>       }
> @@ -835,10 +840,8 @@ static const MemoryRegionOps tpm_tis_memory_ops = {
>   /*
>    * Get the TPMVersion of the backend device being used
>    */
> -static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
> +static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
>   {
> -    TPMState *s = TPM(ti);
> -
>       if (tpm_backend_had_startup_error(s->be_driver)) {
>           return TPM_VERSION_UNSPEC;
>       }
> @@ -850,9 +853,8 @@ static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
>    * This function is called when the machine starts, resets or due to
>    * S3 resume.
>    */
> -static void tpm_tis_reset(DeviceState *dev)
> +static void tpm_tis_reset(TPMState *s)
>   {
> -    TPMState *s = TPM(dev);
>       int c;
>   
>       s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver);
> @@ -896,15 +898,14 @@ static void tpm_tis_reset(DeviceState *dev)
>   
>   /* persistent state handling */
>   
> -static int tpm_tis_pre_save(void *opaque)
> +static int tpm_tis_pre_save(TPMState *s)
>   {
> -    TPMState *s = opaque;
>       uint8_t locty = s->active_locty;
>   
>       trace_tpm_tis_pre_save(locty, s->rw_offset);
>   
>       if (DEBUG_TIS) {
> -        tpm_tis_dump_state(opaque, 0);
> +        tpm_tis_dump_state(s, 0);
>       }
>   
>       /*
> @@ -929,34 +930,78 @@ static const VMStateDescription vmstate_locty = {
>       }
>   };
>   
> -static const VMStateDescription vmstate_tpm_tis = {
> +/* ISA */
> +
> +static int tpm_tis_pre_save_isa(void *opaque)
> +{
> +    TPMStateISA *isadev = opaque;
> +
> +    return tpm_tis_pre_save(&isadev->state);
> +}
> +
> +static const VMStateDescription vmstate_tpm_tis_isa = {
>       .name = "tpm-tis",
>       .version_id = 0,
> -    .pre_save  = tpm_tis_pre_save,
> +    .pre_save  = tpm_tis_pre_save_isa,
>       .fields = (VMStateField[]) {
> -        VMSTATE_BUFFER(buffer, TPMState),
> -        VMSTATE_UINT16(rw_offset, TPMState),
> -        VMSTATE_UINT8(active_locty, TPMState),
> -        VMSTATE_UINT8(aborting_locty, TPMState),
> -        VMSTATE_UINT8(next_locty, TPMState),
> +        VMSTATE_BUFFER(state.buffer, TPMStateISA),
> +        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
> +        VMSTATE_UINT8(state.active_locty, TPMStateISA),
> +        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
> +        VMSTATE_UINT8(state.next_locty, TPMStateISA),

On a second thought these fields seem to belong to a TPMCommonState.
Why not QOM'ify an abstract common parent between ISA and SysBus?

>   
> -        VMSTATE_STRUCT_ARRAY(loc, TPMState, TPM_TIS_NUM_LOCALITIES, 0,
> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
>                                vmstate_locty, TPMLocality),
>   
>           VMSTATE_END_OF_LIST()
>       }
>   };
>   
> -static Property tpm_tis_properties[] = {
> -    DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ),
> -    DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver),
> -    DEFINE_PROP_BOOL("ppi", TPMState, ppi_enabled, true),
> +static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> +    TPMState *s = &isadev->state;
> +
> +    tpm_tis_request_completed(s, ret);
> +}
> +
> +static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
> +    TPMState *s = &isadev->state;
> +
> +    return tpm_tis_get_tpm_version(s);
> +}
> +
> +static void tpm_tis_isa_reset(DeviceState *dev)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> +    TPMState *s = &isadev->state;
> +
> +    return tpm_tis_reset(s);
> +}
> +
> +static Property tpm_tis_isa_properties[] = {
> +    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
> +    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
>       DEFINE_PROP_END_OF_LIST(),
>   };
>   
> -static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
> +static void tpm_tis_isa_initfn(Object *obj)
>   {
> -    TPMState *s = TPM(dev);
> +    TPMStateISA *isadev = TPM_TIS_ISA(obj);
> +    TPMState *s = &isadev->state;
> +
> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
> +                          s, "tpm-tis-mmio",
> +                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> +}
> +
> +static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
> +{
> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
> +    TPMState *s = &isadev->state;
>   
>       if (!tpm_find()) {
>           error_setg(errp, "at most one TPM device is permitted");
> @@ -973,55 +1018,46 @@ static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
>           return;
>       }
>   
> -    isa_init_irq(&s->busdev, &s->irq, s->irq_num);
> +    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
>   
>       memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
>                                   TPM_TIS_ADDR_BASE, &s->mmio);
>   
>       if (s->ppi_enabled) {
>           tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
> -                     TPM_PPI_ADDR_BASE, OBJECT(s));
> +                     TPM_PPI_ADDR_BASE, OBJECT(dev));
>       }
>   }
>   
> -static void tpm_tis_initfn(Object *obj)
> -{
> -    TPMState *s = TPM(obj);
> -
> -    memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
> -                          s, "tpm-tis-mmio",
> -                          TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
> -}
> -
> -static void tpm_tis_class_init(ObjectClass *klass, void *data)
> +static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
>   {
>       DeviceClass *dc = DEVICE_CLASS(klass);
>       TPMIfClass *tc = TPM_IF_CLASS(klass);
>   
> -    dc->realize = tpm_tis_realizefn;
> -    device_class_set_props(dc, tpm_tis_properties);
> -    dc->reset = tpm_tis_reset;
> -    dc->vmsd  = &vmstate_tpm_tis;
> +    device_class_set_props(dc, tpm_tis_isa_properties);
> +    dc->vmsd  = &vmstate_tpm_tis_isa;
>       tc->model = TPM_MODEL_TPM_TIS;
> -    tc->get_version = tpm_tis_get_tpm_version;
> -    tc->request_completed = tpm_tis_request_completed;
> +    dc->realize = tpm_tis_isa_realizefn;
> +    dc->reset = tpm_tis_isa_reset;
> +    tc->request_completed = tpm_tis_isa_request_completed;
> +    tc->get_version = tpm_tis_isa_get_tpm_version;
>   }
>   
> -static const TypeInfo tpm_tis_info = {
> +static const TypeInfo tpm_tis_isa_info = {
>       .name = TYPE_TPM_TIS_ISA,
>       .parent = TYPE_ISA_DEVICE,
> -    .instance_size = sizeof(TPMState),
> -    .instance_init = tpm_tis_initfn,
> -    .class_init  = tpm_tis_class_init,
> +    .instance_size = sizeof(TPMStateISA),
> +    .instance_init = tpm_tis_isa_initfn,
> +    .class_init  = tpm_tis_isa_class_init,
>       .interfaces = (InterfaceInfo[]) {
>           { TYPE_TPM_IF },
>           { }
>       }
>   };
>   
> -static void tpm_tis_register(void)
> +static void tpm_tis_isa_register(void)
>   {
> -    type_register_static(&tpm_tis_info);
> +    type_register_static(&tpm_tis_isa_info);
>   }
>   
> -type_init(tpm_tis_register)
> +type_init(tpm_tis_isa_register)
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 2/6] tpm: Use TPMState as a common struct
  2020-02-17  9:21   ` Philippe Mathieu-Daudé
@ 2020-02-17 18:01     ` Auger Eric
  2020-02-25 10:18       ` Auger Eric
  0 siblings, 1 reply; 30+ messages in thread
From: Auger Eric @ 2020-02-17 18:01 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé,
	eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

Hi Philippe,

On 2/17/20 10:21 AM, Philippe Mathieu-Daudé wrote:
> Hi Eric,
> 
> On 2/14/20 7:37 PM, Eric Auger wrote:
>> As we plan to introdce a SysBus TPM TIS device, let's
>> make the TPMState a common struct usable by both the
>> ISADevice and the SysBusDevice. TPMStateISA embeds the
>> struct and inherits from the ISADevice.
>>
>> The prototype of functions bound to be used by both
>> the ISA and SysBus devices is changed to take TPMState
>> handle.
>>
>> A bunch of structs also are renamed to be specialized
>> for the ISA device. Besides those transformations, no
>> functional change is expected.
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>> ---
>>   hw/tpm/tpm_tis.c | 146 +++++++++++++++++++++++++++++------------------
>>   1 file changed, 91 insertions(+), 55 deletions(-)
>>
>> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
>> index c609737272..fc6d7ca579 100644
>> --- a/hw/tpm/tpm_tis.c
>> +++ b/hw/tpm/tpm_tis.c
>> @@ -65,7 +65,6 @@ typedef struct TPMLocality {
>>   } TPMLocality;
>>     typedef struct TPMState {
>> -    ISADevice busdev;
>>       MemoryRegion mmio;
>>         unsigned char buffer[TPM_TIS_BUFFER_MAX];
>> @@ -91,7 +90,15 @@ typedef struct TPMState {
>>       TPMPPI ppi;
>>   } TPMState;
>>   -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
>> +typedef struct TPMStateISA {
>> +    /*< private >*/
>> +    ISADevice parent_obj;
>> +
>> +    /*< public >*/
>> +    TPMState state; /* not a QOM object */
>> +} TPMStateISA;
>> +
>> +#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj),
>> TYPE_TPM_TIS_ISA)
>>     #define DEBUG_TIS 0
>>   @@ -281,9 +288,8 @@ static void tpm_tis_prep_abort(TPMState *s,
>> uint8_t locty, uint8_t newlocty)
>>   /*
>>    * Callback from the TPM to indicate that the response was received.
>>    */
>> -static void tpm_tis_request_completed(TPMIf *ti, int ret)
>> +static void tpm_tis_request_completed(TPMState *s, int ret)
>>   {
>> -    TPMState *s = TPM(ti);
>>       uint8_t locty = s->cmd.locty;
>>       uint8_t l;
>>   @@ -338,7 +344,7 @@ static uint32_t tpm_tis_data_read(TPMState *s,
>> uint8_t locty)
>>   }
>>     #ifdef DEBUG_TIS
>> -static void tpm_tis_dump_state(void *opaque, hwaddr addr)
>> +static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
>>   {
>>       static const unsigned regs[] = {
>>           TPM_TIS_REG_ACCESS,
>> @@ -353,7 +359,6 @@ static void tpm_tis_dump_state(void *opaque,
>> hwaddr addr)
>>       int idx;
>>       uint8_t locty = tpm_tis_locality_from_addr(addr);
>>       hwaddr base = addr & ~0xfff;
>> -    TPMState *s = opaque;
>>         printf("tpm_tis: active locality      : %d\n"
>>              "tpm_tis: state of locality %d : %d\n"
>> @@ -363,7 +368,7 @@ static void tpm_tis_dump_state(void *opaque,
>> hwaddr addr)
>>         for (idx = 0; regs[idx] != 0xfff; idx++) {
>>           printf("tpm_tis: 0x%04x : 0x%08x\n", regs[idx],
>> -               (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4));
>> +               (int)tpm_tis_mmio_read(s, base + regs[idx], 4));
>>       }
>>         printf("tpm_tis: r/w offset    : %d\n"
>> @@ -488,7 +493,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque,
>> hwaddr addr,
>>           break;
>>   #ifdef DEBUG_TIS
>>       case TPM_TIS_REG_DEBUG:
>> -        tpm_tis_dump_state(opaque, addr);
>> +        tpm_tis_dump_state(s, addr);
>>           break;
>>   #endif
>>       }
>> @@ -835,10 +840,8 @@ static const MemoryRegionOps tpm_tis_memory_ops = {
>>   /*
>>    * Get the TPMVersion of the backend device being used
>>    */
>> -static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
>> +static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
>>   {
>> -    TPMState *s = TPM(ti);
>> -
>>       if (tpm_backend_had_startup_error(s->be_driver)) {
>>           return TPM_VERSION_UNSPEC;
>>       }
>> @@ -850,9 +853,8 @@ static enum TPMVersion
>> tpm_tis_get_tpm_version(TPMIf *ti)
>>    * This function is called when the machine starts, resets or due to
>>    * S3 resume.
>>    */
>> -static void tpm_tis_reset(DeviceState *dev)
>> +static void tpm_tis_reset(TPMState *s)
>>   {
>> -    TPMState *s = TPM(dev);
>>       int c;
>>         s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver);
>> @@ -896,15 +898,14 @@ static void tpm_tis_reset(DeviceState *dev)
>>     /* persistent state handling */
>>   -static int tpm_tis_pre_save(void *opaque)
>> +static int tpm_tis_pre_save(TPMState *s)
>>   {
>> -    TPMState *s = opaque;
>>       uint8_t locty = s->active_locty;
>>         trace_tpm_tis_pre_save(locty, s->rw_offset);
>>         if (DEBUG_TIS) {
>> -        tpm_tis_dump_state(opaque, 0);
>> +        tpm_tis_dump_state(s, 0);
>>       }
>>         /*
>> @@ -929,34 +930,78 @@ static const VMStateDescription vmstate_locty = {
>>       }
>>   };
>>   -static const VMStateDescription vmstate_tpm_tis = {
>> +/* ISA */
>> +
>> +static int tpm_tis_pre_save_isa(void *opaque)
>> +{
>> +    TPMStateISA *isadev = opaque;
>> +
>> +    return tpm_tis_pre_save(&isadev->state);
>> +}
>> +
>> +static const VMStateDescription vmstate_tpm_tis_isa = {
>>       .name = "tpm-tis",
>>       .version_id = 0,
>> -    .pre_save  = tpm_tis_pre_save,
>> +    .pre_save  = tpm_tis_pre_save_isa,
>>       .fields = (VMStateField[]) {
>> -        VMSTATE_BUFFER(buffer, TPMState),
>> -        VMSTATE_UINT16(rw_offset, TPMState),
>> -        VMSTATE_UINT8(active_locty, TPMState),
>> -        VMSTATE_UINT8(aborting_locty, TPMState),
>> -        VMSTATE_UINT8(next_locty, TPMState),
>> +        VMSTATE_BUFFER(state.buffer, TPMStateISA),
>> +        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
>> +        VMSTATE_UINT8(state.active_locty, TPMStateISA),
>> +        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
>> +        VMSTATE_UINT8(state.next_locty, TPMStateISA),
> 
> On a second thought these fields seem to belong to a TPMCommonState.
> Why not QOM'ify an abstract common parent between ISA and SysBus?
This would mean we have double QOM inheritance (ISADevice and Sysbus
device already being QOM devices). As far as I know this is not
supported (see qom/object.h). Here I use the same trick as the one being
used in hw/vfio/platform or pci.c and also used in hw/block/fdc.c - as
pointed out by Stefan- : see FDCtrlSysBus and FDCtrlISABus.

Thanks

Eric
> 
>>   -        VMSTATE_STRUCT_ARRAY(loc, TPMState, TPM_TIS_NUM_LOCALITIES, 0,
>> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA,
>> TPM_TIS_NUM_LOCALITIES, 0,
>>                                vmstate_locty, TPMLocality),
>>             VMSTATE_END_OF_LIST()
>>       }
>>   };
>>   -static Property tpm_tis_properties[] = {
>> -    DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ),
>> -    DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver),
>> -    DEFINE_PROP_BOOL("ppi", TPMState, ppi_enabled, true),
>> +static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
>> +{
>> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
>> +    TPMState *s = &isadev->state;
>> +
>> +    tpm_tis_request_completed(s, ret);
>> +}
>> +
>> +static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
>> +{
>> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
>> +    TPMState *s = &isadev->state;
>> +
>> +    return tpm_tis_get_tpm_version(s);
>> +}
>> +
>> +static void tpm_tis_isa_reset(DeviceState *dev)
>> +{
>> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
>> +    TPMState *s = &isadev->state;
>> +
>> +    return tpm_tis_reset(s);
>> +}
>> +
>> +static Property tpm_tis_isa_properties[] = {
>> +    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
>> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
>> +    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
>>       DEFINE_PROP_END_OF_LIST(),
>>   };
>>   -static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
>> +static void tpm_tis_isa_initfn(Object *obj)
>>   {
>> -    TPMState *s = TPM(dev);
>> +    TPMStateISA *isadev = TPM_TIS_ISA(obj);
>> +    TPMState *s = &isadev->state;
>> +
>> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
>> +                          s, "tpm-tis-mmio",
>> +                          TPM_TIS_NUM_LOCALITIES <<
>> TPM_TIS_LOCALITY_SHIFT);
>> +}
>> +
>> +static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
>> +{
>> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
>> +    TPMState *s = &isadev->state;
>>         if (!tpm_find()) {
>>           error_setg(errp, "at most one TPM device is permitted");
>> @@ -973,55 +1018,46 @@ static void tpm_tis_realizefn(DeviceState *dev,
>> Error **errp)
>>           return;
>>       }
>>   -    isa_init_irq(&s->busdev, &s->irq, s->irq_num);
>> +    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
>>         memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
>>                                   TPM_TIS_ADDR_BASE, &s->mmio);
>>         if (s->ppi_enabled) {
>>           tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
>> -                     TPM_PPI_ADDR_BASE, OBJECT(s));
>> +                     TPM_PPI_ADDR_BASE, OBJECT(dev));
>>       }
>>   }
>>   -static void tpm_tis_initfn(Object *obj)
>> -{
>> -    TPMState *s = TPM(obj);
>> -
>> -    memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
>> -                          s, "tpm-tis-mmio",
>> -                          TPM_TIS_NUM_LOCALITIES <<
>> TPM_TIS_LOCALITY_SHIFT);
>> -}
>> -
>> -static void tpm_tis_class_init(ObjectClass *klass, void *data)
>> +static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
>>   {
>>       DeviceClass *dc = DEVICE_CLASS(klass);
>>       TPMIfClass *tc = TPM_IF_CLASS(klass);
>>   -    dc->realize = tpm_tis_realizefn;
>> -    device_class_set_props(dc, tpm_tis_properties);
>> -    dc->reset = tpm_tis_reset;
>> -    dc->vmsd  = &vmstate_tpm_tis;
>> +    device_class_set_props(dc, tpm_tis_isa_properties);
>> +    dc->vmsd  = &vmstate_tpm_tis_isa;
>>       tc->model = TPM_MODEL_TPM_TIS;
>> -    tc->get_version = tpm_tis_get_tpm_version;
>> -    tc->request_completed = tpm_tis_request_completed;
>> +    dc->realize = tpm_tis_isa_realizefn;
>> +    dc->reset = tpm_tis_isa_reset;
>> +    tc->request_completed = tpm_tis_isa_request_completed;
>> +    tc->get_version = tpm_tis_isa_get_tpm_version;
>>   }
>>   -static const TypeInfo tpm_tis_info = {
>> +static const TypeInfo tpm_tis_isa_info = {
>>       .name = TYPE_TPM_TIS_ISA,
>>       .parent = TYPE_ISA_DEVICE,
>> -    .instance_size = sizeof(TPMState),
>> -    .instance_init = tpm_tis_initfn,
>> -    .class_init  = tpm_tis_class_init,
>> +    .instance_size = sizeof(TPMStateISA),
>> +    .instance_init = tpm_tis_isa_initfn,
>> +    .class_init  = tpm_tis_isa_class_init,
>>       .interfaces = (InterfaceInfo[]) {
>>           { TYPE_TPM_IF },
>>           { }
>>       }
>>   };
>>   -static void tpm_tis_register(void)
>> +static void tpm_tis_isa_register(void)
>>   {
>> -    type_register_static(&tpm_tis_info);
>> +    type_register_static(&tpm_tis_isa_info);
>>   }
>>   -type_init(tpm_tis_register)
>> +type_init(tpm_tis_isa_register)
>>
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 0/6] vTPM for aarch64
  2020-02-16 16:35 ` [RFC v2 0/6] vTPM for aarch64 Ard Biesheuvel
@ 2020-02-17 18:03   ` Auger Eric
  0 siblings, 0 replies; 30+ messages in thread
From: Auger Eric @ 2020-02-17 18:03 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	QEMU Developers, qemu-arm, Marc-André Lureau, Eric Auger,
	Laszlo Ersek, Stefan Berger

Hi Ard,

On 2/16/20 5:35 PM, Ard Biesheuvel wrote:
> On Fri, 14 Feb 2020 at 19:37, Eric Auger <eric.auger@redhat.com> wrote:
>>
>> This series adds the capability to instantiate an MMIO TPM TIS
>> in ARM virt.
>>
>> The existing TPM TIS code is reshuffled into a generic part,
>> the ISA device and the sysbus device. The last patch allows
>> the instantiation of the TPM TIS sysbus device in ARM virt.
>>
>> The series was tested with the swtpm/libtpms emulator.
>> Automatic guest LUKS volume unlocking (tpm2) was successful.
>> EDK2 support is under development [3]. Thanks to Ard
>> for supporting me when setting up the test environment.
>>
>> Best Regards
>>
>> Eric
>>
>> Testing:
>>
>> mkdir /tmp/tpm
>> swtpm socket \
>> --tpm2 \
>> -t -d \
>> --tpmstate dir=/tmp/tpm \
>> --ctrl type=unixio,path=/tmp/swtpm-sock
>>
>> qemu command line must be augmented with the following options:
>>
>> -chardev socket,id=chrtpm,path=/tmp/swtpm-sock \
>> -tpmdev emulator,id=tpm0,chardev=chrtpm \
>> -device tpm-tis-device,tpmdev=tpm0 \
>>
>> References:
>> [1] libtpms: https://github.com/stefanberger/libtpms/wiki
>> [2] swtpm: https://github.com/stefanberger/swtpm/wiki
>> [3] [PATCH 0/4] ArmVirtPkg: implement measured boot for ArmVirtQemu
>>
>> This series can be found at:
>> https://github.com/eauger/qemu/tree/v4.2.0-tpm-rfc-v2
>>
>> History:
>>
>> RFC v1 -> RFC v2:
>> - restructure the existing code with common, ISA and sysbus part.
>> - both ARM and x86 integration were tested.
>> - acknowledgement: migration has not been tested
>>
>> Eric Auger (6):
>>   tpm: rename TPM_TIS into TPM_TIS_ISA
>>   tpm: Use TPMState as a common struct
>>   tpm: Separate tpm_tis common functions from isa code
>>   tpm: Separate TPM_TIS and TPM_TIS_ISA configs
>>   tpm: Add the SysBus TPM TIS device
>>   hw/arm/virt: vTPM support
>>
> 
> Many thanks to Eric for his work on this.
> 
> I have tested this with my EDK2 code and a Linux/arm64 VM booting in
> DT mode with the tpm-tis driver enabled, and it works as expected. I
> can observe the firmware taking measurements during boot, and can dump
> the PCRs from Linux using tpm2_pcrlist.
> 
> Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

Many thanks for the testing.
> 
> Note that the UEFI firmware is itself a consumer of the DT
> description, so we need the DT related changes regardless of whether
> the VM boots in DT or ACPI mode.

OK thank you for the note. I will add this in my cover letter.

Best Regards

Eric
> 
> 
> 
>>  default-configs/i386-softmmu.mak       |   2 +-
>>  hw/arm/Kconfig                         |   1 +
>>  hw/arm/sysbus-fdt.c                    |  36 +++++
>>  hw/arm/virt.c                          |   7 +
>>  hw/i386/Kconfig                        |   2 +-
>>  hw/i386/acpi-build.c                   |   6 +-
>>  hw/tpm/Kconfig                         |  12 +-
>>  hw/tpm/Makefile.objs                   |   4 +-
>>  hw/tpm/tpm_tis.h                       |  91 +++++++++++++
>>  hw/tpm/{tpm_tis.c => tpm_tis_common.c} | 181 ++-----------------------
>>  hw/tpm/tpm_tis_isa.c                   | 170 +++++++++++++++++++++++
>>  hw/tpm/tpm_tis_sysbus.c                | 159 ++++++++++++++++++++++
>>  include/sysemu/tpm.h                   |   7 +-
>>  tests/qtest/Makefile.include           |   4 +-
>>  14 files changed, 502 insertions(+), 180 deletions(-)
>>  create mode 100644 hw/tpm/tpm_tis.h
>>  rename hw/tpm/{tpm_tis.c => tpm_tis_common.c} (85%)
>>  create mode 100644 hw/tpm/tpm_tis_isa.c
>>  create mode 100644 hw/tpm/tpm_tis_sysbus.c
>>
>> --
>> 2.20.1
>>
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 5/6] tpm: Add the SysBus TPM TIS device
  2020-02-16 18:32   ` Stefan Berger
@ 2020-02-17 18:13     ` Auger Eric
  2020-02-25  9:18       ` Auger Eric
  0 siblings, 1 reply; 30+ messages in thread
From: Auger Eric @ 2020-02-17 18:13 UTC (permalink / raw)
  To: Stefan Berger, eric.auger.pro, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

Hi Stefan,

On 2/16/20 7:32 PM, Stefan Berger wrote:
> On 2/14/20 1:37 PM, Eric Auger wrote:
>> Introduce the tpm-tis-device which is a sysbus device
>> and is bound to be used on ARM.
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>> ---
>>   hw/tpm/Kconfig          |   5 ++
>>   hw/tpm/Makefile.objs    |   1 +
>>   hw/tpm/tpm_tis_sysbus.c | 159 ++++++++++++++++++++++++++++++++++++++++
>>   include/sysemu/tpm.h    |   1 +
>>   4 files changed, 166 insertions(+)
>>   create mode 100644 hw/tpm/tpm_tis_sysbus.c
>>
>> diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
>> index 686f8206bb..4794e7fe28 100644
>> --- a/hw/tpm/Kconfig
>> +++ b/hw/tpm/Kconfig
>> @@ -7,6 +7,11 @@ config TPM_TIS_ISA
>>       depends on TPM && ISA_BUS
>>       select TPM_TIS
>>   +config TPM_TIS_SYSBUS
>> +    bool
>> +    depends on TPM
>> +    select TPM_TIS
>> +
>>   config TPM_TIS
>>       bool
>>       depends on TPM
>> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
>> index 3ef2036cca..f1ec4beb95 100644
>> --- a/hw/tpm/Makefile.objs
>> +++ b/hw/tpm/Makefile.objs
>> @@ -1,6 +1,7 @@
>>   common-obj-$(CONFIG_TPM) += tpm_util.o
>>   obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
>>   common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
>> +common-obj-$(CONFIG_TPM_TIS_SYSBUS) += tpm_tis_sysbus.o
>>   common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
>>   common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
>>   common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
>> diff --git a/hw/tpm/tpm_tis_sysbus.c b/hw/tpm/tpm_tis_sysbus.c
>> new file mode 100644
>> index 0000000000..18c02aed67
>> --- /dev/null
>> +++ b/hw/tpm/tpm_tis_sysbus.c
>> @@ -0,0 +1,159 @@
>> +/*
>> + * tpm_tis_sysbus.c - QEMU's TPM TIS SYSBUS Device
>> + *
>> + * Copyright (C) 2006,2010-2013 IBM Corporation
>> + *
>> + * Authors:
>> + *  Stefan Berger <stefanb@us.ibm.com>
>> + *  David Safford <safford@us.ibm.com>
>> + *
>> + * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
>> + *
>> + * This work is licensed under the terms of the GNU GPL, version 2 or
>> later.
>> + * See the COPYING file in the top-level directory.
>> + *
>> + * Implementation of the TIS interface according to specs found at
>> + * http://www.trustedcomputinggroup.org. This implementation currently
>> + * supports version 1.3, 21 March 2013
>> + * In the developers menu choose the PC Client section then find the TIS
>> + * specification.
>> + *
>> + * TPM TIS for TPM 2 implementation following TCG PC Client Platform
>> + * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
>> + */
>> +
>> +#include "qemu/osdep.h"
>> +#include "hw/qdev-properties.h"
>> +#include "migration/vmstate.h"
>> +#include "tpm_util.h"
>> +#include "hw/sysbus.h"
>> +#include "tpm_tis.h"
>> +
>> +typedef struct TPMStateSysBus {
>> +    /*< private >*/
>> +    SysBusDevice parent_obj;
>> +
>> +    /*< public >*/
>> +    TPMState state; /* not a QOM object */
>> +} TPMStateSysBus;
>> +
>> +#define TPM_TIS_SYSBUS(obj) OBJECT_CHECK(TPMStateSysBus, (obj),
>> TYPE_TPM_TIS_SYSBUS)
>> +
>> +static int tpm_tis_pre_save_sysbus(void *opaque)
>> +{
>> +    TPMStateSysBus *sbdev = opaque;
>> +
>> +    return tpm_tis_pre_save(&sbdev->state);
>> +}
>> +
>> +static const VMStateDescription vmstate_tpm_tis_sysbus = {
>> +    .name = "tpm-tis",
>> +    .version_id = 0,
>> +    .pre_save  = tpm_tis_pre_save_sysbus,
>> +    .fields = (VMStateField[]) {
>> +        VMSTATE_BUFFER(state.buffer, TPMStateSysBus),
>> +        VMSTATE_UINT16(state.rw_offset, TPMStateSysBus),
>> +        VMSTATE_UINT8(state.active_locty, TPMStateSysBus),
>> +        VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus),
>> +        VMSTATE_UINT8(state.next_locty, TPMStateSysBus),
>> +
>> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus,
>> TPM_TIS_NUM_LOCALITIES,
>> +                             0, vmstate_locty, TPMLocality),
>> +
>> +        VMSTATE_END_OF_LIST()
>> +    }
>> +};
>> +
>> +static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret)
>> +{
>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
>> +    TPMState *s = &sbdev->state;
>> +
>> +    tpm_tis_request_completed(s, ret);
>> +}
>> +
>> +static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti)
>> +{
>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
>> +    TPMState *s = &sbdev->state;
>> +
>> +    return tpm_tis_get_tpm_version(s);
>> +}
>> +
>> +static void tpm_tis_sysbus_reset(DeviceState *dev)
>> +{
>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
>> +    TPMState *s = &sbdev->state;
>> +
>> +    return tpm_tis_reset(s);
>> +}
>> +
>> +static Property tpm_tis_sysbus_properties[] = {
>> +    DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num,
>> TPM_TIS_IRQ),
>> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
>> +    DEFINE_PROP_BOOL("ppi", TPMStateSysBus, state.ppi_enabled, true),
>> +    DEFINE_PROP_END_OF_LIST(),
>> +};
>> +
>> +static void tpm_tis_sysbus_initfn(Object *obj)
>> +{
>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
>> +    TPMState *s = &sbdev->state;
>> +
>> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
>> +                          s, "tpm-tis-mmio",
>> +                          TPM_TIS_NUM_LOCALITIES <<
>> TPM_TIS_LOCALITY_SHIFT);
>> +
>> +    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
>> +    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
>> +}
>> +
>> +static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
>> +{
>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
>> +    TPMState *s = &sbdev->state;
>> +
>> +    if (!tpm_find()) {
>> +        error_setg(errp, "at most one TPM device is permitted");
>> +        return;
>> +    }
>> +
>> +    if (!s->be_driver) {
>> +        error_setg(errp, "'tpmdev' property is required");
>> +        return;
>> +    }
>> +}
>> +
>> +static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
>> +{
>> +    DeviceClass *dc = DEVICE_CLASS(klass);
>> +    TPMIfClass *tc = TPM_IF_CLASS(klass);
>> +
>> +    device_class_set_props(dc, tpm_tis_sysbus_properties);
>> +    dc->vmsd  = &vmstate_tpm_tis_sysbus;
>> +    tc->model = TPM_MODEL_TPM_TIS;
>> +    dc->realize = tpm_tis_sysbus_realizefn;
>> +    dc->user_creatable = true;
>> +    dc->reset = tpm_tis_sysbus_reset;
>> +    tc->request_completed = tpm_tis_sysbus_request_completed;
>> +    tc->get_version = tpm_tis_sysbus_get_tpm_version;
>> +}
>> +
>> +static const TypeInfo tpm_tis_sysbus_info = {
>> +    .name = TYPE_TPM_TIS_SYSBUS,
>> +    .parent = TYPE_SYS_BUS_DEVICE,
>> +    .instance_size = sizeof(TPMStateSysBus),
>> +    .instance_init = tpm_tis_sysbus_initfn,
>> +    .class_init  = tpm_tis_sysbus_class_init,
>> +    .interfaces = (InterfaceInfo[]) {
>> +        { TYPE_TPM_IF },
>> +        { }
>> +    }
>> +};
>> +
>> +static void tpm_tis_sysbus_register(void)
>> +{
>> +    type_register_static(&tpm_tis_sysbus_info);
>> +}
>> +
>> +type_init(tpm_tis_sysbus_register)
>> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
>> index 1691b92c28..f37851b1aa 100644
>> --- a/include/sysemu/tpm.h
>> +++ b/include/sysemu/tpm.h
>> @@ -44,6 +44,7 @@ typedef struct TPMIfClass {
>>   } TPMIfClass;
>>     #define TYPE_TPM_TIS_ISA            "tpm-tis"
>> +#define TYPE_TPM_TIS_SYSBUS         "tpm-tis-device"
> 
> 
> hm, replace the rather generic 'device' with 'sysbus'?
I used the "-device" suffix because this kind of naming was used for
virtio-<type>-device when based on MMIO rather than virtio-<type>-pci.
For instance virtio-net-device instead of virtio-net-pci. There are
quite a lot of devices using that suffix. I only see xen-sysbus with the
sysbus suffix.

Now personally I don't have any strong preference and I will pick up the
name chosen by consensus.
> 
> 
> Otherwise looks really good.
Thank you

Best Regards

Eric
> 
> 
> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
> 
> 
>>   #define TYPE_TPM_CRB                "tpm-crb"
>>   #define TYPE_TPM_SPAPR              "tpm-spapr"
>>   
> 
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 6/6] hw/arm/virt: vTPM support
  2020-02-16 18:47   ` Stefan Berger
@ 2020-02-17 18:26     ` Auger Eric
  0 siblings, 0 replies; 30+ messages in thread
From: Auger Eric @ 2020-02-17 18:26 UTC (permalink / raw)
  To: Stefan Berger, eric.auger.pro, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

Hi Stefan,

On 2/16/20 7:47 PM, Stefan Berger wrote:
> On 2/14/20 1:37 PM, Eric Auger wrote:
>> Let the TPM TIS SYSBUS device be dynamically instantiable
>> in ARM virt.  A device tree node is dynamically created
>> (TPM via MMIO).
>>
>> The TPM Physical Presence interface (PPI) is not supported.
>>
>> To run with the swtmp TPM emulator, the qemu command line must
>> be augmented with:
>>
>>          -chardev socket,id=chrtpm,path=swtpm-sock \
>>          -tpmdev emulator,id=tpm0,chardev=chrtpm \
>>          -device tpm-tis-device,tpmdev=tpm0 \
>>
>> swtpm/libtpms command line example:
>>
>> swtpm socket --tpm2 -t -d --tpmstate dir=/tmp/tpm \
>> --ctrl type=unixio,path=swtpm-sock
> 
> 
> Can you also extend docs/spec/tpm.rst for the arm case?
Sure

 And the test
> cases should probably also cover the arm sysbus device.
Yes I agree. At the moment I have made sure existing x86-64 qtests were
running as before.

I will investigate what I can do to cover the sysbus device.

> 
> 
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>> ---
>>   hw/arm/Kconfig      |  1 +
>>   hw/arm/sysbus-fdt.c | 36 ++++++++++++++++++++++++++++++++++++
>>   hw/arm/virt.c       |  7 +++++++
>>   3 files changed, 44 insertions(+)
>>
>> diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
>> index 3d86691ae0..b6f03f7f53 100644
>> --- a/hw/arm/Kconfig
>> +++ b/hw/arm/Kconfig
>> @@ -5,6 +5,7 @@ config ARM_VIRT
>>       imply VFIO_AMD_XGBE
>>       imply VFIO_PLATFORM
>>       imply VFIO_XGMAC
>> +    imply TPM_TIS_SYSBUS
>>       select A15MPCORE
>>       select ACPI
>>       select ARM_SMMUV3
>> diff --git a/hw/arm/sysbus-fdt.c b/hw/arm/sysbus-fdt.c
>> index 022fc97ecd..adf50444c2 100644
>> --- a/hw/arm/sysbus-fdt.c
>> +++ b/hw/arm/sysbus-fdt.c
>> @@ -30,6 +30,7 @@
>>   #include "hw/arm/sysbus-fdt.h"
>>   #include "qemu/error-report.h"
>>   #include "sysemu/device_tree.h"
>> +#include "sysemu/tpm.h"
>>   #include "hw/platform-bus.h"
>>   #include "hw/vfio/vfio-platform.h"
>>   #include "hw/vfio/vfio-calxeda-xgmac.h"
>> @@ -434,6 +435,40 @@ static bool vfio_platform_match(SysBusDevice *sbdev,
>>   #define VFIO_PLATFORM_BINDING(compat, add_fn) \
>>       {TYPE_VFIO_PLATFORM, (compat), (add_fn), vfio_platform_match}
>>   +/*
>> + * add_tpm_tis_fdt_node: Create a DT node for TPM TIS
>> + *
>> + * See kernel documentation:
>> + * Documentation/devicetree/bindings/security/tpm/tpm_tis_mmio.txt
>> + * Optional interrupt for command completion is not exposed
>> + */
>> +static int add_tpm_tis_fdt_node(SysBusDevice *sbdev, void *opaque)
>> +{
>> +    PlatformBusFDTData *data = opaque;
>> +    PlatformBusDevice *pbus = data->pbus;
>> +    void *fdt = data->fdt;
>> +    const char *parent_node = data->pbus_node_name;
>> +    int compat_str_len;
>> +    char *nodename;
>> +    uint32_t reg_attr[2];
>> +    uint64_t mmio_base;
>> +
>> +    mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
> 
> I suppose any conditional creation of this device tree entry is covered
> with the TYPE_BINDING below, meaning no device tree is created if the
> device wasn't added.
Yes exactly, the dt node is not created if the -device tpm-tis-device is
not passed in the qemu cmd line.
> 
> 
>> +    nodename = g_strdup_printf("%s/tpm_tis@%" PRIx64, parent_node,
>> mmio_base);
>> +    qemu_fdt_add_subnode(fdt, nodename);
>> +
>> +    compat_str_len = strlen("tcg,tpm-tis-mmio") + 1;
>> +    qemu_fdt_setprop(fdt, nodename, "compatible", "tcg,tpm-tis-mmio",
>> +                     compat_str_len);
> 
> You probably can use qemu_fdt_setprop_string()?
indeed!
> 
> 
>> +
>> +    reg_attr[0] = cpu_to_be32(mmio_base);
>> +    reg_attr[1] = cpu_to_be32(0x5000);
>> +    qemu_fdt_setprop(fdt, nodename, "reg", reg_attr, 2 *
>> sizeof(uint32_t));
>> +
>> +    g_free(nodename);
>> +    return 0;
>> +}
>> +
>>   #endif /* CONFIG_LINUX */
>>     static int no_fdt_node(SysBusDevice *sbdev, void *opaque)
>> @@ -455,6 +490,7 @@ static const BindingEntry bindings[] = {
>>       TYPE_BINDING(TYPE_VFIO_CALXEDA_XGMAC,
>> add_calxeda_midway_xgmac_fdt_node),
>>       TYPE_BINDING(TYPE_VFIO_AMD_XGBE, add_amd_xgbe_fdt_node),
>>       VFIO_PLATFORM_BINDING("amd,xgbe-seattle-v1a",
>> add_amd_xgbe_fdt_node),
>> +    TYPE_BINDING(TYPE_TPM_TIS_SYSBUS, add_tpm_tis_fdt_node),
>>   #endif
>>       TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node),
>>       TYPE_BINDING("", NULL), /* last element */
>> diff --git a/hw/arm/virt.c b/hw/arm/virt.c
>> index f788fe27d6..4b967e39d1 100644
>> --- a/hw/arm/virt.c
>> +++ b/hw/arm/virt.c
>> @@ -47,6 +47,7 @@
>>   #include "sysemu/numa.h"
>>   #include "sysemu/runstate.h"
>>   #include "sysemu/sysemu.h"
>> +#include "sysemu/tpm.h"
>>   #include "sysemu/kvm.h"
>>   #include "hw/loader.h"
>>   #include "exec/address-spaces.h"
>> @@ -2041,6 +2042,7 @@ static void virt_machine_class_init(ObjectClass
>> *oc, void *data)
>>       machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
>>       machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
>>       machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
>> +    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
>>       mc->block_default_type = IF_VIRTIO;
>>       mc->no_cdrom = 1;
>>       mc->pci_allow_0_address = true;
>> @@ -2153,6 +2155,11 @@ type_init(machvirt_machine_init);
>>     static void virt_machine_5_0_options(MachineClass *mc)
>>   {
>> +    static GlobalProperty compat[] = {
>> +        { TYPE_TPM_TIS_SYSBUS, "ppi", "false" },
>> +    };
>> +
>> +    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
>>   }
>>   DEFINE_VIRT_MACHINE_AS_LATEST(5, 0)
>>   
> 
> 
Thanks

Eric



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 5/6] tpm: Add the SysBus TPM TIS device
  2020-02-17 18:13     ` Auger Eric
@ 2020-02-25  9:18       ` Auger Eric
  2020-02-25  9:52         ` Ard Biesheuvel
  0 siblings, 1 reply; 30+ messages in thread
From: Auger Eric @ 2020-02-25  9:18 UTC (permalink / raw)
  To: Stefan Berger, eric.auger.pro, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb, philmd

Hi,

On 2/17/20 7:13 PM, Auger Eric wrote:
> Hi Stefan,
> 
> On 2/16/20 7:32 PM, Stefan Berger wrote:
>> On 2/14/20 1:37 PM, Eric Auger wrote:
>>> Introduce the tpm-tis-device which is a sysbus device
>>> and is bound to be used on ARM.
>>>
>>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>> ---
>>>   hw/tpm/Kconfig          |   5 ++
>>>   hw/tpm/Makefile.objs    |   1 +
>>>   hw/tpm/tpm_tis_sysbus.c | 159 ++++++++++++++++++++++++++++++++++++++++
>>>   include/sysemu/tpm.h    |   1 +
>>>   4 files changed, 166 insertions(+)
>>>   create mode 100644 hw/tpm/tpm_tis_sysbus.c
>>>
>>> diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
>>> index 686f8206bb..4794e7fe28 100644
>>> --- a/hw/tpm/Kconfig
>>> +++ b/hw/tpm/Kconfig
>>> @@ -7,6 +7,11 @@ config TPM_TIS_ISA
>>>       depends on TPM && ISA_BUS
>>>       select TPM_TIS
>>>   +config TPM_TIS_SYSBUS
>>> +    bool
>>> +    depends on TPM
>>> +    select TPM_TIS
>>> +
>>>   config TPM_TIS
>>>       bool
>>>       depends on TPM
>>> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
>>> index 3ef2036cca..f1ec4beb95 100644
>>> --- a/hw/tpm/Makefile.objs
>>> +++ b/hw/tpm/Makefile.objs
>>> @@ -1,6 +1,7 @@
>>>   common-obj-$(CONFIG_TPM) += tpm_util.o
>>>   obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
>>>   common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
>>> +common-obj-$(CONFIG_TPM_TIS_SYSBUS) += tpm_tis_sysbus.o
>>>   common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
>>>   common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
>>>   common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
>>> diff --git a/hw/tpm/tpm_tis_sysbus.c b/hw/tpm/tpm_tis_sysbus.c
>>> new file mode 100644
>>> index 0000000000..18c02aed67
>>> --- /dev/null
>>> +++ b/hw/tpm/tpm_tis_sysbus.c
>>> @@ -0,0 +1,159 @@
>>> +/*
>>> + * tpm_tis_sysbus.c - QEMU's TPM TIS SYSBUS Device
>>> + *
>>> + * Copyright (C) 2006,2010-2013 IBM Corporation
>>> + *
>>> + * Authors:
>>> + *  Stefan Berger <stefanb@us.ibm.com>
>>> + *  David Safford <safford@us.ibm.com>
>>> + *
>>> + * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
>>> + *
>>> + * This work is licensed under the terms of the GNU GPL, version 2 or
>>> later.
>>> + * See the COPYING file in the top-level directory.
>>> + *
>>> + * Implementation of the TIS interface according to specs found at
>>> + * http://www.trustedcomputinggroup.org. This implementation currently
>>> + * supports version 1.3, 21 March 2013
>>> + * In the developers menu choose the PC Client section then find the TIS
>>> + * specification.
>>> + *
>>> + * TPM TIS for TPM 2 implementation following TCG PC Client Platform
>>> + * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
>>> + */
>>> +
>>> +#include "qemu/osdep.h"
>>> +#include "hw/qdev-properties.h"
>>> +#include "migration/vmstate.h"
>>> +#include "tpm_util.h"
>>> +#include "hw/sysbus.h"
>>> +#include "tpm_tis.h"
>>> +
>>> +typedef struct TPMStateSysBus {
>>> +    /*< private >*/
>>> +    SysBusDevice parent_obj;
>>> +
>>> +    /*< public >*/
>>> +    TPMState state; /* not a QOM object */
>>> +} TPMStateSysBus;
>>> +
>>> +#define TPM_TIS_SYSBUS(obj) OBJECT_CHECK(TPMStateSysBus, (obj),
>>> TYPE_TPM_TIS_SYSBUS)
>>> +
>>> +static int tpm_tis_pre_save_sysbus(void *opaque)
>>> +{
>>> +    TPMStateSysBus *sbdev = opaque;
>>> +
>>> +    return tpm_tis_pre_save(&sbdev->state);
>>> +}
>>> +
>>> +static const VMStateDescription vmstate_tpm_tis_sysbus = {
>>> +    .name = "tpm-tis",
>>> +    .version_id = 0,
>>> +    .pre_save  = tpm_tis_pre_save_sysbus,
>>> +    .fields = (VMStateField[]) {
>>> +        VMSTATE_BUFFER(state.buffer, TPMStateSysBus),
>>> +        VMSTATE_UINT16(state.rw_offset, TPMStateSysBus),
>>> +        VMSTATE_UINT8(state.active_locty, TPMStateSysBus),
>>> +        VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus),
>>> +        VMSTATE_UINT8(state.next_locty, TPMStateSysBus),
>>> +
>>> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus,
>>> TPM_TIS_NUM_LOCALITIES,
>>> +                             0, vmstate_locty, TPMLocality),
>>> +
>>> +        VMSTATE_END_OF_LIST()
>>> +    }
>>> +};
>>> +
>>> +static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret)
>>> +{
>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
>>> +    TPMState *s = &sbdev->state;
>>> +
>>> +    tpm_tis_request_completed(s, ret);
>>> +}
>>> +
>>> +static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti)
>>> +{
>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
>>> +    TPMState *s = &sbdev->state;
>>> +
>>> +    return tpm_tis_get_tpm_version(s);
>>> +}
>>> +
>>> +static void tpm_tis_sysbus_reset(DeviceState *dev)
>>> +{
>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
>>> +    TPMState *s = &sbdev->state;
>>> +
>>> +    return tpm_tis_reset(s);
>>> +}
>>> +
>>> +static Property tpm_tis_sysbus_properties[] = {
>>> +    DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num,
>>> TPM_TIS_IRQ),
>>> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
>>> +    DEFINE_PROP_BOOL("ppi", TPMStateSysBus, state.ppi_enabled, true),
>>> +    DEFINE_PROP_END_OF_LIST(),
>>> +};
>>> +
>>> +static void tpm_tis_sysbus_initfn(Object *obj)
>>> +{
>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
>>> +    TPMState *s = &sbdev->state;
>>> +
>>> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
>>> +                          s, "tpm-tis-mmio",
>>> +                          TPM_TIS_NUM_LOCALITIES <<
>>> TPM_TIS_LOCALITY_SHIFT);
>>> +
>>> +    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
>>> +    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
>>> +}
>>> +
>>> +static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
>>> +{
>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
>>> +    TPMState *s = &sbdev->state;
>>> +
>>> +    if (!tpm_find()) {
>>> +        error_setg(errp, "at most one TPM device is permitted");
>>> +        return;
>>> +    }
>>> +
>>> +    if (!s->be_driver) {
>>> +        error_setg(errp, "'tpmdev' property is required");
>>> +        return;
>>> +    }
>>> +}
>>> +
>>> +static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
>>> +{
>>> +    DeviceClass *dc = DEVICE_CLASS(klass);
>>> +    TPMIfClass *tc = TPM_IF_CLASS(klass);
>>> +
>>> +    device_class_set_props(dc, tpm_tis_sysbus_properties);
>>> +    dc->vmsd  = &vmstate_tpm_tis_sysbus;
>>> +    tc->model = TPM_MODEL_TPM_TIS;
>>> +    dc->realize = tpm_tis_sysbus_realizefn;
>>> +    dc->user_creatable = true;
>>> +    dc->reset = tpm_tis_sysbus_reset;
>>> +    tc->request_completed = tpm_tis_sysbus_request_completed;
>>> +    tc->get_version = tpm_tis_sysbus_get_tpm_version;
>>> +}
>>> +
>>> +static const TypeInfo tpm_tis_sysbus_info = {
>>> +    .name = TYPE_TPM_TIS_SYSBUS,
>>> +    .parent = TYPE_SYS_BUS_DEVICE,
>>> +    .instance_size = sizeof(TPMStateSysBus),
>>> +    .instance_init = tpm_tis_sysbus_initfn,
>>> +    .class_init  = tpm_tis_sysbus_class_init,
>>> +    .interfaces = (InterfaceInfo[]) {
>>> +        { TYPE_TPM_IF },
>>> +        { }
>>> +    }
>>> +};
>>> +
>>> +static void tpm_tis_sysbus_register(void)
>>> +{
>>> +    type_register_static(&tpm_tis_sysbus_info);
>>> +}
>>> +
>>> +type_init(tpm_tis_sysbus_register)
>>> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
>>> index 1691b92c28..f37851b1aa 100644
>>> --- a/include/sysemu/tpm.h
>>> +++ b/include/sysemu/tpm.h
>>> @@ -44,6 +44,7 @@ typedef struct TPMIfClass {
>>>   } TPMIfClass;
>>>     #define TYPE_TPM_TIS_ISA            "tpm-tis"
>>> +#define TYPE_TPM_TIS_SYSBUS         "tpm-tis-device"
>>
>>
>> hm, replace the rather generic 'device' with 'sysbus'?
> I used the "-device" suffix because this kind of naming was used for
> virtio-<type>-device when based on MMIO rather than virtio-<type>-pci.
> For instance virtio-net-device instead of virtio-net-pci. There are
> quite a lot of devices using that suffix. I only see xen-sysbus with the
> sysbus suffix.
> 
> Now personally I don't have any strong preference and I will pick up the
> name chosen by consensus.

Does anyone else have an opinion on the name to be chosen for this new
device:

1) tpm-tis-device or
2) tpm-tis-sysbus ?

Thanks

Eric

>>
>>
>> Otherwise looks really good.
> Thank you
> 
> Best Regards
> 
> Eric
>>
>>
>> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
>>
>>
>>>   #define TYPE_TPM_CRB                "tpm-crb"
>>>   #define TYPE_TPM_SPAPR              "tpm-spapr"
>>>   
>>
>>
> 
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 5/6] tpm: Add the SysBus TPM TIS device
  2020-02-25  9:18       ` Auger Eric
@ 2020-02-25  9:52         ` Ard Biesheuvel
  2020-02-25 10:18           ` Philippe Mathieu-Daudé
  2020-02-26  8:38           ` Auger Eric
  0 siblings, 2 replies; 30+ messages in thread
From: Ard Biesheuvel @ 2020-02-25  9:52 UTC (permalink / raw)
  To: Auger Eric
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	QEMU Developers, qemu-arm, Marc-André Lureau, Stefan Berger,
	Laszlo Ersek, Eric Auger

On Tue, 25 Feb 2020 at 10:19, Auger Eric <eric.auger@redhat.com> wrote:
>
> Hi,
>
> On 2/17/20 7:13 PM, Auger Eric wrote:
> > Hi Stefan,
> >
> > On 2/16/20 7:32 PM, Stefan Berger wrote:
> >> On 2/14/20 1:37 PM, Eric Auger wrote:
> >>> Introduce the tpm-tis-device which is a sysbus device
> >>> and is bound to be used on ARM.
> >>>
> >>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> >>> ---
> >>>   hw/tpm/Kconfig          |   5 ++
> >>>   hw/tpm/Makefile.objs    |   1 +
> >>>   hw/tpm/tpm_tis_sysbus.c | 159 ++++++++++++++++++++++++++++++++++++++++
> >>>   include/sysemu/tpm.h    |   1 +
> >>>   4 files changed, 166 insertions(+)
> >>>   create mode 100644 hw/tpm/tpm_tis_sysbus.c
> >>>
> >>> diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
> >>> index 686f8206bb..4794e7fe28 100644
> >>> --- a/hw/tpm/Kconfig
> >>> +++ b/hw/tpm/Kconfig
> >>> @@ -7,6 +7,11 @@ config TPM_TIS_ISA
> >>>       depends on TPM && ISA_BUS
> >>>       select TPM_TIS
> >>>   +config TPM_TIS_SYSBUS
> >>> +    bool
> >>> +    depends on TPM
> >>> +    select TPM_TIS
> >>> +
> >>>   config TPM_TIS
> >>>       bool
> >>>       depends on TPM
> >>> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
> >>> index 3ef2036cca..f1ec4beb95 100644
> >>> --- a/hw/tpm/Makefile.objs
> >>> +++ b/hw/tpm/Makefile.objs
> >>> @@ -1,6 +1,7 @@
> >>>   common-obj-$(CONFIG_TPM) += tpm_util.o
> >>>   obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
> >>>   common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
> >>> +common-obj-$(CONFIG_TPM_TIS_SYSBUS) += tpm_tis_sysbus.o
> >>>   common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
> >>>   common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
> >>>   common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
> >>> diff --git a/hw/tpm/tpm_tis_sysbus.c b/hw/tpm/tpm_tis_sysbus.c
> >>> new file mode 100644
> >>> index 0000000000..18c02aed67
> >>> --- /dev/null
> >>> +++ b/hw/tpm/tpm_tis_sysbus.c
> >>> @@ -0,0 +1,159 @@
> >>> +/*
> >>> + * tpm_tis_sysbus.c - QEMU's TPM TIS SYSBUS Device
> >>> + *
> >>> + * Copyright (C) 2006,2010-2013 IBM Corporation
> >>> + *
> >>> + * Authors:
> >>> + *  Stefan Berger <stefanb@us.ibm.com>
> >>> + *  David Safford <safford@us.ibm.com>
> >>> + *
> >>> + * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
> >>> + *
> >>> + * This work is licensed under the terms of the GNU GPL, version 2 or
> >>> later.
> >>> + * See the COPYING file in the top-level directory.
> >>> + *
> >>> + * Implementation of the TIS interface according to specs found at
> >>> + * http://www.trustedcomputinggroup.org. This implementation currently
> >>> + * supports version 1.3, 21 March 2013
> >>> + * In the developers menu choose the PC Client section then find the TIS
> >>> + * specification.
> >>> + *
> >>> + * TPM TIS for TPM 2 implementation following TCG PC Client Platform
> >>> + * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
> >>> + */
> >>> +
> >>> +#include "qemu/osdep.h"
> >>> +#include "hw/qdev-properties.h"
> >>> +#include "migration/vmstate.h"
> >>> +#include "tpm_util.h"
> >>> +#include "hw/sysbus.h"
> >>> +#include "tpm_tis.h"
> >>> +
> >>> +typedef struct TPMStateSysBus {
> >>> +    /*< private >*/
> >>> +    SysBusDevice parent_obj;
> >>> +
> >>> +    /*< public >*/
> >>> +    TPMState state; /* not a QOM object */
> >>> +} TPMStateSysBus;
> >>> +
> >>> +#define TPM_TIS_SYSBUS(obj) OBJECT_CHECK(TPMStateSysBus, (obj),
> >>> TYPE_TPM_TIS_SYSBUS)
> >>> +
> >>> +static int tpm_tis_pre_save_sysbus(void *opaque)
> >>> +{
> >>> +    TPMStateSysBus *sbdev = opaque;
> >>> +
> >>> +    return tpm_tis_pre_save(&sbdev->state);
> >>> +}
> >>> +
> >>> +static const VMStateDescription vmstate_tpm_tis_sysbus = {
> >>> +    .name = "tpm-tis",
> >>> +    .version_id = 0,
> >>> +    .pre_save  = tpm_tis_pre_save_sysbus,
> >>> +    .fields = (VMStateField[]) {
> >>> +        VMSTATE_BUFFER(state.buffer, TPMStateSysBus),
> >>> +        VMSTATE_UINT16(state.rw_offset, TPMStateSysBus),
> >>> +        VMSTATE_UINT8(state.active_locty, TPMStateSysBus),
> >>> +        VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus),
> >>> +        VMSTATE_UINT8(state.next_locty, TPMStateSysBus),
> >>> +
> >>> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus,
> >>> TPM_TIS_NUM_LOCALITIES,
> >>> +                             0, vmstate_locty, TPMLocality),
> >>> +
> >>> +        VMSTATE_END_OF_LIST()
> >>> +    }
> >>> +};
> >>> +
> >>> +static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret)
> >>> +{
> >>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
> >>> +    TPMState *s = &sbdev->state;
> >>> +
> >>> +    tpm_tis_request_completed(s, ret);
> >>> +}
> >>> +
> >>> +static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti)
> >>> +{
> >>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
> >>> +    TPMState *s = &sbdev->state;
> >>> +
> >>> +    return tpm_tis_get_tpm_version(s);
> >>> +}
> >>> +
> >>> +static void tpm_tis_sysbus_reset(DeviceState *dev)
> >>> +{
> >>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
> >>> +    TPMState *s = &sbdev->state;
> >>> +
> >>> +    return tpm_tis_reset(s);
> >>> +}
> >>> +
> >>> +static Property tpm_tis_sysbus_properties[] = {
> >>> +    DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num,
> >>> TPM_TIS_IRQ),
> >>> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
> >>> +    DEFINE_PROP_BOOL("ppi", TPMStateSysBus, state.ppi_enabled, true),
> >>> +    DEFINE_PROP_END_OF_LIST(),
> >>> +};
> >>> +
> >>> +static void tpm_tis_sysbus_initfn(Object *obj)
> >>> +{
> >>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
> >>> +    TPMState *s = &sbdev->state;
> >>> +
> >>> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
> >>> +                          s, "tpm-tis-mmio",
> >>> +                          TPM_TIS_NUM_LOCALITIES <<
> >>> TPM_TIS_LOCALITY_SHIFT);
> >>> +
> >>> +    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
> >>> +    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
> >>> +}
> >>> +
> >>> +static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
> >>> +{
> >>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
> >>> +    TPMState *s = &sbdev->state;
> >>> +
> >>> +    if (!tpm_find()) {
> >>> +        error_setg(errp, "at most one TPM device is permitted");
> >>> +        return;
> >>> +    }
> >>> +
> >>> +    if (!s->be_driver) {
> >>> +        error_setg(errp, "'tpmdev' property is required");
> >>> +        return;
> >>> +    }
> >>> +}
> >>> +
> >>> +static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
> >>> +{
> >>> +    DeviceClass *dc = DEVICE_CLASS(klass);
> >>> +    TPMIfClass *tc = TPM_IF_CLASS(klass);
> >>> +
> >>> +    device_class_set_props(dc, tpm_tis_sysbus_properties);
> >>> +    dc->vmsd  = &vmstate_tpm_tis_sysbus;
> >>> +    tc->model = TPM_MODEL_TPM_TIS;
> >>> +    dc->realize = tpm_tis_sysbus_realizefn;
> >>> +    dc->user_creatable = true;
> >>> +    dc->reset = tpm_tis_sysbus_reset;
> >>> +    tc->request_completed = tpm_tis_sysbus_request_completed;
> >>> +    tc->get_version = tpm_tis_sysbus_get_tpm_version;
> >>> +}
> >>> +
> >>> +static const TypeInfo tpm_tis_sysbus_info = {
> >>> +    .name = TYPE_TPM_TIS_SYSBUS,
> >>> +    .parent = TYPE_SYS_BUS_DEVICE,
> >>> +    .instance_size = sizeof(TPMStateSysBus),
> >>> +    .instance_init = tpm_tis_sysbus_initfn,
> >>> +    .class_init  = tpm_tis_sysbus_class_init,
> >>> +    .interfaces = (InterfaceInfo[]) {
> >>> +        { TYPE_TPM_IF },
> >>> +        { }
> >>> +    }
> >>> +};
> >>> +
> >>> +static void tpm_tis_sysbus_register(void)
> >>> +{
> >>> +    type_register_static(&tpm_tis_sysbus_info);
> >>> +}
> >>> +
> >>> +type_init(tpm_tis_sysbus_register)
> >>> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
> >>> index 1691b92c28..f37851b1aa 100644
> >>> --- a/include/sysemu/tpm.h
> >>> +++ b/include/sysemu/tpm.h
> >>> @@ -44,6 +44,7 @@ typedef struct TPMIfClass {
> >>>   } TPMIfClass;
> >>>     #define TYPE_TPM_TIS_ISA            "tpm-tis"
> >>> +#define TYPE_TPM_TIS_SYSBUS         "tpm-tis-device"
> >>
> >>
> >> hm, replace the rather generic 'device' with 'sysbus'?
> > I used the "-device" suffix because this kind of naming was used for
> > virtio-<type>-device when based on MMIO rather than virtio-<type>-pci.
> > For instance virtio-net-device instead of virtio-net-pci. There are
> > quite a lot of devices using that suffix. I only see xen-sysbus with the
> > sysbus suffix.
> >
> > Now personally I don't have any strong preference and I will pick up the
> > name chosen by consensus.
>
> Does anyone else have an opinion on the name to be chosen for this new
> device:
>
> 1) tpm-tis-device or
> 2) tpm-tis-sysbus ?
>

It is slightly unfortunate that we cannot retain the 'tpm-tis' name,
given that this is simply a TPM with a memory mapped TIS frame, like
the ISA one, and the fact that QEMU instantiates this differently
based on the emulated architecture is really an implementation detail.

So I prefer 'tpm-tis-device', since it doesn't define how it is backed
under the hood, and allows us to potentially instantiate it in a
different way in the future if we wanted to. Alternatively, something
like 'tpm-tis-mmmio' might be appropriate.

-- 
Ard.


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 4/6] tpm: Separate TPM_TIS and TPM_TIS_ISA configs
  2020-02-14 19:03   ` Philippe Mathieu-Daudé
@ 2020-02-25 10:05     ` Auger Eric
  0 siblings, 0 replies; 30+ messages in thread
From: Auger Eric @ 2020-02-25 10:05 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé,
	eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

Hi Philippe,

On 2/14/20 8:03 PM, Philippe Mathieu-Daudé wrote:
> On 2/14/20 7:37 PM, Eric Auger wrote:
>> Let's separate the compilation of tpm_tis_common.c from
>> the compilation of tpm_tis_isa.c
>>
>> The common part will be also compiled along with the
>> tpm_tis_sysbus device.
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>> ---
>>   default-configs/i386-softmmu.mak | 2 +-
>>   hw/i386/Kconfig                  | 2 +-
>>   hw/tpm/Kconfig                   | 7 ++++++-
>>   hw/tpm/Makefile.objs             | 3 ++-
>>   tests/qtest/Makefile.include     | 4 ++--
>>   5 files changed, 12 insertions(+), 6 deletions(-)
>>
>> diff --git a/default-configs/i386-softmmu.mak
>> b/default-configs/i386-softmmu.mak
>> index 4cc64dafa2..84d1a2487c 100644
>> --- a/default-configs/i386-softmmu.mak
>> +++ b/default-configs/i386-softmmu.mak
>> @@ -20,7 +20,7 @@
>>   #CONFIG_SGA=n
>>   #CONFIG_TEST_DEVICES=n
>>   #CONFIG_TPM_CRB=n
>> -#CONFIG_TPM_TIS=n
>> +#CONFIG_TPM_TIS_ISA=n
>>   #CONFIG_VTD=n
>>     # Boards:
>> diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig
>> index cdc851598c..c93f32f657 100644
>> --- a/hw/i386/Kconfig
>> +++ b/hw/i386/Kconfig
>> @@ -20,7 +20,7 @@ config PC
>>       imply SGA
>>       imply TEST_DEVICES
>>       imply TPM_CRB
>> -    imply TPM_TIS
>> +    imply TPM_TIS_ISA
>>       imply VGA_PCI
>>       imply VIRTIO_VGA
>>       select FDC
>> diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
>> index 9e67d990e8..686f8206bb 100644
>> --- a/hw/tpm/Kconfig
>> +++ b/hw/tpm/Kconfig
>> @@ -2,9 +2,14 @@ config TPMDEV
>>       bool
>>       depends on TPM
>>   -config TPM_TIS
>> +config TPM_TIS_ISA
>>       bool
>>       depends on TPM && ISA_BUS
> 
> Maybe you can relax to "depends on ISA_BUS" ...

No I am forced to keep it to pass
make docker-test-mingw@fedora

Otherwise I get:
make: *** No rule to make target 'x86_64-softmmu/config-devices.mak',
needed by 'config-all-devices.mak'.  Stop.
Traceback (most recent call last):

As we discussed earlier with mingw configure sets TPM to false.


> 
>> +    select TPM_TIS
>> +
>> +config TPM_TIS
>> +    bool
>> +    depends on TPM
> 
> ... since TPM_TIS depends on TPM.
> 
>>       select TPMDEV
>>     config TPM_CRB
>> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
>> index fcc4c2f27c..3ef2036cca 100644
>> --- a/hw/tpm/Makefile.objs
>> +++ b/hw/tpm/Makefile.objs
>> @@ -1,6 +1,7 @@
>>   common-obj-$(CONFIG_TPM) += tpm_util.o
>>   obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
>> -common-obj-$(CONFIG_TPM_TIS) += tpm_tis_isa.o tpm_tis_common.o
>> +common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
>> +common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
>>   common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
>>   common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
>>   common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o
>> diff --git a/tests/qtest/Makefile.include b/tests/qtest/Makefile.include
>> index eb0f23b108..33dd3c89cc 100644
>> --- a/tests/qtest/Makefile.include
>> +++ b/tests/qtest/Makefile.include
>> @@ -54,8 +54,8 @@ check-qtest-i386-y += q35-test
>>   check-qtest-i386-y += vmgenid-test
>>   check-qtest-i386-$(CONFIG_TPM_CRB) += tpm-crb-swtpm-test
>>   check-qtest-i386-$(CONFIG_TPM_CRB) += tpm-crb-test
>> -check-qtest-i386-$(CONFIG_TPM_TIS) += tpm-tis-swtpm-test
>> -check-qtest-i386-$(CONFIG_TPM_TIS) += tpm-tis-test
>> +check-qtest-i386-$(CONFIG_TPM_TIS_ISA) += tpm-tis-swtpm-test
>> +check-qtest-i386-$(CONFIG_TPM_TIS_ISA) += tpm-tis-test
>>   check-qtest-i386-$(CONFIG_SLIRP) += test-netfilter
>>   check-qtest-i386-$(CONFIG_POSIX) += test-filter-mirror
>>   check-qtest-i386-$(CONFIG_RTL8139_PCI) += test-filter-redirector
>>
> 
> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Thanks!

Eric
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA
  2020-02-14 18:55   ` Philippe Mathieu-Daudé
@ 2020-02-25 10:16     ` Auger Eric
  2020-02-25 10:22       ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 30+ messages in thread
From: Auger Eric @ 2020-02-25 10:16 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé,
	eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

Hi Phil,

On 2/14/20 7:55 PM, Philippe Mathieu-Daudé wrote:
> On 2/14/20 7:36 PM, Eric Auger wrote:
>> As we plan to introduce a sysbus TPM_TIS, let's rename
>> TPM_TIS into TPM_TIS_ISA.
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>> ---
>>   hw/i386/acpi-build.c | 6 +++---
>>   hw/tpm/tpm_tis.c     | 4 ++--
>>   include/sysemu/tpm.h | 6 +++---
>>   3 files changed, 8 insertions(+), 8 deletions(-)
>>
>> diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
>> index 9c4e46fa74..26777f8828 100644
>> --- a/hw/i386/acpi-build.c
>> +++ b/hw/i386/acpi-build.c
>> @@ -2026,7 +2026,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
>>           }
>>       }
>>   -    if (TPM_IS_TIS(tpm_find())) {
>> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>>           aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
>>                      TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
>>       }
>> @@ -2197,7 +2197,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
>>               /* Scan all PCI buses. Generate tables to support
>> hotplug. */
>>               build_append_pci_bus_devices(scope, bus,
>> pm->pcihp_bridge_en);
>>   -            if (TPM_IS_TIS(tpm)) {
>> +            if (TPM_IS_TIS_ISA(tpm)) {
>>                   if (misc->tpm_version == TPM_VERSION_2_0) {
>>                       dev = aml_device("TPM");
>>                       aml_append(dev, aml_name_decl("_HID",
>> @@ -2304,7 +2304,7 @@ build_tpm2(GArray *table_data, BIOSLinker
>> *linker, GArray *tcpalog)
>>           (char *)&tpm2_ptr->log_area_start_address - table_data->data;
>>         tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
>> -    if (TPM_IS_TIS(tpm_find())) {
>> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>>           tpm2_ptr->control_area_address = cpu_to_le64(0);
>>           tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
>>       } else if (TPM_IS_CRB(tpm_find())) {
>> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
>> index 31facb896d..c609737272 100644
>> --- a/hw/tpm/tpm_tis.c
>> +++ b/hw/tpm/tpm_tis.c
>> @@ -91,7 +91,7 @@ typedef struct TPMState {
>>       TPMPPI ppi;
>>   } TPMState;
>>   -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
>> +#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
>>     #define DEBUG_TIS 0
>>   @@ -1008,7 +1008,7 @@ static void tpm_tis_class_init(ObjectClass
>> *klass, void *data)
>>   }
>>     static const TypeInfo tpm_tis_info = {
>> -    .name = TYPE_TPM_TIS,
>> +    .name = TYPE_TPM_TIS_ISA,
>>       .parent = TYPE_ISA_DEVICE,
>>       .instance_size = sizeof(TPMState),
>>       .instance_init = tpm_tis_initfn,
>> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
>> index 15979a3647..1691b92c28 100644
>> --- a/include/sysemu/tpm.h
>> +++ b/include/sysemu/tpm.h
>> @@ -43,12 +43,12 @@ typedef struct TPMIfClass {
>>       enum TPMVersion (*get_version)(TPMIf *obj);
>>   } TPMIfClass;
>>   -#define TYPE_TPM_TIS                "tpm-tis"
>> +#define TYPE_TPM_TIS_ISA            "tpm-tis"
> 
> It should be safe to rename this "tpm-tis-isa" in this patch.
This would change the name of the legacy ISA device and also the way we
instantiate it through the cmd line. To avoid breaking the compatibility
I kept the same name and used tpm-tis-device (?) for the new sysbus one.

Thanks

Eric
> 
> Regardless:
> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> 
>>   #define TYPE_TPM_CRB                "tpm-crb"
>>   #define TYPE_TPM_SPAPR              "tpm-spapr"
>>   -#define TPM_IS_TIS(chr)                             \
>> -    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS)
>> +#define TPM_IS_TIS_ISA(chr)                         \
>> +    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
>>   #define TPM_IS_CRB(chr)                             \
>>       object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
>>   #define TPM_IS_SPAPR(chr)                           \
>>
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 2/6] tpm: Use TPMState as a common struct
  2020-02-17 18:01     ` Auger Eric
@ 2020-02-25 10:18       ` Auger Eric
  0 siblings, 0 replies; 30+ messages in thread
From: Auger Eric @ 2020-02-25 10:18 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé,
	eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

Hi Phil,

On 2/17/20 7:01 PM, Auger Eric wrote:
> Hi Philippe,
> 
> On 2/17/20 10:21 AM, Philippe Mathieu-Daudé wrote:
>> Hi Eric,
>>
>> On 2/14/20 7:37 PM, Eric Auger wrote:
>>> As we plan to introdce a SysBus TPM TIS device, let's
>>> make the TPMState a common struct usable by both the
>>> ISADevice and the SysBusDevice. TPMStateISA embeds the
>>> struct and inherits from the ISADevice.
>>>
>>> The prototype of functions bound to be used by both
>>> the ISA and SysBus devices is changed to take TPMState
>>> handle.
>>>
>>> A bunch of structs also are renamed to be specialized
>>> for the ISA device. Besides those transformations, no
>>> functional change is expected.
>>>
>>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>> ---
>>>   hw/tpm/tpm_tis.c | 146 +++++++++++++++++++++++++++++------------------
>>>   1 file changed, 91 insertions(+), 55 deletions(-)
>>>
>>> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
>>> index c609737272..fc6d7ca579 100644
>>> --- a/hw/tpm/tpm_tis.c
>>> +++ b/hw/tpm/tpm_tis.c
>>> @@ -65,7 +65,6 @@ typedef struct TPMLocality {
>>>   } TPMLocality;
>>>     typedef struct TPMState {
>>> -    ISADevice busdev;
>>>       MemoryRegion mmio;
>>>         unsigned char buffer[TPM_TIS_BUFFER_MAX];
>>> @@ -91,7 +90,15 @@ typedef struct TPMState {
>>>       TPMPPI ppi;
>>>   } TPMState;
>>>   -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
>>> +typedef struct TPMStateISA {
>>> +    /*< private >*/
>>> +    ISADevice parent_obj;
>>> +
>>> +    /*< public >*/
>>> +    TPMState state; /* not a QOM object */
>>> +} TPMStateISA;
>>> +
>>> +#define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj),
>>> TYPE_TPM_TIS_ISA)
>>>     #define DEBUG_TIS 0
>>>   @@ -281,9 +288,8 @@ static void tpm_tis_prep_abort(TPMState *s,
>>> uint8_t locty, uint8_t newlocty)
>>>   /*
>>>    * Callback from the TPM to indicate that the response was received.
>>>    */
>>> -static void tpm_tis_request_completed(TPMIf *ti, int ret)
>>> +static void tpm_tis_request_completed(TPMState *s, int ret)
>>>   {
>>> -    TPMState *s = TPM(ti);
>>>       uint8_t locty = s->cmd.locty;
>>>       uint8_t l;
>>>   @@ -338,7 +344,7 @@ static uint32_t tpm_tis_data_read(TPMState *s,
>>> uint8_t locty)
>>>   }
>>>     #ifdef DEBUG_TIS
>>> -static void tpm_tis_dump_state(void *opaque, hwaddr addr)
>>> +static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
>>>   {
>>>       static const unsigned regs[] = {
>>>           TPM_TIS_REG_ACCESS,
>>> @@ -353,7 +359,6 @@ static void tpm_tis_dump_state(void *opaque,
>>> hwaddr addr)
>>>       int idx;
>>>       uint8_t locty = tpm_tis_locality_from_addr(addr);
>>>       hwaddr base = addr & ~0xfff;
>>> -    TPMState *s = opaque;
>>>         printf("tpm_tis: active locality      : %d\n"
>>>              "tpm_tis: state of locality %d : %d\n"
>>> @@ -363,7 +368,7 @@ static void tpm_tis_dump_state(void *opaque,
>>> hwaddr addr)
>>>         for (idx = 0; regs[idx] != 0xfff; idx++) {
>>>           printf("tpm_tis: 0x%04x : 0x%08x\n", regs[idx],
>>> -               (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4));
>>> +               (int)tpm_tis_mmio_read(s, base + regs[idx], 4));
>>>       }
>>>         printf("tpm_tis: r/w offset    : %d\n"
>>> @@ -488,7 +493,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque,
>>> hwaddr addr,
>>>           break;
>>>   #ifdef DEBUG_TIS
>>>       case TPM_TIS_REG_DEBUG:
>>> -        tpm_tis_dump_state(opaque, addr);
>>> +        tpm_tis_dump_state(s, addr);
>>>           break;
>>>   #endif
>>>       }
>>> @@ -835,10 +840,8 @@ static const MemoryRegionOps tpm_tis_memory_ops = {
>>>   /*
>>>    * Get the TPMVersion of the backend device being used
>>>    */
>>> -static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
>>> +static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s)
>>>   {
>>> -    TPMState *s = TPM(ti);
>>> -
>>>       if (tpm_backend_had_startup_error(s->be_driver)) {
>>>           return TPM_VERSION_UNSPEC;
>>>       }
>>> @@ -850,9 +853,8 @@ static enum TPMVersion
>>> tpm_tis_get_tpm_version(TPMIf *ti)
>>>    * This function is called when the machine starts, resets or due to
>>>    * S3 resume.
>>>    */
>>> -static void tpm_tis_reset(DeviceState *dev)
>>> +static void tpm_tis_reset(TPMState *s)
>>>   {
>>> -    TPMState *s = TPM(dev);
>>>       int c;
>>>         s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver);
>>> @@ -896,15 +898,14 @@ static void tpm_tis_reset(DeviceState *dev)
>>>     /* persistent state handling */
>>>   -static int tpm_tis_pre_save(void *opaque)
>>> +static int tpm_tis_pre_save(TPMState *s)
>>>   {
>>> -    TPMState *s = opaque;
>>>       uint8_t locty = s->active_locty;
>>>         trace_tpm_tis_pre_save(locty, s->rw_offset);
>>>         if (DEBUG_TIS) {
>>> -        tpm_tis_dump_state(opaque, 0);
>>> +        tpm_tis_dump_state(s, 0);
>>>       }
>>>         /*
>>> @@ -929,34 +930,78 @@ static const VMStateDescription vmstate_locty = {
>>>       }
>>>   };
>>>   -static const VMStateDescription vmstate_tpm_tis = {
>>> +/* ISA */
>>> +
>>> +static int tpm_tis_pre_save_isa(void *opaque)
>>> +{
>>> +    TPMStateISA *isadev = opaque;
>>> +
>>> +    return tpm_tis_pre_save(&isadev->state);
>>> +}
>>> +
>>> +static const VMStateDescription vmstate_tpm_tis_isa = {
>>>       .name = "tpm-tis",
>>>       .version_id = 0,
>>> -    .pre_save  = tpm_tis_pre_save,
>>> +    .pre_save  = tpm_tis_pre_save_isa,
>>>       .fields = (VMStateField[]) {
>>> -        VMSTATE_BUFFER(buffer, TPMState),
>>> -        VMSTATE_UINT16(rw_offset, TPMState),
>>> -        VMSTATE_UINT8(active_locty, TPMState),
>>> -        VMSTATE_UINT8(aborting_locty, TPMState),
>>> -        VMSTATE_UINT8(next_locty, TPMState),
>>> +        VMSTATE_BUFFER(state.buffer, TPMStateISA),
>>> +        VMSTATE_UINT16(state.rw_offset, TPMStateISA),
>>> +        VMSTATE_UINT8(state.active_locty, TPMStateISA),
>>> +        VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
>>> +        VMSTATE_UINT8(state.next_locty, TPMStateISA),
>>
>> On a second thought these fields seem to belong to a TPMCommonState.
>> Why not QOM'ify an abstract common parent between ISA and SysBus?
> This would mean we have double QOM inheritance (ISADevice and Sysbus
> device already being QOM devices). As far as I know this is not
> supported (see qom/object.h). Here I use the same trick as the one being
> used in hw/vfio/platform or pci.c and also used in hw/block/fdc.c - as
> pointed out by Stefan- : see FDCtrlSysBus and FDCtrlISABus.
Does this justification make sense?

In the positive do you keep your R-b or shall I remove it?

Thanks

Eric
> 
> Thanks
> 
> Eric
>>
>>>   -        VMSTATE_STRUCT_ARRAY(loc, TPMState, TPM_TIS_NUM_LOCALITIES, 0,
>>> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA,
>>> TPM_TIS_NUM_LOCALITIES, 0,
>>>                                vmstate_locty, TPMLocality),
>>>             VMSTATE_END_OF_LIST()
>>>       }
>>>   };
>>>   -static Property tpm_tis_properties[] = {
>>> -    DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ),
>>> -    DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver),
>>> -    DEFINE_PROP_BOOL("ppi", TPMState, ppi_enabled, true),
>>> +static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
>>> +{
>>> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
>>> +    TPMState *s = &isadev->state;
>>> +
>>> +    tpm_tis_request_completed(s, ret);
>>> +}
>>> +
>>> +static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
>>> +{
>>> +    TPMStateISA *isadev = TPM_TIS_ISA(ti);
>>> +    TPMState *s = &isadev->state;
>>> +
>>> +    return tpm_tis_get_tpm_version(s);
>>> +}
>>> +
>>> +static void tpm_tis_isa_reset(DeviceState *dev)
>>> +{
>>> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
>>> +    TPMState *s = &isadev->state;
>>> +
>>> +    return tpm_tis_reset(s);
>>> +}
>>> +
>>> +static Property tpm_tis_isa_properties[] = {
>>> +    DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
>>> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
>>> +    DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
>>>       DEFINE_PROP_END_OF_LIST(),
>>>   };
>>>   -static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
>>> +static void tpm_tis_isa_initfn(Object *obj)
>>>   {
>>> -    TPMState *s = TPM(dev);
>>> +    TPMStateISA *isadev = TPM_TIS_ISA(obj);
>>> +    TPMState *s = &isadev->state;
>>> +
>>> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
>>> +                          s, "tpm-tis-mmio",
>>> +                          TPM_TIS_NUM_LOCALITIES <<
>>> TPM_TIS_LOCALITY_SHIFT);
>>> +}
>>> +
>>> +static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
>>> +{
>>> +    TPMStateISA *isadev = TPM_TIS_ISA(dev);
>>> +    TPMState *s = &isadev->state;
>>>         if (!tpm_find()) {
>>>           error_setg(errp, "at most one TPM device is permitted");
>>> @@ -973,55 +1018,46 @@ static void tpm_tis_realizefn(DeviceState *dev,
>>> Error **errp)
>>>           return;
>>>       }
>>>   -    isa_init_irq(&s->busdev, &s->irq, s->irq_num);
>>> +    isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
>>>         memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
>>>                                   TPM_TIS_ADDR_BASE, &s->mmio);
>>>         if (s->ppi_enabled) {
>>>           tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
>>> -                     TPM_PPI_ADDR_BASE, OBJECT(s));
>>> +                     TPM_PPI_ADDR_BASE, OBJECT(dev));
>>>       }
>>>   }
>>>   -static void tpm_tis_initfn(Object *obj)
>>> -{
>>> -    TPMState *s = TPM(obj);
>>> -
>>> -    memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
>>> -                          s, "tpm-tis-mmio",
>>> -                          TPM_TIS_NUM_LOCALITIES <<
>>> TPM_TIS_LOCALITY_SHIFT);
>>> -}
>>> -
>>> -static void tpm_tis_class_init(ObjectClass *klass, void *data)
>>> +static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
>>>   {
>>>       DeviceClass *dc = DEVICE_CLASS(klass);
>>>       TPMIfClass *tc = TPM_IF_CLASS(klass);
>>>   -    dc->realize = tpm_tis_realizefn;
>>> -    device_class_set_props(dc, tpm_tis_properties);
>>> -    dc->reset = tpm_tis_reset;
>>> -    dc->vmsd  = &vmstate_tpm_tis;
>>> +    device_class_set_props(dc, tpm_tis_isa_properties);
>>> +    dc->vmsd  = &vmstate_tpm_tis_isa;
>>>       tc->model = TPM_MODEL_TPM_TIS;
>>> -    tc->get_version = tpm_tis_get_tpm_version;
>>> -    tc->request_completed = tpm_tis_request_completed;
>>> +    dc->realize = tpm_tis_isa_realizefn;
>>> +    dc->reset = tpm_tis_isa_reset;
>>> +    tc->request_completed = tpm_tis_isa_request_completed;
>>> +    tc->get_version = tpm_tis_isa_get_tpm_version;
>>>   }
>>>   -static const TypeInfo tpm_tis_info = {
>>> +static const TypeInfo tpm_tis_isa_info = {
>>>       .name = TYPE_TPM_TIS_ISA,
>>>       .parent = TYPE_ISA_DEVICE,
>>> -    .instance_size = sizeof(TPMState),
>>> -    .instance_init = tpm_tis_initfn,
>>> -    .class_init  = tpm_tis_class_init,
>>> +    .instance_size = sizeof(TPMStateISA),
>>> +    .instance_init = tpm_tis_isa_initfn,
>>> +    .class_init  = tpm_tis_isa_class_init,
>>>       .interfaces = (InterfaceInfo[]) {
>>>           { TYPE_TPM_IF },
>>>           { }
>>>       }
>>>   };
>>>   -static void tpm_tis_register(void)
>>> +static void tpm_tis_isa_register(void)
>>>   {
>>> -    type_register_static(&tpm_tis_info);
>>> +    type_register_static(&tpm_tis_isa_info);
>>>   }
>>>   -type_init(tpm_tis_register)
>>> +type_init(tpm_tis_isa_register)
>>>
>>



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 5/6] tpm: Add the SysBus TPM TIS device
  2020-02-25  9:52         ` Ard Biesheuvel
@ 2020-02-25 10:18           ` Philippe Mathieu-Daudé
  2020-02-26  8:38           ` Auger Eric
  1 sibling, 0 replies; 30+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-02-25 10:18 UTC (permalink / raw)
  To: Ard Biesheuvel, Auger Eric
  Cc: Peter Maydell, QEMU Developers, qemu-arm, Marc-André Lureau,
	Stefan Berger, Laszlo Ersek, Eric Auger

On 2/25/20 10:52 AM, Ard Biesheuvel wrote:
> On Tue, 25 Feb 2020 at 10:19, Auger Eric <eric.auger@redhat.com> wrote:
>>
>> Hi,
>>
>> On 2/17/20 7:13 PM, Auger Eric wrote:
>>> Hi Stefan,
>>>
>>> On 2/16/20 7:32 PM, Stefan Berger wrote:
>>>> On 2/14/20 1:37 PM, Eric Auger wrote:
>>>>> Introduce the tpm-tis-device which is a sysbus device
>>>>> and is bound to be used on ARM.
>>>>>
>>>>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>>>> ---
>>>>>    hw/tpm/Kconfig          |   5 ++
>>>>>    hw/tpm/Makefile.objs    |   1 +
>>>>>    hw/tpm/tpm_tis_sysbus.c | 159 ++++++++++++++++++++++++++++++++++++++++
>>>>>    include/sysemu/tpm.h    |   1 +
>>>>>    4 files changed, 166 insertions(+)
>>>>>    create mode 100644 hw/tpm/tpm_tis_sysbus.c
>>>>>
>>>>> diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
>>>>> index 686f8206bb..4794e7fe28 100644
>>>>> --- a/hw/tpm/Kconfig
>>>>> +++ b/hw/tpm/Kconfig
>>>>> @@ -7,6 +7,11 @@ config TPM_TIS_ISA
>>>>>        depends on TPM && ISA_BUS
>>>>>        select TPM_TIS
>>>>>    +config TPM_TIS_SYSBUS
>>>>> +    bool
>>>>> +    depends on TPM
>>>>> +    select TPM_TIS
>>>>> +
>>>>>    config TPM_TIS
>>>>>        bool
>>>>>        depends on TPM
>>>>> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
>>>>> index 3ef2036cca..f1ec4beb95 100644
>>>>> --- a/hw/tpm/Makefile.objs
>>>>> +++ b/hw/tpm/Makefile.objs
>>>>> @@ -1,6 +1,7 @@
>>>>>    common-obj-$(CONFIG_TPM) += tpm_util.o
>>>>>    obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
>>>>>    common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
>>>>> +common-obj-$(CONFIG_TPM_TIS_SYSBUS) += tpm_tis_sysbus.o
>>>>>    common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
>>>>>    common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
>>>>>    common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
>>>>> diff --git a/hw/tpm/tpm_tis_sysbus.c b/hw/tpm/tpm_tis_sysbus.c
>>>>> new file mode 100644
>>>>> index 0000000000..18c02aed67
>>>>> --- /dev/null
>>>>> +++ b/hw/tpm/tpm_tis_sysbus.c
>>>>> @@ -0,0 +1,159 @@
>>>>> +/*
>>>>> + * tpm_tis_sysbus.c - QEMU's TPM TIS SYSBUS Device
>>>>> + *
>>>>> + * Copyright (C) 2006,2010-2013 IBM Corporation
>>>>> + *
>>>>> + * Authors:
>>>>> + *  Stefan Berger <stefanb@us.ibm.com>
>>>>> + *  David Safford <safford@us.ibm.com>
>>>>> + *
>>>>> + * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
>>>>> + *
>>>>> + * This work is licensed under the terms of the GNU GPL, version 2 or
>>>>> later.
>>>>> + * See the COPYING file in the top-level directory.
>>>>> + *
>>>>> + * Implementation of the TIS interface according to specs found at
>>>>> + * http://www.trustedcomputinggroup.org. This implementation currently
>>>>> + * supports version 1.3, 21 March 2013
>>>>> + * In the developers menu choose the PC Client section then find the TIS
>>>>> + * specification.
>>>>> + *
>>>>> + * TPM TIS for TPM 2 implementation following TCG PC Client Platform
>>>>> + * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
>>>>> + */
>>>>> +
>>>>> +#include "qemu/osdep.h"
>>>>> +#include "hw/qdev-properties.h"
>>>>> +#include "migration/vmstate.h"
>>>>> +#include "tpm_util.h"
>>>>> +#include "hw/sysbus.h"
>>>>> +#include "tpm_tis.h"
>>>>> +
>>>>> +typedef struct TPMStateSysBus {
>>>>> +    /*< private >*/
>>>>> +    SysBusDevice parent_obj;
>>>>> +
>>>>> +    /*< public >*/
>>>>> +    TPMState state; /* not a QOM object */
>>>>> +} TPMStateSysBus;
>>>>> +
>>>>> +#define TPM_TIS_SYSBUS(obj) OBJECT_CHECK(TPMStateSysBus, (obj),
>>>>> TYPE_TPM_TIS_SYSBUS)
>>>>> +
>>>>> +static int tpm_tis_pre_save_sysbus(void *opaque)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = opaque;
>>>>> +
>>>>> +    return tpm_tis_pre_save(&sbdev->state);
>>>>> +}
>>>>> +
>>>>> +static const VMStateDescription vmstate_tpm_tis_sysbus = {
>>>>> +    .name = "tpm-tis",
>>>>> +    .version_id = 0,
>>>>> +    .pre_save  = tpm_tis_pre_save_sysbus,
>>>>> +    .fields = (VMStateField[]) {
>>>>> +        VMSTATE_BUFFER(state.buffer, TPMStateSysBus),
>>>>> +        VMSTATE_UINT16(state.rw_offset, TPMStateSysBus),
>>>>> +        VMSTATE_UINT8(state.active_locty, TPMStateSysBus),
>>>>> +        VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus),
>>>>> +        VMSTATE_UINT8(state.next_locty, TPMStateSysBus),
>>>>> +
>>>>> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus,
>>>>> TPM_TIS_NUM_LOCALITIES,
>>>>> +                             0, vmstate_locty, TPMLocality),
>>>>> +
>>>>> +        VMSTATE_END_OF_LIST()
>>>>> +    }
>>>>> +};
>>>>> +
>>>>> +static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    tpm_tis_request_completed(s, ret);
>>>>> +}
>>>>> +
>>>>> +static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    return tpm_tis_get_tpm_version(s);
>>>>> +}
>>>>> +
>>>>> +static void tpm_tis_sysbus_reset(DeviceState *dev)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    return tpm_tis_reset(s);
>>>>> +}
>>>>> +
>>>>> +static Property tpm_tis_sysbus_properties[] = {
>>>>> +    DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num,
>>>>> TPM_TIS_IRQ),
>>>>> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
>>>>> +    DEFINE_PROP_BOOL("ppi", TPMStateSysBus, state.ppi_enabled, true),
>>>>> +    DEFINE_PROP_END_OF_LIST(),
>>>>> +};
>>>>> +
>>>>> +static void tpm_tis_sysbus_initfn(Object *obj)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
>>>>> +                          s, "tpm-tis-mmio",
>>>>> +                          TPM_TIS_NUM_LOCALITIES <<
>>>>> TPM_TIS_LOCALITY_SHIFT);
>>>>> +
>>>>> +    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
>>>>> +    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
>>>>> +}
>>>>> +
>>>>> +static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    if (!tpm_find()) {
>>>>> +        error_setg(errp, "at most one TPM device is permitted");
>>>>> +        return;
>>>>> +    }
>>>>> +
>>>>> +    if (!s->be_driver) {
>>>>> +        error_setg(errp, "'tpmdev' property is required");
>>>>> +        return;
>>>>> +    }
>>>>> +}
>>>>> +
>>>>> +static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
>>>>> +{
>>>>> +    DeviceClass *dc = DEVICE_CLASS(klass);
>>>>> +    TPMIfClass *tc = TPM_IF_CLASS(klass);
>>>>> +
>>>>> +    device_class_set_props(dc, tpm_tis_sysbus_properties);
>>>>> +    dc->vmsd  = &vmstate_tpm_tis_sysbus;
>>>>> +    tc->model = TPM_MODEL_TPM_TIS;
>>>>> +    dc->realize = tpm_tis_sysbus_realizefn;
>>>>> +    dc->user_creatable = true;
>>>>> +    dc->reset = tpm_tis_sysbus_reset;
>>>>> +    tc->request_completed = tpm_tis_sysbus_request_completed;
>>>>> +    tc->get_version = tpm_tis_sysbus_get_tpm_version;
>>>>> +}
>>>>> +
>>>>> +static const TypeInfo tpm_tis_sysbus_info = {
>>>>> +    .name = TYPE_TPM_TIS_SYSBUS,
>>>>> +    .parent = TYPE_SYS_BUS_DEVICE,
>>>>> +    .instance_size = sizeof(TPMStateSysBus),
>>>>> +    .instance_init = tpm_tis_sysbus_initfn,
>>>>> +    .class_init  = tpm_tis_sysbus_class_init,
>>>>> +    .interfaces = (InterfaceInfo[]) {
>>>>> +        { TYPE_TPM_IF },
>>>>> +        { }
>>>>> +    }
>>>>> +};
>>>>> +
>>>>> +static void tpm_tis_sysbus_register(void)
>>>>> +{
>>>>> +    type_register_static(&tpm_tis_sysbus_info);
>>>>> +}
>>>>> +
>>>>> +type_init(tpm_tis_sysbus_register)
>>>>> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
>>>>> index 1691b92c28..f37851b1aa 100644
>>>>> --- a/include/sysemu/tpm.h
>>>>> +++ b/include/sysemu/tpm.h
>>>>> @@ -44,6 +44,7 @@ typedef struct TPMIfClass {
>>>>>    } TPMIfClass;
>>>>>      #define TYPE_TPM_TIS_ISA            "tpm-tis"
>>>>> +#define TYPE_TPM_TIS_SYSBUS         "tpm-tis-device"
>>>>
>>>>
>>>> hm, replace the rather generic 'device' with 'sysbus'?
>>> I used the "-device" suffix because this kind of naming was used for
>>> virtio-<type>-device when based on MMIO rather than virtio-<type>-pci.
>>> For instance virtio-net-device instead of virtio-net-pci. There are
>>> quite a lot of devices using that suffix. I only see xen-sysbus with the
>>> sysbus suffix.
>>>
>>> Now personally I don't have any strong preference and I will pick up the
>>> name chosen by consensus.
>>
>> Does anyone else have an opinion on the name to be chosen for this new
>> device:
>>
>> 1) tpm-tis-device or
>> 2) tpm-tis-sysbus ?
>>
> 
> It is slightly unfortunate that we cannot retain the 'tpm-tis' name,
> given that this is simply a TPM with a memory mapped TIS frame, like
> the ISA one, and the fact that QEMU instantiates this differently
> based on the emulated architecture is really an implementation detail.

Agreed.

As long as we don't change vmstate_tpm_tis::name, we can rename the QOM 
type names.

So I'd rather rename ISA as:

   #define TYPE_TPM_TIS_ISA            "tpm-tis-isa"

And the generic name for the sysbus/mmio device:

   #define TYPE_TPM_TIS_SYSBUS         "tpm-tis"

If we add the common ancestor, we can name it "tpm-tis-common".

> So I prefer 'tpm-tis-device', since it doesn't define how it is backed
> under the hood, and allows us to potentially instantiate it in a
> different way in the future if we wanted to. Alternatively, something
> like 'tpm-tis-mmmio' might be appropriate.



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA
  2020-02-25 10:16     ` Auger Eric
@ 2020-02-25 10:22       ` Philippe Mathieu-Daudé
  2020-02-25 10:28         ` Auger Eric
  0 siblings, 1 reply; 30+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-02-25 10:22 UTC (permalink / raw)
  To: Auger Eric, eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

On 2/25/20 11:16 AM, Auger Eric wrote:
> Hi Phil,
> 
> On 2/14/20 7:55 PM, Philippe Mathieu-Daudé wrote:
>> On 2/14/20 7:36 PM, Eric Auger wrote:
>>> As we plan to introduce a sysbus TPM_TIS, let's rename
>>> TPM_TIS into TPM_TIS_ISA.
>>>
>>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>> ---
>>>    hw/i386/acpi-build.c | 6 +++---
>>>    hw/tpm/tpm_tis.c     | 4 ++--
>>>    include/sysemu/tpm.h | 6 +++---
>>>    3 files changed, 8 insertions(+), 8 deletions(-)
>>>
>>> diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
>>> index 9c4e46fa74..26777f8828 100644
>>> --- a/hw/i386/acpi-build.c
>>> +++ b/hw/i386/acpi-build.c
>>> @@ -2026,7 +2026,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
>>>            }
>>>        }
>>>    -    if (TPM_IS_TIS(tpm_find())) {
>>> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>>>            aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
>>>                       TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
>>>        }
>>> @@ -2197,7 +2197,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
>>>                /* Scan all PCI buses. Generate tables to support
>>> hotplug. */
>>>                build_append_pci_bus_devices(scope, bus,
>>> pm->pcihp_bridge_en);
>>>    -            if (TPM_IS_TIS(tpm)) {
>>> +            if (TPM_IS_TIS_ISA(tpm)) {
>>>                    if (misc->tpm_version == TPM_VERSION_2_0) {
>>>                        dev = aml_device("TPM");
>>>                        aml_append(dev, aml_name_decl("_HID",
>>> @@ -2304,7 +2304,7 @@ build_tpm2(GArray *table_data, BIOSLinker
>>> *linker, GArray *tcpalog)
>>>            (char *)&tpm2_ptr->log_area_start_address - table_data->data;
>>>          tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
>>> -    if (TPM_IS_TIS(tpm_find())) {
>>> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>>>            tpm2_ptr->control_area_address = cpu_to_le64(0);
>>>            tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
>>>        } else if (TPM_IS_CRB(tpm_find())) {
>>> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
>>> index 31facb896d..c609737272 100644
>>> --- a/hw/tpm/tpm_tis.c
>>> +++ b/hw/tpm/tpm_tis.c
>>> @@ -91,7 +91,7 @@ typedef struct TPMState {
>>>        TPMPPI ppi;
>>>    } TPMState;
>>>    -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
>>> +#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
>>>      #define DEBUG_TIS 0
>>>    @@ -1008,7 +1008,7 @@ static void tpm_tis_class_init(ObjectClass
>>> *klass, void *data)
>>>    }
>>>      static const TypeInfo tpm_tis_info = {
>>> -    .name = TYPE_TPM_TIS,
>>> +    .name = TYPE_TPM_TIS_ISA,
>>>        .parent = TYPE_ISA_DEVICE,
>>>        .instance_size = sizeof(TPMState),
>>>        .instance_init = tpm_tis_initfn,
>>> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
>>> index 15979a3647..1691b92c28 100644
>>> --- a/include/sysemu/tpm.h
>>> +++ b/include/sysemu/tpm.h
>>> @@ -43,12 +43,12 @@ typedef struct TPMIfClass {
>>>        enum TPMVersion (*get_version)(TPMIf *obj);
>>>    } TPMIfClass;
>>>    -#define TYPE_TPM_TIS                "tpm-tis"
>>> +#define TYPE_TPM_TIS_ISA            "tpm-tis"
>>
>> It should be safe to rename this "tpm-tis-isa" in this patch.
> This would change the name of the legacy ISA device and also the way we
> instantiate it through the cmd line. To avoid breaking the compatibility
> I kept the same name and used tpm-tis-device (?) for the new sysbus one.

I thought ISA devices were not user-creatable...

> 
> Thanks
> 
> Eric
>>
>> Regardless:
>> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
>>
>>>    #define TYPE_TPM_CRB                "tpm-crb"
>>>    #define TYPE_TPM_SPAPR              "tpm-spapr"
>>>    -#define TPM_IS_TIS(chr)                             \
>>> -    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS)
>>> +#define TPM_IS_TIS_ISA(chr)                         \
>>> +    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
>>>    #define TPM_IS_CRB(chr)                             \
>>>        object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
>>>    #define TPM_IS_SPAPR(chr)                           \
>>>
>>
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA
  2020-02-25 10:22       ` Philippe Mathieu-Daudé
@ 2020-02-25 10:28         ` Auger Eric
  0 siblings, 0 replies; 30+ messages in thread
From: Auger Eric @ 2020-02-25 10:28 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé,
	eric.auger.pro, stefanb, qemu-devel, qemu-arm, peter.maydell
  Cc: marcandre.lureau, lersek, ardb

Hi Phil,

On 2/25/20 11:22 AM, Philippe Mathieu-Daudé wrote:
> On 2/25/20 11:16 AM, Auger Eric wrote:
>> Hi Phil,
>>
>> On 2/14/20 7:55 PM, Philippe Mathieu-Daudé wrote:
>>> On 2/14/20 7:36 PM, Eric Auger wrote:
>>>> As we plan to introduce a sysbus TPM_TIS, let's rename
>>>> TPM_TIS into TPM_TIS_ISA.
>>>>
>>>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>>> ---
>>>>    hw/i386/acpi-build.c | 6 +++---
>>>>    hw/tpm/tpm_tis.c     | 4 ++--
>>>>    include/sysemu/tpm.h | 6 +++---
>>>>    3 files changed, 8 insertions(+), 8 deletions(-)
>>>>
>>>> diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
>>>> index 9c4e46fa74..26777f8828 100644
>>>> --- a/hw/i386/acpi-build.c
>>>> +++ b/hw/i386/acpi-build.c
>>>> @@ -2026,7 +2026,7 @@ build_dsdt(GArray *table_data, BIOSLinker
>>>> *linker,
>>>>            }
>>>>        }
>>>>    -    if (TPM_IS_TIS(tpm_find())) {
>>>> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>>>>            aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
>>>>                       TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
>>>>        }
>>>> @@ -2197,7 +2197,7 @@ build_dsdt(GArray *table_data, BIOSLinker
>>>> *linker,
>>>>                /* Scan all PCI buses. Generate tables to support
>>>> hotplug. */
>>>>                build_append_pci_bus_devices(scope, bus,
>>>> pm->pcihp_bridge_en);
>>>>    -            if (TPM_IS_TIS(tpm)) {
>>>> +            if (TPM_IS_TIS_ISA(tpm)) {
>>>>                    if (misc->tpm_version == TPM_VERSION_2_0) {
>>>>                        dev = aml_device("TPM");
>>>>                        aml_append(dev, aml_name_decl("_HID",
>>>> @@ -2304,7 +2304,7 @@ build_tpm2(GArray *table_data, BIOSLinker
>>>> *linker, GArray *tcpalog)
>>>>            (char *)&tpm2_ptr->log_area_start_address -
>>>> table_data->data;
>>>>          tpm2_ptr->platform_class =
>>>> cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
>>>> -    if (TPM_IS_TIS(tpm_find())) {
>>>> +    if (TPM_IS_TIS_ISA(tpm_find())) {
>>>>            tpm2_ptr->control_area_address = cpu_to_le64(0);
>>>>            tpm2_ptr->start_method =
>>>> cpu_to_le32(TPM2_START_METHOD_MMIO);
>>>>        } else if (TPM_IS_CRB(tpm_find())) {
>>>> diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
>>>> index 31facb896d..c609737272 100644
>>>> --- a/hw/tpm/tpm_tis.c
>>>> +++ b/hw/tpm/tpm_tis.c
>>>> @@ -91,7 +91,7 @@ typedef struct TPMState {
>>>>        TPMPPI ppi;
>>>>    } TPMState;
>>>>    -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
>>>> +#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA)
>>>>      #define DEBUG_TIS 0
>>>>    @@ -1008,7 +1008,7 @@ static void tpm_tis_class_init(ObjectClass
>>>> *klass, void *data)
>>>>    }
>>>>      static const TypeInfo tpm_tis_info = {
>>>> -    .name = TYPE_TPM_TIS,
>>>> +    .name = TYPE_TPM_TIS_ISA,
>>>>        .parent = TYPE_ISA_DEVICE,
>>>>        .instance_size = sizeof(TPMState),
>>>>        .instance_init = tpm_tis_initfn,
>>>> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
>>>> index 15979a3647..1691b92c28 100644
>>>> --- a/include/sysemu/tpm.h
>>>> +++ b/include/sysemu/tpm.h
>>>> @@ -43,12 +43,12 @@ typedef struct TPMIfClass {
>>>>        enum TPMVersion (*get_version)(TPMIf *obj);
>>>>    } TPMIfClass;
>>>>    -#define TYPE_TPM_TIS                "tpm-tis"
>>>> +#define TYPE_TPM_TIS_ISA            "tpm-tis"
>>>
>>> It should be safe to rename this "tpm-tis-isa" in this patch.
>> This would change the name of the legacy ISA device and also the way we
>> instantiate it through the cmd line. To avoid breaking the compatibility
>> I kept the same name and used tpm-tis-device (?) for the new sysbus one.
> 
> I thought ISA devices were not user-creatable...

See docs/specs/tpm.rst and the cmd line used for x86:

    ../..
    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
    -tpmdev emulator,id=tpm0,chardev=chrtpm \
    -device tpm-tis,tpmdev=tpm0 test.img

Thanks

Eric
> 
>>
>> Thanks
>>
>> Eric
>>>
>>> Regardless:
>>> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
>>>
>>>>    #define TYPE_TPM_CRB                "tpm-crb"
>>>>    #define TYPE_TPM_SPAPR              "tpm-spapr"
>>>>    -#define TPM_IS_TIS(chr)                             \
>>>> -    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS)
>>>> +#define TPM_IS_TIS_ISA(chr)                         \
>>>> +    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
>>>>    #define TPM_IS_CRB(chr)                             \
>>>>        object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
>>>>    #define TPM_IS_SPAPR(chr)                           \
>>>>
>>>
>>
> 
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC v2 5/6] tpm: Add the SysBus TPM TIS device
  2020-02-25  9:52         ` Ard Biesheuvel
  2020-02-25 10:18           ` Philippe Mathieu-Daudé
@ 2020-02-26  8:38           ` Auger Eric
  1 sibling, 0 replies; 30+ messages in thread
From: Auger Eric @ 2020-02-26  8:38 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	QEMU Developers, qemu-arm, Marc-André Lureau, Stefan Berger,
	Laszlo Ersek, Eric Auger

Hi Ard,

On 2/25/20 10:52 AM, Ard Biesheuvel wrote:
> On Tue, 25 Feb 2020 at 10:19, Auger Eric <eric.auger@redhat.com> wrote:
>>
>> Hi,
>>
>> On 2/17/20 7:13 PM, Auger Eric wrote:
>>> Hi Stefan,
>>>
>>> On 2/16/20 7:32 PM, Stefan Berger wrote:
>>>> On 2/14/20 1:37 PM, Eric Auger wrote:
>>>>> Introduce the tpm-tis-device which is a sysbus device
>>>>> and is bound to be used on ARM.
>>>>>
>>>>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>>>> ---
>>>>>   hw/tpm/Kconfig          |   5 ++
>>>>>   hw/tpm/Makefile.objs    |   1 +
>>>>>   hw/tpm/tpm_tis_sysbus.c | 159 ++++++++++++++++++++++++++++++++++++++++
>>>>>   include/sysemu/tpm.h    |   1 +
>>>>>   4 files changed, 166 insertions(+)
>>>>>   create mode 100644 hw/tpm/tpm_tis_sysbus.c
>>>>>
>>>>> diff --git a/hw/tpm/Kconfig b/hw/tpm/Kconfig
>>>>> index 686f8206bb..4794e7fe28 100644
>>>>> --- a/hw/tpm/Kconfig
>>>>> +++ b/hw/tpm/Kconfig
>>>>> @@ -7,6 +7,11 @@ config TPM_TIS_ISA
>>>>>       depends on TPM && ISA_BUS
>>>>>       select TPM_TIS
>>>>>   +config TPM_TIS_SYSBUS
>>>>> +    bool
>>>>> +    depends on TPM
>>>>> +    select TPM_TIS
>>>>> +
>>>>>   config TPM_TIS
>>>>>       bool
>>>>>       depends on TPM
>>>>> diff --git a/hw/tpm/Makefile.objs b/hw/tpm/Makefile.objs
>>>>> index 3ef2036cca..f1ec4beb95 100644
>>>>> --- a/hw/tpm/Makefile.objs
>>>>> +++ b/hw/tpm/Makefile.objs
>>>>> @@ -1,6 +1,7 @@
>>>>>   common-obj-$(CONFIG_TPM) += tpm_util.o
>>>>>   obj-$(call lor,$(CONFIG_TPM_TIS),$(CONFIG_TPM_CRB)) += tpm_ppi.o
>>>>>   common-obj-$(CONFIG_TPM_TIS_ISA) += tpm_tis_isa.o
>>>>> +common-obj-$(CONFIG_TPM_TIS_SYSBUS) += tpm_tis_sysbus.o
>>>>>   common-obj-$(CONFIG_TPM_TIS) += tpm_tis_common.o
>>>>>   common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o
>>>>>   common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
>>>>> diff --git a/hw/tpm/tpm_tis_sysbus.c b/hw/tpm/tpm_tis_sysbus.c
>>>>> new file mode 100644
>>>>> index 0000000000..18c02aed67
>>>>> --- /dev/null
>>>>> +++ b/hw/tpm/tpm_tis_sysbus.c
>>>>> @@ -0,0 +1,159 @@
>>>>> +/*
>>>>> + * tpm_tis_sysbus.c - QEMU's TPM TIS SYSBUS Device
>>>>> + *
>>>>> + * Copyright (C) 2006,2010-2013 IBM Corporation
>>>>> + *
>>>>> + * Authors:
>>>>> + *  Stefan Berger <stefanb@us.ibm.com>
>>>>> + *  David Safford <safford@us.ibm.com>
>>>>> + *
>>>>> + * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
>>>>> + *
>>>>> + * This work is licensed under the terms of the GNU GPL, version 2 or
>>>>> later.
>>>>> + * See the COPYING file in the top-level directory.
>>>>> + *
>>>>> + * Implementation of the TIS interface according to specs found at
>>>>> + * http://www.trustedcomputinggroup.org. This implementation currently
>>>>> + * supports version 1.3, 21 March 2013
>>>>> + * In the developers menu choose the PC Client section then find the TIS
>>>>> + * specification.
>>>>> + *
>>>>> + * TPM TIS for TPM 2 implementation following TCG PC Client Platform
>>>>> + * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
>>>>> + */
>>>>> +
>>>>> +#include "qemu/osdep.h"
>>>>> +#include "hw/qdev-properties.h"
>>>>> +#include "migration/vmstate.h"
>>>>> +#include "tpm_util.h"
>>>>> +#include "hw/sysbus.h"
>>>>> +#include "tpm_tis.h"
>>>>> +
>>>>> +typedef struct TPMStateSysBus {
>>>>> +    /*< private >*/
>>>>> +    SysBusDevice parent_obj;
>>>>> +
>>>>> +    /*< public >*/
>>>>> +    TPMState state; /* not a QOM object */
>>>>> +} TPMStateSysBus;
>>>>> +
>>>>> +#define TPM_TIS_SYSBUS(obj) OBJECT_CHECK(TPMStateSysBus, (obj),
>>>>> TYPE_TPM_TIS_SYSBUS)
>>>>> +
>>>>> +static int tpm_tis_pre_save_sysbus(void *opaque)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = opaque;
>>>>> +
>>>>> +    return tpm_tis_pre_save(&sbdev->state);
>>>>> +}
>>>>> +
>>>>> +static const VMStateDescription vmstate_tpm_tis_sysbus = {
>>>>> +    .name = "tpm-tis",
>>>>> +    .version_id = 0,
>>>>> +    .pre_save  = tpm_tis_pre_save_sysbus,
>>>>> +    .fields = (VMStateField[]) {
>>>>> +        VMSTATE_BUFFER(state.buffer, TPMStateSysBus),
>>>>> +        VMSTATE_UINT16(state.rw_offset, TPMStateSysBus),
>>>>> +        VMSTATE_UINT8(state.active_locty, TPMStateSysBus),
>>>>> +        VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus),
>>>>> +        VMSTATE_UINT8(state.next_locty, TPMStateSysBus),
>>>>> +
>>>>> +        VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus,
>>>>> TPM_TIS_NUM_LOCALITIES,
>>>>> +                             0, vmstate_locty, TPMLocality),
>>>>> +
>>>>> +        VMSTATE_END_OF_LIST()
>>>>> +    }
>>>>> +};
>>>>> +
>>>>> +static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    tpm_tis_request_completed(s, ret);
>>>>> +}
>>>>> +
>>>>> +static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    return tpm_tis_get_tpm_version(s);
>>>>> +}
>>>>> +
>>>>> +static void tpm_tis_sysbus_reset(DeviceState *dev)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    return tpm_tis_reset(s);
>>>>> +}
>>>>> +
>>>>> +static Property tpm_tis_sysbus_properties[] = {
>>>>> +    DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num,
>>>>> TPM_TIS_IRQ),
>>>>> +    DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
>>>>> +    DEFINE_PROP_BOOL("ppi", TPMStateSysBus, state.ppi_enabled, true),
>>>>> +    DEFINE_PROP_END_OF_LIST(),
>>>>> +};
>>>>> +
>>>>> +static void tpm_tis_sysbus_initfn(Object *obj)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
>>>>> +                          s, "tpm-tis-mmio",
>>>>> +                          TPM_TIS_NUM_LOCALITIES <<
>>>>> TPM_TIS_LOCALITY_SHIFT);
>>>>> +
>>>>> +    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
>>>>> +    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
>>>>> +}
>>>>> +
>>>>> +static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
>>>>> +{
>>>>> +    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
>>>>> +    TPMState *s = &sbdev->state;
>>>>> +
>>>>> +    if (!tpm_find()) {
>>>>> +        error_setg(errp, "at most one TPM device is permitted");
>>>>> +        return;
>>>>> +    }
>>>>> +
>>>>> +    if (!s->be_driver) {
>>>>> +        error_setg(errp, "'tpmdev' property is required");
>>>>> +        return;
>>>>> +    }
>>>>> +}
>>>>> +
>>>>> +static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
>>>>> +{
>>>>> +    DeviceClass *dc = DEVICE_CLASS(klass);
>>>>> +    TPMIfClass *tc = TPM_IF_CLASS(klass);
>>>>> +
>>>>> +    device_class_set_props(dc, tpm_tis_sysbus_properties);
>>>>> +    dc->vmsd  = &vmstate_tpm_tis_sysbus;
>>>>> +    tc->model = TPM_MODEL_TPM_TIS;
>>>>> +    dc->realize = tpm_tis_sysbus_realizefn;
>>>>> +    dc->user_creatable = true;
>>>>> +    dc->reset = tpm_tis_sysbus_reset;
>>>>> +    tc->request_completed = tpm_tis_sysbus_request_completed;
>>>>> +    tc->get_version = tpm_tis_sysbus_get_tpm_version;
>>>>> +}
>>>>> +
>>>>> +static const TypeInfo tpm_tis_sysbus_info = {
>>>>> +    .name = TYPE_TPM_TIS_SYSBUS,
>>>>> +    .parent = TYPE_SYS_BUS_DEVICE,
>>>>> +    .instance_size = sizeof(TPMStateSysBus),
>>>>> +    .instance_init = tpm_tis_sysbus_initfn,
>>>>> +    .class_init  = tpm_tis_sysbus_class_init,
>>>>> +    .interfaces = (InterfaceInfo[]) {
>>>>> +        { TYPE_TPM_IF },
>>>>> +        { }
>>>>> +    }
>>>>> +};
>>>>> +
>>>>> +static void tpm_tis_sysbus_register(void)
>>>>> +{
>>>>> +    type_register_static(&tpm_tis_sysbus_info);
>>>>> +}
>>>>> +
>>>>> +type_init(tpm_tis_sysbus_register)
>>>>> diff --git a/include/sysemu/tpm.h b/include/sysemu/tpm.h
>>>>> index 1691b92c28..f37851b1aa 100644
>>>>> --- a/include/sysemu/tpm.h
>>>>> +++ b/include/sysemu/tpm.h
>>>>> @@ -44,6 +44,7 @@ typedef struct TPMIfClass {
>>>>>   } TPMIfClass;
>>>>>     #define TYPE_TPM_TIS_ISA            "tpm-tis"
>>>>> +#define TYPE_TPM_TIS_SYSBUS         "tpm-tis-device"
>>>>
>>>>
>>>> hm, replace the rather generic 'device' with 'sysbus'?
>>> I used the "-device" suffix because this kind of naming was used for
>>> virtio-<type>-device when based on MMIO rather than virtio-<type>-pci.
>>> For instance virtio-net-device instead of virtio-net-pci. There are
>>> quite a lot of devices using that suffix. I only see xen-sysbus with the
>>> sysbus suffix.
>>>
>>> Now personally I don't have any strong preference and I will pick up the
>>> name chosen by consensus.
>>
>> Does anyone else have an opinion on the name to be chosen for this new
>> device:
>>
>> 1) tpm-tis-device or
>> 2) tpm-tis-sysbus ?
>>
> 
> It is slightly unfortunate that we cannot retain the 'tpm-tis' name,
> given that this is simply a TPM with a memory mapped TIS frame, like
> the ISA one, and the fact that QEMU instantiates this differently
> based on the emulated architecture is really an implementation detail.
I agree with you. However I don't see any other solution. The
#ifdef'eries are frown upon. As I mentioned we have the same case for
virtio devices (virtio-net-pci vs virtio-net-device for instance).
> 
> So I prefer 'tpm-tis-device', since it doesn't define how it is backed
> under the hood, and allows us to potentially instantiate it in a
> different way in the future if we wanted to. Alternatively, something
> like 'tpm-tis-mmmio' might be appropriate.

At the moment I have chosen to keep tpm-tis-device. But as I mentioned I
am open to other choices if any consensus arises.

Thanks

Eric
> 



^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2020-02-26  8:39 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-14 18:36 [RFC v2 0/6] vTPM for aarch64 Eric Auger
2020-02-14 18:36 ` [RFC v2 1/6] tpm: rename TPM_TIS into TPM_TIS_ISA Eric Auger
2020-02-14 18:55   ` Philippe Mathieu-Daudé
2020-02-25 10:16     ` Auger Eric
2020-02-25 10:22       ` Philippe Mathieu-Daudé
2020-02-25 10:28         ` Auger Eric
2020-02-16 18:14   ` Stefan Berger
2020-02-14 18:37 ` [RFC v2 2/6] tpm: Use TPMState as a common struct Eric Auger
2020-02-14 19:01   ` Philippe Mathieu-Daudé
2020-02-16 18:22   ` Stefan Berger
2020-02-17  9:21   ` Philippe Mathieu-Daudé
2020-02-17 18:01     ` Auger Eric
2020-02-25 10:18       ` Auger Eric
2020-02-14 18:37 ` [RFC v2 3/6] tpm: Separate tpm_tis common functions from isa code Eric Auger
2020-02-16 18:27   ` Stefan Berger
2020-02-14 18:37 ` [RFC v2 4/6] tpm: Separate TPM_TIS and TPM_TIS_ISA configs Eric Auger
2020-02-14 19:03   ` Philippe Mathieu-Daudé
2020-02-25 10:05     ` Auger Eric
2020-02-14 18:37 ` [RFC v2 5/6] tpm: Add the SysBus TPM TIS device Eric Auger
2020-02-16 18:32   ` Stefan Berger
2020-02-17 18:13     ` Auger Eric
2020-02-25  9:18       ` Auger Eric
2020-02-25  9:52         ` Ard Biesheuvel
2020-02-25 10:18           ` Philippe Mathieu-Daudé
2020-02-26  8:38           ` Auger Eric
2020-02-14 18:37 ` [RFC v2 6/6] hw/arm/virt: vTPM support Eric Auger
2020-02-16 18:47   ` Stefan Berger
2020-02-17 18:26     ` Auger Eric
2020-02-16 16:35 ` [RFC v2 0/6] vTPM for aarch64 Ard Biesheuvel
2020-02-17 18:03   ` Auger Eric

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).