From nobody Sun Feb 8 13:45:36 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E093E278E51; Fri, 11 Apr 2025 10:15:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744366548; cv=none; b=N+ba5noYnzRsu/66kJrpLFVbmdjiDSgvWs7XMPj0+V4HcHWkI9mwJpGbM2iaU3PINmjNvPZU5xOzicLgotlOVQBSiPjAoEqOG+BfFEbBwan+7GOZl+vZjn/rS4zDyC3/zlID2GDAxYT4HAxZJvzEW3rhHNQ+AufZWV5qRbSAJEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744366548; c=relaxed/simple; bh=Jh8DD3IFc9zI0c4Jtpz8t6t+NCItFvt6dAI799oyBXc=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=JwXDzpNpF4fGL/noNEgvH33giNP57hj6H25ye7z8tj8k8LZDQNPk6rQRyNpUsV9dRMrHVfNtSWtP7DTdCzemhX7+7dBDU4b7oMzywWq0xjt8x+mOn2RRRTYpTdx5STd8LVOTIuX2Fb59kfo46Mquok0w2Q/rGLqUa5XKfiNiL10= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tEItYojZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qEIHPgS1; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tEItYojZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qEIHPgS1" Date: Fri, 11 Apr 2025 10:15:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1744366542; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sVZR7fCMZ3UbVw/IWtglDPHL2+xVWxpNNBaVWQ5QBOY=; b=tEItYojZVFUCPjAsl7z29sQwwWHnwDLK1tpyiYQ7dNH4zJXeq4RajBzSXpnhA4MPalD5NQ gS0tPOyrSeV65YFEveWYMT1LID4Cf8AoUcrPxKOuNS9NEQLu0DPX1+lKAcSsMfM72RRN0W R09hnPq8t3n6Cmc+y4b4mzs0dY338RKEhXpeX46KzGSVExk0E6c0rDW5ZBt3MJI7WLlc2t DKTB3K2sBj5FKjrJi1tR+Xc/sIUMJwcEp2QftwI4QKIGhUv4pSKeIQRFplNLDCDUTymxwU sOKXQSFHMb+vTZW+t5Qwgnv5wCqBwwS+BDe1hZsbbGT7Q9ijFE0N93MFqOz98Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1744366542; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sVZR7fCMZ3UbVw/IWtglDPHL2+xVWxpNNBaVWQ5QBOY=; b=qEIHPgS1BhwdB6EsQi/pjm1XUnUvE+/E3YsnnW5//+1yFCUhfwIrhz+7XKzmZoojdifbvK XL6ETw5OScasklCA== From: "tip-bot2 for Stefano Garzarella" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/sev] tpm: Add SNP SVSM vTPM driver Cc: Stefano Garzarella , "Borislav Petkov (AMD)" , Jarkko Sakkinen , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20250410135118.133240-4-sgarzare@redhat.com> References: <20250410135118.133240-4-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <174436654133.31282.1172867310137729031.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the x86/sev branch of tip: Commit-ID: 93b7c6b3ce918c3d24de82dcff7a87b8bd631b2e Gitweb: https://git.kernel.org/tip/93b7c6b3ce918c3d24de82dcff7a87b8b= d631b2e Author: Stefano Garzarella AuthorDate: Thu, 10 Apr 2025 15:51:15 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Thu, 10 Apr 2025 16:24:29 +02:00 tpm: Add SNP SVSM vTPM driver Add driver for the vTPM defined by the AMD SVSM spec [1]. The specification defines a protocol that a SEV-SNP guest OS can use to discover and talk to a vTPM emulated by the Secure VM Service Module (SVSM)= in the guest context, but at a more privileged level (VMPL0). The new tpm-svsm platform driver uses API exposed by the x86/sev core implementation interface to a SVSM to send commands and receive responses. The device cannot be hot-plugged/unplugged as it is emulated by the platfor= m, so module_platform_driver_probe() can be used. The device will be registered by the platform only when it's available, so the probe function just needs = to setup the tpm_chip. This device does not support interrupts and sends responses to commands synchronously. In order to have .recv() called just after .send() in tpm_try_transmit(), t= he .status() callback is not implemented as recently supported by commit 980a573621ea ("tpm: Make chip->{status,cancel,req_canceled} opt"). [1] "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 [ bp: Massage commit message. ] Signed-off-by: Stefano Garzarella Signed-off-by: Borislav Petkov (AMD) Reviewed-by: Jarkko Sakkinen Link: https://lore.kernel.org/r/20250410135118.133240-4-sgarzare@redhat.com --- drivers/char/tpm/Kconfig | 10 +++- drivers/char/tpm/Makefile | 1 +- drivers/char/tpm/tpm_svsm.c | 125 +++++++++++++++++++++++++++++++++++- 3 files changed, 136 insertions(+) create mode 100644 drivers/char/tpm/tpm_svsm.c diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig index fe4f3a6..dddd702 100644 --- a/drivers/char/tpm/Kconfig +++ b/drivers/char/tpm/Kconfig @@ -234,5 +234,15 @@ config TCG_FTPM_TEE help This driver proxies for firmware TPM running in TEE. =20 +config TCG_SVSM + tristate "SNP SVSM vTPM interface" + depends on AMD_MEM_ENCRYPT + help + This is a driver for the AMD SVSM vTPM protocol that a SEV-SNP guest + OS can use to discover and talk to a vTPM emulated by the Secure VM + Service Module (SVSM) in the guest context, but at a more privileged + level (usually VMPL0). To compile this driver as a module, choose M + here; the module will be called tpm_svsm. + source "drivers/char/tpm/st33zp24/Kconfig" endif # TCG_TPM diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile index 2b004df..9de1b3e 100644 --- a/drivers/char/tpm/Makefile +++ b/drivers/char/tpm/Makefile @@ -45,3 +45,4 @@ obj-$(CONFIG_TCG_CRB) +=3D tpm_crb.o obj-$(CONFIG_TCG_ARM_CRB_FFA) +=3D tpm_crb_ffa.o obj-$(CONFIG_TCG_VTPM_PROXY) +=3D tpm_vtpm_proxy.o obj-$(CONFIG_TCG_FTPM_TEE) +=3D tpm_ftpm_tee.o +obj-$(CONFIG_TCG_SVSM) +=3D tpm_svsm.o diff --git a/drivers/char/tpm/tpm_svsm.c b/drivers/char/tpm/tpm_svsm.c new file mode 100644 index 0000000..4280edf --- /dev/null +++ b/drivers/char/tpm/tpm_svsm.c @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved. + * + * Driver for the vTPM defined by the AMD SVSM spec [1]. + * + * The specification defines a protocol that a SEV-SNP guest OS can use to + * discover and talk to a vTPM emulated by the Secure VM Service Module (S= VSM) + * in the guest context, but at a more privileged level (usually VMPL0). + * + * [1] "Secure VM Service Module for SEV-SNP Guests" + * Publication # 58019 Revision: 1.00 + */ + +#include +#include +#include +#include + +#include + +#include "tpm.h" + +struct tpm_svsm_priv { + void *buffer; +}; + +static int tpm_svsm_send(struct tpm_chip *chip, u8 *buf, size_t len) +{ + struct tpm_svsm_priv *priv =3D dev_get_drvdata(&chip->dev); + int ret; + + ret =3D svsm_vtpm_cmd_request_fill(priv->buffer, 0, buf, len); + if (ret) + return ret; + + /* + * The SVSM call uses the same buffer for the command and for the + * response, so after this call, the buffer will contain the response + * that can be used by .recv() op. + */ + return snp_svsm_vtpm_send_command(priv->buffer); +} + +static int tpm_svsm_recv(struct tpm_chip *chip, u8 *buf, size_t len) +{ + struct tpm_svsm_priv *priv =3D dev_get_drvdata(&chip->dev); + + /* + * The internal buffer contains the response after we send the command + * to SVSM. + */ + return svsm_vtpm_cmd_response_parse(priv->buffer, buf, len); +} + +static struct tpm_class_ops tpm_chip_ops =3D { + .flags =3D TPM_OPS_AUTO_STARTUP, + .recv =3D tpm_svsm_recv, + .send =3D tpm_svsm_send, +}; + +static int __init tpm_svsm_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct tpm_svsm_priv *priv; + struct tpm_chip *chip; + int err; + + priv =3D devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + /* + * The maximum buffer supported is one page (see SVSM_VTPM_MAX_BUFFER + * in tpm_svsm.h). + */ + priv->buffer =3D (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); + if (!priv->buffer) + return -ENOMEM; + + chip =3D tpmm_chip_alloc(dev, &tpm_chip_ops); + if (IS_ERR(chip)) + return PTR_ERR(chip); + + dev_set_drvdata(&chip->dev, priv); + + err =3D tpm2_probe(chip); + if (err) + return err; + + err =3D tpm_chip_register(chip); + if (err) + return err; + + dev_info(dev, "SNP SVSM vTPM %s device\n", + (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2"); + + return 0; +} + +static void __exit tpm_svsm_remove(struct platform_device *pdev) +{ + struct tpm_chip *chip =3D platform_get_drvdata(pdev); + + tpm_chip_unregister(chip); +} + +/* + * tpm_svsm_remove() lives in .exit.text. For drivers registered via + * module_platform_driver_probe() this is ok because they cannot get unbou= nd + * at runtime. So mark the driver struct with __refdata to prevent modpost + * triggering a section mismatch warning. + */ +static struct platform_driver tpm_svsm_driver __refdata =3D { + .remove =3D __exit_p(tpm_svsm_remove), + .driver =3D { + .name =3D "tpm-svsm", + }, +}; + +module_platform_driver_probe(tpm_svsm_driver, tpm_svsm_probe); + +MODULE_DESCRIPTION("SNP SVSM vTPM Driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:tpm-svsm");