mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yan Zhao <yan.y.zhao@intel.com>
To: iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: alex.williamson@redhat.com, jgg@nvidia.com, pbonzini@redhat.com,
	seanjc@google.com, joro@8bytes.org, will@kernel.org,
	robin.murphy@arm.com, kevin.tian@intel.com,
	baolu.lu@linux.intel.com, dwmw2@infradead.org,
	yi.l.liu@intel.com, Yan Zhao <yan.y.zhao@intel.com>
Subject: [RFC PATCH 18/42] iommu/vt-d: Support of IOMMU_DOMAIN_KVM domain in Intel IOMMU
Date: Sat,  2 Dec 2023 17:24:21 +0800	[thread overview]
Message-ID: <20231202092421.14524-1-yan.y.zhao@intel.com> (raw)
In-Reply-To: <20231202091211.13376-1-yan.y.zhao@intel.com>

Add support of IOMMU_DOMAIN_KVM domain. Paging structures allocation/free,
page mapping and unmapping of this damain are managed by KVM rather than by
Intel IOMMU driver.

The meta data of paging structures of KVM domain is read from the
allocation "data" passed in from KVM through IOMMUFD. The format to parse
the meta data is defined in arch header "asm/kvm_exported_tdp.h".

KVM domain's gaw(guest witdh), agaw, pgd, max_add, max super page level are
all read from the paging structure meta data from KVM. Snoop and paging
structure coherency are forced to be true.

IOMMU hardware are checked against the requirement of KVM domain at domain
allocation phase and later device attachment phase (in a later patch).

CONFIG_INTEL_IOMMU_KVM is provided to turn on/off KVM domain support.

Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
---
 drivers/iommu/intel/Kconfig  |   9 +++
 drivers/iommu/intel/Makefile |   1 +
 drivers/iommu/intel/iommu.c  |  18 ++++-
 drivers/iommu/intel/iommu.h  |   5 ++
 drivers/iommu/intel/kvm.c    | 128 +++++++++++++++++++++++++++++++++++
 5 files changed, 160 insertions(+), 1 deletion(-)
 create mode 100644 drivers/iommu/intel/kvm.c

diff --git a/drivers/iommu/intel/Kconfig b/drivers/iommu/intel/Kconfig
index a4a125666293f..78078103d4280 100644
--- a/drivers/iommu/intel/Kconfig
+++ b/drivers/iommu/intel/Kconfig
@@ -108,4 +108,13 @@ config INTEL_IOMMU_PERF_EVENTS
 	  to aid performance tuning and debug. These are available on modern
 	  processors which support Intel VT-d 4.0 and later.
 
+config INTEL_IOMMU_KVM
+        bool "Support of stage 2 paging structures/mappings managed by KVM"
+        help
+          Selecting this option will enable Intel IOMMU to use paging
+          structures shared from KVM MMU as the stage 2 paging structures
+          in IOMMU hardware. The page mapping/unmapping, paging struture
+          allocation/free of this stage 2 paging structures are not managed
+          by Intel IOMMU driver, but by KVM MMU.
+
 endif # INTEL_IOMMU
diff --git a/drivers/iommu/intel/Makefile b/drivers/iommu/intel/Makefile
index 5dabf081a7793..c097bdd6ee13d 100644
--- a/drivers/iommu/intel/Makefile
+++ b/drivers/iommu/intel/Makefile
@@ -7,3 +7,4 @@ obj-$(CONFIG_INTEL_IOMMU_DEBUGFS) += debugfs.o
 obj-$(CONFIG_INTEL_IOMMU_SVM) += svm.o
 obj-$(CONFIG_IRQ_REMAP) += irq_remapping.o
 obj-$(CONFIG_INTEL_IOMMU_PERF_EVENTS) += perfmon.o
+obj-$(CONFIG_INTEL_IOMMU_KVM) += kvm.o
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 924006cda18c5..fcdee40f30ed1 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -375,6 +375,15 @@ static inline int domain_type_is_si(struct dmar_domain *domain)
 	return domain->domain.type == IOMMU_DOMAIN_IDENTITY;
 }
 
