2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG
5 * Author: Corvin Köhne <corvink@FreeBSD.org>
18 #include "acpi_device.h"
20 #include "tpm_device.h"
25 #define TPM_ACPI_DEVICE_NAME "TPM"
26 #define TPM_ACPI_HARDWARE_ID "MSFT0101"
28 SET_DECLARE(tpm_emul_set, struct tpm_emul);
29 SET_DECLARE(tpm_intf_set, struct tpm_intf);
30 SET_DECLARE(tpm_ppi_set, struct tpm_ppi);
34 struct acpi_device *acpi_dev;
35 struct tpm_emul *emul;
37 struct tpm_intf *intf;
44 tpm_build_acpi_table(const struct acpi_device *const dev)
46 const struct tpm_device *const tpm = acpi_device_get_softc(dev);
48 if (tpm->intf->build_acpi_table == NULL) {
52 return (tpm->intf->build_acpi_table(tpm->intf_sc, tpm->vm_ctx));
56 tpm_write_dsdt(const struct acpi_device *const dev)
60 const struct tpm_device *const tpm = acpi_device_get_softc(dev);
61 const struct tpm_ppi *const ppi = tpm->ppi;
64 * packages for returns
66 dsdt_line("Name(TPM2, Package(2) {0, 0})");
67 dsdt_line("Name(TPM3, Package(3) {0, 0, 0})");
69 if (ppi->write_dsdt_regions) {
70 error = ppi->write_dsdt_regions(tpm->ppi_sc);
72 warnx("%s: failed to write ppi dsdt regions\n",
79 * Device Specific Method
82 * Arg2: Function Index
85 dsdt_line("Method(_DSM, 4, Serialized)");
88 if (ppi->write_dsdt_dsm) {
89 error = ppi->write_dsdt_dsm(tpm->ppi_sc);
91 warnx("%s: failed to write ppi dsdt dsm\n", __func__);
101 static const struct acpi_device_emul tpm_acpi_device_emul = {
102 .name = TPM_ACPI_DEVICE_NAME,
103 .hid = TPM_ACPI_HARDWARE_ID,
104 .build_table = tpm_build_acpi_table,
105 .write_dsdt = tpm_write_dsdt,
109 tpm_device_destroy(struct tpm_device *const dev)
114 if (dev->ppi != NULL && dev->ppi->deinit != NULL)
115 dev->ppi->deinit(dev->ppi_sc);
116 if (dev->intf != NULL && dev->intf->deinit != NULL)
117 dev->intf->deinit(dev->intf_sc);
118 if (dev->emul != NULL && dev->emul->deinit != NULL)
119 dev->emul->deinit(dev->emul_sc);
121 acpi_device_destroy(dev->acpi_dev);
126 tpm_device_create(struct tpm_device **const new_dev, struct vmctx *const vm_ctx,
129 struct tpm_device *dev = NULL;
130 struct tpm_emul **ppemul;
131 struct tpm_intf **ppintf;
132 struct tpm_ppi **pp_ppi;
136 if (new_dev == NULL || vm_ctx == NULL) {
141 set_config_value_node_if_unset(nvl, "intf", "crb");
142 set_config_value_node_if_unset(nvl, "ppi", "qemu");
144 value = get_config_value_node(nvl, "version");
145 assert(value != NULL);
146 if (strcmp(value, "2.0")) {
147 warnx("%s: unsupported tpm version %s", __func__, value);
152 dev = calloc(1, sizeof(*dev));
158 dev->vm_ctx = vm_ctx;
160 error = acpi_device_create(&dev->acpi_dev, dev, dev->vm_ctx,
161 &tpm_acpi_device_emul);
165 value = get_config_value_node(nvl, "type");
166 assert(value != NULL);
167 SET_FOREACH(ppemul, tpm_emul_set) {
168 if (strcmp(value, (*ppemul)->name))
173 if (dev->emul == NULL) {
174 warnx("TPM emulation \"%s\" not found", value);
179 if (dev->emul->init) {
180 error = dev->emul->init(&dev->emul_sc, nvl);
185 value = get_config_value_node(nvl, "intf");
186 SET_FOREACH(ppintf, tpm_intf_set) {
187 if (strcmp(value, (*ppintf)->name)) {
193 if (dev->intf == NULL) {
194 warnx("TPM interface \"%s\" not found", value);
199 if (dev->intf->init) {
200 error = dev->intf->init(&dev->intf_sc, dev->emul, dev->emul_sc,
206 value = get_config_value_node(nvl, "ppi");
207 SET_FOREACH(pp_ppi, tpm_ppi_set) {
208 if (strcmp(value, (*pp_ppi)->name)) {
214 if (dev->ppi == NULL) {
215 warnx("TPM PPI \"%s\" not found\n", value);
220 if (dev->ppi->init) {
221 error = dev->ppi->init(&dev->ppi_sc);
231 tpm_device_destroy(dev);
236 static struct tpm_device *lpc_tpm;
239 init_tpm(struct vmctx *ctx)
244 nvl = find_config_node("tpm");
248 error = tpm_device_create(&lpc_tpm, ctx, nvl);
250 warnx("%s: unable to create a TPM device (%d)",