tpm_acpi.c revision 1.12 1 /* $NetBSD: tpm_acpi.c,v 1.12 2021/01/16 01:23:04 thorpej 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: tpm_acpi.c,v 1.12 2021/01/16 01:23:04 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/bus.h>
39 #include <sys/pmf.h>
40
41 #include <dev/ic/tpmreg.h>
42 #include <dev/ic/tpmvar.h>
43
44 #include <dev/acpi/acpireg.h>
45 #include <dev/acpi/acpivar.h>
46
47 #include "ioconf.h"
48
49 #define _COMPONENT ACPI_RESOURCE_COMPONENT
50 ACPI_MODULE_NAME ("tpm_acpi")
51
52 static int tpm_acpi_match(device_t, cfdata_t, void *);
53 static void tpm_acpi_attach(device_t, device_t, void *);
54
55 CFATTACH_DECL_NEW(tpm_acpi, sizeof(struct tpm_softc), tpm_acpi_match,
56 tpm_acpi_attach, NULL, NULL);
57
58 /*
59 * Supported TPM 1.2 devices.
60 */
61 static const char * const tpm_1_2_acpi_ids[] = {
62 "PNP0C31",
63 NULL
64 };
65
66 /*
67 * Supported TPM 2.0 devices.
68 */
69 static const char * const tpm2_acpi_ids[] = {
70 "MSFT0101",
71 NULL
72 };
73
74 static int
75 tpm_acpi_match(device_t parent, cfdata_t match, void *aux)
76 {
77 struct acpi_attach_args *aa = aux;
78 ACPI_TABLE_TPM2 *tpm2;
79 ACPI_STATUS rv;
80
81 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
82 return 0;
83
84 /* We support only one TPM. */
85 if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
86 return 0;
87
88 if (acpi_match_hid(aa->aa_node->ad_devinfo, tpm_1_2_acpi_ids)) {
89 /* XXX assume TPM 1.2 devices are memory-mapped. */
90 return 1;
91 }
92
93 if (!acpi_match_hid(aa->aa_node->ad_devinfo, tpm2_acpi_ids))
94 return 0;
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 1;
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 struct acpi_resources res;
112 struct acpi_mem *mem;
113 bus_addr_t base;
114 bus_addr_t size;
115 int rv;
116
117 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", &res,
118 &acpi_resource_parse_ops_default);
119 if (ACPI_FAILURE(rv)) {
120 aprint_error_dev(self, "cannot parse resources %d\n", rv);
121 return;
122 }
123
124 mem = acpi_res_mem(&res, 0);
125 if (mem == NULL) {
126 aprint_error_dev(self, "cannot find mem\n");
127 goto out;
128 }
129 if (mem->ar_length != TPM_SPACE_SIZE) {
130 aprint_error_dev(self, "wrong size mem %"PRIu64" != %u\n",
131 (uint64_t)mem->ar_length, TPM_SPACE_SIZE);
132 goto out;
133 }
134 base = mem->ar_base;
135 size = mem->ar_length;
136
137 sc->sc_dev = self;
138 if (acpi_match_hid(aa->aa_node->ad_devinfo, tpm_1_2_acpi_ids)) {
139 sc->sc_ver = TPM_1_2;
140 } else {
141 sc->sc_ver = TPM_2_0;
142 }
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