Skip to content

Commit 93b7c6b

Browse files
stefano-garzarellabp3tk0v
authored andcommitted
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 platform, 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(), the .status() callback is not implemented as recently supported by commit 980a573 ("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 <sgarzare@redhat.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Link: https://lore.kernel.org/r/20250410135118.133240-4-sgarzare@redhat.com
1 parent b2849b0 commit 93b7c6b

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

drivers/char/tpm/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,15 @@ config TCG_FTPM_TEE
234234
help
235235
This driver proxies for firmware TPM running in TEE.
236236

237+
config TCG_SVSM
238+
tristate "SNP SVSM vTPM interface"
239+
depends on AMD_MEM_ENCRYPT
240+
help
241+
This is a driver for the AMD SVSM vTPM protocol that a SEV-SNP guest
242+
OS can use to discover and talk to a vTPM emulated by the Secure VM
243+
Service Module (SVSM) in the guest context, but at a more privileged
244+
level (usually VMPL0). To compile this driver as a module, choose M
245+
here; the module will be called tpm_svsm.
246+
237247
source "drivers/char/tpm/st33zp24/Kconfig"
238248
endif # TCG_TPM

drivers/char/tpm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ obj-$(CONFIG_TCG_CRB) += tpm_crb.o
4545
obj-$(CONFIG_TCG_ARM_CRB_FFA) += tpm_crb_ffa.o
4646
obj-$(CONFIG_TCG_VTPM_PROXY) += tpm_vtpm_proxy.o
4747
obj-$(CONFIG_TCG_FTPM_TEE) += tpm_ftpm_tee.o
48+
obj-$(CONFIG_TCG_SVSM) += tpm_svsm.o

drivers/char/tpm/tpm_svsm.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
4+
*
5+
* Driver for the vTPM defined by the AMD SVSM spec [1].
6+
*
7+
* The specification defines a protocol that a SEV-SNP guest OS can use to
8+
* discover and talk to a vTPM emulated by the Secure VM Service Module (SVSM)
9+
* in the guest context, but at a more privileged level (usually VMPL0).
10+
*
11+
* [1] "Secure VM Service Module for SEV-SNP Guests"
12+
* Publication # 58019 Revision: 1.00
13+
*/
14+
15+
#include <linux/module.h>
16+
#include <linux/kernel.h>
17+
#include <linux/platform_device.h>
18+
#include <linux/tpm_svsm.h>
19+
20+
#include <asm/sev.h>
21+
22+
#include "tpm.h"
23+
24+
struct tpm_svsm_priv {
25+
void *buffer;
26+
};
27+
28+
static int tpm_svsm_send(struct tpm_chip *chip, u8 *buf, size_t len)
29+
{
30+
struct tpm_svsm_priv *priv = dev_get_drvdata(&chip->dev);
31+
int ret;
32+
33+
ret = svsm_vtpm_cmd_request_fill(priv->buffer, 0, buf, len);
34+
if (ret)
35+
return ret;
36+
37+
/*
38+
* The SVSM call uses the same buffer for the command and for the
39+
* response, so after this call, the buffer will contain the response
40+
* that can be used by .recv() op.
41+
*/
42+
return snp_svsm_vtpm_send_command(priv->buffer);
43+
}
44+
45+
static int tpm_svsm_recv(struct tpm_chip *chip, u8 *buf, size_t len)
46+
{
47+
struct tpm_svsm_priv *priv = dev_get_drvdata(&chip->dev);
48+
49+
/*
50+
* The internal buffer contains the response after we send the command
51+
* to SVSM.
52+
*/
53+
return svsm_vtpm_cmd_response_parse(priv->buffer, buf, len);
54+
}
55+
56+
static struct tpm_class_ops tpm_chip_ops = {
57+
.flags = TPM_OPS_AUTO_STARTUP,
58+
.recv = tpm_svsm_recv,
59+
.send = tpm_svsm_send,
60+
};
61+
62+
static int __init tpm_svsm_probe(struct platform_device *pdev)
63+
{
64+
struct device *dev = &pdev->dev;
65+
struct tpm_svsm_priv *priv;
66+
struct tpm_chip *chip;
67+
int err;
68+
69+
priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL);
70+
if (!priv)
71+
return -ENOMEM;
72+
73+
/*
74+
* The maximum buffer supported is one page (see SVSM_VTPM_MAX_BUFFER
75+
* in tpm_svsm.h).
76+
*/
77+
priv->buffer = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
78+
if (!priv->buffer)
79+
return -ENOMEM;
80+
81+
chip = tpmm_chip_alloc(dev, &tpm_chip_ops);
82+
if (IS_ERR(chip))
83+
return PTR_ERR(chip);
84+
85+
dev_set_drvdata(&chip->dev, priv);
86+
87+
err = tpm2_probe(chip);
88+
if (err)
89+
return err;
90+
91+
err = tpm_chip_register(chip);
92+
if (err)
93+
return err;
94+
95+
dev_info(dev, "SNP SVSM vTPM %s device\n",
96+
(chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2");
97+
98+
return 0;
99+
}
100+
101+
static void __exit tpm_svsm_remove(struct platform_device *pdev)
102+
{
103+
struct tpm_chip *chip = platform_get_drvdata(pdev);
104+
105+
tpm_chip_unregister(chip);
106+
}
107+
108+
/*
109+
* tpm_svsm_remove() lives in .exit.text. For drivers registered via
110+
* module_platform_driver_probe() this is ok because they cannot get unbound
111+
* at runtime. So mark the driver struct with __refdata to prevent modpost
112+
* triggering a section mismatch warning.
113+
*/
114+
static struct platform_driver tpm_svsm_driver __refdata = {
115+
.remove = __exit_p(tpm_svsm_remove),
116+
.driver = {
117+
.name = "tpm-svsm",
118+
},
119+
};
120+
121+
module_platform_driver_probe(tpm_svsm_driver, tpm_svsm_probe);
122+
123+
MODULE_DESCRIPTION("SNP SVSM vTPM Driver");
124+
MODULE_LICENSE("GPL");
125+
MODULE_ALIAS("platform:tpm-svsm");

0 commit comments

Comments
 (0)