Skip to contentSkip to conversation

vTPM/aarch64 ACPI support

PATCH Superseded Eric Auger 4 participants 7 messages May 2, 2020 – May 4, 2020

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

cover vTPM/aarch64 ACPI support

Those 2 patches bring MMIO TPM TIS ACPI support in machvirt. The TPM2.0 table is added and the TPM2 device object is described in the DSDT.

Many thanks to Ard for his support.

Tested with LUKS partition automatic decryption.

Best Regards

Eric

This series can be found at:
https://github.com/eauger/qemu/tree/v5.0-tpm-acpi-v1

Eric Auger (2):
arm/acpi: TPM2 ACPI table support
arm/acpi: Add the TPM2.0 device under the DSDT

 include/sysemu/tpm.h     |  2 ++
 hw/arm/virt-acpi-build.c | 70 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/2 arm/acpi: TPM2 ACPI table support

Add a TPM2 ACPI table if a TPM2.0 sysbus device has been dynamically instantiated.

---
 include/sysemu/tpm.h     |  2 ++
 hw/arm/virt-acpi-build.c | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
include/sysemu/tpm.h +2 -0
index f37851b1aa..03fb25941c 100644
@@ -50,6 +50,8 @@ typedef struct TPMIfClass {
 
 #define TPM_IS_TIS_ISA(chr)                         \
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
+#define TPM_IS_TIS_SYSBUS(chr)                      \
+    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_SYSBUS)
 #define TPM_IS_CRB(chr)                             \
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
 #define TPM_IS_SPAPR(chr)                           \
hw/arm/virt-acpi-build.c +36 -0
index 81d41a3990..cc5863eaf2 100644
@@ -41,11 +41,13 @@
 #include "hw/acpi/pci.h"
 #include "hw/acpi/memory_hotplug.h"
 #include "hw/acpi/generic_event_device.h"
+#include "hw/acpi/tpm.h"
 #include "hw/pci/pcie_host.h"
 #include "hw/pci/pci.h"
 #include "hw/arm/virt.h"
 #include "sysemu/numa.h"
 #include "sysemu/reset.h"
+#include "sysemu/tpm.h"
 #include "kvm_arm.h"
 #include "migration/vmstate.h"
 
@@ -704,6 +706,35 @@ static void build_fadt_rev5(GArray *table_data, BIOSLinker *linker,
     build_fadt(table_data, linker, &fadt, NULL, NULL);
 }
 
+static void
+build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog)
+{
+    Acpi20TPM2 *tpm2_ptr = acpi_data_push(table_data, sizeof *tpm2_ptr);
+    unsigned log_addr_size = sizeof(tpm2_ptr->log_area_start_address);
+    unsigned log_addr_offset =
+        (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_SYSBUS(tpm_find())) {
+        tpm2_ptr->control_area_address = cpu_to_le64(0);
+        tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
+    } else {
+        g_warn_if_reached();
+    }
+
+    tpm2_ptr->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
+    acpi_data_push(tcpalog, le32_to_cpu(tpm2_ptr->log_area_minimum_length));
+
+    /* log area start address to be filled by Guest linker */
+    bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE,
+                             tcpalog, 1, false);
+    bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
+                                   log_addr_offset, log_addr_size,
+                                   ACPI_BUILD_TPMLOG_FILE, 0);
+    build_header(linker, table_data,
+                 (void *)tpm2_ptr, "TPM2", sizeof(*tpm2_ptr), 4, NULL, NULL);
+}
+
 /* DSDT */
 static void
 build_dsdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
@@ -831,6 +862,11 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
         build_iort(tables_blob, tables->linker, vms);
     }
 
+    if (tpm_get_version(tpm_find()) == TPM_VERSION_2_0) {
+        acpi_add_table(table_offsets, tables_blob);
+        build_tpm2(tables_blob, tables->linker, tables->tcpalog);
+    }
+
     /* XSDT is pointed to by RSDP */
     xsdt = tables_blob->len;
     build_xsdt(tables_blob, tables->linker, table_offsets, NULL, NULL);
Signature · 1 line
2.20.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] arm/acpi: TPM2 ACPI table support

Quoted 78 lines · Eric Auger

Add a TPM2 ACPI table if a TPM2.0 sysbus device has been dynamically instantiated.

---
  include/sysemu/tpm.h     |  2 ++
  hw/arm/virt-acpi-build.c | 36 ++++++++++++++++++++++++++++++++++++
  2 files changed, 38 insertions(+)
include/sysemu/tpm.h +2 -0
index f37851b1aa..03fb25941c 100644
@@ -50,6 +50,8 @@ typedef struct TPMIfClass {
 
  #define TPM_IS_TIS_ISA(chr)                         \
      object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_ISA)
