From patchwork Wed Jul 7 16:25:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 470810 Delivered-To: patch@linaro.org Received: by 2002:a02:c94a:0:0:0:0:0 with SMTP id u10csp6156382jao; Wed, 7 Jul 2021 09:26:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0Y35kDgqkJKGMKFNUfpyFgmLJ1QNKZvqXfJj8xWQeUQKbPwslkN8iEf9ZlA58wQTYBhoO X-Received: by 2002:a17:906:2f15:: with SMTP id v21mr24476149eji.220.1625675189808; Wed, 07 Jul 2021 09:26:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625675189; cv=none; d=google.com; s=arc-20160816; b=k6Zv54phvQjV+SzwvZAL8zba8OMUd/VOIJR23aG2VqqU61osjacibuQVyEM5kWrDZi tVtwA9z7DXEbwYWc0k8GiXlqlNEJgnGuAxzW1W1x/eVhKvOWotLuW0ue4CHSDqiq/gT6 R9e9eMDRkrE+daWD1JEB5NdoqoyhosZpr/iKWBB5vUb2RZgaX9zUq3PF1UF8e6pdRa7c YIPAjyKUR3dSdUV8U4J/GaVzIylbg22lX1Vk9uz8uC6ubJnaFP4BOkcIgAfP0uLPZrDq llEkFoNP06JfvD+PvzuVTNKY3guHDs3Z3RhnepF7sccF2tS9H+xJWFlQcA/eX+RGP6RN TDKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=crceJdheh/QQVgWojHS+0SYocJXu47rlr3fRJRbsuiI=; b=W4Y9/+VQAbUr2re8G4YXi7cJaruYllARm7OsMuZswxgAJaE6VQTHv9CP+Uq8wjF9La zNU1G32GFO7VzHZCw8gqwK4UHgCKy055E+LOI7PQG/2iETzSjPm4FYYFLQGAZHO1aeXE 04Xfhj+qMS0ItNl087riG/YB5lXvZ4p0L6mD+VGqvEsSZYfK1zLqYR9h2TrwzSEVa0QY vmyTI0Nw/rCvRHOz/1rvw5H8RmudsauNkJCbWXoNzZvQzaRsI5ulXtXR6SrBkVhu2yA6 0EzF13Nn/0ixWWqrVczCTSI+stQrOeSHzRwmxlJnPPbPTdD2vGX50WB+shQcEK685Z/V 55zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VAx5nXjA; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id g2si17859048ejp.350.2021.07.07.09.26.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 09:26:29 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VAx5nXjA; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 86FEC82E10; Wed, 7 Jul 2021 18:26:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="VAx5nXjA"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BAE0082E10; Wed, 7 Jul 2021 18:26:20 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 94F008009C for ; Wed, 7 Jul 2021 18:26:16 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-wm1-x332.google.com with SMTP id q18-20020a1ce9120000b02901f259f3a250so2092439wmc.2 for ; Wed, 07 Jul 2021 09:26:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=crceJdheh/QQVgWojHS+0SYocJXu47rlr3fRJRbsuiI=; b=VAx5nXjADwajNzg+LvfDVnozERuKFSBLe13kc52e2FxVggp1d8WYzXnpsGoQ/1tozq p8zWcsFqRhgKIIC4qKI0FncvFPID5kKtUzvDGAUP+ohay5xndQ+Ntv61+ihncbLMUFku gIjRDngdI+UfJybCMUhBmh7KNughG5kcTSFYefpfr2ni5UFr9Q8WCCp2owIsphacqjGw OWkjgUxFzv7qRlJpaPO2D13Km7QMUIQkvIB4KVIoomRyaTaNgeawMg9VTCaUtV1srbPO r34cateZp3L6/4hUKbUFISlbBpSsC/zesoToUVQRj+r0VeCj3ntV12F4I2VmGL/cl9xe dMLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=crceJdheh/QQVgWojHS+0SYocJXu47rlr3fRJRbsuiI=; b=KLcFXRZe0BCAncchZWSGw26q66h1QuvQQOgcPGEZBiXLA/CfBklYn3XlMKCaVnPg0j OLbC58hZUnFSbGIf598a2FxfcBuZjwu6MbZD4y1bsrEd5lDgDnV/MFcmz5daXEXhXx2R kG4AOtYUsgUlCATrST7kICmDUvp/QkwK0h17ubP0d5PG24FZ9D/bWVP+WNGMukyl8LrS AxGl8HdVm0KsA34gVbFoP7GADwawpXE+/62f63wE0Cc2CvZHaUdHWD6NzlCZ2sh4bbvH VDyFNOhlbyCf33Tez5OEZCnPltlf20pUe70ET2+kx2dFeAX6SUxT/CUtDByu19sf5W8v xYFg== X-Gm-Message-State: AOAM5319MmtRjp6sERWUyMljCsHNTUxl+VWX+3Y5bVWZUPtQTro7riqk zM2hX5JU4QmZPt+kmL/vwVRIyA== X-Received: by 2002:a1c:5985:: with SMTP id n127mr27531622wmb.75.1625675176146; Wed, 07 Jul 2021 09:26:16 -0700 (PDT) Received: from localhost.localdomain (ppp-94-66-242-227.home.otenet.gr. [94.66.242.227]) by smtp.gmail.com with ESMTPSA id c125sm3796838wme.36.2021.07.07.09.26.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 09:26:15 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de Cc: Ilias Apalodimas , Simon Glass , Johannes Holland , Masahisa Kojima , Dhananjay Phadke , u-boot@lists.denx.de Subject: [PATCH 2/2] tpm2: Add a TPMv2 MMIO TIS driver Date: Wed, 7 Jul 2021 19:25:59 +0300 Message-Id: <20210707162604.84196-2-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.32.0.rc0 In-Reply-To: <20210707162604.84196-1-ilias.apalodimas@linaro.org> References: <20210707162604.84196-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Add support for devices that expose a TPMv2 though MMIO. Apart from those devices, we can use the driver in our QEMU setups and test TPM related code which is difficult to achieve using the sandbox driver (e.g test the EFI TCG2 protocol). It's worth noting that a previous patch added TPMv2 TIS core functions, which the current driver is consuming. Signed-off-by: Ilias Apalodimas --- Changes since v1: - split off the tis core code into a different file drivers/tpm/Kconfig | 9 +++ drivers/tpm/Makefile | 1 + drivers/tpm/tpm2_tis_mmio.c | 156 ++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 drivers/tpm/tpm2_tis_mmio.c -- 2.32.0.rc0 diff --git a/drivers/tpm/Kconfig b/drivers/tpm/Kconfig index 9eebab5cfd90..406ee8716e1e 100644 --- a/drivers/tpm/Kconfig +++ b/drivers/tpm/Kconfig @@ -161,6 +161,15 @@ config TPM2_FTPM_TEE help This driver supports firmware TPM running in TEE. +config TPM2_MMIO + bool "MMIO based TPM2 Interface" + depends on TPM_V2 + help + This driver supports firmware TPM2.0 MMIO interface. + The usual TPM operations and the 'tpm' command can be used to talk + to the device using the standard TPM Interface Specification (TIS) + protocol. + endif # TPM_V2 endmenu diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile index f64d20067f88..1065c1874f58 100644 --- a/drivers/tpm/Makefile +++ b/drivers/tpm/Makefile @@ -14,3 +14,4 @@ obj-$(CONFIG_$(SPL_TPL_)TPM2_CR50_I2C) += cr50_i2c.o obj-$(CONFIG_TPM2_TIS_SANDBOX) += tpm2_tis_sandbox.o obj-$(CONFIG_TPM2_TIS_SPI) += tpm2_tis_spi.o obj-$(CONFIG_TPM2_FTPM_TEE) += tpm2_ftpm_tee.o +obj-$(CONFIG_TPM2_MMIO) += tpm2_tis_core.o tpm2_tis_mmio.o diff --git a/drivers/tpm/tpm2_tis_mmio.c b/drivers/tpm/tpm2_tis_mmio.c new file mode 100644 index 000000000000..2183a2807162 --- /dev/null +++ b/drivers/tpm/tpm2_tis_mmio.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * driver for mmio TCG/TIS TPM (trusted platform module). + * + * Specifications at www.trustedcomputinggroup.org + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tpm_tis.h" +#include "tpm_internal.h" + +struct tpm_tis_chip_data { + unsigned int pcr_count; + unsigned int pcr_select_min; + unsigned int time_before_first_cmd_ms; + void __iomem *iobase; +}; + +static int mmio_read_bytes(struct udevice *udev, u32 addr, u16 len, + u8 *result) +{ + struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(udev); + + while (len--) + *result++ = ioread8(drv_data->iobase + addr); + return 0; +} + +static int mmio_write_bytes(struct udevice *udev, u32 addr, u16 len, + const u8 *value) +{ + struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(udev); + + while (len--) + iowrite8(*value++, drv_data->iobase + addr); + return 0; +} + +static int mmio_read16(struct udevice *udev, u32 addr, u16 *result) +{ + struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(udev); + + *result = ioread16(drv_data->iobase + addr); + return 0; +} + +static int mmio_read32(struct udevice *udev, u32 addr, u32 *result) +{ + struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(udev); + + *result = ioread32(drv_data->iobase + addr); + return 0; +} + +static int mmio_write32(struct udevice *udev, u32 addr, u32 value) +{ + struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(udev); + + iowrite32(value, drv_data->iobase + addr); + return 0; +} + +static struct tpm_tis_phy_ops phy_ops = { + .read_bytes = mmio_read_bytes, + .write_bytes = mmio_write_bytes, + .read16 = mmio_read16, + .read32 = mmio_read32, + .write32 = mmio_write32, +}; + +static int tpm_tis_probe(struct udevice *udev) +{ + struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(udev); + struct tpm_chip_priv *priv = dev_get_uclass_priv(udev); + int ret = 0; + fdt_addr_t ioaddr; + u64 sz; + + ioaddr = dev_read_addr(udev); + if (ioaddr == FDT_ADDR_T_NONE) + return -EINVAL; + + ret = dev_read_u64(udev, "reg", &sz); + if (ret) + return -EINVAL; + + drv_data->iobase = ioremap(ioaddr, sz); + log_info("Remapped TPM2 base: 0x%llx size: 0x%llx\n", ioaddr, sz); + tpm_tis_ops_register(udev, &phy_ops); + ret = tpm_tis_init(udev); + if (ret) + goto iounmap; + + priv->pcr_count = drv_data->pcr_count; + priv->pcr_select_min = drv_data->pcr_select_min; + /* + * Although the driver probably works with a TPMv1 our Kconfig + * limits the driver to TPMv2 only + */ + priv->version = TPM_V2; + + return ret; +iounmap: + iounmap(drv_data->iobase); + return -EINVAL; +} + +static int tpm_tis_remove(struct udevice *udev) +{ + struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(udev); + + iounmap(drv_data->iobase); + return tpm_tis_cleanup(udev); +} + +static const struct tpm_ops tpm_tis_ops = { + .open = tpm_tis_open, + .close = tpm_tis_close, + .get_desc = tpm_tis_get_desc, + .send = tpm_tis_send, + .recv = tpm_tis_recv, + .cleanup = tpm_tis_cleanup, +}; + +static const struct tpm_tis_chip_data tpm_tis_std_chip_data = { + .pcr_count = 24, + .pcr_select_min = 3, +}; + +static const struct udevice_id tpm_tis_ids[] = { + { + .compatible = "tcg,tpm-tis-mmio", + .data = (ulong)&tpm_tis_std_chip_data, + }, + { } +}; + +U_BOOT_DRIVER(tpm_tis_mmio) = { + .name = "tpm_tis_mmio", + .id = UCLASS_TPM, + .of_match = tpm_tis_ids, + .ops = &tpm_tis_ops, + .probe = tpm_tis_probe, + .remove = tpm_tis_remove, + .priv_auto = sizeof(struct tpm_chip), +};