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