+#define TPM_IS_TIS_SYSBUS(chr)                      \
+    object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_SYSBUS)
  #define TPM_IS_CRB(chr)                             \
      object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
  #define TPM_IS_SPAPR(chr)                           \
hw/arm/virt-acpi-build.c +31 -0
index 81d41a3990..cc5863eaf2 100644
@@ -41,11 +41,13 @@
  #include "hw/acpi/pci.h"
  #include "hw/acpi/memory_hotplug.h"
  #include "hw/acpi/generic_event_device.h"
+#include "hw/acpi/tpm.h"
  #include "hw/pci/pcie_host.h"
  #include "hw/pci/pci.h"
  #include "hw/arm/virt.h"
  #include "sysemu/numa.h"
  #include "sysemu/reset.h"
+#include "sysemu/tpm.h"
  #include "kvm_arm.h"
  #include "migration/vmstate.h"
 
@@ -704,6 +706,35 @@ static void build_fadt_rev5(GArray *table_data, BIOSLinker *linker,
      build_fadt(table_data, linker, &fadt, NULL, NULL);
  }
 
+static void
+build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog)
+{
+    Acpi20TPM2 *tpm2_ptr = acpi_data_push(table_data, sizeof *tpm2_ptr);
+    unsigned log_addr_size = sizeof(tpm2_ptr->log_area_start_address);
+    unsigned log_addr_offset =
+        (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_SYSBUS(tpm_find())) {
+        tpm2_ptr->control_area_address = cpu_to_le64(0);
+        tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
+    } else {
+        g_warn_if_reached();
+    }
+
+    tpm2_ptr->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
+    acpi_data_push(tcpalog, le32_to_cpu(tpm2_ptr->log_area_minimum_length));
+
+    /* log area start address to be filled by Guest linker */
+    bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE,
+                             tcpalog, 1, false);
+    bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
+                                   log_addr_offset, log_addr_size,
+                                   ACPI_BUILD_TPMLOG_FILE, 0);
+    build_header(linker, table_data,
+                 (void *)tpm2_ptr, "TPM2", sizeof(*tpm2_ptr), 4, NULL, NULL);
+}
+
  /* DSDT */
  static void
  build_dsdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
@@ -831,6 +862,11 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
          build_iort(tables_blob, tables->linker, vms);

hw/arm/virt-acpi-build.c void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables) · line 872

Quoted 10 lines
hw/arm/virt-acpi-build.c +5 -0
      }
 
+    if (tpm_get_version(tpm_find()) == TPM_VERSION_2_0) {
+        acpi_add_table(table_offsets, tables_blob);
+        build_tpm2(tables_blob, tables->linker, tables->tcpalog);
+    }
+
      /* XSDT is pointed to by RSDP */
      xsdt = tables_blob->len;
      build_xsdt(tables_blob, tables->linker, table_offsets, NULL, NULL);
author
#3 lore raw

PATCH 2/2 arm/acpi: Add the TPM2.0 device under the DSDT

In reply to Eric Auger

In case it is dynamically instantiated, add the TPM 2.0 device object under the DSDT table in the ACPI namespace. Its HID is MSFT0101 while its current resource settings (CRS) property is initialized with the guest physical address and MMIO size of the device.

---
 hw/arm/virt-acpi-build.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
hw/arm/virt-acpi-build.c +34 -0
index cc5863eaf2..0cb9cdb2ce 100644
@@ -45,6 +45,7 @@
 #include "hw/pci/pcie_host.h"
 #include "hw/pci/pci.h"
 #include "hw/arm/virt.h"
+#include "hw/platform-bus.h"
 #include "sysemu/numa.h"
 #include "sysemu/reset.h"
 #include "sysemu/tpm.h"
@@ -362,6 +363,38 @@ static void acpi_dsdt_add_power_button(Aml *scope)
     aml_append(scope, dev);
 }
 
+static void acpi_dsdt_add_tpm(Aml *scope, VirtMachineState *vms)
+{
+    hwaddr pbus_base = vms->memmap[VIRT_PLATFORM_BUS].base;
+    PlatformBusDevice *pbus = PLATFORM_BUS_DEVICE(vms->platform_bus_dev);
+    MemoryRegion *sbdev_mr;
+    SysBusDevice *sbdev;
+    hwaddr tpm_base;
+
+    sbdev = (SysBusDevice *)object_dynamic_cast(OBJECT(tpm_find()),
+                                                TYPE_SYS_BUS_DEVICE);
+    if (!sbdev) {
+        return;
+    }
+
+    tpm_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
+    assert(tpm_base != -1);
+
+    tpm_base += pbus_base;
+
+    sbdev_mr = sysbus_mmio_get_region(sbdev, 0);
+
+    Aml *dev = aml_device("TPM0");
+    aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
+    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
+
+    Aml *crs = aml_resource_template();
+    aml_append(crs,
+               aml_memory32_fixed(tpm_base, sbdev_mr->size, AML_READ_WRITE));
+    aml_append(dev, aml_name_decl("_CRS", crs));
+    aml_append(scope, dev);
+}
+
 static void
 build_iort(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
 {
@@ -785,6 +818,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
     }
 
     acpi_dsdt_add_power_button(scope);
+    acpi_dsdt_add_tpm(scope, vms);
 
     aml_append(dsdt, scope);
 
Signature · 1 line
2.20.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] arm/acpi: Add the TPM2.0 device under the DSDT

