tpm_acpi.c revision 1.14
1/* $NetBSD: tpm_acpi.c,v 1.14 2021/11/14 21:18:30 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#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: tpm_acpi.c,v 1.14 2021/11/14 21:18:30 riastradh 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
50ACPI_MODULE_NAME	("tpm_acpi")
51
52static int	tpm_acpi_match(device_t, cfdata_t, void *);
53static void	tpm_acpi_attach(device_t, device_t, void *);
54
55CFATTACH_DECL_NEW(tpm_acpi, sizeof(struct tpm_softc), tpm_acpi_match,
56    tpm_acpi_attach, NULL, NULL);
57
58static const struct device_compatible_entry compat_data[] = {
59	{ .compat = "PNP0C31",		.value = TPM_1_2 },
60	{ .compat = "MSFT0101",		.value = TPM_2_0 },
61	DEVICE_COMPAT_EOL
62};
63
64static int
65tpm_acpi_match(device_t parent, cfdata_t match, void *aux)
66{
67	struct acpi_attach_args *aa = aux;
68	const struct device_compatible_entry *dce;
69	ACPI_TABLE_TPM2 *tpm2;
70	ACPI_STATUS rv;
71	int ret;
72
73	/* We support only one TPM. */
74	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
75		return 0;
76
77	ret = acpi_compatible_match(aa, compat_data);
78	if (ret == 0)
79		return 0;
80
81	dce = acpi_compatible_lookup(aa, compat_data);
82	KASSERT(dce != NULL);
83
84	if (dce->value == TPM_1_2) {
85		/* XXX assume TPM 1.2 devices are memory-mapped. */
86		return ret;
87	}
88
89	/* Make sure it uses TIS, and not CRB. */
90	rv = AcpiGetTable(ACPI_SIG_TPM2, 1, (ACPI_TABLE_HEADER **)&tpm2);
91	if (ACPI_FAILURE(rv))
92		return 0;
93	if (tpm2->StartMethod != ACPI_TPM2_MEMORY_MAPPED)
94		return 0;
95
96	return ret;
97}
98
99static void
100tpm_acpi_attach(device_t parent, device_t self, void *aux)
101{
102	struct tpm_softc *sc = device_private(self);
103	struct acpi_attach_args *aa = aux;
104	const struct device_compatible_entry *dce;
105	struct acpi_resources res;
106	struct acpi_mem *mem;
107	bus_addr_t base;
108	bus_addr_t size;
109	int rv;
110
111	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", &res,
112	    &acpi_resource_parse_ops_default);
113	if (ACPI_FAILURE(rv)) {
114		aprint_error_dev(self, "cannot parse resources %d\n", rv);
115		return;
116	}
117
118	mem = acpi_res_mem(&res, 0);
119	if (mem == NULL) {
120		aprint_error_dev(self, "cannot find mem\n");
121		goto out;
122	}
123	if (mem->ar_length < TPM_SPACE_SIZE) {
124		aprint_error_dev(self, "wrong size mem %"PRIu64" < %u\n",
125		    (uint64_t)mem->ar_length, TPM_SPACE_SIZE);
126		goto out;
127	}
128	base = mem->ar_base;
129	size = mem->ar_length;
130
131	dce = acpi_compatible_lookup(aa, compat_data);
132	KASSERT(dce != NULL);
133
134	sc->sc_dev = self;
135	sc->sc_ver = dce->value;
136	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
137	sc->sc_busy = false;
138	sc->sc_intf = &tpm_intf_tis12;
139	sc->sc_bt = aa->aa_memt;
140	if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
141		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
142		goto out;
143	}
144
145	if ((rv = (*sc->sc_intf->probe)(sc->sc_bt, sc->sc_bh)) != 0) {
146		aprint_error_dev(sc->sc_dev, "probe failed, rv=%d\n", rv);
147		goto out1;
148	}
149	if ((rv = (*sc->sc_intf->init)(sc)) != 0) {
150		aprint_error_dev(sc->sc_dev, "cannot init device, rv=%d\n", rv);
151		goto out1;
152	}
153
154	if (!pmf_device_register(self, tpm_suspend, tpm_resume))
155		aprint_error_dev(self, "couldn't establish power handler\n");
156	acpi_resource_cleanup(&res);
157	return;
158
159out1:
160	bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
161out:
162	acpi_resource_cleanup(&res);
163}
164