From nobody Tue Dec 16 16:56:28 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 586DC25DD03 for ; Mon, 24 Mar 2025 10:47:06 +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=1742813228; cv=none; b=gcBVIfqsaLJ08uFWWQSjygC6+bvcyq50jaCXaUWqNzQFNUw9YJqx1wjtT5Ki79Zxj6phfXQDe7Dt7UA1NtxaP3MsWequnplNe4vgiK6Mjl2l176vzXXPnjItBKCFzoCnRLFAn2guSrz5U9DGBXHjx2Pj5SX2cyPfomwGzxLkczw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742813228; c=relaxed/simple; bh=gBGsgkYxSR1LlsmDYsqyNxaFopHRVY2DcTqfkPwuqkw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FNX6eAoCoEIEbUJUiQNnwv4miOsNbGgI0ZtRuQX8jXTvV8b3BgpE+RthI4dQaZATrDHtcfA0tNQegGlXs0t5KIwJYRi9cc9Khv4MxhW4lCUb7OjL2AxIFsGZ9Mw3offpW03xtEDxpJgFFalMK+NXaH/AxwtX2Cq0z6gHe1hILno= 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=Lqugqeh+; 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="Lqugqeh+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1742813225; 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=asa3lke4A0956hCElOUU/2LBwaCP9NEWyWZZnZLv9nU=; b=Lqugqeh+oVgrkwpU4C1E1/zYwRQUhfnbD6W5eu3CxLxrsrG+HAUto5zYpb/sEsSrcLRw5s utezLYCOVmHoGkhMup8LLwXu+OAWUBT/YzST964xcDvFGxaDbu3US/FME9SEPhmRAP5ufx y9NyNjB5zAh8kbpdg+1AAaI0kd8u0AA= 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-681-j_fYKj9lNKaTdLEPB5smMw-1; Mon, 24 Mar 2025 06:47:04 -0400 X-MC-Unique: j_fYKj9lNKaTdLEPB5smMw-1 X-Mimecast-MFC-AGG-ID: j_fYKj9lNKaTdLEPB5smMw_1742813223 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3913aaf1e32so2614790f8f.0 for ; Mon, 24 Mar 2025 03:47:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742813223; x=1743418023; 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=asa3lke4A0956hCElOUU/2LBwaCP9NEWyWZZnZLv9nU=; b=W0XrGVBNDf8Unwc+lkrbE77/wugTYMzb2+x14AMWIdVx9Xsv4NvhF6zHTcQHbhQyFJ E5dZAUGulhf9AZPIT8b2l4R7Zyx4kpCsGU4ki7bjHar8VDoGTGPkR/gG2nIxFG5blFKM pk2P+BIMnN61xykI0hsnjV6ihvaaGMfL5xsDmgPcKI/IJZZ3qI9wlpY31nXFYNpO3+0R JDdkDpMrUy3woS9eX4/iaICHP6Zu2q9w5Xyz9yg1ZW04K4j1Z8EeLIPqM6+pRPnkWeoL U+wRgaU6qCKn97LZc5xUbY/nKVTWGsSxwc5LGWYd78HVV5VntmlNr5j6KIbCql0fbQSQ 2pyQ== X-Forwarded-Encrypted: i=1; AJvYcCXChTi+QWFsgKYemsFPvfLrTeonIsMyMmymQYj+GpNClEAAHCp3Dn4c564w+SrOQseqBVgq+RViBv/jnWI=@vger.kernel.org X-Gm-Message-State: AOJu0YwSXta766KWaBQQKE/jWQzQW9tfbs5x+kWWzwSMN1A9kcQXCdC9 abNBsNXqRyemq1UQfDcSQQpB4Ia9gA69gi+giYR4fs+HE8Ymo1po1NK3l+9YaWq6HxCcL/kcMJM og7IkB8kO1esZZqi6Ykl8psUy35p6u7JwtyuS4Foahznf+FoUs8h+T8dKCk8BmA== X-Gm-Gg: ASbGncu1y/AZsMm+Tv5+I6cSW625iysFQ4gYbUIPQv5BF1wYReHSCnu0zbxl57EguSm Kh6TNpoRjicsp5t3jsosDa0nEQPbijMxoC/4bbvWIDYXw/xGD54KdaTNK7GjnNpFJ8QoJ/FUbJz 1AwING1+70lLp8NIy5C9hbZQQrvdM5QFUkjMvzSHu89CCzn0tdadBaoSNqxr959r4kycVwINSvC 9upQ5cZ+p3nKqC4ON2w4BSAWYtfHtAl6Xhy+Pdyj/bGYDOlCK3KlQ6xgKKRy1VboAfHuPrdhd4D KsGp3j4beCbIhV6dvFclf9Nx422IGRMIRj3yBp/P8lv5I0dW14pULIvWilLwHAokqg== X-Received: by 2002:a05:6000:210e:b0:391:1923:5a91 with SMTP id ffacd0b85a97d-3997f948179mr8809073f8f.55.1742813222872; Mon, 24 Mar 2025 03:47:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQQQjWB7MGZTF/Alp9AIGFhaJswe14E9cfG3Bz9+LPf0xUX4SLyvp8rPUkx9+IAagPCdpi+A== X-Received: by 2002:a05:6000:210e:b0:391:1923:5a91 with SMTP id ffacd0b85a97d-3997f948179mr8809032f8f.55.1742813222274; Mon, 24 Mar 2025 03:47:02 -0700 (PDT) Received: from stex1.redhat.com (host-87-12-25-55.business.telecomitalia.it. [87.12.25.55]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9955c0sm10448152f8f.3.2025.03.24.03.47.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 03:47:01 -0700 (PDT) From: Stefano Garzarella To: Jarkko Sakkinen Cc: Joerg Roedel , Ingo Molnar , Dave Hansen , Peter Huewe , Tom Lendacky , Thomas Gleixner , x86@kernel.org, James Bottomley , linux-kernel@vger.kernel.org, Borislav Petkov , Jason Gunthorpe , "H. Peter Anvin" , linux-coco@lists.linux.dev, Claudio Carvalho , Dov Murik , Dionna Glaze , linux-integrity@vger.kernel.org, Stefano Garzarella Subject: [PATCH v4 1/4] x86/sev: add SVSM vTPM probe/send_command functions Date: Mon, 24 Mar 2025 11:46:46 +0100 Message-ID: <20250324104653.138663-2-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250324104653.138663-1-sgarzare@redhat.com> References: <20250324104653.138663-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 these functions to be used by other modules such as 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 Signed-off-by: Stefano Garzarella Reviewed-by: Jarkko Sakkinen --- 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 | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ba7999f66abe..09471d058ce5 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,9 @@ 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 +bool snp_svsm_vtpm_probe(void); +int snp_svsm_vtpm_send_command(u8 *buffer); + void __init snp_secure_tsc_prepare(void); 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 96c7bc698e6b..034aab7e76d2 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -2628,6 +2628,65 @@ 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. + */ +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); +} +EXPORT_SYMBOL_GPL(snp_svsm_vtpm_probe); + +/** + * 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 Tue Dec 16 16:56:28 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 1D30125D558 for ; Mon, 24 Mar 2025 10:47:12 +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=1742813234; cv=none; b=Ih1cgURimxL1tjDoP85gI41SMOkjMThsMiVIk3d1556MglCeQSagBlVtFNwULasl9AFRKIBLkKHRfyyGrGU9bHUnEMTYH5AUV12VI1OFKlCy2qQFf0DZdb2sJOakZGXTRrkOh/4f9I4SkH2IdZ/D+h8LS/JiUA3AAr0wYNiQqfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742813234; c=relaxed/simple; bh=VGky2X7R0BqRkgR/rmPA6Zn9o6b4Oc0GAhRciwiCiGg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AZpqRMYs7mbVkV7KCORndGGR7rzudRhxlKP8PfCoIUMK5lGmm9XpU86o4haUsiTxKqErfHmLNLtW6Gc8bZqWnVXhflcBV7Mx5bQwSdYtC1doenIXCVerGjv22W3zvbj9kp1tcrEtKOqzudiL2OUwFsDRKcdaSjhRLPXoRCx6Q9U= 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=R2526C1o; 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="R2526C1o" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1742813232; 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=O3ofNKkLlwWze0oEt1bz6Y5Zm/5JtrpuEhbONzfCg84=; b=R2526C1onyBZw/eP01+c1z0XSS+umRpmvr68KySWIS9tMYyDFiFRCVzUivPNDXF3SJws30 5UL7LkTTCelrT4INwkBGW0bIaI/1vMGQ0J0h959RJW3ZDkqsC42QAOJlF6cViKm9mltnHh HePXAaDR2SUQ2lACStpS4xlkFvpZiBk= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-683-04cHd0IDPSWXeYrOgFTC5g-1; Mon, 24 Mar 2025 06:47:08 -0400 X-MC-Unique: 04cHd0IDPSWXeYrOgFTC5g-1 X-Mimecast-MFC-AGG-ID: 04cHd0IDPSWXeYrOgFTC5g_1742813228 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-39142ce2151so1631524f8f.1 for ; Mon, 24 Mar 2025 03:47:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742813227; x=1743418027; 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=O3ofNKkLlwWze0oEt1bz6Y5Zm/5JtrpuEhbONzfCg84=; b=ekZBC7ASsJxGMJwcqiQrt5cGWPfsRGuXYbsKnFWwJRQUqTjwl0YxkdExAtVYvbRoWv 51h28aTKE9mA2NKiOwc+/ldKLSHPxCXRR3b5cNOrLqBo37rinIsM2UQG6V2YDXrLPgzI 6qIzPu/rTzvcEMEaii2wE9ojEYnJslP9NWu7/fv3KvwZl7JlMUvvZ3WwF8/mc0luUL5W edMDkJRqXoc4OvOAjr8jO/otAjEbkKkrteqVTC26QdV/yPTRyC1shhc8NIjkAtXyG4TM /59vjZ0lDbrzx6WM4ixhCPy5JOIaQ26H+Wpn0VpvH7w6lURtajz8ea+pF1sOsaTh2Zyp AY/g== X-Forwarded-Encrypted: i=1; AJvYcCXzKke5sO+DWkrsjMHwV4629XhE1TPRhAVsNUbW6csEe0VeeAjQrRDDkWMiQ1euRuQcaHlKfiju5qK+cAY=@vger.kernel.org X-Gm-Message-State: AOJu0YxiH+KNvYlZkSTr7h3UB007NtDrKpmBVKIbUVeFxVFzamhG3TjK FpSsNB9lljjqyaJUJ7CJQY19AvvLxniqmp20BT2JOvwV4N9+HsVo/aRpOWkML5yKmnw+AFNYefA K2kEMa+Vi9GHNfrMW/frF8NGKao34hVvXOVKvG2+CyCKU4w2fSBZ1AW0KdLHk6Q== X-Gm-Gg: ASbGncvW6jTlDOudQMNCb+r9x2xOzMxuT2yhW3Y2CvGdHeKp20v4KUrlY3jfa4oaTac oUn7Mbr1hKKS5l9YJz+hpICiN6oZRi5MEjw6dAoCFL7sEZzUwyGlmALLAMHwDhURPMT1AyYXXaD 08fX4BGq+iHBtzRcjjS17RqwitwMJNFIQ0SzwPPyTqE+/GICk0N8s7bbKo8PPwEkbF/Mtja4fJU KTqfaWFuRdhHLr72vWRfZfh7ZKaR47REj9vxLLeLFtR9All9CnzTdNlRVvb0sxCKC/cCUELPKnV jjRi8H6zzHCFcVb/av+RvBGeUKhKCFW72KHOuF7ShB2zplW5CrfwL3tIxGY061JZCA== X-Received: by 2002:a5d:6da1:0:b0:391:31c8:ba58 with SMTP id ffacd0b85a97d-3997f8eefe9mr9222459f8f.10.1742813227534; Mon, 24 Mar 2025 03:47:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFeA29Zi2YUeUijn9LA3N9z6ryWBOskTdw0WtG15UsubeQ8FdCLGLqSLeIH79wulVpz8VsxXQ== X-Received: by 2002:a5d:6da1:0:b0:391:31c8:ba58 with SMTP id ffacd0b85a97d-3997f8eefe9mr9222423f8f.10.1742813226949; Mon, 24 Mar 2025 03:47:06 -0700 (PDT) Received: from stex1.redhat.com (host-87-12-25-55.business.telecomitalia.it. [87.12.25.55]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9b4ce9sm10712355f8f.53.2025.03.24.03.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 03:47:06 -0700 (PDT) From: Stefano Garzarella To: Jarkko Sakkinen Cc: Joerg Roedel , Ingo Molnar , Dave Hansen , Peter Huewe , Tom Lendacky , Thomas Gleixner , x86@kernel.org, James Bottomley , linux-kernel@vger.kernel.org, Borislav Petkov , Jason Gunthorpe , "H. Peter Anvin" , linux-coco@lists.linux.dev, Claudio Carvalho , Dov Murik , Dionna Glaze , linux-integrity@vger.kernel.org, Stefano Garzarella Subject: [PATCH v4 2/4] svsm: add header with SVSM_VTPM_CMD helpers Date: Mon, 24 Mar 2025 11:46:47 +0100 Message-ID: <20250324104653.138663-3-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250324104653.138663-1-sgarzare@redhat.com> References: <20250324104653.138663-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 Signed-off-by: Stefano Garzarella Reviewed-by: Jarkko Sakkinen --- 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 Tue Dec 16 16:56:28 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 DB82125DD03 for ; Mon, 24 Mar 2025 10:47:15 +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=1742813237; cv=none; b=PN8wrMcQcT3IeVVKMd6upWVt22RYlmrLP6UxbldghGP6ahlx4xSgDnKhyXOyiT/1YEy5NQONRS+ronzP2qgHL3lMVgmlxtMO3aURvq5eSXo9Up2QyWSCOFtLIvGpmXE3O2ChN1q2fMyMwSDi0OZkxzw3rGU0NyX1sbvhdnHlMXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742813237; c=relaxed/simple; bh=fYETXP1a8TOssFuTHBuHbCaB4dw0eo0gT/SC7oA1iIg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u+xZAcjCT/3IvKPeYr0GjCqiZilcCLYDWhrdNhZaXHgM4jCBE8wFDtSFjhZgOTGpakJRuvpzRFxwr2nEb4KYZc9o5zg0AdOmidXxXtLoOAWLWdqkrBF69QtpHkCEVAKWWJ1bgzEP6GVmALl7bRjUqnG9myjtC0F6GWsaBNfVhf8= 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=ZQpFeEdj; 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="ZQpFeEdj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1742813234; 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=qyDOxzFU6JupwzWJAiTz5FZI+LekQY7HIxOgSfWPZ30=; b=ZQpFeEdjhzkbqDF2SjaRxQbWWKwRBbt4PWOJeEoqqjQAQP15jNhab5KxHLREPm9mnCPz4I XksWZHivLeF7NfjFgTanYzwXtBb2oJX2YLoF7evQef887ALklqblTbE+BhQIjP6m0B0MKm WGttjZxQgZSdZ7IdF0SOIY4ColSIMug= 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-171-hBdtX7JNNPWvVs7hWAcARg-1; Mon, 24 Mar 2025 06:47:13 -0400 X-MC-Unique: hBdtX7JNNPWvVs7hWAcARg-1 X-Mimecast-MFC-AGG-ID: hBdtX7JNNPWvVs7hWAcARg_1742813232 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-39130f02631so1677056f8f.2 for ; Mon, 24 Mar 2025 03:47:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742813232; x=1743418032; 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=qyDOxzFU6JupwzWJAiTz5FZI+LekQY7HIxOgSfWPZ30=; b=KMqKc0lvYf/AACEjRi4wjbsHSyw7kuUNY6WovGUzc2GuQ9wYf8WGQDa5qtSuWTICjv Bm8OtRiKuxPQ8p3NK8COlhvqenLBFpGiaxxvh5Y6nwPgFQKzCC//oF5RKu8pHSHNC9HX wG/N4El51arCT7CmOAbD4wcLF1o0faQVwNjr4HTG3c/5fvtI5ncj7+FmPZHQyq6wugtj y8YrqhFOamaxpx2bTtmOvskZxvyoDiH1lDAIPiRpQBGiXsq5mAii68DpvwNeiyTlnx56 5pHqVnqALDrneq57264x6R9HGiTBZsSkyxB+V5EJMJ5q01HMM1x3OhSPvm8t+3CHFEBI UOPw== X-Forwarded-Encrypted: i=1; AJvYcCX/ehSyvor0k6xugd+riVirht8pKUtTrHw/z7Am3+D7VmqRB8jWs3cakG0KCUY5Khd/wPc0sKVCIh+3m0o=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0u5ljadqxvzqxSa4q9ET1G1fbOMwWgIL5jBxbe+EL4Y24LGmW 9GeSkA/1ukZPALDh0DG1olFQr3F0pgp7WQiebfEeHJtfEvL+x2plrcjuRXhSX+W2oT0vdNnKkep bksvZRlZbl+VUFq89BiTfHJx4rmKvruEjx31YTnkLnbyLv2d2CP/3ZhK+iQgxcg== X-Gm-Gg: ASbGnctK7i67xxK8jdPwJ8KCmwa7CtyxpDGo/i8L1HObH8j8olVhlP1Co8KlH9cn27h M+fxoynm7pcUMLUNEiO1NUYvXQ1X5sKrAf/qcVLHCvmBV1EnwS0IRWNkMosQlu2jvUDR02Dmv0b PD6BRLpQpAXVb3VB5cnr8Ke92tr1wwdk685UAygJofiJnL1RrA8FqshbKhX+3Hxm8YDAgNJ/og7 fth1+GClMJmXV5zEocWfHb2FrSjy++mM+H37/QTYwy7VI4N5Un7NcGsxLkQmQt0hGbQW40CLEee YaWTI73b5UI6CG6eEM+grct1UCD/ClYFPUo/pZdnC5w+kpH4OjL09/iFOZSmbVcvCw== X-Received: by 2002:a05:6000:1f86:b0:391:3988:1c97 with SMTP id ffacd0b85a97d-3997f8fedcemr10427622f8f.17.1742813232204; Mon, 24 Mar 2025 03:47:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrTWSDELo5BJGWG2u5TFc15xbVywo2ZKnMk5vePN5f5p6dnyWwAdc8cl4oqo1bOvngslZPTQ== X-Received: by 2002:a05:6000:1f86:b0:391:3988:1c97 with SMTP id ffacd0b85a97d-3997f8fedcemr10427578f8f.17.1742813231553; Mon, 24 Mar 2025 03:47:11 -0700 (PDT) Received: from stex1.redhat.com (host-87-12-25-55.business.telecomitalia.it. [87.12.25.55]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d43d7c6a5sm168667675e9.0.2025.03.24.03.47.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 03:47:10 -0700 (PDT) From: Stefano Garzarella To: Jarkko Sakkinen Cc: Joerg Roedel , Ingo Molnar , Dave Hansen , Peter Huewe , Tom Lendacky , Thomas Gleixner , x86@kernel.org, James Bottomley , linux-kernel@vger.kernel.org, Borislav Petkov , Jason Gunthorpe , "H. Peter Anvin" , linux-coco@lists.linux.dev, Claudio Carvalho , Dov Murik , Dionna Glaze , linux-integrity@vger.kernel.org, Stefano Garzarella Subject: [PATCH v4 3/4] tpm: add SNP SVSM vTPM driver Date: Mon, 24 Mar 2025 11:46:48 +0100 Message-ID: <20250324104653.138663-4-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250324104653.138663-1-sgarzare@redhat.com> References: <20250324104653.138663-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 two functions exposed by x86/sev to verify that the device is actually emulated by the platform and 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 probe function will only check whether in the current runtime configuration, SVSM is present and provides a vTPM. 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 returns 0, and both .req_complete_mask and .req_complete_val are set to 0. [1] "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 Signed-off-by: Stefano Garzarella --- 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 | 155 ++++++++++++++++++++++++++++++++++++ drivers/char/tpm/Kconfig | 10 +++ drivers/char/tpm/Makefile | 1 + 3 files changed, 166 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..1281ff265927 --- /dev/null +++ b/drivers/char/tpm/tpm_svsm.c @@ -0,0 +1,155 @@ +// 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; + u8 locality; +}; + +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, priv->locality, 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 void tpm_svsm_cancel(struct tpm_chip *chip) +{ + /* not supported */ +} + +static u8 tpm_svsm_status(struct tpm_chip *chip) +{ + return 0; +} + +static bool tpm_svsm_req_canceled(struct tpm_chip *chip, u8 status) +{ + return false; +} + +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, + .cancel =3D tpm_svsm_cancel, + .status =3D tpm_svsm_status, + .req_complete_mask =3D 0, + .req_complete_val =3D 0, + .req_canceled =3D tpm_svsm_req_canceled, +}; + +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; + + if (!snp_svsm_vtpm_probe()) + return -ENODEV; + + 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; + + /* + * FIXME: before implementing locality we need to agree what it means + * for the SNP SVSM vTPM + */ + priv->locality =3D 0; + + 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 0fc9a510e059..fc3f1d10d31d 100644 --- a/drivers/char/tpm/Kconfig +++ b/drivers/char/tpm/Kconfig @@ -225,5 +225,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 9bb142c75243..52d9d80a0f56 100644 --- a/drivers/char/tpm/Makefile +++ b/drivers/char/tpm/Makefile @@ -44,3 +44,4 @@ obj-$(CONFIG_TCG_XEN) +=3D xen-tpmfront.o obj-$(CONFIG_TCG_CRB) +=3D tpm_crb.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 Tue Dec 16 16:56:28 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 C6E0925E468 for ; Mon, 24 Mar 2025 10:47:20 +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=1742813242; cv=none; b=tZWbk3SA3gl/SJlpmfcSvf9/0lb5xctZTl0BAL46na4viKCrZ5QJleJa9th1hFr6cGljx9lbALwMrLs0aiQUnq+czJG+X/eJv1Ex4/RbZLMvRGj2vskCkEOiJYJeKkQWYoaNAzq99SZLeWMPvbyj/PcMoX+SgkRgjODiV9ZkzK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742813242; c=relaxed/simple; bh=9z0RxKjeC8RwUmLb5SarlKv6HJUVpqk+rJh1s528L40=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rLeNeipVPEhZCBIhljkymk5knCY4Bw7AftOeGByF183OcelthrjL9BObO6Vvar4s8HNX4hChZAHb+AsX8j3OP5P2C8MVNBou69xNWVhPQNBwKobCwFxeltVUsCmh/F3A2ZuyLNuq9mS1P5z9/q5VIBT8LtOCX96p7Stjphh9yWA= 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=dppOvNLF; 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="dppOvNLF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1742813239; 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=ILxEYoeVE+MpXO0Glj+Y1U0EAxeFxdsejgciRSsrwF8=; b=dppOvNLFItHM6qTB60vUOhBgJMsV8FETTb9bV01Ww0ZI82zMbYT5R4f/wJ6IG0yxxFuTn8 4Rg+lDQ+6JTBZbbcQXoWZQxpawLEVuzdtTLNB6ocBDjW8rS98grEAz6rRf0h4LXOwa0joO 2yxk7Zi0IypmlxPXpWAjeKtlUOFzSHk= 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-382-5p0Q9gH5O6uT1alkYX8gsg-1; Mon, 24 Mar 2025 06:47:18 -0400 X-MC-Unique: 5p0Q9gH5O6uT1alkYX8gsg-1 X-Mimecast-MFC-AGG-ID: 5p0Q9gH5O6uT1alkYX8gsg_1742813237 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-43d51bd9b41so25044545e9.3 for ; Mon, 24 Mar 2025 03:47:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742813237; x=1743418037; 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=ILxEYoeVE+MpXO0Glj+Y1U0EAxeFxdsejgciRSsrwF8=; b=MXmpLgvCp/Mo6jDCHIXmlvkgsloO3D7yW2QufAHjVA9lz+X3IyICCYIerg++ET81Q8 1EJfe5LjmxFMw3hAr3d1JkBAptFsJ1Ual1V7H0lxFBTGCEWY2ff2JWnWpLtNBNtNe1UA 4yIdRovDae4vjtN+Vq57mULH8GkgWdeJ7x9o2mvv3kFSmgH0PRijwlGq4o0azWIdqEiJ RB1DwOMe9geOsNYbBO0J164sFpmLvhC23gxdCu0X87iz4TjdTnna72nnwF48SC785a74 3oecHvlY13wMQ9fbIIDvwtJXx3rSs2+WT4p1pckUnWlbiFfL88r2d7/OIDTs9jpC9X8J Gihw== X-Forwarded-Encrypted: i=1; AJvYcCW6RMi8fWN+ZJYUWR96Ht4WsBiGDJ1nWmw/GxSc09SVxG1DzmQ3KtInGL2fg5oppkRBr1xAsQQF8hIsr+s=@vger.kernel.org X-Gm-Message-State: AOJu0YzFrcjlVZRnIHN1mXNeYxR9aIQ5lRod7O5syrHRVjTiPuKwsrPF EwSW3zT3f8XpkjNqiB7MHyvaLUfJQsRKFMXSzfvm/3ZGr7jUJgk1JglYl0vml9TsvA2NXhu35xC d9YSzMacqmVi33wOHFwzYA2bFG6hF9SoQECf9BSynxxcvRDt6LIs+i88f1q3srw== X-Gm-Gg: ASbGncs7Q+1tWvVuN6T7tR4mMTC3fftJIMvGXRJv8CXLDThGjLltDn0IzDo8YWGEwyI 2FO85TlsPXl/NNMgrMrOKveRyKcRZaOXwSHkFnQxMFHbwjwYxLZ3YcJNZo9CHO3vf078eg9xtZM C3SORlBNOd75Ba22PdDASDGBN98O0LCKYw3efLS+KioyLZQx+PXhkrN2jT6vzgYfnSfcPGPW2we k3wnD+xgMeLhiCmP6yGVEfzS4FS8+IC6r0iFn97sl6dbk+dqK4iovUiYRL+cuoSsGx99lOIaOKg iJNyTF6kYUR7sEJ8a6B5DT2IU2b9Ue9M42I4ywWqwSlgq6BHR6HoFrtpK9pTIG1rQw== X-Received: by 2002:a05:600c:1d9f:b0:43d:83a:417d with SMTP id 5b1f17b1804b1-43d509ec5d7mr113154335e9.12.1742813236957; Mon, 24 Mar 2025 03:47:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFWZa9wuN2PTDGpTipt3fq+aoFWpOBqvRgoGNR3/VEGSUJWm0DGpVCjLokA7c7167YRmdsfkw== X-Received: by 2002:a05:600c:1d9f:b0:43d:83a:417d with SMTP id 5b1f17b1804b1-43d509ec5d7mr113154055e9.12.1742813236335; Mon, 24 Mar 2025 03:47:16 -0700 (PDT) Received: from stex1.redhat.com (host-87-12-25-55.business.telecomitalia.it. [87.12.25.55]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d4fd9e96bsm118452815e9.25.2025.03.24.03.47.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 03:47:15 -0700 (PDT) From: Stefano Garzarella To: Jarkko Sakkinen Cc: Joerg Roedel , Ingo Molnar , Dave Hansen , Peter Huewe , Tom Lendacky , Thomas Gleixner , x86@kernel.org, James Bottomley , linux-kernel@vger.kernel.org, Borislav Petkov , Jason Gunthorpe , "H. Peter Anvin" , linux-coco@lists.linux.dev, Claudio Carvalho , Dov Murik , Dionna Glaze , linux-integrity@vger.kernel.org, Stefano Garzarella Subject: [PATCH v4 4/4] x86/sev: register tpm-svsm platform device Date: Mon, 24 Mar 2025 11:46:49 +0100 Message-ID: <20250324104653.138663-5-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250324104653.138663-1-sgarzare@redhat.com> References: <20250324104653.138663-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 device unconditionally. The support check (e.g. SVSM, cmd) is in snp_svsm_vtpm_probe(), keeping all logic in one place. This function is called during the driver's probe along with other setup tasks like memory allocation. Signed-off-by: Stefano Garzarella --- v4: - explained better why we register it anyway in the commit message --- arch/x86/coco/sev/core.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index 034aab7e76d2..0abcac87af89 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -2692,6 +2692,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)) @@ -2700,6 +2705,9 @@ static int __init snp_init_platform_device(void) if (platform_device_register(&sev_guest_device)) return -ENODEV; =20 + if (platform_device_register(&tpm_svsm_device)) + return -ENODEV; + pr_info("SNP guest platform device initialized.\n"); return 0; } --=20 2.49.0