tpm_acpi.c revision 1.11
1/* $NetBSD: tpm_acpi.c,v 1.11 2019/10/09 14:03:57 maxv 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.11 2019/10/09 14:03:57 maxv 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
58/*
59 * Supported TPM 2.0 devices.
60 */
61static const char * const tpm2_acpi_ids[] = {
62	"MSFT0101",
63	NULL
64};
65
66static int
67tpm_acpi_match(device_t parent, cfdata_t match, void *aux)
68{
69	struct acpi_attach_args *aa = aux;
70	ACPI_TABLE_TPM2 *tpm2;
71	ACPI_STATUS rv;
72
73	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
74		return 0;
75
76	/* We support only one TPM. */
77	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
78		return 0;
79
80	if (!acpi_match_hid(aa->aa_node->ad_devinfo, tpm2_acpi_ids))
81		return 0;
82
83	/* Make sure it uses TIS, and not CRB. */
84	rv = AcpiGetTable(ACPI_SIG_TPM2, 1, (ACPI_TABLE_HEADER **)&tpm2);
85	if (ACPI_FAILURE(rv))
86		return 0;
87	if (tpm2->StartMethod != ACPI_TPM2_MEMORY_MAPPED)
88		return 0;
89
90	return 1;
91}
92
93static void
94tpm_acpi_attach(device_t parent, device_t self, void *aux)
95{
96	struct tpm_softc *sc = device_private(self);
97	struct acpi_attach_args *aa = aux;
98	struct acpi_resources res;
99	struct acpi_mem *mem;
100	bus_addr_t base;
101	bus_addr_t size;
102	int rv;
103
104	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", &res,
105	    &acpi_resource_parse_ops_default);
106	if (ACPI_FAILURE(rv)) {
107		aprint_error_dev(self, "cannot parse resources %d\n", rv);
108		return;
109	}
110
111	mem = acpi_res_mem(&res, 0);
112	if (mem == NULL) {
113		aprint_error_dev(self, "cannot find mem\n");
114		goto out;
115	}
116	if (mem->ar_length != TPM_SPACE_SIZE) {
117		aprint_error_dev(self, "wrong size mem %"PRIu64" != %u\n",
118		    (uint64_t)mem->ar_length, TPM_SPACE_SIZE);
119		goto out;
120	}
121	base = mem->ar_base;
122	size = mem->ar_length;
123
124	sc->sc_dev = self;
125	sc->sc_ver = TPM_2_0;
126	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
127	sc->sc_busy = false;
128	sc->sc_intf = &tpm_intf_tis12;
129	sc->sc_bt = aa->aa_memt;
130	if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
131		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
132		goto out;
133	}
134
135	if ((rv = (*sc->sc_intf->probe)(sc->sc_bt, sc->sc_bh)) != 0) {
136		aprint_error_dev(sc->sc_dev, "probe failed, rv=%d\n", rv);
137		goto out1;
138	}
139	if ((rv = (*sc->sc_intf->init)(sc)) != 0) {
140		aprint_error_dev(sc->sc_dev, "cannot init device, rv=%d\n", rv);
141		goto out1;
142	}
143
144	if (!pmf_device_register(self, tpm_suspend, tpm_resume))
145		aprint_error_dev(self, "couldn't establish power handler\n");
146	acpi_resource_cleanup(&res);
147	return;
148
149out1:
150	bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
151out:
152	acpi_resource_cleanup(&res);
153}
154