Quoted 62 lines · Eric Auger

In case it is dynamically instantiated, add the TPM 2.0 device object under the DSDT table in the ACPI namespace. Its HID is MSFT0101 while its current resource settings (CRS) property is initialized with the guest physical address and MMIO size of the device.

---
  hw/arm/virt-acpi-build.c | 34 ++++++++++++++++++++++++++++++++++
  1 file changed, 34 insertions(+)
hw/arm/virt-acpi-build.c +33 -0
index cc5863eaf2..0cb9cdb2ce 100644
@@ -45,6 +45,7 @@
  #include "hw/pci/pcie_host.h"
  #include "hw/pci/pci.h"
  #include "hw/arm/virt.h"
+#include "hw/platform-bus.h"
  #include "sysemu/numa.h"
  #include "sysemu/reset.h"
  #include "sysemu/tpm.h"
@@ -362,6 +363,38 @@ static void acpi_dsdt_add_power_button(Aml *scope)
      aml_append(scope, dev);
  }
 
+static void acpi_dsdt_add_tpm(Aml *scope, VirtMachineState *vms)
+{
+    hwaddr pbus_base = vms->memmap[VIRT_PLATFORM_BUS].base;
+    PlatformBusDevice *pbus = PLATFORM_BUS_DEVICE(vms->platform_bus_dev);
+    MemoryRegion *sbdev_mr;
+    SysBusDevice *sbdev;
+    hwaddr tpm_base;
+
+    sbdev = (SysBusDevice *)object_dynamic_cast(OBJECT(tpm_find()),
+                                                TYPE_SYS_BUS_DEVICE);
+    if (!sbdev) {
+        return;
+    }
+
+    tpm_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
+    assert(tpm_base != -1);
+
+    tpm_base += pbus_base;
+
+    sbdev_mr = sysbus_mmio_get_region(sbdev, 0);
+
+    Aml *dev = aml_device("TPM0");
+    aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
+    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
+
+    Aml *crs = aml_resource_template();
+    aml_append(crs,
+               aml_memory32_fixed(tpm_base, sbdev_mr->size, AML_READ_WRITE));
+    aml_append(dev, aml_name_decl("_CRS", crs));
+    aml_append(scope, dev);
+}
+
  static void
  build_iort(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
  {

hw/arm/virt-acpi-build.c build_dsdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms) · line 824

Quoted 7 lines
hw/arm/virt-acpi-build.c +1 -0
@@ -785,6 +818,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
      }
 
      acpi_dsdt_add_power_button(scope);
+    acpi_dsdt_add_tpm(scope, vms);
 
      aml_append(dsdt, scope);
 
#5 lore raw

Re: [PATCH 2/2] arm/acpi: Add the TPM2.0 device under the DSDT

In reply to Eric Auger
Quoted 44 lines · Eric Auger

In case it is dynamically instantiated, add the TPM 2.0 device object under the DSDT table in the ACPI namespace. Its HID is MSFT0101 while its current resource settings (CRS) property is initialized with the guest physical address and MMIO size of the device.

---
 hw/arm/virt-acpi-build.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
hw/arm/virt-acpi-build.c +19 -0
index cc5863eaf2..0cb9cdb2ce 100644
@@ -45,6 +45,7 @@
 #include "hw/pci/pcie_host.h"
 #include "hw/pci/pci.h"
 #include "hw/arm/virt.h"
+#include "hw/platform-bus.h"
 #include "sysemu/numa.h"
 #include "sysemu/reset.h"
 #include "sysemu/tpm.h"
@@ -362,6 +363,38 @@ static void acpi_dsdt_add_power_button(Aml *scope)
     aml_append(scope, dev);
 }
 
