From nobody Fri Dec 19 15:57:11 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 CD07528C5B0 for ; Thu, 10 Apr 2025 13:51:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744293092; cv=none; b=iUM4m7c1KtfKkfEtrLWo9jz15MVeYLDDNwo3dH3RZIWUsJcsrUUwTeJrtPbEqFLdwqjzzX5qEN/FkWD7HiHymjplMn3ryrbHYR79Zv7xYnNEKmWXVlq1WYM29B0VXykP/Do4QkPZtpxIh1TEKIu/n6qG9KZqIyOrHg6aliByCDM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744293092; c=relaxed/simple; bh=RqYMqp3Ujj1yablcXK4m8qctcMpdGLtxgb9YCEHWtfE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=juCNwbAlf1E4NRLKZ25PnCnyaD1AoNziWArGfftciMFJVLqUHMTne+U8os1kLxIVkgSneqnes6hbrIjGfu2sJ9Hi9sfuiPKbkJDs6s+ZFHTJWtI+G+sI2j444Eq2EeNZODct48ELpB6UOLnWoLM3r5IZuGGL48JNRup41eJI7/8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZDfrvNTe; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZDfrvNTe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744293089; h=from:from: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=2hayB2itOF52swyYqnft2InIWkfIXl2u+4tuyLp7J20=; b=ZDfrvNTehm32kZ2cAkP0+A9VzODYpxIXD/ZsQAUX+AxdqkZVeo5pQtjrg+3aReLM8bJlJ4 aMDQvetEzAWm+AMjBPOd9cgtzm1DDy1PIqkQFIQWPYJghUqnzRkkzvdiq4kyYJm4DMs2HM DM6MIfVjuWmC2MI4xJUcKCrjhXhJ6oQ= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-523-HtK8R6F3PN2m_fTKIPVl4Q-1; Thu, 10 Apr 2025 09:51:28 -0400 X-MC-Unique: HtK8R6F3PN2m_fTKIPVl4Q-1 X-Mimecast-MFC-AGG-ID: HtK8R6F3PN2m_fTKIPVl4Q_1744293087 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-39134c762ebso368232f8f.0 for ; Thu, 10 Apr 2025 06:51:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744293087; x=1744897887; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2hayB2itOF52swyYqnft2InIWkfIXl2u+4tuyLp7J20=; b=U0WNS/fSJliUgAl51kakLx4FTVwezs5E8RqjWyapN59YMm+SeUJS55ne64JaFDPQLx oLIsSdcTMFi2tMpJ1Qq7xaSGSrXl+ILTxSFttoX8JhqDr6i+qXBUyQMT3YjXp1oYuc0J JLbVl2hd65Lx9U736oJJcBfgQ6YhWY95UQ2TSs/b3+lEEf6PVaF5TpFjGVwhakxRjm50 JnqA8dR5pmO1h+n20rony5aoan709S/H7mYqfv8FP5NwNzxWDClVEHq0ikPSPFZa9Oho zFAD+zOOYwTqOv5X1ET3mbHCAJlmItBQUgtPNxTsrgAboJNESZmsF+83QwzTkSYIjPzf mByw== X-Forwarded-Encrypted: i=1; AJvYcCUNUVMT1c63hMYUn93HBvdvFLODoVf8E0NrtlpHxdv1ZNhwY/ib8Gx91bxVhteF1z4X/tPu8xCi8EoRGJA=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5HxoKS/W+vAV6Q5EjGE77EX0R+MHAAmZgltqOP3j62WxgEtU2 iWRcT+Bgk9tIsDfJc3QleV9bJ8oo4BjJcTibhABk/XrD4HJwcKQP559Z7BdHECLJ3oWs5r0G3bO 5LyjJKRLWaiuiZyDqmT/R6Z7QfCpZTbRjt6ZFkR8Gnzq0rTDqyFn8tRUjfnaFQQ== X-Gm-Gg: ASbGncvbXI+r59V86bdIoi8BJ12bujXq4G27IeEHVxK25FQt4rPUElc+opYBUYRl71R oKOFkKzENKe8Bh62/mYJqrVxLp7A8l4azXsXvfqkglg8W2bBQZ37Ekf+FNzC/EmoY0PsWH4EUVt GYidevbnw6x7KliflcmRyNsg4InDc1sL1GDQthP9RN241p9GlQ4kghGmF5mwAQvfxbpgc+L86/J pgTG9YtKA1gO7/FQEZ9Ey6NbD/JYpgKwBp5OWE5mWkQh2Wyp4BcR1KZnLBvERlI2jIIuYuIIXUu zgam0FAewdWu1BgkUa/7dqGKMbd9gD4nEr9TSilOqBJUX7YOTi2JrQkAzi4khA== X-Received: by 2002:a05:6000:2cd:b0:39a:ca04:3e4d with SMTP id ffacd0b85a97d-39d8f273573mr2757997f8f.7.1744293087101; Thu, 10 Apr 2025 06:51:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH74r2mCwQMz4b5b40UpkJVX/gWuuE0hyyRu8AvG8fHXAQ9RwrpeFNISL0qnwEahYkR49vqdg== X-Received: by 2002:a05:6000:2cd:b0:39a:ca04:3e4d with SMTP id ffacd0b85a97d-39d8f273573mr2757948f8f.7.1744293086504; Thu, 10 Apr 2025 06:51:26 -0700 (PDT) Received: from stex1.redhat.com (host-79-53-30-213.retail.telecomitalia.it. [79.53.30.213]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39d8938a761sm4932104f8f.51.2025.04.10.06.51.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 06:51:25 -0700 (PDT) From: Stefano Garzarella To: Borislav Petkov , Jarkko Sakkinen Cc: Joerg Roedel , "H. Peter Anvin" , Dionna Glaze , Ingo Molnar , linux-kernel@vger.kernel.org, Dov Murik , linux-integrity@vger.kernel.org, Dave Hansen , Claudio Carvalho , Tom Lendacky , Thomas Gleixner , Peter Huewe , Jason Gunthorpe , x86@kernel.org, James Bottomley , linux-coco@lists.linux.dev, Stefano Garzarella Subject: [PATCH v7 1/4] x86/sev: add SVSM vTPM probe/send_command functions Date: Thu, 10 Apr 2025 15:51:13 +0200 Message-ID: <20250410135118.133240-2-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250410135118.133240-1-sgarzare@redhat.com> References: <20250410135118.133240-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Stefano Garzarella Add two new functions to probe and send commands to the SVSM vTPM. They leverage the two calls defined by the AMD SVSM specification [1] for the vTPM protocol: SVSM_VTPM_QUERY and SVSM_VTPM_CMD. Expose snp_svsm_vtpm_send_command() to be used by a tpm driver. [1] "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 Co-developed-by: James Bottomley Signed-off-by: James Bottomley Co-developed-by: Claudio Carvalho Signed-off-by: Claudio Carvalho Reviewed-by: Tom Lendacky Reviewed-by: Jarkko Sakkinen Signed-off-by: Stefano Garzarella --- v7: - avoided exporting snp_svsm_vtpm_probe() because we will only use it internally v5: - added stubs when !CONFIG_AMD_MEM_ENCRYPT [Dionna] - added Jarkko's R-b v4: - added Tom's R-b - added functions documentation [Jarkko] - simplified TPM_SEND_COMMAND check [Tom/Jarkko] v3: - removed link to the spec because those URLs are unstable [Borislav] - squashed "x86/sev: add SVSM call macros for the vTPM protocol" patch in this one [Borislav] - slimmed down snp_svsm_vtpm_probe() [Borislav] - removed features check and any print related [Tom] --- arch/x86/include/asm/sev.h | 7 +++++ arch/x86/coco/sev/core.c | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ba7999f66abe..d9ba035d8609 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -384,6 +384,10 @@ struct svsm_call { #define SVSM_ATTEST_SERVICES 0 #define SVSM_ATTEST_SINGLE_SERVICE 1 =20 +#define SVSM_VTPM_CALL(x) ((2ULL << 32) | (x)) +#define SVSM_VTPM_QUERY 0 +#define SVSM_VTPM_CMD 1 + #ifdef CONFIG_AMD_MEM_ENCRYPT =20 extern u8 snp_vmpl; @@ -481,6 +485,8 @@ void snp_msg_free(struct snp_msg_desc *mdesc); int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_re= q *req, struct snp_guest_request_ioctl *rio); =20 +int snp_svsm_vtpm_send_command(u8 *buffer); + void __init snp_secure_tsc_prepare(void); void __init snp_secure_tsc_init(void); =20 @@ -524,6 +530,7 @@ static inline struct snp_msg_desc *snp_msg_alloc(void) = { return NULL; } static inline void snp_msg_free(struct snp_msg_desc *mdesc) { } static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struc= t snp_guest_req *req, struct snp_guest_request_ioctl *rio) { return -ENODEV; } +static inline int snp_svsm_vtpm_send_command(u8 *buffer) { return -ENODEV;= } static inline void __init snp_secure_tsc_prepare(void) { } static inline void __init snp_secure_tsc_init(void) { } =20 diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index b0c1a7a57497..3bc5b47e7304 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -2625,6 +2625,64 @@ static int snp_issue_guest_request(struct snp_guest_= req *req, struct snp_req_dat return ret; } =20 +/** + * snp_svsm_vtpm_probe() - Probe if SVSM provides a vTPM device + * + * This function checks that there is SVSM and that it supports at least + * TPM_SEND_COMMAND which is the only request we use so far. + * + * Return: true if the platform provides a vTPM SVSM device, false otherwi= se. + */ +static bool snp_svsm_vtpm_probe(void) +{ + struct svsm_call call =3D {}; + + /* The vTPM device is available only if a SVSM is present */ + if (!snp_vmpl) + return false; + + call.caa =3D svsm_get_caa(); + call.rax =3D SVSM_VTPM_CALL(SVSM_VTPM_QUERY); + + if (svsm_perform_call_protocol(&call)) + return false; + + /* Check platform commands contains TPM_SEND_COMMAND - platform command 8= */ + return call.rcx_out & BIT_ULL(8); +} + +/** + * snp_svsm_vtpm_send_command() - execute a vTPM operation on SVSM + * @buffer: A buffer used to both send the command and receive the respons= e. + * + * This function executes a SVSM_VTPM_CMD call as defined by + * "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revis= ion: 1.00 + * + * All command request/response buffers have a common structure as specifi= ed by + * the following table: + * Byte Size =C2=A0=C2=A0 =C2=A0In/Out=C2=A0=C2=A0=C2=A0=C2=A0D= escription + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0In=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Platform command +=C2=A0*=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Out=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Platform command respons= e size + * + * Each command can build upon this common request/response structure to c= reate + * a structure specific to the command. + * See include/linux/tpm_svsm.h for more details. + * + * Return: 0 on success, -errno on failure + */ +int snp_svsm_vtpm_send_command(u8 *buffer) +{ + struct svsm_call call =3D {}; + + call.caa =3D svsm_get_caa(); + call.rax =3D SVSM_VTPM_CALL(SVSM_VTPM_CMD); + call.rcx =3D __pa(buffer); + + return svsm_perform_call_protocol(&call); +} +EXPORT_SYMBOL_GPL(snp_svsm_vtpm_send_command); + static struct platform_device sev_guest_device =3D { .name =3D "sev-guest", .id =3D -1, --=20 2.49.0 From nobody Fri Dec 19 15:57:11 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 08C8E28C5BC for ; Thu, 10 Apr 2025 13:51:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744293098; cv=none; b=IbdnS+6LL/ZQLQ9WNBxiHeEK0RdqAxTz3ehlYXut1lfMuDQRwNJc+l+5qoniClZ/8v++iPRGVGnc7BbflDaG3L3Rw854YHJD9STxyT7OjOgJRqMu8TPqd/+vct0p4Cw1LNNrZtjDCFUmsPwAgo6ajUFIWkyVk1PurtNZ5dYVNjU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744293098; c=relaxed/simple; bh=JanhuPR/iml68kEFk6VsdfPYwqYcdQYvtjajdTMi5bI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ivmr5bkpcENWHMQYoukODqQ6fmDfq2Ob0wlyUJTSUH6vKggPAzuTLuosjd581Avj4S2g0K1ixh/T13giX2r5sUEsGSqqLHEU5bL2b8F73NQz/3D5IiZOk5deHqY6JkfNb88aDXRDZG6zPvZbymqg57o7/bZh0+HeXjHvcl58SlY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dqRwBhmn; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dqRwBhmn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744293095; h=from:from: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=9cwx8PB++1jPTpqj+0lA5MhEHp2F2B5hQ/5H2FoJu9E=; b=dqRwBhmnLb0IGJ+FOPctJCDFQzF4OcekK4EegCv7GfSMwXOOR3Lrw7d593cQIInqDI2rj+ LdqWv1RZwW6zTjQo3CPkhK1oKTK524cduwTW2sc4KFmFzyxAOZV8oAHrkI3Ua+g45oV7Wh eSiXIfbTWq8VFP+91AqTb5jwXHD1JLo= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-358-dENiG3rfP5aO_6pF6kkzmw-1; Thu, 10 Apr 2025 09:51:34 -0400 X-MC-Unique: dENiG3rfP5aO_6pF6kkzmw-1 X-Mimecast-MFC-AGG-ID: dENiG3rfP5aO_6pF6kkzmw_1744293092 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-43ceeaf1524so5824145e9.1 for ; Thu, 10 Apr 2025 06:51:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744293092; x=1744897892; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9cwx8PB++1jPTpqj+0lA5MhEHp2F2B5hQ/5H2FoJu9E=; b=eScGNs4/46ZZSBOXNg+VHCaHrJavfI2ViuzxP0pbtihVygwrtmPxnzP2IFpSYVfhiD dz1N0JHvBPAsaPekc2c7InSsjuomx2p7xNMU8LQTORCH9CtdJ9hRGACUOU1+d6PIgt0R qzWwsy+n92nP+aHZx16/wV0bMFsYb6ZzQqgMyFTSSDoE9TKslRgXp7DekJ7XAUcAp6JH rSVNyVKIJMsQLI6Siiew/XHKdbx8n7+MqySdQ7TnmkYeLZhS58BNJb4pUQJfkwv1u/Q0 vO1ySU9RL/CGgrvyXuAa9jUCIUos2yxFy6OnAunTVqX3HQFpCw09FM3zXPYyZSAQ9Ygg ei6w== X-Forwarded-Encrypted: i=1; AJvYcCW5NauivPnQG2Pi+3n1EE4fXf851pHmGYAS2Ks3JwGT9L9rHag4qf67GDLIKK/osW+GJJVFGBNg+RZ8LUo=@vger.kernel.org X-Gm-Message-State: AOJu0YxES8dHemf0XXEoJAG037hDSaSrb6IBPzFeTwqmSxIh8u3R0dxA lUPBn7jpMzPbgf4Ad92cB60Y2C9i/0SJNoIaGjIw2xNL/uDZg18XfSSTT3GCOKEUhEqXZ5FYrtm /y7DgXo1a1O9oOYUjjqRZ1fDvRYcNxBSKuBsXfbfpwmcJ7F7yS2+9xxCMmKwJew== X-Gm-Gg: ASbGncuPN/xCpUag/9bE5jinJ8rVekXuL8wOHXyt/xTIHrn9qCCmWqEyMlZABWicFJy DG8YRdcT+SeIQq6eXzpFHM/g2huDd8QSRvV042/nZckDCA7Z/5j6Nki3H7pV3QAC9k+/kchbQmr VxVyCPu0WVSepoZanUg/1h5mx+STKW1sDD0NvhgnqrZpG2+tLD1FN6oKFe2S6M1fa1jEI2a1T+x yz8OY9OkerlvNRr4YLHjSZ4TaIWopBC9i8h91VPFQU9+KI8i3NKimQ8MAzjVPBtYF6IpJfX76oc +0h0k6qYaXnIElmkNI1mRWgWgWbMBJrRhha75mukG3DxxHytRgfD56lljutwuA== X-Received: by 2002:a05:600c:1614:b0:43d:fa5f:7d04 with SMTP id 5b1f17b1804b1-43f2edc7c63mr20711505e9.16.1744293091881; Thu, 10 Apr 2025 06:51:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEuGhwEkIOIQmV9vyTTjh08E802YQY2NMK9rYdrO7U9thvFq8ZhNT2/VmlP/YCA83my8ZlkmQ== X-Received: by 2002:a05:600c:1614:b0:43d:fa5f:7d04 with SMTP id 5b1f17b1804b1-43f2edc7c63mr20711175e9.16.1744293091252; Thu, 10 Apr 2025 06:51:31 -0700 (PDT) Received: from stex1.redhat.com (host-79-53-30-213.retail.telecomitalia.it. [79.53.30.213]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43f2075fc83sm57126385e9.26.2025.04.10.06.51.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 06:51:30 -0700 (PDT) From: Stefano Garzarella To: Borislav Petkov , Jarkko Sakkinen Cc: Joerg Roedel , "H. Peter Anvin" , Dionna Glaze , Ingo Molnar , linux-kernel@vger.kernel.org, Dov Murik , linux-integrity@vger.kernel.org, Dave Hansen , Claudio Carvalho , Tom Lendacky , Thomas Gleixner , Peter Huewe , Jason Gunthorpe , x86@kernel.org, James Bottomley , linux-coco@lists.linux.dev, Stefano Garzarella Subject: [PATCH v7 2/4] svsm: add header with SVSM_VTPM_CMD helpers Date: Thu, 10 Apr 2025 15:51:14 +0200 Message-ID: <20250410135118.133240-3-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250410135118.133240-1-sgarzare@redhat.com> References: <20250410135118.133240-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Stefano Garzarella Helpers for the SVSM_VTPM_CMD calls used by the vTPM protocol defined by the AMD SVSM spec [1]. The vTPM protocol follows the Official TPM 2.0 Reference Implementation (originally by Microsoft, now part of the TCG) simulator protocol. [1] "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 Co-developed-by: James Bottomley Signed-off-by: James Bottomley Co-developed-by: Claudio Carvalho Signed-off-by: Claudio Carvalho Reviewed-by: Jarkko Sakkinen Signed-off-by: Stefano Garzarella --- v5: - added Jarkko's R-b v4: - used svsm_vtpm_ prefix consistently [Jarkko] - removed __packed where not needed [Jarkko] - expanded headers to avoid obfuscation [Jarkko] - used `buf` instead of `inbuf`/`outbuf` [Jarkko] - added more documentation quoting the specification - removed TPM_* macros since we only use TPM_SEND_COMMAND in one place and don't want dependencies on external headers, but put the value directly as specified in the AMD SVSM specification - header renamed in tpm_svsm.h so it will fall under TPM DEVICE DRIVER section [Borislav, Jarkko] v3: - renamed header and prefix to make clear it's related to the SVSM vTPM protocol - renamed fill/parse functions [Tom] - removed link to the spec because those URLs are unstable [Borislav] --- include/linux/tpm_svsm.h | 149 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 include/linux/tpm_svsm.h diff --git a/include/linux/tpm_svsm.h b/include/linux/tpm_svsm.h new file mode 100644 index 000000000000..38e341f9761a --- /dev/null +++ b/include/linux/tpm_svsm.h @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2023 James.Bottomley@HansenPartnership.com + * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved. + * + * Helpers for the SVSM_VTPM_CMD calls used by the vTPM protocol defined b= y the + * AMD SVSM spec [1]. + * + * The vTPM protocol follows the Official TPM 2.0 Reference Implementation + * (originally by Microsoft, now part of the TCG) simulator protocol. + * + * [1] "Secure VM Service Module for SEV-SNP Guests" + * Publication # 58019 Revision: 1.00 + */ +#ifndef _TPM_SVSM_H_ +#define _TPM_SVSM_H_ + +#include +#include +#include + +#define SVSM_VTPM_MAX_BUFFER 4096 /* max req/resp buffer size */ + +/** + * struct svsm_vtpm_request - Generic request for single word command + * @cmd: The command to send + * + * Defined by AMD SVSM spec [1] in section "8.2 SVSM_VTPM_CMD Call" - + * Table 15: vTPM Common Request/Response Structure + * Byte Size =C2=A0=C2=A0 =C2=A0In/Out=C2=A0=C2=A0=C2=A0=C2=A0D= escription + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0In=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Platform command +=C2=A0*=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Out=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Platform command respons= e size + */ +struct svsm_vtpm_request { + u32 cmd; +}; + +/** + * struct svsm_vtpm_response - Generic response + * @size: The response size (zero if nothing follows) + * + * Defined by AMD SVSM spec [1] in section "8.2 SVSM_VTPM_CMD Call" - + * Table 15: vTPM Common Request/Response Structure + * Byte Size =C2=A0=C2=A0 =C2=A0In/Out=C2=A0=C2=A0=C2=A0=C2=A0D= escription + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0In=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Platform command +=C2=A0*=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Out=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Platform command respons= e size + * + * Note: most TCG Simulator commands simply return zero here with no indic= ation + * of success or failure. + */ +struct svsm_vtpm_response { + u32 size; +}; + +/** + * struct svsm_vtpm_cmd_request - Structure for a TPM_SEND_COMMAND request + * @cmd: The command to send (must be TPM_SEND_COMMAND) + * @locality: The locality + * @buf_size: The size of the input buffer following + * @buf: A buffer of size buf_size + * + * Defined by AMD SVSM spec [1] in section "8.2 SVSM_VTPM_CMD Call" - + * Table 16: TPM_SEND_COMMAND Request Structure + * Byte Size Meaning + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Platform command (8) + * 0x004=C2=A0=C2=A0=C2=A0=C2=A0=C2=A01=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Locality (must-be-0) + * 0x005=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0TPM Command size (in bytes) + * 0x009=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Variable=C2=A0=C2=A0=C2=A0TPM Co= mmand + * + * Note: the TCG Simulator expects @buf_size to be equal to the size of the + * specific TPM command, otherwise an TPM_RC_COMMAND_SIZE error is returne= d. + */ +struct svsm_vtpm_cmd_request { + u32 cmd; + u8 locality; + u32 buf_size; + u8 buf[]; +} __packed; + +/** + * struct svsm_vtpm_cmd_response - Structure for a TPM_SEND_COMMAND respon= se + * @buf_size: The size of the output buffer following + * @buf: A buffer of size buf_size + * + * Defined by AMD SVSM spec [1] in section "8.2 SVSM_VTPM_CMD Call" - + * Table 17: TPM_SEND_COMMAND Response Structure + * Byte Size Meaning + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Response size (in bytes) + * 0x004=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Variable=C2=A0=C2=A0=C2=A0Respon= se + */ +struct svsm_vtpm_cmd_response { + u32 buf_size; + u8 buf[]; +}; + +/** + * svsm_vtpm_cmd_request_fill() - Fill a TPM_SEND_COMMAND request to be se= nt to SVSM + * @req: The struct svsm_vtpm_cmd_request to fill + * @locality: The locality + * @buf: The buffer from where to copy the payload of the command + * @len: The size of the buffer + * + * Return: 0 on success, negative error code on failure. + */ +static inline int +svsm_vtpm_cmd_request_fill(struct svsm_vtpm_cmd_request *req, u8 locality, + const u8 *buf, size_t len) +{ + if (len > SVSM_VTPM_MAX_BUFFER - sizeof(*req)) + return -EINVAL; + + req->cmd =3D 8; /* TPM_SEND_COMMAND */ + req->locality =3D locality; + req->buf_size =3D len; + + memcpy(req->buf, buf, len); + + return 0; +} + +/** + * svsm_vtpm_cmd_response_parse() - Parse a TPM_SEND_COMMAND response rece= ived from SVSM + * @resp: The struct svsm_vtpm_cmd_response to parse + * @buf: The buffer where to copy the response + * @len: The size of the buffer + * + * Return: buffer size filled with the response on success, negative error + * code on failure. + */ +static inline int +svsm_vtpm_cmd_response_parse(const struct svsm_vtpm_cmd_response *resp, u8= *buf, + size_t len) +{ + if (len < resp->buf_size) + return -E2BIG; + + if (resp->buf_size > SVSM_VTPM_MAX_BUFFER - sizeof(*resp)) + return -EINVAL; // Invalid response from the platform TPM + + memcpy(buf, resp->buf, resp->buf_size); + + return resp->buf_size; +} + +#endif /* _TPM_SVSM_H_ */ --=20 2.49.0 From nobody Fri Dec 19 15:57:11 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 E945428C5CF for ; Thu, 10 Apr 2025 13:51:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744293101; cv=none; b=mo7fvxKlDHzWFEAW2CY7uno+cVvJZRtzlGJ65Ndfu93VGjCtTv83kq322UuPmzoRy5xLUYvQ9TmjVPQnxKtH4U+zAdlO98ibfOJ2HJCN38Mb19iXBrZrZ0L6QxMhu/8xNG9f++t2+0cZJ27rnpsbMCFCWPxGx5aJ9zfP/Ukrajo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744293101; c=relaxed/simple; bh=BTUc9p0P7fj/IdeyhhIG8U1tRS0y26+L6mvGHy8buuU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HrK2jQG8VWgkc8fCl+8ohPj6prNAlaAngohUpbv7lsuJRWxd4nYeoYfNOUTIH2j/wtQVttNleZcu3s6zTB7r05eVA92WysQ66y1ISeBH4r2U7ldjbI65Q1r4vEBzRTRjQMmnlcuMnjLCUrbm8Lo1oARDVS2iumdjqHG9qOtw3Ww= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=B5JktRit; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="B5JktRit" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744293099; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a/4sxX4zYlWzMuPRt3jzKJXuc3Oa7Ar74ewr3j43ie4=; b=B5JktRitBIlWN08IhgMa9/gPHFL3f7H2k3L6fRfdlq73qKO5kXNDUo5ArUCyEkyGqu0nDO H6IW6AcxpKhqu0JtHWOq1sXC0l23bw4Fzu4R0gFcxzPBUIdhoBHwM0QCqFVGe45oGnL7/R 9t7kgpgl2feVmNlU70tDPAcpZwg1qXY= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-185-hvclkzvaP2ib70aST3R6nw-1; Thu, 10 Apr 2025 09:51:38 -0400 X-MC-Unique: hvclkzvaP2ib70aST3R6nw-1 X-Mimecast-MFC-AGG-ID: hvclkzvaP2ib70aST3R6nw_1744293097 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-43d00017e9dso6358275e9.0 for ; Thu, 10 Apr 2025 06:51:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744293097; x=1744897897; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a/4sxX4zYlWzMuPRt3jzKJXuc3Oa7Ar74ewr3j43ie4=; b=hOcb0QPfztZFWGeKg++JJC3yDJnILU4pofgkmEX4DUh6DhqwDZU+bd+fwXJSQSSXhc 1eDxKWX+7r1XsLy2cYP5JAebKHiy1/1tFEjKzx6FDhDXy/Gc9SfPliGrHm12twGYnhnt O760A7e37k1b1mduXsTwW2POETsTsYiaXA/OVtFFCfGalRDT1MwDNGz1BeEQxJWfKBmr HcHzRgdLiiyM2vMGTlcS2VA3i1hOnEbFc11sWksyGOZ3Nixf/SVVUd1Q2/LmF5x5Sd3U 56B4R4v8E9dWLS2Mfm1XKYJAt58gZEaPcAWzRffIZiWJeXVVF0GONcKMEWbwGlEY2i9O AX6w== X-Forwarded-Encrypted: i=1; AJvYcCXGocb7TEmuYwhlAclSDfZFI5bPL0jQBgBxGdL3GNK0j5Hr+KXoCcfR6pvtO1hMd6lxzwAk63WnbsUm3xQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyjfaZ5Cg2irgGm8M94x7x4uErNlQh+cyuyvwsCZ5frlm9Z3aVZ ja/0ta9MY8ffPDd7lkNWNQ7thgQZqHC27fH1ML5C6TwFW3IF01Qs92KDLXgO+u7XD47hy4qkAhV YR2IfHn53mgJBHo1Ea16lmP6ixp/iulTuzlpecn++Z/bmIakhodp497UPaFUSZw== X-Gm-Gg: ASbGncuZK71IVZQGkEAUrn8trMTZzDoijG5wtRpOmqjExxXVjOW7sPddYZH+jNRFsjJ RC5B69VzibsEjnkOhgSIYGkIZZ7nyosnNDz+xNnzYxcAvf8OJej/O1mEWUAfzTQ88FO6opvSwJu Z9uXpP1PR3PaYuf3iN8r3giZvXOr6THZ4XGBicBWjONKFkZC27opBA1GAYovq5CPTabn1EITTjk qfJeIOVWyrEx+ews1VVigVjzrx7E4BwyZvDkLKphvC7D/8J21IP+0zxClzwf8ypBHUFu023Yb+v GSGNrZwwRPxFGtOHgTMx6LuS+N0M5tvpHxH8mWhMgE+m2lWJWjzIWqcON0+u7Q== X-Received: by 2002:a05:600c:1d10:b0:43d:42b:e186 with SMTP id 5b1f17b1804b1-43f2d7bbe9dmr25367695e9.8.1744293096537; Thu, 10 Apr 2025 06:51:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFBFkr3gV1YG3NMqGEsLFTmg6/59TSCe3mxGOyIVGTKktzJth53MhEnXCXkeFjtq7SaUo9uHw== X-Received: by 2002:a05:600c:1d10:b0:43d:42b:e186 with SMTP id 5b1f17b1804b1-43f2d7bbe9dmr25367365e9.8.1744293096036; Thu, 10 Apr 2025 06:51:36 -0700 (PDT) Received: from stex1.redhat.com (host-79-53-30-213.retail.telecomitalia.it. [79.53.30.213]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39d8938a761sm4932483f8f.51.2025.04.10.06.51.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 06:51:35 -0700 (PDT) From: Stefano Garzarella To: Borislav Petkov , Jarkko Sakkinen Cc: Joerg Roedel , "H. Peter Anvin" , Dionna Glaze , Ingo Molnar , linux-kernel@vger.kernel.org, Dov Murik , linux-integrity@vger.kernel.org, Dave Hansen , Claudio Carvalho , Tom Lendacky , Thomas Gleixner , Peter Huewe , Jason Gunthorpe , x86@kernel.org, James Bottomley , linux-coco@lists.linux.dev, Stefano Garzarella Subject: [PATCH v7 3/4] tpm: add SNP SVSM vTPM driver Date: Thu, 10 Apr 2025 15:51:15 +0200 Message-ID: <20250410135118.133240-4-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250410135118.133240-1-sgarzare@redhat.com> References: <20250410135118.133240-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Stefano Garzarella 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 x86/sev to send commands and receive responses. The device cannot be hot-plugged/unplugged as it is emulated by the platform, so we can use module_platform_driver_probe(). 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 980a573621ea ("tpm: Make chip->{status,cancel,req_canceled} opt"). [1] "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 Reviewed-by: Jarkko Sakkinen Signed-off-by: Stefano Garzarella --- v7: - added Jarkko's R-b - avoided to call snp_svsm_vtpm_probe() since the device will be registered by SEV core only if the device is detected [Borislav] - updated commit description v6: - removed the `locality` field (set to 0) and the FIXME comment [Jarkko] v5: - removed cancel/status/req_* ops after rebase on master that cotains commit 980a573621ea ("tpm: Make chip->{status,cancel,req_canceled} opt") v4: - moved "asm" includes after the "linux" includes [Tom] - allocated buffer separately [Tom/Jarkko/Jason] v3: - removed send_recv() ops and followed the ftpm driver implementing .status, .req_complete_mask, .req_complete_val, etc. [Jarkko] - removed link to the spec because those URLs are unstable [Borislav] --- drivers/char/tpm/tpm_svsm.c | 125 ++++++++++++++++++++++++++++++++++++ drivers/char/tpm/Kconfig | 10 +++ drivers/char/tpm/Makefile | 1 + 3 files changed, 136 insertions(+) create mode 100644 drivers/char/tpm/tpm_svsm.c diff --git a/drivers/char/tpm/tpm_svsm.c b/drivers/char/tpm/tpm_svsm.c new file mode 100644 index 000000000000..4280edf427d6 --- /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"); diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig index fe4f3a609934..dddd702b2454 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 2b004df8c04b..9de1b3ea34a9 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 --=20 2.49.0 From nobody Fri Dec 19 15:57:11 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 BCA4C28CF55 for ; Thu, 10 Apr 2025 13:51:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744293106; cv=none; b=ddPD0LoiQ56oePoa1FONuOPXrO+sySTSfSZEOnKUyxkSasBMUyRHGt/zIuz4/yQze0QIO8xGUKDDrz+VpjVbL1MK2GYmtUaVi0lRUsMjF5PWXRxL2igRdI2Qxe8GRH6aQ1CrvTbX0RkMRULq1MCRYT8ONqjvLceCnoOpjSctbmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744293106; c=relaxed/simple; bh=jjdOFFgqlMZcJK0ENY0EeRcZvGY/PqG+Cg3KwCJxajM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LT+SkUwTtLi/m9pRKX42rJqvpZGDXOD0JP9ClqPOI9azNjTcU1FNJg3Oh8A6iy4Ur284RVMYsm7PUp6EmyBRIQNmUzjB5dIxWZmrkGasY7gCIPf6afTpCvoBFPwDDioHXyzRYM1Xl9zYYfI5C0Vat1VWMmS/bVSnCSnFFnMb+L4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Gfi3aRmW; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Gfi3aRmW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744293103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hirGwPCk5S8fr8wcRk+QWxpexKbLzBx8zDafwqq8PVw=; b=Gfi3aRmWaU2PwbOF4asO/LZwJwQ3yoD6Ai2dh0rwLCpC+xBsFMcsrHHneAS2kG2GI11RIi +k+65vkXvROTYRENdNzbAisILkI4egOfSl2KC8J+xB4aQaBzOHcBGitgS/URgz3zdPBYaE BSzqUr+dC1W+DOFfBRbxwEslYuqfO1M= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-62-rHoxc2yKPPS_ZHS0TyL-Tg-1; Thu, 10 Apr 2025 09:51:42 -0400 X-MC-Unique: rHoxc2yKPPS_ZHS0TyL-Tg-1 X-Mimecast-MFC-AGG-ID: rHoxc2yKPPS_ZHS0TyL-Tg_1744293101 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-43e9a3d2977so7716985e9.1 for ; Thu, 10 Apr 2025 06:51:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744293101; x=1744897901; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hirGwPCk5S8fr8wcRk+QWxpexKbLzBx8zDafwqq8PVw=; b=NImr3SfV+OMH78I/COSPXtmCZCvzQ5r1+LIzVWgZJmGcZP3sCCYbwVm9f2gvkodWg3 TqISqZh7okYn1Xn1ZffzNs24S/qyaTaqjcc3s7oEJ/4EjQkVG3KS2TILepatvrDsyE7u C8hRp0FW9A8GKzehw7xJDYwbwjQg7JLm9fNtskGjCqjg3O2H0fg97O5h30En4Jt8uW4y dIzlu6Vterj+e8GIkd9k5v+KkZzA6Od+8OgGkVj/6XwfDk/FSAAamwyKXsNAxDDb0KUt d9y7M/+ynyV6micu1WtBkGwza+u24rV5BOFeVg50LLJrduMb5xdNLREBFg9p+p/U0F5F EMFA== X-Forwarded-Encrypted: i=1; AJvYcCWK2lNIH1aE6zQng6p5uhpsXyAi9KvoWy/JVXkg90EP75H35OT6XjsS2ED3FyrZeqoGjHjLi2D42LAd758=@vger.kernel.org X-Gm-Message-State: AOJu0Ywg2NWGXk8X1eY7hsyYLh/gciMayZ6uZLKclA+cbG1Vc7UHvkUD XAq7rI2DdAoUoPvdjAEvfeYxuN2P1uxreiVOennOXxVFKNpLF7kLhYycQSbCWGA4BUvjIT+cdWq QOssklA7bjLtNblXxaE4qoydfsVSONjUz7ybXdbxE/gqCrW8GrfqRF78lIMhvCA== X-Gm-Gg: ASbGncsby6Wb8rYUi2frvurSMAKsHi1XaLEwrdWoO/7Haobpj8/+1ytN+N8nFtraniL 38rDXwebgL4ZwdM55AvZBP2zhY1sRdkxR8NI0ad6kL/be7CyGD9UEhcVE9WsxtGeqL+k3tdZaLG QoD4nKlF2NYSvBkj/91hyCn/QaS9/wBtuVGj/6NjEX46tk5W+JgTIKq7tjI9KpI30Xduz/JsUE7 fg1HIl+hxEE8wwD0mGQPKLe+WeTu9ARfSMpPP4bmZ/7MZsetpwVb4yHNuioSvLeWX2m1T4mic8D BLhrzQFbrk3qCSPsaIu7+Fa+LaElJwoGAza9I8KZq/RkyegC12uNdEKJAhwqCQ== X-Received: by 2002:a05:600c:1e21:b0:43c:fa0e:471a with SMTP id 5b1f17b1804b1-43f2fdcece5mr23351965e9.5.1744293101428; Thu, 10 Apr 2025 06:51:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFW/+UygbSs3rimVhsZR25l1U5u5O39Yy7PeRlqUDpdrrg1Z1uyMNvSAu+c2o2wo8JQdf6sGQ== X-Received: by 2002:a05:600c:1e21:b0:43c:fa0e:471a with SMTP id 5b1f17b1804b1-43f2fdcece5mr23351565e9.5.1744293100851; Thu, 10 Apr 2025 06:51:40 -0700 (PDT) Received: from stex1.redhat.com (host-79-53-30-213.retail.telecomitalia.it. [79.53.30.213]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39d893fdfbesm4768782f8f.88.2025.04.10.06.51.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 06:51:40 -0700 (PDT) From: Stefano Garzarella To: Borislav Petkov , Jarkko Sakkinen Cc: Joerg Roedel , "H. Peter Anvin" , Dionna Glaze , Ingo Molnar , linux-kernel@vger.kernel.org, Dov Murik , linux-integrity@vger.kernel.org, Dave Hansen , Claudio Carvalho , Tom Lendacky , Thomas Gleixner , Peter Huewe , Jason Gunthorpe , x86@kernel.org, James Bottomley , linux-coco@lists.linux.dev, Stefano Garzarella Subject: [PATCH v7 4/4] x86/sev: register tpm-svsm platform device Date: Thu, 10 Apr 2025 15:51:16 +0200 Message-ID: <20250410135118.133240-5-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250410135118.133240-1-sgarzare@redhat.com> References: <20250410135118.133240-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Stefano Garzarella SNP platform can provide a vTPM device emulated by SVSM. The "tpm-svsm" device can be handled by the platform driver added by the previous commit in drivers/char/tpm/tpm_svsm.c Register the platform device only when SVSM is available and it supports vTPM commands as checked by snp_svsm_vtpm_probe(). Reviewed-by: Tom Lendacky Reviewed-by: Jarkko Sakkinen Signed-off-by: Stefano Garzarella --- v7: - added Jarkko's R-b - call snp_svsm_vtpm_probe() before registering the device [Borislav] - s/device/devices in pr_info [Tom] - updated commit description v6: - added Tom's R-b v4: - explained better why we register it anyway in the commit message --- arch/x86/coco/sev/core.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index 3bc5b47e7304..54272c9777cf 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -2688,6 +2688,11 @@ static struct platform_device sev_guest_device =3D { .id =3D -1, }; =20 +static struct platform_device tpm_svsm_device =3D { + .name =3D "tpm-svsm", + .id =3D -1, +}; + static int __init snp_init_platform_device(void) { if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) @@ -2696,7 +2701,11 @@ static int __init snp_init_platform_device(void) if (platform_device_register(&sev_guest_device)) return -ENODEV; =20 - pr_info("SNP guest platform device initialized.\n"); + if (snp_svsm_vtpm_probe() && + platform_device_register(&tpm_svsm_device)) + return -ENODEV; + + pr_info("SNP guest platform devices initialized.\n"); return 0; } device_initcall(snp_init_platform_device); --=20 2.49.0