1 /* $NetBSD: tpm_acpi.c,v 1.15 2025/10/31 03:25:16 riastradh Exp $ */ 2 3 /* 4 * Copyright (c) 2012, 2019 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas and Maxime Villard. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Trusted Computing Group, TCG ACPI Specification, Version 1.4, 34 * Revision 15, 2024-04-03 35 * https://trustedcomputinggroup.org/wp-content/uploads/TCG-ACPI-Specification-Version-1.4-Revision-15_pub.pdf 36 * https://web.archive.org/web/20240611010643/https://trustedcomputinggroup.org/wp-content/uploads/TCG-ACPI-Specification-Version-1.4-Revision-15_pub.pdf 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: tpm_acpi.c,v 1.15 2025/10/31 03:25:16 riastradh Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/device.h> 45 #include <sys/bus.h> 46 #include <sys/pmf.h> 47 48 #include <dev/ic/tpmreg.h> 49 #include <dev/ic/tpmvar.h> 50 51 #include <dev/acpi/acpireg.h> 52 #include <dev/acpi/acpivar.h> 53 54 #include "ioconf.h" 55 56 #define _COMPONENT ACPI_RESOURCE_COMPONENT 57 ACPI_MODULE_NAME ("tpm_acpi") 58 59 static int tpm_acpi_match(device_t, cfdata_t, void *); 60 static void tpm_acpi_attach(device_t, device_t, void *); 61 62 CFATTACH_DECL_NEW(tpm_acpi, sizeof(struct tpm_softc), tpm_acpi_match, 63 tpm_acpi_attach, NULL, NULL); 64 65 static const struct device_compatible_entry compat_data[] = { 66 { .compat = "PNP0C31", .value = TPM_1_2 }, 67 { .compat = "MSFT0101", .value = TPM_2_0 }, 68 DEVICE_COMPAT_EOL 69 }; 70 71 static int 72 tpm_acpi_match(device_t parent, cfdata_t match, void *aux) 73 { 74 struct acpi_attach_args *aa = aux; 75 const struct device_compatible_entry *dce; 76 ACPI_TABLE_TPM2 *tpm2; 77 ACPI_STATUS rv; 78 int ret; 79 80 /* We support only one TPM. */ 81 if (tpm_cd.cd_devs && tpm_cd.cd_devs[0]) 82 return 0; 83 84 ret = acpi_compatible_match(aa, compat_data); 85 if (ret == 0) 86 return 0; 87 88 dce = acpi_compatible_lookup(aa, compat_data); 89 KASSERT(dce != NULL); 90 91 if (dce->value == TPM_1_2) { 92 /* XXX assume TPM 1.2 devices are memory-mapped. */ 93 return ret; 94 } 95 96 /* Make sure it uses TIS, and not CRB. */ 97 rv = AcpiGetTable(ACPI_SIG_TPM2, 1, (ACPI_TABLE_HEADER **)&tpm2); 98 if (ACPI_FAILURE(rv)) 99 return 0; 100 if (tpm2->StartMethod != ACPI_TPM2_MEMORY_MAPPED) 101 return 0; 102 103 return ret; 104 } 105 106 static void 107 tpm_acpi_attach(device_t parent, device_t self, void *aux) 108 { 109 struct tpm_softc *sc = device_private(self); 110 struct acpi_attach_args *aa = aux; 111 const struct device_compatible_entry *dce; 112 struct acpi_resources res; 113 struct acpi_mem *mem; 114 bus_addr_t base; 115 bus_addr_t size; 116 int rv; 117 118 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", &res, 119 &acpi_resource_parse_ops_default); 120 if (ACPI_FAILURE(rv)) { 121 aprint_error_dev(self, "cannot parse resources %d\n", rv); 122 return; 123 } 124 125 mem = acpi_res_mem(&res, 0); 126 if (mem == NULL) { 127 aprint_error_dev(self, "cannot find mem\n"); 128 goto out; 129 } 130 if (mem->ar_length < TPM_SPACE_SIZE) { 131 aprint_error_dev(self, "wrong size mem %"PRIu64" < %u\n", 132 (uint64_t)mem->ar_length, TPM_SPACE_SIZE); 133 goto out; 134 } 135 base = mem->ar_base; 136 size = mem->ar_length; 137 138 dce = acpi_compatible_lookup(aa, compat_data); 139 KASSERT(dce != NULL); 140 141 sc->sc_dev = self; 142 sc->sc_ver = dce->value; 143 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 144 sc->sc_busy = false; 145 sc->sc_intf = &tpm_intf_tis12; 146 sc->sc_bt = aa->aa_memt; 147 if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) { 148 aprint_error_dev(sc->sc_dev, "cannot map registers\n"); 149 goto out; 150 } 151 152 if ((rv = (*sc->sc_intf->probe)(sc->sc_bt, sc->sc_bh)) != 0) { 153 aprint_error_dev(sc->sc_dev, "probe failed, rv=%d\n", rv); 154 goto out1; 155 } 156 if ((rv = (*sc->sc_intf->init)(sc)) != 0) { 157 aprint_error_dev(sc->sc_dev, "cannot init device, rv=%d\n", rv); 158 goto out1; 159 } 160 161 if (!pmf_device_register(self, tpm_suspend, tpm_resume)) 162 aprint_error_dev(self, "couldn't establish power handler\n"); 163 acpi_resource_cleanup(&res); 164 return; 165 166 out1: 167 bus_space_unmap(sc->sc_bt, sc->sc_bh, size); 168 out: 169 acpi_resource_cleanup(&res); 170 } 171