Home | History | Annotate | Line # | Download | only in acpi
tpm_acpi.c revision 1.6.4.1
      1  1.6.4.1  christos /* $NetBSD: tpm_acpi.c,v 1.6.4.1 2019/06/10 22:07:05 christos Exp $ */
      2      1.1  christos 
      3      1.1  christos /*-
      4      1.1  christos  * Copyright (c) 2012 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.1  christos  * by Christos Zoulas.
      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  * Copyright (c) 2008, 2009 Michael Shalayeff
     33      1.1  christos  * Copyright (c) 2009, 2010 Hans-Jrg Hxer
     34      1.1  christos  * All rights reserved.
     35      1.1  christos  *
     36      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
     37      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     38      1.1  christos  * copyright notice and this permission notice appear in all copies.
     39      1.1  christos  *
     40      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     41      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     42      1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     43      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     44      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
     45      1.1  christos  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     46      1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     47      1.1  christos  */
     48      1.1  christos 
     49      1.1  christos /*
     50      1.1  christos  * ACPI attachment for the Infineon SLD 9630 TT 1.1 and SLB 9635 TT 1.2
     51      1.1  christos  * trusted platform module. See www.trustedcomputinggroup.org
     52      1.1  christos  */
     53      1.1  christos 
     54      1.1  christos #include <sys/cdefs.h>
     55  1.6.4.1  christos __KERNEL_RCSID(0, "$NetBSD: tpm_acpi.c,v 1.6.4.1 2019/06/10 22:07:05 christos Exp $");
     56      1.1  christos 
     57      1.1  christos #include <sys/param.h>
     58      1.1  christos #include <sys/device.h>
     59      1.1  christos #include <sys/systm.h>
     60      1.1  christos #include <sys/device.h>
     61      1.1  christos #include <sys/bus.h>
     62      1.1  christos #include <sys/pmf.h>
     63      1.1  christos 
     64      1.1  christos #include <dev/ic/tpmreg.h>
     65      1.1  christos #include <dev/ic/tpmvar.h>
     66      1.1  christos 
     67      1.1  christos #include <dev/acpi/acpireg.h>
     68      1.1  christos #include <dev/acpi/acpivar.h>
     69      1.1  christos 
     70      1.1  christos #include <dev/isa/isavar.h>
     71      1.1  christos 
     72      1.5  riastrad #include "ioconf.h"
     73      1.5  riastrad 
     74      1.1  christos #define _COMPONENT          ACPI_RESOURCE_COMPONENT
     75      1.1  christos ACPI_MODULE_NAME            ("tpm_acpi")
     76      1.1  christos 
     77      1.1  christos static int	tpm_acpi_match(device_t, cfdata_t, void *);
     78      1.1  christos static void	tpm_acpi_attach(device_t, device_t, void *);
     79      1.1  christos 
     80      1.1  christos 
     81      1.1  christos CFATTACH_DECL_NEW(tpm_acpi, sizeof(struct tpm_softc), tpm_acpi_match,
     82      1.1  christos     tpm_acpi_attach, NULL, NULL);
     83      1.1  christos 
     84      1.1  christos /*
     85      1.1  christos  * Supported device IDs
     86      1.1  christos  */
     87      1.1  christos 
     88      1.6  christos #ifdef notyet
     89      1.1  christos static const char * const tpm_acpi_ids[] = {
     90      1.1  christos 	"IFX0101",
     91      1.1  christos 	"IFX0102",
     92      1.1  christos 	NULL
     93      1.1  christos };
     94      1.6  christos #endif
     95      1.1  christos 
     96      1.1  christos static int
     97      1.1  christos tpm_acpi_match(device_t parent, cfdata_t match, void *aux)
     98      1.1  christos {
     99      1.1  christos 	struct acpi_attach_args *aa = aux;
    100      1.1  christos 
    101      1.1  christos 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    102      1.1  christos 		return 0;
    103      1.1  christos 
    104      1.1  christos 	/* There can be only one. */
    105      1.1  christos 	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
    106      1.1  christos 		return 0;
    107      1.1  christos #ifdef notyet
    108      1.3  christos 	return acpi_match_hid(aa->aa_node->ad_devinfo, tpm_acpi_ids);
    109      1.3  christos #else
    110      1.2  christos 	return 0;
    111      1.1  christos #endif
    112      1.1  christos }
    113      1.1  christos 
    114      1.1  christos static void
    115      1.1  christos tpm_acpi_attach(device_t parent, device_t self, void *aux)
    116      1.1  christos {
    117      1.1  christos 	struct tpm_softc *sc = device_private(self);
    118      1.1  christos 	struct acpi_attach_args *aa = aux;
    119      1.1  christos 	struct acpi_resources res;
    120      1.2  christos 	struct acpi_io *io;
    121      1.1  christos 	struct acpi_mem *mem;
    122      1.1  christos 	struct acpi_irq *irq;
    123      1.2  christos 	bus_addr_t base;
    124      1.2  christos 	bus_addr_t size;
    125      1.2  christos 	int rv, inum;
    126      1.1  christos 
    127      1.1  christos 	sc->sc_dev = self;
    128      1.1  christos 
    129      1.1  christos         /* Parse our resources */
    130      1.1  christos         rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", &res,
    131      1.1  christos             &acpi_resource_parse_ops_default);
    132      1.1  christos 
    133      1.1  christos         if (ACPI_FAILURE(rv)) {
    134      1.1  christos 		aprint_error_dev(sc->sc_dev, "cannot parse resources %d\n", rv);
    135      1.1  christos                 return;
    136      1.1  christos 	}
    137      1.1  christos 
    138      1.2  christos 	io = acpi_res_io(&res, 0);
    139      1.2  christos 	if (io && tpm_legacy_probe(aa->aa_iot, io->ar_base)) {
    140      1.2  christos 		sc->sc_bt = aa->aa_iot;
    141      1.2  christos 		base = io->ar_base;
    142      1.2  christos 		size = io->ar_length;
    143      1.2  christos 		sc->sc_batm = aa->aa_iot;
    144      1.2  christos 		sc->sc_init = tpm_legacy_init;
    145      1.2  christos 		sc->sc_start = tpm_legacy_start;
    146      1.2  christos 		sc->sc_read = tpm_legacy_read;
    147      1.2  christos 		sc->sc_write = tpm_legacy_write;
    148      1.2  christos 		sc->sc_end = tpm_legacy_end;
    149      1.2  christos 		mem = NULL;
    150      1.2  christos 	} else {
    151      1.2  christos 		mem = acpi_res_mem(&res, 0);
    152      1.2  christos 		if (mem == NULL) {
    153      1.2  christos 			aprint_error_dev(sc->sc_dev, "cannot find mem\n");
    154      1.2  christos 			goto out;
    155      1.2  christos 		}
    156      1.2  christos 
    157      1.2  christos 		if (mem->ar_length != TPM_SIZE) {
    158      1.2  christos 			aprint_error_dev(sc->sc_dev,
    159  1.6.4.1  christos 			    "wrong size mem %"PRIu64" != %u\n",
    160  1.6.4.1  christos 			    (uint64_t)mem->ar_length, TPM_SIZE);
    161      1.2  christos 			goto out;
    162      1.2  christos 		}
    163      1.2  christos 
    164      1.2  christos 		base = mem->ar_base;
    165      1.2  christos 		size = mem->ar_length;
    166      1.2  christos 		sc->sc_bt = aa->aa_memt;
    167      1.2  christos 		sc->sc_init = tpm_tis12_init;
    168      1.2  christos 		sc->sc_start = tpm_tis12_start;
    169      1.2  christos 		sc->sc_read = tpm_tis12_read;
    170      1.2  christos 		sc->sc_write = tpm_tis12_write;
    171      1.2  christos 		sc->sc_end = tpm_tis12_end;
    172      1.1  christos 	}
    173      1.1  christos 
    174      1.2  christos 	if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
    175      1.2  christos 		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
    176      1.1  christos 		goto out;
    177      1.1  christos 	}
    178      1.1  christos 
    179      1.2  christos 	if (mem && !tpm_tis12_probe(sc->sc_bt, sc->sc_bh)) {
    180      1.2  christos 		aprint_error_dev(sc->sc_dev, "1.2 probe failed\n");
    181      1.2  christos 		goto out1;
    182      1.1  christos 	}
    183      1.1  christos 
    184      1.1  christos 	irq = acpi_res_irq(&res, 0);
    185      1.2  christos 	if (irq == NULL)
    186      1.2  christos 		inum = -1;
    187      1.2  christos 	else
    188      1.2  christos 		inum = irq->ar_irq;
    189      1.1  christos 
    190      1.4      maxv 	if ((rv = (*sc->sc_init)(sc, inum, device_xname(sc->sc_dev))) != 0) {
    191      1.1  christos 		aprint_error_dev(sc->sc_dev, "cannot init device %d\n", rv);
    192      1.1  christos 		goto out1;
    193      1.4      maxv 	}
    194      1.1  christos 
    195      1.2  christos 	if (inum != -1 &&
    196      1.2  christos 	    (sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq,
    197      1.1  christos 	    IST_EDGE, IPL_TTY, tpm_intr, sc)) == NULL) {
    198      1.1  christos 		aprint_error_dev(sc->sc_dev, "cannot establish interrupt\n");
    199      1.1  christos 		goto out1;
    200      1.1  christos 	}
    201      1.1  christos 
    202      1.1  christos 	if (!pmf_device_register(sc->sc_dev, tpm_suspend, tpm_resume))
    203      1.1  christos 		aprint_error_dev(sc->sc_dev, "Cannot set power mgmt handler\n");
    204      1.1  christos 	return;
    205      1.1  christos out1:
    206      1.2  christos 	bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
    207      1.1  christos out:
    208      1.1  christos 	acpi_resource_cleanup(&res);
    209      1.1  christos }
    210