mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stefano Garzarella <sgarzare@redhat.com>
To: linux-coco@lists.linux.dev
Cc: Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Peter Huewe <peterhuewe@gmx.de>, "H. Peter Anvin" <hpa@zytor.com>,
	linux-integrity@vger.kernel.org,
	James Bottomley <James.Bottomley@HansenPartnership.com>,
	x86@kernel.org, Joerg Roedel <jroedel@suse.de>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	Jarkko Sakkinen <jarkko@kernel.org>,
	linux-kernel@vger.kernel.org, Ingo Molnar <mingo@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Claudio Carvalho <cclaudio@linux.ibm.com>,
	Dov Murik <dovmurik@linux.ibm.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Stefano Garzarella <sgarzare@redhat.com>
Subject: [PATCH 3/3] x86/sev: add a SVSM vTPM platform device
Date: Tue, 10 Dec 2024 15:34:23 +0100	[thread overview]
Message-ID: <20241210143423.101774-4-sgarzare@redhat.com> (raw)
In-Reply-To: <20241210143423.101774-1-sgarzare@redhat.com>

From: James Bottomley <James.Bottomley@HansenPartnership.com>

If the SNP boot has a SVSM, probe for the vTPM device by sending a
SVSM_VTPM_QUERY call (function 8). The SVSM will return a bitmap with
the TPM_SEND_COMMAND bit set only if the vTPM is present and it is able
to handle TPM commands at runtime.

If a vTPM is found, register a platform device as "platform:tpm" so it
can be attached to the tpm_platform.c driver.

Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
[CC] Used SVSM_VTPM_QUERY to probe the TPM
Signed-off-by: Claudio Carvalho <cclaudio@linux.ibm.com>
[SG] Code adjusted with some changes introduced in 6.11
[SG] Used macro for SVSM_VTPM_CALL
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
 arch/x86/coco/sev/core.c | 64 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index c5b0148b8c0a..ec0153fddc9e 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -21,6 +21,7 @@
 #include <linux/cpumask.h>
 #include <linux/efi.h>
 #include <linux/platform_device.h>
+#include <linux/tpm_platform.h>
 #include <linux/io.h>
 #include <linux/psp-sev.h>
 #include <linux/dmi.h>
@@ -2578,6 +2579,51 @@ static struct platform_device sev_guest_device = {
 	.id		= -1,
 };
 
+static struct platform_device tpm_device = {
+	.name		= "tpm",
+	.id		= -1,
+};
+
+static int snp_issue_svsm_vtpm_send_command(u8 *buffer)
+{
+	struct svsm_call call = {};
+
+	call.caa = svsm_get_caa();
+	call.rax = SVSM_VTPM_CALL(SVSM_VTPM_CMD);
+	call.rcx = __pa(buffer);
+
+	return svsm_perform_call_protocol(&call);
+}
+
+static bool is_svsm_vtpm_send_command_supported(void)
+{
+	struct svsm_call call = {};
+	u64 send_cmd_mask = 0;
+	u64 platform_cmds;
+	u64 features;
+	int ret;
+
+	call.caa = svsm_get_caa();
+	call.rax = SVSM_VTPM_CALL(SVSM_VTPM_QUERY);
+
+	ret = svsm_perform_call_protocol(&call);
+
+	if (ret != SVSM_SUCCESS)
+		return false;
+
+	features = call.rdx_out;
+	platform_cmds = call.rcx_out;
+
+	/* No feature supported, it must be zero */
+	if (features)
+		return false;
+
+	/* TPM_SEND_COMMAND - platform command 8 */
+	send_cmd_mask = 1 << 8;
+
+	return (platform_cmds & send_cmd_mask) == send_cmd_mask;
+}
+
 static int __init snp_init_platform_device(void)
 {
 	struct sev_guest_platform_data data;
@@ -2593,6 +2639,24 @@ static int __init snp_init_platform_device(void)
 		return -ENODEV;
 
 	pr_info("SNP guest platform device initialized.\n");
+
+	/*
+	 * The VTPM device is available only if we have a SVSM and
+	 * its VTPM supports the TPM_SEND_COMMAND platform command
+	 */
+	if (IS_ENABLED(CONFIG_TCG_PLATFORM) && snp_vmpl &&
+	    is_svsm_vtpm_send_command_supported()) {
+		struct tpm_platform_ops pops = {
+			.sendrcv = snp_issue_svsm_vtpm_send_command,
+		};
+
+		if (platform_device_add_data(&tpm_device, &pops, sizeof(pops)))
+			return -ENODEV;
+		if (platform_device_register(&tpm_device))
+			return -ENODEV;
+		pr_info("SNP SVSM VTPM platform device initialized\n");
+	}
+
 	return 0;
 }
 device_initcall(snp_init_platform_device);
-- 
2.47.1


  parent reply	other threads:[~2024-12-10 14:35 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-10 14:34 [PATCH 0/3] Enlightened vTPM support for SVSM on SEV-SNP Stefano Garzarella
2024-12-10 14:34 ` [PATCH 1/3] tpm: add generic platform device Stefano Garzarella
2024-12-12  9:51   ` Stefano Garzarella
2024-12-12 14:35     ` James Bottomley
2024-12-12 15:30       ` Stefano Garzarella
2024-12-12 15:41         ` James Bottomley
2024-12-12 16:12           ` Stefano Garzarella
2024-12-10 14:34 ` [PATCH 2/3] x86/sev: add SVSM call macros for the vTPM protocol Stefano Garzarella
2024-12-10 14:34 ` Stefano Garzarella [this message]
2024-12-10 14:40   ` [PATCH 3/3] x86/sev: add a SVSM vTPM platform device Jason Gunthorpe
2024-12-10 14:55     ` James Bottomley
2024-12-10 15:04       ` Jason Gunthorpe
2024-12-11  8:19         ` Stefano Garzarella
2024-12-11 15:00           ` Jason Gunthorpe
2024-12-11 15:38             ` Stefano Garzarella
2024-12-11 15:53               ` Jason Gunthorpe
2024-12-11 16:42                 ` Stefano Garzarella
2024-12-19 15:35             ` Stefano Garzarella
2024-12-19 15:40               ` Jarkko Sakkinen
2024-12-19 16:06                 ` Stefano Garzarella
2025-01-14 10:42                   ` Stefano Garzarella
2025-01-14 13:07                     ` Jason Gunthorpe
2025-01-14 16:51                       ` Stefano Garzarella
2025-01-14 17:33                         ` Jason Gunthorpe
2025-01-14 22:46                     ` Jarkko Sakkinen
2025-01-14 22:48                       ` Jarkko Sakkinen
2025-01-14 23:12                         ` Jarkko Sakkinen
2025-01-22 21:29                           ` Dionna Amalie Glaze
2025-01-23  9:50                             ` Jarkko Sakkinen
2025-01-23 10:09                               ` Stefano Garzarella
2025-01-23 11:46                                 ` Jarkko Sakkinen
2025-01-23 11:49                                   ` Jarkko Sakkinen
2025-01-23 12:29                                     ` Stefano Garzarella
2024-12-11 16:30   ` Tom Lendacky
2024-12-11 16:55     ` Stefano Garzarella
2024-12-11 17:02     ` James Bottomley
2024-12-13 11:48       ` Stefano Garzarella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241210143423.101774-4-sgarzare@redhat.com \
    --to=sgarzare@redhat.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=bp@alien8.de \
    --cc=cclaudio@linux.ibm.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=dovmurik@linux.ibm.com \
    --cc=hpa@zytor.com \
    --cc=jarkko@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=jroedel@suse.de \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterhuewe@gmx.de \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®