Home | History | Annotate | Line # | Download | only in acpi
tpm_acpi.c revision 1.8
      1 /* $NetBSD: tpm_acpi.c,v 1.8 2019/06/22 12:57:40 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.8 2019/06/22 12:57:40 maxv Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/device.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <sys/bus.h>
     40 #include <sys/pmf.h>
     41 
     42 #include <dev/ic/tpmreg.h>
     43 #include <dev/ic/tpmvar.h>
     44 
     45 #include <dev/acpi/acpireg.h>
     46 #include <dev/acpi/acpivar.h>
     47 
     48 #include <dev/isa/isavar.h>
     49 
     50 #include "ioconf.h"
     51 
     52 #define _COMPONENT	ACPI_RESOURCE_COMPONENT
     53 ACPI_MODULE_NAME	("tpm_acpi")
     54 
     55 static int	tpm_acpi_match(device_t, cfdata_t, void *);
     56 static void	tpm_acpi_attach(device_t, device_t, void *);
     57 
     58 CFATTACH_DECL_NEW(tpm_acpi, sizeof(struct tpm_softc), tpm_acpi_match,
     59     tpm_acpi_attach, NULL, NULL);
     60 
     61 /*
     62  * Supported TPM 2.0 devices.
     63  */
     64 static const char * const tpm2_acpi_ids[] = {
     65 	"MSFT0101",
     66 	NULL
     67 };
     68 
     69 static int
     70 tpm_acpi_match(device_t parent, cfdata_t match, void *aux)
     71 {
     72 	struct acpi_attach_args *aa = aux;
     73 	ACPI_TABLE_TPM2 *tpm2;
     74 	ACPI_STATUS rv;
     75 
     76 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     77 		return 0;
     78 
     79 	/* We support only one TPM. */
     80 	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
     81 		return 0;
     82 
     83 	if (!acpi_match_hid(aa->aa_node->ad_devinfo, tpm2_acpi_ids))
     84 		return 0;
     85 
     86 	/* Make sure it uses TIS, and not CRB. */
     87 	rv = AcpiGetTable(ACPI_SIG_TPM2, 1, (ACPI_TABLE_HEADER **)&tpm2);
     88 	if (ACPI_FAILURE(rv))
     89 		return 0;
     90 	if (tpm2->StartMethod != ACPI_TPM2_MEMORY_MAPPED)
     91 		return 0;
     92 
     93 	return 1;
     94 }
     95 
     96 static void
     97 tpm_acpi_attach(device_t parent, device_t self, void *aux)
     98 {
     99 	struct tpm_softc *sc = device_private(self);
    100 	struct acpi_attach_args *aa = aux;
    101 	struct acpi_resources res;
    102 	struct acpi_mem *mem;
    103 	struct acpi_irq *irq;
    104 	bus_addr_t base;
    105 	bus_addr_t size;
    106 	int rv, inum;
    107 
    108 	sc->sc_dev = self;
    109 	sc->sc_ver = TPM_2_0;
    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(sc->sc_dev, "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(sc->sc_dev, "cannot find mem\n");
    121 		goto out;
    122 	}
    123 	if (mem->ar_length != TPM_SPACE_SIZE) {
    124 		aprint_error_dev(sc->sc_dev,
    125 		    "wrong size mem %"PRIu64" != %u\n",
    126 		    (uint64_t)mem->ar_length, TPM_SPACE_SIZE);
    127 		goto out;
    128 	}
    129 
    130 	base = mem->ar_base;
    131 	size = mem->ar_length;
    132 	sc->sc_bt = aa->aa_memt;
    133 	sc->sc_init = tpm_tis12_init;
    134 	sc->sc_start = tpm_tis12_start;
    135 	sc->sc_read = tpm_tis12_read;
    136 	sc->sc_write = tpm_tis12_write;
    137 	sc->sc_end = tpm_tis12_end;
    138 
    139 	if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
    140 		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
    141 		goto out;
    142 	}
    143 
    144 	if (!tpm_tis12_probe(sc->sc_bt, sc->sc_bh)) {
    145 		aprint_error_dev(sc->sc_dev, "TIS1.2 probe failed\n");
    146 		goto out1;
    147 	}
    148 
    149 	irq = acpi_res_irq(&res, 0);
    150 	if (irq == NULL)
    151 		inum = -1;
    152 	else
    153 		inum = irq->ar_irq;
    154 
    155 	if ((rv = (*sc->sc_init)(sc, inum)) != 0) {
    156 		aprint_error_dev(sc->sc_dev, "cannot init device %d\n", rv);
    157 		goto out1;
    158 	}
    159 
    160 	if (inum != -1 &&
    161 	    (sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq,
    162 	    IST_EDGE, IPL_TTY, tpm_intr, sc)) == NULL) {
    163 		aprint_error_dev(sc->sc_dev, "cannot establish interrupt\n");
    164 		goto out1;
    165 	}
    166 
    167 	acpi_resource_cleanup(&res);
    168 	return;
    169 
    170 out1:
    171 	bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
    172 out:
    173 	acpi_resource_cleanup(&res);
    174 }
    175