linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
To: <linux-kernel@vger.kernel.org>, <iommu@lists.linux.dev>
Cc: <joro@8bytes.org>, <robin.murphy@arm.com>, <ashish.kalra@amd.com>,
	<thomas.lendacky@amd.com>, <vasant.hegde@amd.com>,
	<jon.grimm@amd.com>,
	Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Subject: [PATCH 4/4] iommu/amd: Force SNP-enabled VFIO domain to 4K page size
Date: Tue, 10 Jan 2023 08:31:37 -0600	[thread overview]
Message-ID: <20230110143137.54517-5-suravee.suthikulpanit@amd.com> (raw)
In-Reply-To: <20230110143137.54517-1-suravee.suthikulpanit@amd.com>

SNP only supports 2M and 4K page sizes. Other page sizes requires
page smashing to supported sizes. For SNP-enabled guests
with pass-through devices (via VFIO), it also requires RMP and IOMMU
page sizes to match.

To simplify the support, for SNP-enabled guest, SNP will smash guest pages
to 4K page size only, and IOMMU driver will setup the IOMMU v1 page table
for the VFIO domain of the guest to match the 4K page size.

Co-developed-by: Vasant Hegde <vasant.hegde@amd.com>
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
 drivers/iommu/amd/amd_iommu_types.h |  2 ++
 drivers/iommu/amd/iommu.c           | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
index ad124959a26a..5249ac18ce6e 100644
--- a/drivers/iommu/amd/amd_iommu_types.h
+++ b/drivers/iommu/amd/amd_iommu_types.h
@@ -279,6 +279,7 @@
 #define AMD_IOMMU_PGSIZES	((~0xFFFUL) & ~(2ULL << 38))
 /* 4K, 2MB, 1G page sizes are supported */
 #define AMD_IOMMU_PGSIZES_V2	(PAGE_SIZE | (1ULL << 21) | (1ULL << 30))
+#define AMD_IOMMU_PGSIZES_4K	(PAGE_SIZE)
 
 /* Bit value definition for dte irq remapping fields*/
 #define DTE_IRQ_PHYS_ADDR_MASK	(((1ULL << 45)-1) << 6)
@@ -440,6 +441,7 @@
 #define PD_IOMMUV2_MASK		(1UL << 3) /* domain has gcr3 table */
 #define PD_GIOV_MASK		(1UL << 4) /* domain enable GIOV support */
 #define PD_VFIO_MASK		(1UL << 5) /* domain enable VFIO support */
+#define PD_SNP_MASK		(1UL << 6) /* domain enable SNP support */
 
 extern bool amd_iommu_dump;
 #define DUMP_printk(format, arg...)				\
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index a03723930f70..9a1b010a7d00 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2422,6 +2422,33 @@ static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
 	return true;
 }
 
+static void amd_iommu_set_kvm(struct iommu_domain *domain, struct kvm *kvm)
+{
+	struct protection_domain *pdom = to_pdomain(domain);
+
+	if (!amd_iommu_snp_en || !amd_iommu_svm_ops ||
+	    !pdom || !(pdom->flags & PD_VFIO_MASK))
+		return;
+
+	/*
+	 * The parameter kvm can be NULL when calling from kvm_vfio_group_del()
+	 * and kvm_vfio_destroy().
+	 */
+	if (!kvm ||
+	    !amd_iommu_svm_ops->is_snp_guest ||
+	    !amd_iommu_svm_ops->is_snp_guest(kvm))
+		return;
+
+	/*
+	 * VFIO Domain for SNP guest requires IOMMU and RMP page-size to match,
+	 * which can only support 4K and 2M. Currently, only support 4K
+	 * IOMMU page-size.
+	 */
+	pdom->flags |= PD_SNP_MASK;
+	pdom->domain.pgsize_bitmap = AMD_IOMMU_PGSIZES_4K;
+	pr_debug("%s: Force domain %u page size to 4K.\n", __func__, pdom->id);
+}
+
 const struct iommu_ops amd_iommu_ops = {
 	.capable = amd_iommu_capable,
 	.domain_alloc = amd_iommu_domain_alloc,
@@ -2444,6 +2471,7 @@ const struct iommu_ops amd_iommu_ops = {
 		.iotlb_sync	= amd_iommu_iotlb_sync,
 		.free		= amd_iommu_domain_free,
 		.enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
+		.set_kvm	= amd_iommu_set_kvm,
 	}
 };
 
-- 
2.32.0


  parent reply	other threads:[~2023-01-10 14:33 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-10 14:31 [PATCH 0/4] iommu/amd: Force SNP-enabled VFIO domain to 4K page size Suravee Suthikulpanit
2023-01-10 14:31 ` [PATCH 1/4] iommu/amd: Introduce Protection-domain flag VFIO Suravee Suthikulpanit
2023-01-11  3:31   ` kernel test robot
2023-01-13 15:33   ` Jason Gunthorpe
2023-01-19  8:54     ` Kalra, Ashish
2023-01-19 17:44       ` Jason Gunthorpe
2023-01-20 15:12         ` Kalra, Ashish
2023-01-20 16:13           ` Jason Gunthorpe
2023-01-20 17:01             ` Kalra, Ashish
2023-01-20 17:50               ` Jason Gunthorpe
2023-01-20 19:55                 ` Kalra, Ashish
2023-01-20 22:42                   ` Tom Lendacky
2023-01-21  0:09                     ` Jason Gunthorpe
2023-01-10 14:31 ` [PATCH 2/4] iommu/amd: Introduce structure amd_iommu_svm_ops.is_snp_guest() Suravee Suthikulpanit
2023-01-10 14:31 ` [PATCH 3/4] iommu: Introduce IOMMU call-back for processing struct KVM assigned to VFIO Suravee Suthikulpanit
2023-01-10 15:11   ` Robin Murphy
2023-01-17  4:20     ` Suthikulpanit, Suravee
2023-01-17 12:51       ` Robin Murphy
2023-01-13 15:35   ` Jason Gunthorpe
2023-01-17  5:31     ` Suthikulpanit, Suravee
2023-01-17 14:19       ` Jason Gunthorpe
2023-01-10 14:31 ` Suravee Suthikulpanit [this message]
2023-01-17 13:10   ` [PATCH 4/4] iommu/amd: Force SNP-enabled VFIO domain to 4K page size Eric van Tassell
2023-01-16 13:17 ` [PATCH 0/4] " Eric van Tassell

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20230110143137.54517-5-suravee.suthikulpanit@amd.com \
    --to=suravee.suthikulpanit@amd.com \
    --cc=ashish.kalra@amd.com \
    --cc=iommu@lists.linux.dev \
    --cc=jon.grimm@amd.com \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=thomas.lendacky@amd.com \
    --cc=vasant.hegde@amd.com \
    /path/to/YOUR_REPLY

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

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