+static void acpi_dsdt_add_tpm(Aml *scope, VirtMachineState *vms)
+{
+    hwaddr pbus_base = vms->memmap[VIRT_PLATFORM_BUS].base;
+    PlatformBusDevice *pbus = PLATFORM_BUS_DEVICE(vms->platform_bus_dev);
+    MemoryRegion *sbdev_mr;
+    SysBusDevice *sbdev;
+    hwaddr tpm_base;
+
+    sbdev = (SysBusDevice *)object_dynamic_cast(OBJECT(tpm_find()),
+                                                TYPE_SYS_BUS_DEVICE);
+    if (!sbdev) {
+        return;
+    }
+
+    tpm_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
+    assert(tpm_base != -1);
+
+    tpm_base += pbus_base;

hw/arm/virt-acpi-build.c static void acpi_dsdt_add_power_button(Aml *scope) · line 393

Quoted 10 lines
hw/arm/virt-acpi-build.c +10 -0
+
+    sbdev_mr = sysbus_mmio_get_region(sbdev, 0);
+
+    Aml *dev = aml_device("TPM0");
+    aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
+    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
+
+    Aml *crs = aml_resource_template();
+    aml_append(crs,
+               aml_memory32_fixed(tpm_base, sbdev_mr->size, AML_READ_WRITE));

I don't think you are supposed to poke at memory region struct internals like this.

Quoted 17 lines
hw/arm/virt-acpi-build.c +5 -0
+    aml_append(dev, aml_name_decl("_CRS", crs));
+    aml_append(scope, dev);
+}
+
 static void
 build_iort(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
 {
@@ -785,6 +818,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
     }
 
     acpi_dsdt_add_power_button(scope);
+    acpi_dsdt_add_tpm(scope, vms);
 
     aml_append(dsdt, scope);
 
Signature · 1 line
2.20.1
#6 lore raw
In reply to Eric Auger
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the docker-mingw@fedora build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#! /bin/bash
export ARCH=x86_64
make docker-image-fedora V=1 NETWORK=1
time make docker-test-mingw@fedora J=14 NETWORK=1
=== TEST SCRIPT END ===

CC x86_64-softmmu/target/i386/smm_helper.o
CC aarch64-softmmu/hw/arm/orangepi.o
/tmp/qemu-test/src/hw/arm/virt-acpi-build.c: In function 'acpi_dsdt_add_tpm':
/tmp/qemu-test/src/hw/arm/virt-acpi-build.c:393:53: error: incompatible type for argument 2 of 'aml_memory32_fixed'

                aml_memory32_fixed(tpm_base, sbdev_mr->size, AML_READ_WRITE));
                                             ~~~~~~~~^~~~~~
In file included from /tmp/qemu-test/src/hw/arm/virt-acpi-build.c:39:
/tmp/qemu-test/src/include/hw/acpi/aml-build.h:317:6: note: expected 'uint32_t' {aka 'unsigned int'} but argument is of type 'Int128' {aka 'struct Int128'}
 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size,
      ^~~~~~~~~~~~~~~~~~

make[1]: *** [/tmp/qemu-test/src/rules.mak:69: hw/arm/virt-acpi-build.o] Error 1 make[1]: *** Waiting for unfinished jobs....
CC x86_64-softmmu/target/i386/svm_helper.o
CC x86_64-softmmu/target/i386/kvm-stub.o
---

  CC      x86_64-softmmu/softmmu/main.o
  CC      x86_64-softmmu/gdbstub-xml.o
  CC      x86_64-softmmu/trace/generated-helpers.o

make: *** [Makefile:527: aarch64-softmmu/all] Error 2
make: *** Waiting for unfinished jobs....
LINK x86_64-softmmu/qemu-system-x86_64w.exe
GEN x86_64-softmmu/qemu-system-x86_64.exe
---
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sudo', '-n', 'docker', 'run', '--label', 'com.qemu.instance.uuid=0a6f397fde554332865bf4ebe15583d2', '-u', '1001', '--security-opt', 'seccomp=unconfined', '--rm', '-e', 'TARGET_LIST=', '-e', 'EXTRA_CONFIGURE_OPTS=', '-e', 'V=', '-e', 'J=14', '-e', 'DEBUG=', '-e', 'SHOW_ENV=', '-e', 'CCACHE_DIR=/var/tmp/ccache', '-v', '/home/patchew/.cache/qemu-docker-ccache:/var/tmp/ccache:z', '-v', '/var/tmp/patchew-tester-tmp-4bk1lfg4/src/docker-src.2020-05-02-17.54.35.24242:/var/tmp/qemu:z,ro', 'qemu:fedora', '/var/tmp/qemu/run', 'test-mingw']' returned non-zero exit status 2. filter=--filter=label=com.qemu.instance.uuid=0a6f397fde554332865bf4ebe15583d2
make[1]: *** [docker-run] Error 1
make[1]: Leaving directory `/var/tmp/patchew-tester-tmp-4bk1lfg4/src'
make: *** [docker-run-test-mingw@fedora] Error 2

real 6m0.460s
user 0m8.733s

The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-mingw@fedora/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]