+static inline int domain_type_is_kvm(struct dmar_domain *domain)
+{
+#ifdef CONFIG_INTEL_IOMMU_KVM
+	return domain->domain.type == IOMMU_DOMAIN_KVM;
+#else
+	return false;
+#endif
+}
+
 static inline int domain_pfn_supported(struct dmar_domain *domain,
 				       unsigned long pfn)
 {
@@ -1735,6 +1744,9 @@ static bool first_level_by_default(unsigned int type)
 	if (intel_cap_flts_sanity() ^ intel_cap_slts_sanity())
 		return intel_cap_flts_sanity();
 
+	if (type == IOMMU_DOMAIN_KVM)
+		return false;
+
 	/* Both levels are available, decide it based on domain type */
 	return type != IOMMU_DOMAIN_UNMANAGED;
 }
@@ -1826,7 +1838,8 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)
 
 static void domain_exit(struct dmar_domain *domain)
 {
-	if (domain->pgd) {
+	/* pgd of kvm domain is managed by KVM */
+	if (!domain_type_is_kvm(domain) && (domain->pgd)) {
 		LIST_HEAD(freelist);
 
 		domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw), &freelist);
@@ -4892,6 +4905,9 @@ const struct iommu_ops intel_iommu_ops = {
 	.hw_info		= intel_iommu_hw_info,
 	.domain_alloc		= intel_iommu_domain_alloc,
 	.domain_alloc_user	= intel_iommu_domain_alloc_user,
+#ifdef CONFIG_INTEL_IOMMU_KVM
+	.domain_alloc_kvm	= intel_iommu_domain_alloc_kvm,
+#endif
 	.probe_device		= intel_iommu_probe_device,
 	.probe_finalize		= intel_iommu_probe_finalize,
 	.release_device		= intel_iommu_release_device,
diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
index c76f558ae6323..8826e9248f6ed 100644
--- a/drivers/iommu/intel/iommu.h
+++ b/drivers/iommu/intel/iommu.h
@@ -1056,4 +1056,9 @@ static inline int width_to_agaw(int width)
 	return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
 }
 
+#ifdef CONFIG_INTEL_IOMMU_KVM
+struct iommu_domain *
+intel_iommu_domain_alloc_kvm(struct device *dev, u32 flags, const void *data);
+#endif
+
 #endif
diff --git a/drivers/iommu/intel/kvm.c b/drivers/iommu/intel/kvm.c
new file mode 100644
index 0000000000000..188ec90083051
--- /dev/null
+++ b/drivers/iommu/intel/kvm.c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/iommu.h>
+#include <asm/kvm_exported_tdp.h>
+#include "iommu.h"
+
+/**
+ * Check IOMMU hardware Snoop related caps
+ *
+ * - force_snooping:             Force snoop cpu caches per current KVM implementation.
+ * - scalable-mode:              To enable PGSNP bit in PASIDTE to overwrite SNP
+ *                               bit (bit 11) in stage 2 leaves.
+ * - paging structure coherency: As KVM will not call clflush_cache_range()
+ */
+static bool is_coherency(struct intel_iommu *iommu)
+{
+	return ecap_sc_support(iommu->ecap) && sm_supported(iommu) &&
+	       iommu_paging_structure_coherency(iommu);
+}
+
+static bool is_iommu_cap_compatible_to_kvm_domain(struct dmar_domain *domain,
+						  struct intel_iommu *iommu)
+{
+	if (!is_coherency(iommu))
+		return false;
+
+	if (domain->iommu_superpage > fls(cap_super_page_val(iommu->cap)))
+		return false;
+
+	if (domain->agaw > iommu->agaw || domain->agaw > cap_mgaw(iommu->cap))
+		return false;
+
+	return true;
+}
+
+/*
+ * Cache coherency is always enforced in KVM domain.
+ * IOMMU hardware caps will be checked to allow the cache coherency before
+ * device attachment to the KVM domain.
+ */
+static bool kvm_domain_enforce_cache_coherency(struct iommu_domain *domain)
+{
+	return true;
+}
+
+static const struct iommu_domain_ops intel_kvm_domain_ops = {
+	.free			= intel_iommu_domain_free,
+	.enforce_cache_coherency = kvm_domain_enforce_cache_coherency,
+};
+
+struct iommu_domain *
+intel_iommu_domain_alloc_kvm(struct device *dev, u32 flags, const void *data)
+{
+	bool request_nest_parent = flags & IOMMU_HWPT_ALLOC_NEST_PARENT;
+	const struct kvm_exported_tdp_meta_vmx *tdp = data;
+	struct dmar_domain *dmar_domain;
+	struct iommu_domain *domain;
+	struct intel_iommu *iommu;
+	int adjust_width;
+
+	iommu = device_to_iommu(dev, NULL, NULL);
+
+	if (!iommu)
+		return ERR_PTR(-ENODEV);
+	/*
+	 * In theroy, a KVM domain can be nested as a parent domain to a user
+	 * domain. Turn it off as we don't want to handle cases like IO page
+	 * fault on nested domain for now.
+	 */
+	if ((request_nest_parent)) {
+		pr_err("KVM domain does not work as nested parent currently\n");
+		return ERR_PTR(-EOPNOTSUPP);
+	}
+
+	if (!tdp || tdp->type != KVM_TDP_TYPE_EPT) {
+		pr_err("No meta data or wrong KVM TDP type\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	if (tdp->level != 4 && tdp->level != 5) {
+		pr_err("Unsupported KVM TDP level %d in IOMMU\n", tdp->level);
+		return ERR_PTR(-EOPNOTSUPP);
+	}
+
+	dmar_domain = alloc_domain(IOMMU_DOMAIN_KVM);
+	if (!dmar_domain)
+		return ERR_PTR(-ENOMEM);
+
+	if (dmar_domain->use_first_level)
+		WARN_ON("KVM domain is applying to IOMMU flpt\n");
+
+	domain = &dmar_domain->domain;
+	domain->ops = &intel_kvm_domain_ops;
+	domain->type = IOMMU_DOMAIN_KVM;
+
+	/* read dmar domain meta data from "tdp" */
+	dmar_domain->gaw = tdp->level == 4 ? ADDR_WIDTH_4LEVEL : ADDR_WIDTH_5LEVEL;
+	adjust_width = guestwidth_to_adjustwidth(dmar_domain->gaw);
+	dmar_domain->agaw = width_to_agaw(adjust_width);
+	dmar_domain->iommu_superpage = tdp->max_huge_page_level - 1;
+	dmar_domain->max_addr = (1 << dmar_domain->gaw);
+	dmar_domain->pgd = phys_to_virt(tdp->root_hpa);
+
+	dmar_domain->nested_parent = false;
+	dmar_domain->dirty_tracking = false;
+
+	/*
+	 * force_snooping and paging strucure coherency in KVM domain
+	 * IOMMU hareware cap will be checked before device attach
+	 */
+	dmar_domain->force_snooping = true;
+	dmar_domain->iommu_coherency = true;
+
+	/* no need to let iommu_map/unmap see pgsize_bitmap */
+	domain->pgsize_bitmap = 0;
+
+	/* force aperture */
+	domain->geometry.aperture_start = 0;
+	domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
+	domain->geometry.force_aperture = true;
+
+	if (!is_iommu_cap_compatible_to_kvm_domain(dmar_domain, iommu)) {
+		pr_err("Unsupported KVM TDP\n");
+		kfree(dmar_domain);
+		return ERR_PTR(-EOPNOTSUPP);
+	}
+
+	return domain;
+}
-- 
2.17.1


  parent reply	other threads:[~2023-12-02  9:53 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-02  9:12 [RFC PATCH 00/42] Sharing KVM TDP to IOMMU Yan Zhao
2023-12-02  9:13 ` [RFC PATCH 01/42] KVM: Public header for KVM to export TDP Yan Zhao
2023-12-02  9:15 ` [RFC PATCH 02/42] KVM: x86: Arch header for kvm to export TDP for Intel Yan Zhao
2023-12-02  9:15 ` [RFC PATCH 03/42] KVM: Introduce VM ioctl KVM_CREATE_TDP_FD Yan Zhao
2023-12-02  9:16 ` [RFC PATCH 04/42] KVM: Skeleton of KVM TDP FD object Yan Zhao
2023-12-02  9:16 ` [RFC PATCH 05/42] KVM: Embed "arch" object and call arch init/destroy in TDP FD Yan Zhao
2023-12-02  9:17 ` [RFC PATCH 06/42] KVM: Register/Unregister importers to KVM exported TDP Yan Zhao
2023-12-02  9:18 ` [RFC PATCH 07/42] KVM: Forward page fault requests to arch specific code for " Yan Zhao
2023-12-02  9:18 ` [RFC PATCH 08/42] KVM: Add a helper to notify importers that KVM exported TDP is flushed Yan Zhao
2023-12-02  9:19 ` [RFC PATCH 09/42] iommu: Add IOMMU_DOMAIN_KVM Yan Zhao
2023-12-02  9:20 ` [RFC PATCH 10/42] iommu: Add new iommu op to create domains managed by KVM Yan Zhao
2023-12-04 15:09   ` Jason Gunthorpe
2023-12-02  9:20 ` [RFC PATCH 11/42] iommu: Add new domain op cache_invalidate_kvm Yan Zhao
2023-12-04 15:09   ` Jason Gunthorpe
2023-12-05  6:40     ` Yan Zhao
2023-12-05 14:52       ` Jason Gunthorpe
2023-12-06  1:00         ` Yan Zhao
2023-12-02  9:21 ` [RFC PATCH 12/42] iommufd: Introduce allocation data info and flag for KVM managed HWPT Yan Zhao
2023-12-04 18:29   ` Jason Gunthorpe
2023-12-05  7:08     ` Yan Zhao
2023-12-05 14:53       ` Jason Gunthorpe
2023-12-06  0:58         ` Yan Zhao
2023-12-02  9:21 ` [RFC PATCH 13/42] iommufd: Add a KVM HW pagetable object Yan Zhao
2023-12-02  9:22 ` [RFC PATCH 14/42] iommufd: Enable KVM HW page table object to be proxy between KVM and IOMMU Yan Zhao
2023-12-04 18:34   ` Jason Gunthorpe
2023-12-05  7:09     ` Yan Zhao
2023-12-02  9:22 ` [RFC PATCH 15/42] iommufd: Add iopf handler to KVM hw pagetable Yan Zhao
2023-12-02  9:23 ` [RFC PATCH 16/42] iommufd: Enable device feature IOPF during device attachment to KVM HWPT Yan Zhao
2023-12-04 18:36   ` Jason Gunthorpe
2023-12-05  7:14     ` Yan Zhao
2023-12-05 14:53       ` Jason Gunthorpe
2023-12-06  0:55         ` Yan Zhao
2023-12-02  9:23 ` [RFC PATCH 17/42] iommu/vt-d: Make some macros and helpers to be extern Yan Zhao
2023-12-02  9:24 ` Yan Zhao [this message]
2023-12-02  9:24 ` [RFC PATCH 19/42] iommu/vt-d: Set bit PGSNP in PASIDTE if domain cache coherency is enforced Yan Zhao
2023-12-02  9:25 ` [RFC PATCH 20/42] iommu/vt-d: Support attach devices to IOMMU_DOMAIN_KVM domain Yan Zhao
2023-12-02  9:26 ` [RFC PATCH 21/42] iommu/vt-d: Check reserved bits for " Yan Zhao
2023-12-02  9:26 ` [RFC PATCH 22/42] iommu/vt-d: Support cache invalidate of " Yan Zhao
2023-12-02  9:26 ` [RFC PATCH 23/42] iommu/vt-d: Allow pasid 0 in IOPF Yan Zhao
2023-12-02  9:27 ` [RFC PATCH 24/42] KVM: x86/mmu: Move bit SPTE_MMU_PRESENT from bit 11 to bit 59 Yan Zhao
2023-12-02  9:27 ` [RFC PATCH 25/42] KVM: x86/mmu: Abstract "struct kvm_mmu_common" from "struct kvm_mmu" Yan Zhao
2023-12-02  9:28 ` [RFC PATCH 26/42] KVM: x86/mmu: introduce new op get_default_mt_mask to kvm_x86_ops Yan Zhao
2023-12-02  9:28 ` [RFC PATCH 27/42] KVM: x86/mmu: change param "vcpu" to "kvm" in kvm_mmu_hugepage_adjust() Yan Zhao
2023-12-02  9:29 ` [RFC PATCH 28/42] KVM: x86/mmu: change "vcpu" to "kvm" in page_fault_handle_page_track() Yan Zhao
2023-12-02  9:29 ` [RFC PATCH 29/42] KVM: x86/mmu: remove param "vcpu" from kvm_mmu_get_tdp_level() Yan Zhao
2023-12-02  9:30 ` [RFC PATCH 30/42] KVM: x86/mmu: remove param "vcpu" from kvm_calc_tdp_mmu_root_page_role() Yan Zhao
2023-12-02  9:30 ` [RFC PATCH 31/42] KVM: x86/mmu: add extra param "kvm" to kvm_faultin_pfn() Yan Zhao
2023-12-02  9:31 ` [RFC PATCH 32/42] KVM: x86/mmu: add extra param "kvm" to make_mmio_spte() Yan Zhao
2023-12-02  9:31 ` [RFC PATCH 33/42] KVM: x86/mmu: add extra param "kvm" to make_spte() Yan Zhao
2023-12-02  9:32 ` [RFC PATCH 34/42] KVM: x86/mmu: add extra param "kvm" to tdp_mmu_map_handle_target_level() Yan Zhao
2023-12-02  9:32 ` [RFC PATCH 35/42] KVM: x86/mmu: Get/Put TDP root page to be exported Yan Zhao
2023-12-02  9:33 ` [RFC PATCH 36/42] KVM: x86/mmu: Keep exported TDP root valid Yan Zhao
2023-12-02  9:33 ` [RFC PATCH 37/42] KVM: x86: Implement KVM exported TDP fault handler on x86 Yan Zhao
2023-12-02  9:35 ` [RFC PATCH 38/42] KVM: x86: "compose" and "get" interface for meta data of exported TDP Yan Zhao
2023-12-02  9:35 ` [RFC PATCH 39/42] KVM: VMX: add config KVM_INTEL_EXPORTED_EPT Yan Zhao
2023-12-02  9:36 ` [RFC PATCH 40/42] KVM: VMX: Compose VMX specific meta data for KVM exported TDP Yan Zhao
2023-12-02  9:36 ` [RFC PATCH 41/42] KVM: VMX: Implement ops .flush_remote_tlbs* in VMX when EPT is on Yan Zhao
2023-12-02  9:37 ` [RFC PATCH 42/42] KVM: VMX: Notify importers of exported TDP to flush TLBs on KVM flushes EPT Yan Zhao
2023-12-04 15:08 ` [RFC PATCH 00/42] Sharing KVM TDP to IOMMU Jason Gunthorpe
2023-12-04 16:38   ` Sean Christopherson
2023-12-05  1:31     ` Yan Zhao
2023-12-05  6:45       ` Tian, Kevin
2023-12-05  1:52   ` Yan Zhao
2023-12-05  6:30   ` Tian, Kevin
2023-12-04 17:00 ` Sean Christopherson
2023-12-04 17:30   ` Jason Gunthorpe
2023-12-04 19:22     ` Sean Christopherson
2023-12-04 19:50       ` Jason Gunthorpe
2023-12-04 20:11         ` Sean Christopherson
2023-12-04 23:49           ` Jason Gunthorpe
2023-12-05  7:17         ` Tian, Kevin
2023-12-05  5:53       ` Yan Zhao
2023-12-05  3:51   ` Yan Zhao

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=20231202092421.14524-1-yan.y.zhao@intel.com \
    --to=yan.y.zhao@intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=dwmw2@infradead.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=robin.murphy@arm.com \
    --cc=seanjc@google.com \
    --cc=will@kernel.org \
    --cc=yi.l.liu@intel.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

all inboxes | Powered by JetHome®