Skip to contentSkip to conversation

Hyper-V: root VM iommu kernel only driver

PATCH In review Mukesh R 4 participants 18 messages Nov 5 – Nov 11

Subsystems (5)
lore
Share & tools
lore

Mukesh R

author
#0 lore raw

v4 cover Hyper-V: root VM iommu kernel only driver

This short series introduces hyperv root VM iommu kernel only driver. This driver acts as liason between the hypervisor and iommu layer. Details are both in the actual commits and in the comments in the file implementing it.

Thanks,
-Mukesh

V0: based on upstream hyperv-next commit be0cfab740e5

V1:
- change order of lookup tree deletions to appease all AI agents, carving out hv_iommu_hyp_unmap_pages() from hv_iommu_unmap_pages().
- change unique_id to ida subsystem management so we never ever run out of domain ids.
- change hv_max_iova_width to hv_iommu_max_iova and generate proper mask.
- some other minor changes.

V2:
- Add patch (0001) to allow gfp_flags to be passed to deposit pages function.
- In last main patch:
o remove hv_iommu_setup_skip to skip devices being managed
o remove hv_special_domain()
o Add comments around pgsize_bitmap and IOMMU capabilities.
o Add to lookup tree before mapping iova in the map callback
o Unmap iova before removing from the tree in the unmap callback

V3:
- Rebase to rc1 tag 6628e7d4c6b2 which contains both pviommu and the deposit memory redesign patches.
- Add a tiny patch to rename hv_iommu_init() to hv_pviommu_init(), and only call it for non-root.

V4:
- Use gfp flag in add tree function instead of GFP_KERNEL.
- Check for HV_STATUS_DEVICE_ALREADY_IN_DOMAIN in attach device.
- Don't call delete tree if unmap hypercall failed.
- Don't print error if map device pages hypercall failed with enomem.
- Minor comments fixups.

Mukesh R (5):
mshv: Add gfp flags to deposit memory logic
PCI: hv: Export hv_build_devid_type_pci() and change return type
mshv: Import data structs around device domains from hyperv headers iommu/hyperv: Only call pviommu init on non-root VMs x86/hyperv: Implement root partition IOMMU kernel only driver

 arch/x86/hyperv/hv_init.c             |   3 +-
 arch/x86/hyperv/irqdomain.c           |   9 +-
 arch/x86/include/asm/mshyperv.h       |   8 +-
 arch/x86/kernel/pci-dma.c             |   2 +
 drivers/hv/hv_proc.c                  |  20 +-
 drivers/iommu/hyperv/Kconfig          |  13 +
 drivers/iommu/hyperv/Makefile         |   1 +
 drivers/iommu/hyperv/hv-iommu-guest.c |   2 +-
 drivers/iommu/hyperv/hv-iommu-root.c  | 613 ++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h        |  17 +-
 include/hyperv/hvgdk_mini.h           |   4 +
 include/hyperv/hvhdk_mini.h           |  15 +
 include/linux/hyperv.h                |   6 +
 13 files changed, 694 insertions(+), 19 deletions(-)
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
Signature · 1 line
2.51.2.vfs.0.1
author
#1 lore raw

PATCH v4 1/5 mshv: Add gfp flags to deposit memory logic

Currently GFP_KERNEL is hard coded in hv_alloc_dep_pages(). Add gfp_flags parameter so it can be called in atomic context from map_pages callback in struct iommu_domain_ops and other places in future. A new function hv_deposit_memory_gfp() is added to facilitate this, so hv_deposit_memory() can continue to function as is.

---
 drivers/hv/hv_proc.c           | 20 +++++++++++---------
 include/asm-generic/mshyperv.h | 14 +++++++++++---
 2 files changed, 22 insertions(+), 12 deletions(-)
drivers/hv/hv_proc.c +11 -9
index f811bb5c42a1..8380e9050f46 100644
@@ -22,7 +22,7 @@
  * Returns: number of pages allocated or -ENOMEM
  */
 static int hv_alloc_dep_pages(int node, u64 *pfna, u64 *lastpfnp, int num_pages,
-			      bool single)
+			      bool single, gfp_t gfp_flags)
 {
 	struct page *page;
 	int num_allocd, count = 0;
@@ -37,7 +37,6 @@ static int hv_alloc_dep_pages(int node, u64 *pfna, u64 *lastpfnp, int num_pages,
 	while (num_pages) {
 		/* Find highest order we can actually allocate */
 		int order = 31 - __builtin_clz(num_pages);
-		gfp_t gfp_flags = GFP_KERNEL;
 
 		if (!single)
 			gfp_flags |= __GFP_NOWARN;
@@ -81,7 +80,8 @@ static int hv_alloc_dep_pages(int node, u64 *pfna, u64 *lastpfnp, int num_pages,
  * so we must split that also. Lastly, HV_MAX_CONTIGUOUS_ALLOCATION_PAGES is
  * not guaranteed to always be power of 2.
  */
-static int hv_call_deposit_pages(int node, u64 partition_id, bool contiguous)
+static int hv_call_deposit_pages(int node, u64 partition_id, bool contiguous,
+				 gfp_t gfp_flags)
 {
 	struct hv_deposit_memory *hc_input;
 	int i, rc, num_pages;
@@ -98,14 +98,15 @@ static int hv_call_deposit_pages(int node, u64 partition_id, bool contiguous)
 		num_pages = HV_DEPOSIT_MAX;
 	}
 
-	hc_input = (struct hv_deposit_memory *)get_zeroed_page(GFP_KERNEL);
+	hc_input = (struct hv_deposit_memory *)get_zeroed_page(gfp_flags);
 	if (hc_input == NULL)
 		return -ENOMEM;
 
 	hc_input->partition_id = partition_id;
 	pfna = hc_input->gpa_page_list;
 
-	rc = hv_alloc_dep_pages(node, pfna, &lastpfn, num_pages, contiguous);
+	rc = hv_alloc_dep_pages(node, pfna, &lastpfn, num_pages, contiguous,
+				gfp_flags);
 	if (rc < 0)
 		goto out_free;
 
@@ -159,7 +160,7 @@ static int hv_call_deposit_pages(int node, u64 partition_id, bool contiguous)
 	return rc;
 }
 
-int hv_deposit_memory_node(int node, u64 pt_id, u64 hv_status)
+int hv_deposit_memory_node(int node, u64 pt_id, u64 hv_status, gfp_t gfp_flags)
 {
 	int result = hv_result(hv_status);
 	bool contiguous = false;
@@ -179,7 +180,7 @@ int hv_deposit_memory_node(int node, u64 pt_id, u64 hv_status)
 	    result == HV_STATUS_INSUFFICIENT_CONTIGUOUS_ROOT_MEMORY)
 		contiguous = true;
 
-	return hv_call_deposit_pages(node, pt_id, contiguous);
+	return hv_call_deposit_pages(node, pt_id, contiguous, gfp_flags);
 }
 EXPORT_SYMBOL_GPL(hv_deposit_memory_node);
 
@@ -232,7 +233,7 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
 			break;
 		}
 		ret = hv_deposit_memory_node(node, hv_current_partition_id,
-					     status);
+					     status, GFP_KERNEL);
 	} while (!ret);
 
 	return ret;
@@ -266,7 +267,8 @@ int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
 			}
 			break;
 		}
-		ret = hv_deposit_memory_node(node, partition_id, status);
+		ret = hv_deposit_memory_node(node, partition_id, status,
+					     GFP_KERNEL);
 
 	} while (!ret);
 
include/asm-generic/mshyperv.h +11 -3
index 4c9f3353074a..2584e693d7b3 100644
@@ -346,7 +346,7 @@ static inline bool hv_parent_partition(void)
 }
 
 bool hv_result_needs_memory(u64 status);
-int hv_deposit_memory_node(int node, u64 partition_id, u64 status);
+int hv_deposit_memory_node(int node, u64 partition_id, u64 status, gfp_t gfp);
 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
 int hv_call_notify_all_processors_started(void);
 bool hv_lp_exists(u32 lp_index);
@@ -357,7 +357,8 @@ static inline bool hv_root_partition(void) { return false; }
 static inline bool hv_l1vh_partition(void) { return false; }
 static inline bool hv_parent_partition(void) { return false; }
 static inline bool hv_result_needs_memory(u64 status) { return false; }
-static inline int hv_deposit_memory_node(int node, u64 partition_id, u64 status)
+static inline int hv_deposit_memory_node(int node, u64 partition_id, u64 status,
+					 gfp_t gfp)
 {
 	return -EOPNOTSUPP;
 }
@@ -379,9 +380,16 @@ static inline int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u3
 }
 #endif /* CONFIG_MSHV_ROOT */
 
+/* Generic deposit function with no default numa node and GFP_KERNEL */
 static inline int hv_deposit_memory(u64 partition_id, u64 status)
 {
-	return hv_deposit_memory_node(NUMA_NO_NODE, partition_id, status);
+	return hv_deposit_memory_node(NUMA_NO_NODE, partition_id, status,
+				      GFP_KERNEL);
+}
+
+static inline int hv_deposit_memory_gfp(u64 partition_id, u64 status, gfp_t gfp)
+{
+	return hv_deposit_memory_node(NUMA_NO_NODE, partition_id, status, gfp);
 }
 
 #if IS_ENABLED(CONFIG_HYPERV_VTL_MODE)
Signature · 1 line
2.51.2.vfs.0.1
author
#4 lore raw

PATCH v4 2/5 PCI: hv: Export hv_build_devid_type_pci() and change return type

In reply to Mukesh R

On Hyper-V, most hypercalls related to device domain management like attach device to a device domain, or to map/unmap regions, interrupts, etc need a device ID as a parameter. So, make hv_build_devid_type_pci() public and change return type to u64 to enforce its size.

---
 arch/x86/hyperv/irqdomain.c     | 9 +++++----
 arch/x86/include/asm/mshyperv.h | 6 ++++++
 2 files changed, 11 insertions(+), 4 deletions(-)
arch/x86/hyperv/irqdomain.c +5 -4
index b3ad50a874dc..8780573a4332 100644
@@ -112,7 +112,7 @@ static int get_rid_cb(struct pci_dev *pdev, u16 alias, void *data)
 	return 0;
 }
 
-static union hv_device_id hv_build_devid_type_pci(struct pci_dev *pdev)
+u64 hv_build_devid_type_pci(struct pci_dev *pdev)
 {
 	int pos;
 	union hv_device_id hv_devid;
@@ -172,8 +172,9 @@ static union hv_device_id hv_build_devid_type_pci(struct pci_dev *pdev)
 	}
 
 out:
-	return hv_devid;
+	return hv_devid.as_uint64;
 }
+EXPORT_SYMBOL_GPL(hv_build_devid_type_pci);
 
 /*
  * hv_map_msi_interrupt() - Map the MSI IRQ in the hypervisor.
@@ -196,7 +197,7 @@ int hv_map_msi_interrupt(struct irq_data *data,
 
 	msidesc = irq_data_get_msi_desc(data);
 	pdev = msi_desc_to_pci_dev(msidesc);
-	hv_devid = hv_build_devid_type_pci(pdev);
+	hv_devid.as_uint64 = hv_build_devid_type_pci(pdev);
 	cpu = cpumask_first(irq_data_get_effective_affinity_mask(data));
 
 	return hv_map_interrupt(hv_devid, false, cpu, cfg->vector,
@@ -271,7 +272,7 @@ static int hv_unmap_msi_interrupt(struct pci_dev *pdev,
 {
 	union hv_device_id hv_devid;
 
-	hv_devid = hv_build_devid_type_pci(pdev);
+	hv_devid.as_uint64 = hv_build_devid_type_pci(pdev);
 	return hv_unmap_interrupt(hv_devid.as_uint64, irq_entry);
 }
 
arch/x86/include/asm/mshyperv.h +6 -0
index 20d947c2c758..24b19cd28a05 100644
@@ -248,6 +248,12 @@ void hv_crash_asm_end(void);
 static inline void hv_root_crash_init(void) {}
 #endif  /* CONFIG_MSHV_ROOT && CONFIG_CRASH_DUMP */
 
+#ifdef CONFIG_PCI_MSI
+u64 hv_build_devid_type_pci(struct pci_dev *pdev);
+#else
+static inline u64 hv_build_devid_type_pci(struct pci_dev *pdev) { return 0; }
+#endif
+
 #else /* CONFIG_HYPERV */
 static inline void hyperv_init(void) {}
 static inline void hyperv_setup_mmu_ops(void) {}
Signature · 1 line
2.51.2.vfs.0.1
author
#6 lore raw

PATCH v4 3/5 mshv: Import data structs around device domains from hyperv headers

In reply to Mukesh R

Copy/import from Hyper-V public headers, definitions and hypercalls that are related to checking for device domain availibility, and mapping and unmapping of device pages.

---
 include/hyperv/hvgdk_mini.h |  4 ++++
 include/hyperv/hvhdk_mini.h | 15 +++++++++++++++
 2 files changed, 19 insertions(+)
include/hyperv/hvgdk_mini.h +4 -0
index 966945f0504f..606b16f43e16 100644
@@ -327,6 +327,8 @@ union hv_hypervisor_version_info {
 /* stimer Direct Mode is available */
 #define HV_STIMER_DIRECT_MODE_AVAILABLE			BIT(19)
 
+#define HV_DEVICE_DOMAIN_AVAILABLE			BIT(24)
+
 /*
  * Implementation recommendations. Indicates which behaviors the hypervisor
  * recommends the OS implement for optimal performance.
@@ -489,6 +491,8 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST	0x00b0
 #define HVCALL_CREATE_DEVICE_DOMAIN			0x00b1
 #define HVCALL_ATTACH_DEVICE_DOMAIN			0x00b2
+#define HVCALL_MAP_DEVICE_GPA_PAGES			0x00b3
+#define HVCALL_UNMAP_DEVICE_GPA_PAGES			0x00b4
 #define HVCALL_SIGNAL_EVENT_DIRECT			0x00c0
 #define HVCALL_POST_MESSAGE_DIRECT			0x00c1
 #define HVCALL_DISPATCH_VP				0x00c2
include/hyperv/hvhdk_mini.h +15 -0
index 57b70efe60d4..bb42b81df848 100644
@@ -549,6 +549,7 @@ union hv_device_id {		/* HV_DEVICE_ID */
 } __packed;
 
 /* Device domain types */
+#define HV_DEVICE_DOMAIN_TYPE_S2	0 /* HV_DEVICE_DOMAIN_ID_TYPE_S2 */
 #define HV_DEVICE_DOMAIN_TYPE_S1	1 /* HV_DEVICE_DOMAIN_ID_TYPE_S1 */
 
 /* ID for default domain and NULL domain */
@@ -657,6 +658,20 @@ struct hv_output_get_iommu_capabilities {
 	u64 pgsize_bitmap;
 } __packed;
 
+struct hv_input_map_device_gpa_pages { /* HV_INPUT_MAP_DEVICE_GPA_PAGES */
+	struct hv_input_device_domain device_domain;
+	union hv_input_vtl target_vtl;
+	u8 padding[3];
+	u32 map_flags;
+	u64 target_device_va_base;
+	u64 gpa_page_list[];
+} __packed;
+
+struct hv_input_unmap_device_gpa_pages {  /* HV_INPUT_UNMAP_DEVICE_GPA_PAGES */
+	struct hv_input_device_domain device_domain;
+	u64 target_device_va_base;
+} __packed;
+
 /* HV_LOGICAL_DEVICE_PROPERTY_CODE */
 enum hv_logical_device_property_code {
 	HV_LOGICAL_DEVICE_PROPERTY_PVIOMMU = 10,
Signature · 1 line
2.51.2.vfs.0.1
author
#7 lore raw

PATCH v4 4/5 iommu/hyperv: Only call pviommu init on non-root VMs

In reply to Mukesh R

Next patch introduces root iommu init. Since both can co-exist, set x86 iommu_init to pviommu init only on non-root VMs. Also, rename hv_iommu_init() to hv_pviommu_init(). Lastly, check for null iommu_init before assignement as suggested by one of the pviommu authors.

---
 arch/x86/hyperv/hv_init.c             | 3 ++-
 arch/x86/include/asm/mshyperv.h       | 2 +-
 drivers/iommu/hyperv/hv-iommu-guest.c | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
arch/x86/hyperv/hv_init.c +2 -1
index b90e6f487533..ea50708c3ae6 100644
@@ -580,7 +580,8 @@ void __init hyperv_init(void)
 	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
 
 #ifdef CONFIG_HYPERV_PVIOMMU
-	x86_init.iommu.iommu_init = hv_iommu_init;
+	if (!hv_root_partition())
+		x86_init.iommu.iommu_init = hv_pviommu_init;
 #endif
 
 	hv_apic_init();
arch/x86/include/asm/mshyperv.h +1 -1
index 24b19cd28a05..c95d75ae645e 100644
@@ -320,7 +320,7 @@ static inline void __mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0) {}
 #endif
 
 #ifdef CONFIG_HYPERV_PVIOMMU
-int __init hv_iommu_init(void);
+int __init hv_pviommu_init(void);
 #endif
 
 #include <asm-generic/mshyperv.h>
drivers/iommu/hyperv/hv-iommu-guest.c +1 -1
index 876a22f21f1e..a40a8ceb3261 100644
@@ -601,7 +601,7 @@ hv_init_iommu_device(struct hv_iommu_dev *hv_iommu,
 	hv_iommu_device = hv_iommu;
 }
 
-int __init hv_iommu_init(void)
+int __init hv_pviommu_init(void)
 {
 	u64 status;
 	int ret = 0;
Signature · 1 line
2.51.2.vfs.0.1
author
#10 lore raw

PATCH v4 5/5 x86/hyperv: Implement root partition IOMMU kernel only driver

In reply to Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works on Microsoft Hyper-V hypervisor (aka MSHV) in privileged VMs aka root VMs. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, attaching of devices, mapping and unmapping of pages, etc. During boot, hypervisor automatically creates identity and blocked domains, so there is no need to do hypercalls to create them. This is a kernel only driver and only supported on baremetal root (and not L1VH root) without any guest passthru support. Support for guest device passthru will be added incrementally.

---
 arch/x86/kernel/pci-dma.c            |   2 +
 drivers/iommu/hyperv/Kconfig         |  13 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 613 +++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h       |   3 +
 include/linux/hyperv.h               |   6 +
 6 files changed, 638 insertions(+)
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
arch/x86/kernel/pci-dma.c +2 -0
index 75cf8f6ae8cd..560d8a4f6e1d 100644
@@ -8,6 +8,7 @@
 #include <linux/gfp.h>
 #include <linux/pci.h>
 #include <linux/amd-iommu.h>
+#include <linux/hyperv.h>
 
 #include <asm/proto.h>
 #include <asm/dma.h>
@@ -103,6 +104,7 @@ void __init pci_iommu_alloc(void)
 	gart_iommu_hole_init();
 	amd_iommu_detect();
 	detect_intel_iommu();
+	hv_root_iommu_detect();
 	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
 }
 
drivers/iommu/hyperv/Kconfig +13 -0
index f2f993f647c3..6ba5125ed501 100644
@@ -13,3 +13,16 @@ config HYPERV_PVIOMMU
 	  Microsoft Hyper-V. Provides DMA remapping and IOTLB
 	  flush support to enable DMA isolation for PCI devices
 	  assigned to the guest.
+
+config HYPERV_ROOT_IOMMU
+	bool "Hyper-V IOMMU Device in root partition"
+	depends on HYPERV && X86 && PCI_MSI && MSHV_ROOT
+	select IOMMU_API
+	default HYPERV
+	help
+	  This enables Hyper-V pseudo IOMMU device. When running as privileged
+	  VM aka root on Microsoft Hyper-V hypervisor, this must be enabled
+	  for doing any PCI passthru of devices to guest VMs. This applies to
+	  both PFs and VFs. When enabling this, it is best to disable amd/intel
+	  iommus via: intel_iommu=off amd_iommu=off as the hypervisor really
+	  owns the iommu.
drivers/iommu/hyperv/Makefile +1 -0
index d3b2a259439c..04c66b4409a3 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_IRQ_REMAP) += hv-irq-remap-x86.o
 obj-$(CONFIG_HYPERV_PVIOMMU) += hv-iommu-guest.o
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +613 -0

613 lines not on this page · raw message

include/asm-generic/mshyperv.h +3 -0
index 2584e693d7b3..ad02d6956ae3 100644
@@ -28,6 +28,9 @@
 
 #define VTPM_BASE_ADDRESS 0xfed40000
 
+#define HV_REP_COUNT_MAX        \
+	(HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)
+
 enum hv_partition_type {
 	HV_PARTITION_TYPE_GUEST,
 	HV_PARTITION_TYPE_ROOT,
include/linux/hyperv.h +6 -0
index e85a1a043eac..8aa752e8c4e1 100644
@@ -1823,4 +1823,10 @@ static inline unsigned long virt_to_hvpfn(void *addr)
 #define HVPFN_DOWN(x)	((x) >> HV_HYP_PAGE_SHIFT)
 #define page_to_hvpfn(page)	(page_to_pfn(page) * NR_HV_HYP_PAGES_IN_PAGE)
 
+#ifdef CONFIG_HYPERV_ROOT_IOMMU
+void __init hv_root_iommu_detect(void);
+#else
+static inline void hv_root_iommu_detect(void) { }
+#endif /* CONFIG_HYPERV_ROOT_IOMMU */
+
 #endif /* _HYPERV_H */
Signature · 1 line
2.51.2.vfs.0.1
#8 lore raw

Re: [PATCH V4 4/5] iommu/hyperv: Only call pviommu init on non-root VMs

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] iommu/hyperv: hyperv_init() overwrites iommu_init without prior check --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/hyperv: hyperv_init() overwrites iommu_init without prior check --

commit ea2b425f9f0c2f00b3af04374abd0211b8229429
Author: Mukesh R <[email protected]>

iommu/hyperv: Only call pviommu init on non-root VMs

This commit restricts setting x86_init.iommu.iommu_init to the Hyper-V PVIOMMU initialization callback to non-root partitions and renames hv_iommu_init() to hv_pviommu_init().

Quoted 14 lines

iommu/hyperv: Only call pviommu init on non-root VMs

Next patch introduces root iommu init. Since both can co-exist, set x86 iommu_init to pviommu init only on non-root VMs. Also, rename hv_iommu_init() to hv_pviommu_init(). Lastly, check for null iommu_init before assignement as suggested by one of the pviommu authors.

arch/x86/hyperv/hv_init.c +0 -0
index b90e6f4875335..ea50708c3ae60 100644

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 584

Quoted 6 lines
arch/x86/hyperv/hv_init.c +2 -1
@@ -580,7 +580,8 @@ void __init hyperv_init(void)
 	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
 
 #ifdef CONFIG_HYPERV_PVIOMMU
-	x86_init.iommu.iommu_init = hv_iommu_init;
+	if (!hv_root_partition())
+		x86_init.iommu.iommu_init = hv_pviommu_init;

[Severity: High]
The commit message states:

"Lastly, check for null iommu_init before assignement as suggested by one of the pviommu authors."

However, hyperv_init() in arch/x86/hyperv/hv_init.c only checks !hv_root_partition() before overwriting x86_init.iommu.iommu_init. Was the check accidentally omitted from this patch?

Also, x86_init.iommu.iommu_init is initialized to iommu_init_noop rather than NULL in arch/x86/kernel/x86_init.c:

    .iommu = {
        .iommu_init = iommu_init_noop,
    },

During x86 boot, start_kernel() -> mm_core_init() -> arch_mm_preinit() -> pci_iommu_alloc() runs before init_IRQ() -> apic_intr_mode_init() -> x86_platform.apic_post_init() -> hyperv_init(). If an ACPI DMAR or IVRS table is present on a non-root partition (such as a guest VM with an emulated Intel VT-d or AMD-Vi vIOMMU, or an L1VH partition where hv_root_partition() returns false), detect_intel_iommu() in drivers/iommu/intel/dmar.c or amd_iommu_detect() in drivers/iommu/amd/init.c sets iommu_detected = 1 and sets x86_init.iommu.iommu_init to intel_iommu_init or amd_iommu_init.

When hyperv_init() subsequently overwrites x86_init.iommu.iommu_init with hv_pviommu_init, apic_bsp_setup() -> enable_IR_x2apic() -> irq_remapping_prepare() still allocates early DMAR/IVRS structures (dmar_drhd_unit and intel_iommu, or amd_iommu instances, device tables, and command buffers). Later, when pci_iommu_init() invokes x86_init.iommu.iommu_init(), hv_pviommu_init() in drivers/iommu/hyperv/hv-iommu-guest.c sees iommu_detected set:

    if (no_iommu || iommu_detected)
        return -ENODEV;

and returns -ENODEV without running intel_iommu_init() or amd_iommu_init(). Could this leave neither IOMMU initialized while leaking the early-allocated DMAR/IVRS structures, and should hyperv_init() check !iommu_detected (or x86_init.iommu.iommu_init == iommu_init_noop) before assigning hv_pviommu_init?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#11 lore raw

Re: [PATCH V4 5/5] x86/hyperv: Implement root partition IOMMU kernel only driver

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] iommu/hyperv: missing !hv_root_partition() check in hv_root_iommu_detect() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/hyperv: missing !hv_root_partition() check in hv_root_iommu_detect() --

commit c89a0e9d0fe34e767bd4527e0d99839504bcbea1
Author: Mukesh R <[email protected]>

x86/hyperv: Implement root partition IOMMU kernel only driver

This commit implements a kernel-only virtual IOMMU driver for baremetal Microsoft Hyper-V root partitions, handling paging domain management, device attachment, and IOVA page mapping via hypercalls.

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +0 -0
new file mode 100644
index 0000000000000..e0e3e9d4385e2

[ ... ]

Quoted 34 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +34 -0
+static int hv_iommu_get_caps(struct hv_output_get_iommu_capabilities *caps)
+{
+	u64 status;
+	unsigned long flags;
+	struct hv_input_get_iommu_capabilities *input;
+	struct hv_output_get_iommu_capabilities *output;
+
+	local_irq_save(flags);
+
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	output = *this_cpu_ptr(hyperv_pcpu_output_arg);
+	memset(input, 0, sizeof(*input));
+	input->partition_id = HV_PARTITION_ID_SELF;
+	status = hv_do_hypercall(HVCALL_GET_IOMMU_CAPABILITIES, input, output);
+	*caps = *output;
+
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "\n");
+
+	return hv_result_to_errno(status);
+}
+
+static int __init hv_root_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
+
+	rc = hv_iommu_get_caps(&caps);

[ ... ]

Quoted 4 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+void __init hv_root_iommu_detect(void)
+{
+	if (no_iommu || iommu_detected || hv_l1vh_partition())
+		return;

drivers/iommu/hyperv/hv-iommu-root.c line 613

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+	/* This is set for root partitions only, it is never set for non-root */
+	if (!(ms_hyperv.misc_features & HV_DEVICE_DOMAIN_AVAILABLE))
+		return;
+
+	iommu_detected = 1;
+	x86_init.iommu.iommu_init = hv_root_iommu_init;
+
+	pci_request_acs();
+}

[Severity: High]
As reported by Sashiko on v3 [1], should hv_root_iommu_detect() check !hv_root_partition() instead of hv_l1vh_partition()?

During x86 boot, ms_hyperv_init_platform() calls hv_identify_partition_type() in drivers/hv/hv_common.c, which forces hv_curr_partition_type to HV_PARTITION_TYPE_GUEST whenever HV_ISOLATION is set so that Confidential VMs do not execute root partition paths:

drivers/hv/hv_common.c:hv_identify_partition_type() {
	hv_curr_partition_type = HV_PARTITION_TYPE_GUEST;
	...
	if ((ms_hyperv.priv_high & HV_CREATE_PARTITIONS) &&
	    !(ms_hyperv.priv_high & HV_ISOLATION)) {
		...
	}
}

Later in boot, pci_iommu_alloc() calls hv_root_iommu_detect(). Because hv_root_iommu_detect() only checks hv_l1vh_partition(), a guest partition where HV_DEVICE_DOMAIN_AVAILABLE is set in ms_hyperv.misc_features will set iommu_detected = 1 and x86_init.iommu.iommu_init = hv_root_iommu_init.

When hyperv_init() then calls hv_common_init() in drivers/hv/hv_common.c, hyperv_pcpu_output_arg is only allocated if hv_output_page_exists() is true:

drivers/hv/hv_common.c:hv_common_init() {
	...
	if (hv_output_page_exists()) {
		hyperv_pcpu_output_arg = alloc_percpu(void *);
		BUG_ON(!hyperv_pcpu_output_arg);
	}
}

For HV_PARTITION_TYPE_GUEST (with CONFIG_MSHV_ROOT depending on !HYPERV_VTL_MODE), hv_output_page_exists() is false and hyperv_pcpu_output_arg remains NULL.

If CONFIG_HYPERV_PVIOMMU is disabled, pci_iommu_init() invokes hv_root_iommu_init() -> hv_iommu_get_caps(), which dereferences *this_cpu_ptr(hyperv_pcpu_output_arg) while hyperv_pcpu_output_arg is NULL. If CONFIG_HYPERV_PVIOMMU is enabled, hyperv_init() overwrites x86_init.iommu.iommu_init with hv_pviommu_init, which then fails with -ENODEV because hv_root_iommu_detect() already set iommu_detected = 1.

Would checking !hv_root_partition() in hv_root_iommu_detect() avoid this by excluding both L1VH and guest partitions?

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#12 lore raw

Re: [PATCH V4 5/5] x86/hyperv: Implement root partition IOMMU kernel only driver

Quoted 118 lines · On 11/4/26 18:38, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/hyperv: missing !hv_root_partition() check in hv_root_iommu_detect() --

commit c89a0e9d0fe34e767bd4527e0d99839504bcbea1
Author: Mukesh R <[email protected]>

x86/hyperv: Implement root partition IOMMU kernel only driver

This commit implements a kernel-only virtual IOMMU driver for baremetal Microsoft Hyper-V root partitions, handling paging domain management, device attachment, and IOVA page mapping via hypercalls.

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +0 -0
new file mode 100644
index 0000000000000..e0e3e9d4385e2

[ ... ]

Quoted 34 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +34 -0
+static int hv_iommu_get_caps(struct hv_output_get_iommu_capabilities *caps)
+{
+	u64 status;
+	unsigned long flags;
+	struct hv_input_get_iommu_capabilities *input;
+	struct hv_output_get_iommu_capabilities *output;
+
+	local_irq_save(flags);
+
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	output = *this_cpu_ptr(hyperv_pcpu_output_arg);
+	memset(input, 0, sizeof(*input));
+	input->partition_id = HV_PARTITION_ID_SELF;
+	status = hv_do_hypercall(HVCALL_GET_IOMMU_CAPABILITIES, input, output);
+	*caps = *output;
+
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "\n");
+
+	return hv_result_to_errno(status);
+}
+
+static int __init hv_root_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
+
+	rc = hv_iommu_get_caps(&caps);

[ ... ]

Quoted 4 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+void __init hv_root_iommu_detect(void)
+{
+	if (no_iommu || iommu_detected || hv_l1vh_partition())
+		return;

drivers/iommu/hyperv/hv-iommu-root.c line 613

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+	/* This is set for root partitions only, it is never set for non-root */
+	if (!(ms_hyperv.misc_features & HV_DEVICE_DOMAIN_AVAILABLE))
+		return;
+
+	iommu_detected = 1;
+	x86_init.iommu.iommu_init = hv_root_iommu_init;
+
+	pci_request_acs();
+}

[Severity: High]
As reported by Sashiko on v3 [1], should hv_root_iommu_detect() check !hv_root_partition() instead of hv_l1vh_partition()?

During x86 boot, ms_hyperv_init_platform() calls hv_identify_partition_type() in drivers/hv/hv_common.c, which forces hv_curr_partition_type to HV_PARTITION_TYPE_GUEST whenever HV_ISOLATION is set so that Confidential VMs do not execute root partition paths:

drivers/hv/hv_common.c:hv_identify_partition_type() {
	hv_curr_partition_type = HV_PARTITION_TYPE_GUEST;
	...
	if ((ms_hyperv.priv_high & HV_CREATE_PARTITIONS) &&
	    !(ms_hyperv.priv_high & HV_ISOLATION)) {
		...
	}
}

Later in boot, pci_iommu_alloc() calls hv_root_iommu_detect(). Because hv_root_iommu_detect() only checks hv_l1vh_partition(), a guest partition where HV_DEVICE_DOMAIN_AVAILABLE is set in ms_hyperv.misc_features will set iommu_detected = 1 and x86_init.iommu.iommu_init = hv_root_iommu_init.

When hyperv_init() then calls hv_common_init() in drivers/hv/hv_common.c, hyperv_pcpu_output_arg is only allocated if hv_output_page_exists() is true:

drivers/hv/hv_common.c:hv_common_init() {
	...
	if (hv_output_page_exists()) {
		hyperv_pcpu_output_arg = alloc_percpu(void *);
		BUG_ON(!hyperv_pcpu_output_arg);
	}
}

For HV_PARTITION_TYPE_GUEST (with CONFIG_MSHV_ROOT depending on !HYPERV_VTL_MODE), hv_output_page_exists() is false and hyperv_pcpu_output_arg remains NULL.

If CONFIG_HYPERV_PVIOMMU is disabled, pci_iommu_init() invokes hv_root_iommu_init() -> hv_iommu_get_caps(), which dereferences *this_cpu_ptr(hyperv_pcpu_output_arg) while hyperv_pcpu_output_arg is NULL. If CONFIG_HYPERV_PVIOMMU is enabled, hyperv_init() overwrites x86_init.iommu.iommu_init with hv_pviommu_init, which then fails with -ENODEV because hv_root_iommu_detect() already set iommu_detected = 1.

Quoted 5 lines

Would checking !hv_root_partition() in hv_root_iommu_detect() avoid this by excluding both L1VH and guest partitions?

[1] https://lore.kernel.org/r/[email protected]

No. HV_DEVICE_DOMAIN_AVAILABLE is never set for non-root, ie, guest partitions. So, a guest partition will never set iommu_detected here.

Reviewed-by on v4 4/5
#9 lore raw

Re: [PATCH V4 4/5] iommu/hyperv: Only call pviommu init on non-root VMs

In reply to Mukesh R
Quoted 3 lines · Mukesh R

Next patch introduces root iommu init. Since both can co-exist, set x86 iommu_init to pviommu init only on non-root VMs. Also, rename hv_iommu_init() to hv_pviommu_init().

[...]Lastly, check for null iommu_init

Quoted 1 line

before assignement as suggested by one of the pviommu authors.

You no longer do this below. Also, please wait a week or so before spinning a new revision to collect additional comments.

With that fixed,

Quoted 48 lines
---
 arch/x86/hyperv/hv_init.c             | 3 ++-
 arch/x86/include/asm/mshyperv.h       | 2 +-
 drivers/iommu/hyperv/hv-iommu-guest.c | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
arch/x86/hyperv/hv_init.c +2 -1
index b90e6f487533..ea50708c3ae6 100644
@@ -580,7 +580,8 @@ void __init hyperv_init(void)
 	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
 
 #ifdef CONFIG_HYPERV_PVIOMMU
-	x86_init.iommu.iommu_init = hv_iommu_init;
+	if (!hv_root_partition())
+		x86_init.iommu.iommu_init = hv_pviommu_init;
 #endif
 
 	hv_apic_init();
arch/x86/include/asm/mshyperv.h +1 -1
index 24b19cd28a05..c95d75ae645e 100644
@@ -320,7 +320,7 @@ static inline void __mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0) {}
 #endif
 
 #ifdef CONFIG_HYPERV_PVIOMMU
-int __init hv_iommu_init(void);
+int __init hv_pviommu_init(void);
 #endif
 
 #include <asm-generic/mshyperv.h>
drivers/iommu/hyperv/hv-iommu-guest.c +1 -1
index 876a22f21f1e..a40a8ceb3261 100644
@@ -601,7 +601,7 @@ hv_init_iommu_device(struct hv_iommu_dev *hv_iommu,
 	hv_iommu_device = hv_iommu;
 }
 
-int __init hv_iommu_init(void)
+int __init hv_pviommu_init(void)
 {
 	u64 status;
 	int ret = 0;
Reviewed-by on v4 1/5
#2 lore raw

Re: [PATCH V4 1/5] mshv: Add gfp flags to deposit memory logic

In reply to Mukesh R
Quoted 6 lines · Mukesh R

Currently GFP_KERNEL is hard coded in hv_alloc_dep_pages(). Add gfp_flags parameter so it can be called in atomic context from map_pages callback in struct iommu_domain_ops and other places in future. A new function hv_deposit_memory_gfp() is added to facilitate this, so hv_deposit_memory() can continue to function as is.

Quoted 6 lines
---
 drivers/hv/hv_proc.c           | 20 +++++++++++---------
 include/asm-generic/mshyperv.h | 14 +++++++++++---
 2 files changed, 22 insertions(+), 12 deletions(-)

<snip>

Reviewed-by on v4 2/5
#5 lore raw

Re: [PATCH V4 2/5] PCI: hv: Export hv_build_devid_type_pci() and change return type

In reply to Mukesh R
Quoted 5 lines · Mukesh R

On Hyper-V, most hypercalls related to device domain management like attach device to a device domain, or to map/unmap regions, interrupts, etc need a device ID as a parameter. So, make hv_build_devid_type_pci() public and change return type to u64 to enforce its size.

Quoted 7 lines
---
 arch/x86/hyperv/irqdomain.c     | 9 +++++----
 arch/x86/include/asm/mshyperv.h | 6 ++++++
 2 files changed, 11 insertions(+), 4 deletions(-)
#13 lore raw

Re: [PATCH V4 5/5] x86/hyperv: Implement root partition IOMMU kernel only driver

In reply to Mukesh R
Quoted 21 lines · Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works on Microsoft Hyper-V hypervisor (aka MSHV) in privileged VMs aka root VMs. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, attaching of devices, mapping and unmapping of pages, etc. During boot, hypervisor automatically creates identity and blocked domains, so there is no need to do hypercalls to create them. This is a kernel only driver and only supported on baremetal root (and not L1VH root) without any guest passthru support. Support for guest device passthru will be added incrementally.

---
 arch/x86/kernel/pci-dma.c            |   2 +
 drivers/iommu/hyperv/Kconfig         |  13 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 613 +++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h       |   3 +
 include/linux/hyperv.h               |   6 +
 6 files changed, 638 insertions(+)
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c

<snip>

Quoted 68 lines

obj-$(CONFIG_HYPERV_PVIOMMU) += hv-iommu-guest.o

(quoted diff) +1 -0
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +60 -0
new file mode 100644
index 000000000000..e0e3e9d4385e
@@ -0,0 +1,613 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hyper-V root vIOMMU driver.
+ * Copyright (C) 2026, Microsoft, Inc.
+ */
+#include <linux/pci.h>
+#include <linux/dma-map-ops.h>
+#include <linux/interval_tree.h>
+#include <linux/hyperv.h>
+#include <asm/iommu.h>
+#include <asm/mshyperv.h>
+#include "../dma-iommu.h"
+
+static dma_addr_t hv_iommu_max_iova;
+static struct iommu_domain_ops hv_paging_domain_ops;
+
+/* IOMMU device that we export to the world. MSHV supports max of one */
+static struct iommu_device hv_virt_iommu;
+
+struct hv_iommu_domain {
+	struct iommu_domain iommu_dom;
+	u32 domid_num;			      /* as opposed to domain_id.type */
+	spinlock_t mappings_lock;	      /* protects mappings_tree */
+	struct rb_root_cached mappings_tree;  /* iova to pa lookup tree */
+};
+
+#define to_hv_domain(d) container_of(d, struct hv_iommu_domain, iommu_dom)
+
+struct hv_iommu_mapping {
+	phys_addr_t paddr;
+	struct interval_tree_node iova;
+	u32 flags;
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2) default
+ * domain. It has two types:
+ *   S2 default: access to entire root partition memory. This for the host
+ *		 maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
+ *		 is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
+ *		 (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
+ *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
+ */
+
+/*
+ * Create dummy domains to correspond to hypervisor prebuilt default identity
+ * and null domains (dummy because we do not make hypercalls to create them).
+ */
+static struct hv_iommu_domain hv_def_identity_dom;
+static struct hv_iommu_domain hv_def_blocked_dom;
+
+/*
+ * The hypervisor owns the IOMMU. As such it will choose the best page size
+ * and whenever possible use larger page sizes automatically. The map gpa
+ * hypercall to map the iova however currently uses only 4K pfns as input.
+ * So we specify 4K as the IOMMU PAGE_SIZE.
+ */
+#define HV_IOMMU_PGSIZES SZ_4K
+
+static DEFINE_IDA(hv_iommu_domids);   /* unique numeric ids for new domains */

drivers/iommu/hyperv/hv-iommu-root.c line 70

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+static bool hv_iommu_capable(struct device *dev, enum iommu_cap cap)
+{
+	switch (cap) {
+	case IOMMU_CAP_CACHE_COHERENCY:
+		return true;
+	default:
+		return false;
+	}
+}

Seems like this could match the guest driver and just be: return cap == IOMMU_CAP_CACHE_COHERENCY.

<snip>

Quoted 28 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +28 -0
+
+static struct iommu_domain_ops hv_paging_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+	.map_pages = hv_iommu_map_pages,
+	.unmap_pages = hv_iommu_unmap_pages,
+	.iova_to_phys = hv_iommu_iova_to_phys,
+	.free = hv_iommu_domain_free,
+};
+
+static struct iommu_ops hv_iommu_ops = {
+	.capable	    = hv_iommu_capable,
+	.domain_alloc_paging	= hv_iommu_domain_alloc_paging,
+	.probe_device	    = hv_iommu_probe_device,
+	.device_group	    = pci_device_group,
+	.get_resv_regions   = hv_iommu_get_resv_regions,
+	.owner		    = THIS_MODULE,
+	.identity_domain    = &hv_def_identity_dom.iommu_dom,
+	.blocked_domain     = &hv_def_blocked_dom.iommu_dom,
+	.release_domain     = &hv_def_blocked_dom.iommu_dom,
+};
+
+static const struct iommu_domain_ops hv_special_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+};
+
+static void __init hv_initialize_special_domains(void)
+{
+	hv_def_identity_dom.iommu_dom.type = IOMMU_DOMAIN_IDENTITY;

drivers/iommu/hyperv/hv-iommu-root.c line 108

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	hv_def_identity_dom.iommu_dom.ops = &hv_special_domain_ops;
+	hv_def_identity_dom.iommu_dom.owner = &hv_iommu_ops;
+	hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT; /* 0 */
+
+	hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
+	hv_def_blocked_dom.iommu_dom.ops = &hv_special_domain_ops;
+	hv_def_blocked_dom.iommu_dom.owner = &hv_iommu_ops;
+	hv_def_blocked_dom.domid_num = HV_DEVICE_DOMAIN_ID_NULL;    /* INTMAX */
+}
+

A net negative 9 lines to initialize these with .field notation, as suggested in v0: https://lore.kernel.org/all/[email protected]/

Wasn't super hard to do the 1 forward declaration needed...

drivers/iommu/hyperv/hv-iommu-root.c +30 -39
index e0e3e9d4385e2..4e6bf94413458 100644
@@ -12,6 +12,7 @@
 #include "../dma-iommu.h"
 
 static dma_addr_t hv_iommu_max_iova;
+static struct iommu_ops hv_iommu_ops;
 static struct iommu_domain_ops hv_paging_domain_ops;
 
 /* IOMMU device that we export to the world. MSHV supports max of one */
@@ -32,23 +33,6 @@ struct hv_iommu_mapping {
 	u32 flags;
 };
 
-/*
- * By default, during boot the hypervisor creates one Stage 2 (S2) default
- * domain. It has two types:
- *   S2 default: access to entire root partition memory. This for the host
- *		 maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
- *		 is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
- *		 (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
- *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
- */
-
-/*
- * Create dummy domains to correspond to hypervisor prebuilt default identity
- * and null domains (dummy because we do not make hypercalls to create them).
- */
-static struct hv_iommu_domain hv_def_identity_dom;
-static struct hv_iommu_domain hv_def_blocked_dom;
-
 /*
  * The hypervisor owns the IOMMU. As such it will choose the best page size
  * and whenever possible use larger page sizes automatically. The map gpa
@@ -488,6 +472,35 @@ static void hv_iommu_get_resv_regions(struct device *dev,
 	iommu_add_resv_x86_msi_region(head);
 }
 
+static const struct iommu_domain_ops hv_special_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2) default
+ * domain. It has two types:
+ *   S2 default: access to entire root partition memory. This for the host
+ *		 maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
+ *		 is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
+ *		 (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
+ *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
+ * Create dummy domains to correspond to hypervisor prebuilt default identity
+ * and null domains (dummy because we do not make hypercalls to create them).
+ */
+static struct hv_iommu_domain hv_def_identity_dom = {
+	.iommu_dom.type = IOMMU_DOMAIN_IDENTITY,
+	.iommu_dom.ops = &hv_special_domain_ops,
+	.iommu_dom.owner = &hv_iommu_ops,
+	.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT, /* 0 */
+};
+
+static struct hv_iommu_domain hv_def_blocked_dom = {
+	.iommu_dom.type = IOMMU_DOMAIN_BLOCKED,
+	.iommu_dom.ops = &hv_special_domain_ops,
+	.iommu_dom.owner = &hv_iommu_ops,
+	.domid_num = HV_DEVICE_DOMAIN_ID_NULL,    /* INTMAX */
+};
+
 static struct iommu_domain_ops hv_paging_domain_ops = {
 	.attach_dev = hv_iommu_attach_dev,
 	.map_pages = hv_iommu_map_pages,
@@ -508,23 +521,6 @@ static struct iommu_ops hv_iommu_ops = {
 	.release_domain     = &hv_def_blocked_dom.iommu_dom,
 };
 
-static const struct iommu_domain_ops hv_special_domain_ops = {
-	.attach_dev = hv_iommu_attach_dev,
-};
-
-static void __init hv_initialize_special_domains(void)
-{
-	hv_def_identity_dom.iommu_dom.type = IOMMU_DOMAIN_IDENTITY;
-	hv_def_identity_dom.iommu_dom.ops = &hv_special_domain_ops;
-	hv_def_identity_dom.iommu_dom.owner = &hv_iommu_ops;
-	hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT; /* 0 */
-
-	hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
-	hv_def_blocked_dom.iommu_dom.ops = &hv_special_domain_ops;
-	hv_def_blocked_dom.iommu_dom.owner = &hv_iommu_ops;
-	hv_def_blocked_dom.domid_num = HV_DEVICE_DOMAIN_ID_NULL;    /* INTMAX */
-}
-
 /*
  * In general, the hypervisor does not support the capabilities hypercall for
  * root partitions, an exception was made for max_iova_width. For IOMMU cap,
@@ -571,11 +567,6 @@ static int __init hv_root_iommu_init(void)
 
 	hv_iommu_max_iova = DMA_BIT_MASK(caps.max_iova_width);
 
-	/* This must come before iommu_device_register() because the latter
-	 * calls into the hooks.
-	 */
-	hv_initialize_special_domains();
-
 	rc = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu");
 	if (rc) {
 		pr_err("Hyper-V: iommu_device_sysfs_add failed: %d\n", rc);

<snip>

Thanks,
Easwar (he/him)

author
#14 lore raw

Re: [PATCH V4 5/5] x86/hyperv: Implement root partition IOMMU kernel only driver

Quoted 140 lines · Easwar Hariharan
Quoted 21 lines · Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works on Microsoft Hyper-V hypervisor (aka MSHV) in privileged VMs aka root VMs. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, attaching of devices, mapping and unmapping of pages, etc. During boot, hypervisor automatically creates identity and blocked domains, so there is no need to do hypercalls to create them. This is a kernel only driver and only supported on baremetal root (and not L1VH root) without any guest passthru support. Support for guest device passthru will be added incrementally.

---
  arch/x86/kernel/pci-dma.c            |   2 +
  drivers/iommu/hyperv/Kconfig         |  13 +
  drivers/iommu/hyperv/Makefile        |   1 +
  drivers/iommu/hyperv/hv-iommu-root.c | 613 +++++++++++++++++++++++++++
  include/asm-generic/mshyperv.h       |   3 +
  include/linux/hyperv.h               |   6 +
  6 files changed, 638 insertions(+)

create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c

<snip>

Quoted 68 lines
  obj-$(CONFIG_HYPERV_PVIOMMU) += hv-iommu-guest.o
(quoted diff) +1 -0
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +60 -0
new file mode 100644
index 000000000000..e0e3e9d4385e
@@ -0,0 +1,613 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hyper-V root vIOMMU driver.
+ * Copyright (C) 2026, Microsoft, Inc.
+ */
+#include <linux/pci.h>
+#include <linux/dma-map-ops.h>
+#include <linux/interval_tree.h>
+#include <linux/hyperv.h>
+#include <asm/iommu.h>
+#include <asm/mshyperv.h>
+#include "../dma-iommu.h"
+
+static dma_addr_t hv_iommu_max_iova;
+static struct iommu_domain_ops hv_paging_domain_ops;
+
+/* IOMMU device that we export to the world. MSHV supports max of one */
+static struct iommu_device hv_virt_iommu;
+
+struct hv_iommu_domain {
+	struct iommu_domain iommu_dom;
+	u32 domid_num;			      /* as opposed to domain_id.type */
+	spinlock_t mappings_lock;	      /* protects mappings_tree */
+	struct rb_root_cached mappings_tree;  /* iova to pa lookup tree */
+};
+
+#define to_hv_domain(d) container_of(d, struct hv_iommu_domain, iommu_dom)
+
+struct hv_iommu_mapping {
+	phys_addr_t paddr;
+	struct interval_tree_node iova;
+	u32 flags;
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2) default
+ * domain. It has two types:
+ *   S2 default: access to entire root partition memory. This for the host
+ *		 maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
+ *		 is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
+ *		 (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
+ *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
+ */
+
+/*
+ * Create dummy domains to correspond to hypervisor prebuilt default identity
+ * and null domains (dummy because we do not make hypercalls to create them).
+ */
+static struct hv_iommu_domain hv_def_identity_dom;
+static struct hv_iommu_domain hv_def_blocked_dom;
+
+/*
+ * The hypervisor owns the IOMMU. As such it will choose the best page size
+ * and whenever possible use larger page sizes automatically. The map gpa
+ * hypercall to map the iova however currently uses only 4K pfns as input.
+ * So we specify 4K as the IOMMU PAGE_SIZE.
+ */
+#define HV_IOMMU_PGSIZES SZ_4K
+
+static DEFINE_IDA(hv_iommu_domids);   /* unique numeric ids for new domains */

drivers/iommu/hyperv/hv-iommu-root.c line 70

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+static bool hv_iommu_capable(struct device *dev, enum iommu_cap cap)
+{
+	switch (cap) {
+	case IOMMU_CAP_CACHE_COHERENCY:
+		return true;
+	default:
+		return false;
+	}
+}

Seems like this could match the guest driver and just be: return cap == IOMMU_CAP_CACHE_COHERENCY.

<snip>

Quoted 32 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +32 -0
+
+static struct iommu_domain_ops hv_paging_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+	.map_pages = hv_iommu_map_pages,
+	.unmap_pages = hv_iommu_unmap_pages,
+	.iova_to_phys = hv_iommu_iova_to_phys,
+	.free = hv_iommu_domain_free,
+};
+
+static struct iommu_ops hv_iommu_ops = {
+	.capable	    = hv_iommu_capable,
+	.domain_alloc_paging	= hv_iommu_domain_alloc_paging,
+	.probe_device	    = hv_iommu_probe_device,
+	.device_group	    = pci_device_group,
+	.get_resv_regions   = hv_iommu_get_resv_regions,
+	.owner		    = THIS_MODULE,
+	.identity_domain    = &hv_def_identity_dom.iommu_dom,
+	.blocked_domain     = &hv_def_blocked_dom.iommu_dom,
+	.release_domain     = &hv_def_blocked_dom.iommu_dom,
+};
+
+static const struct iommu_domain_ops hv_special_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+};
+
+static void __init hv_initialize_special_domains(void)
+{
+	hv_def_identity_dom.iommu_dom.type = IOMMU_DOMAIN_IDENTITY;
+	hv_def_identity_dom.iommu_dom.ops = &hv_special_domain_ops;
+	hv_def_identity_dom.iommu_dom.owner = &hv_iommu_ops;
+	hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT; /* 0 */
+
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 108

Quoted 6 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+	hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
+	hv_def_blocked_dom.iommu_dom.ops = &hv_special_domain_ops;
+	hv_def_blocked_dom.iommu_dom.owner = &hv_iommu_ops;
+	hv_def_blocked_dom.domid_num = HV_DEVICE_DOMAIN_ID_NULL;    /* INTMAX */
+}
+

A net negative 9 lines to initialize these with .field notation, as suggested in v0: https://lore.kernel.org/all/[email protected]/

Huh! which are the 9 lines you are claiming to have removed? are you saying the definition part is not a "line"?

Quoted 1 line

Wasn't super hard to do the 1 forward declaration needed...

Not a question of hard or not. Having that upfront in the module provides good introduction to a reader, specially since hyperv is all new around here and internals are not readily available. Moreover, imo, generally just embedding definitions in code abrupts the flow often, and historically hasn't been a common practice.

Thanks,
-Mukesh

Quoted 114 lines
drivers/iommu/hyperv/hv-iommu-root.c +30 -39
index e0e3e9d4385e2..4e6bf94413458 100644
@@ -12,6 +12,7 @@
  #include "../dma-iommu.h"
 
  static dma_addr_t hv_iommu_max_iova;
+static struct iommu_ops hv_iommu_ops;
  static struct iommu_domain_ops hv_paging_domain_ops;
 
  /* IOMMU device that we export to the world. MSHV supports max of one */
@@ -32,23 +33,6 @@ struct hv_iommu_mapping {
  	u32 flags;
  };
 
-/*
- * By default, during boot the hypervisor creates one Stage 2 (S2) default
- * domain. It has two types:
- *   S2 default: access to entire root partition memory. This for the host
- *		 maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
- *		 is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
- *		 (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
- *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
- */
-
-/*
- * Create dummy domains to correspond to hypervisor prebuilt default identity
- * and null domains (dummy because we do not make hypercalls to create them).
- */
-static struct hv_iommu_domain hv_def_identity_dom;
-static struct hv_iommu_domain hv_def_blocked_dom;
-
  /*
   * The hypervisor owns the IOMMU. As such it will choose the best page size
   * and whenever possible use larger page sizes automatically. The map gpa
@@ -488,6 +472,35 @@ static void hv_iommu_get_resv_regions(struct device *dev,
  	iommu_add_resv_x86_msi_region(head);
  }
 
+static const struct iommu_domain_ops hv_special_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2) default
+ * domain. It has two types:
+ *   S2 default: access to entire root partition memory. This for the host
+ *		 maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
+ *		 is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
+ *		 (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
+ *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
+ * Create dummy domains to correspond to hypervisor prebuilt default identity
+ * and null domains (dummy because we do not make hypercalls to create them).
+ */
+static struct hv_iommu_domain hv_def_identity_dom = {
+	.iommu_dom.type = IOMMU_DOMAIN_IDENTITY,
+	.iommu_dom.ops = &hv_special_domain_ops,
+	.iommu_dom.owner = &hv_iommu_ops,
+	.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT, /* 0 */
+};
+
+static struct hv_iommu_domain hv_def_blocked_dom = {
+	.iommu_dom.type = IOMMU_DOMAIN_BLOCKED,
+	.iommu_dom.ops = &hv_special_domain_ops,
+	.iommu_dom.owner = &hv_iommu_ops,
+	.domid_num = HV_DEVICE_DOMAIN_ID_NULL,    /* INTMAX */
+};
+
  static struct iommu_domain_ops hv_paging_domain_ops = {
  	.attach_dev = hv_iommu_attach_dev,
  	.map_pages = hv_iommu_map_pages,
@@ -508,23 +521,6 @@ static struct iommu_ops hv_iommu_ops = {
  	.release_domain     = &hv_def_blocked_dom.iommu_dom,
  };
 
-static const struct iommu_domain_ops hv_special_domain_ops = {
-	.attach_dev = hv_iommu_attach_dev,
-};
-
-static void __init hv_initialize_special_domains(void)
-{
-	hv_def_identity_dom.iommu_dom.type = IOMMU_DOMAIN_IDENTITY;
-	hv_def_identity_dom.iommu_dom.ops = &hv_special_domain_ops;
-	hv_def_identity_dom.iommu_dom.owner = &hv_iommu_ops;
-	hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT; /* 0 */
-
-	hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
-	hv_def_blocked_dom.iommu_dom.ops = &hv_special_domain_ops;
-	hv_def_blocked_dom.iommu_dom.owner = &hv_iommu_ops;
-	hv_def_blocked_dom.domid_num = HV_DEVICE_DOMAIN_ID_NULL;    /* INTMAX */
-}
-
  /*
   * In general, the hypervisor does not support the capabilities hypercall for
   * root partitions, an exception was made for max_iova_width. For IOMMU cap,
@@ -571,11 +567,6 @@ static int __init hv_root_iommu_init(void)
 
  	hv_iommu_max_iova = DMA_BIT_MASK(caps.max_iova_width);
 
-	/* This must come before iommu_device_register() because the latter
-	 * calls into the hooks.
-	 */
-	hv_initialize_special_domains();
-
  	rc = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu");
  	if (rc) {
  		pr_err("Hyper-V: iommu_device_sysfs_add failed: %d\n", rc);
 
 
<snip>

Thanks,
Easwar (he/him)

#15 lore raw

Re: [PATCH V4 5/5] x86/hyperv: Implement root partition IOMMU kernel only driver

Quoted 150 lines · Mukesh R
Quoted 140 lines · Easwar Hariharan
Quoted 21 lines · Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works on Microsoft Hyper-V hypervisor (aka MSHV) in privileged VMs aka root VMs. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, attaching of devices, mapping and unmapping of pages, etc. During boot, hypervisor automatically creates identity and blocked domains, so there is no need to do hypercalls to create them. This is a kernel only driver and only supported on baremetal root (and not L1VH root) without any guest passthru support. Support for guest device passthru will be added incrementally.

---
  arch/x86/kernel/pci-dma.c            |   2 + drivers/iommu/hyperv/Kconfig         |  13 + drivers/iommu/hyperv/Makefile        |   1 + drivers/iommu/hyperv/hv-iommu-root.c | 613 +++++++++++++++++++++++++++ include/asm-generic/mshyperv.h       |   3 + include/linux/hyperv.h               |   6 +

  6 files changed, 638 insertions(+)

  create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c

<snip>

Quoted 68 lines

  obj-$(CONFIG_HYPERV_PVIOMMU) += hv-iommu-guest.o

(quoted diff) +1 -0
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +60 -0
new file mode 100644
index 000000000000..e0e3e9d4385e
@@ -0,0 +1,613 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hyper-V root vIOMMU driver.
+ * Copyright (C) 2026, Microsoft, Inc.
+ */
+#include <linux/pci.h>
+#include <linux/dma-map-ops.h>
+#include <linux/interval_tree.h>
+#include <linux/hyperv.h>
+#include <asm/iommu.h>
+#include <asm/mshyperv.h>
+#include "../dma-iommu.h"
+
+static dma_addr_t hv_iommu_max_iova;
+static struct iommu_domain_ops hv_paging_domain_ops;
+
+/* IOMMU device that we export to the world. MSHV supports max of one */
+static struct iommu_device hv_virt_iommu;
+
+struct hv_iommu_domain {
+    struct iommu_domain iommu_dom;
+    u32 domid_num;                  /* as opposed to domain_id.type */
+    spinlock_t mappings_lock;          /* protects mappings_tree */
+    struct rb_root_cached mappings_tree;  /* iova to pa lookup tree */
+};
+
+#define to_hv_domain(d) container_of(d, struct hv_iommu_domain, iommu_dom)
+
+struct hv_iommu_mapping {
+    phys_addr_t paddr;
+    struct interval_tree_node iova;
+    u32 flags;
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2) default
+ * domain. It has two types:
+ *   S2 default: access to entire root partition memory. This for the host
+ *         maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
+ *         is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
+ *         (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
+ *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
+ */
+
+/*
+ * Create dummy domains to correspond to hypervisor prebuilt default identity
+ * and null domains (dummy because we do not make hypercalls to create them).
+ */
+static struct hv_iommu_domain hv_def_identity_dom;
+static struct hv_iommu_domain hv_def_blocked_dom;
+
+/*
+ * The hypervisor owns the IOMMU. As such it will choose the best page size
+ * and whenever possible use larger page sizes automatically. The map gpa
+ * hypercall to map the iova however currently uses only 4K pfns as input.
+ * So we specify 4K as the IOMMU PAGE_SIZE.
+ */
+#define HV_IOMMU_PGSIZES SZ_4K
+
+static DEFINE_IDA(hv_iommu_domids);   /* unique numeric ids for new domains */

drivers/iommu/hyperv/hv-iommu-root.c line 70

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+static bool hv_iommu_capable(struct device *dev, enum iommu_cap cap)
+{
+    switch (cap) {
+    case IOMMU_CAP_CACHE_COHERENCY:
+        return true;
+    default:
+        return false;
+    }
+}

Seems like this could match the guest driver and just be: return cap == IOMMU_CAP_CACHE_COHERENCY.

<snip>

Quoted 32 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +32 -0
+
+static struct iommu_domain_ops hv_paging_domain_ops = {
+    .attach_dev = hv_iommu_attach_dev,
+    .map_pages = hv_iommu_map_pages,
+    .unmap_pages = hv_iommu_unmap_pages,
+    .iova_to_phys = hv_iommu_iova_to_phys,
+    .free = hv_iommu_domain_free,
+};
+
+static struct iommu_ops hv_iommu_ops = {
+    .capable        = hv_iommu_capable,
+    .domain_alloc_paging    = hv_iommu_domain_alloc_paging,
+    .probe_device        = hv_iommu_probe_device,
+    .device_group        = pci_device_group,
+    .get_resv_regions   = hv_iommu_get_resv_regions,
+    .owner            = THIS_MODULE,
+    .identity_domain    = &hv_def_identity_dom.iommu_dom,
+    .blocked_domain     = &hv_def_blocked_dom.iommu_dom,
+    .release_domain     = &hv_def_blocked_dom.iommu_dom,
+};
+
+static const struct iommu_domain_ops hv_special_domain_ops = {
+    .attach_dev = hv_iommu_attach_dev,
+};
+
+static void __init hv_initialize_special_domains(void)
+{
+    hv_def_identity_dom.iommu_dom.type = IOMMU_DOMAIN_IDENTITY;
+    hv_def_identity_dom.iommu_dom.ops = &hv_special_domain_ops;
+    hv_def_identity_dom.iommu_dom.owner = &hv_iommu_ops;
+    hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT; /* 0 */
+
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 108

Quoted 6 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+    hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
+    hv_def_blocked_dom.iommu_dom.ops = &hv_special_domain_ops;
+    hv_def_blocked_dom.iommu_dom.owner = &hv_iommu_ops;
+    hv_def_blocked_dom.domid_num = HV_DEVICE_DOMAIN_ID_NULL;    /* INTMAX */
+}
+

A net negative 9 lines to initialize these with .field notation, as suggested in v0: https://lore.kernel.org/all/[email protected]/

Quoted 2 lines

Huh! which are the 9 lines you are claiming to have removed? are you saying the definition part is not a "line"?

It was the diffstat of the diff I pasted below.

 drivers/iommu/hyperv/hv-iommu-root.c | 69 ++++++++++++++++++++++++++++++---------------------------------------
 1 file changed, 30 insertions(+), 39 deletions(-)
Quoted 4 lines
Quoted 2 lines

Wasn't super hard to do the 1 forward declaration needed...

Not a question of hard or not.

I was referencing your response to v0:

"it has dependencies on things declared above it, so not worth forward declarations etc.."

Having that upfront in the module provides

Quoted 4 lines

good introduction to a reader, specially since hyperv is all new around here and internals are not readily available. Moreover, imo, generally just embedding definitions in code abrupts the flow often, and historically hasn't been a common practice.

I'm having difficulty parsing this, but I *think* you're saying that putting data structure definitions in between functions hasn't been a common practice, and that may be true, but I'd just ask you to look at the intel/, amd/, and arm-smmu-v3/ drivers, it's a well-established pattern in this subsystem.

Thanks,
Easwar (he/him)

Quoted 110 lines
Quoted 109 lines
drivers/iommu/hyperv/hv-iommu-root.c +1 -0
index e0e3e9d4385e2..4e6bf94413458 100644
@@ -12,6 +12,7 @@
   #include "../dma-iommu.h"
     static dma_addr_t hv_iommu_max_iova;
+static struct iommu_ops hv_iommu_ops;
   static struct iommu_domain_ops hv_paging_domain_ops;
     /* IOMMU device that we export to the world. MSHV supports max of one */
@@ -32,23 +33,6 @@ struct hv_iommu_mapping {
       u32 flags;
   };

  -/*
- * By default, during boot the hypervisor creates one Stage 2 (S2) default
- * domain. It has two types:
- *   S2 default: access to entire root partition memory. This for the host
- *         maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
- *         is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
- *         (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
- *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
- */

drivers/iommu/hyperv/hv-iommu-root.c +0 -8
-
-/*
- * Create dummy domains to correspond to hypervisor prebuilt default identity
- * and null domains (dummy because we do not make hypercalls to create them).
- */
-static struct hv_iommu_domain hv_def_identity_dom;
-static struct hv_iommu_domain hv_def_blocked_dom;
-

  /*
   * The hypervisor owns the IOMMU. As such it will choose the best page size
   * and whenever possible use larger page sizes automatically. The map gpa

drivers/iommu/hyperv/hv-iommu-root.c +28 -16
@@ -488,6 +472,35 @@ static void hv_iommu_get_resv_regions(struct device *dev,
       iommu_add_resv_x86_msi_region(head);
   }
   +static const struct iommu_domain_ops hv_special_domain_ops = {
+    .attach_dev = hv_iommu_attach_dev,
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2) default
+ * domain. It has two types:
+ *   S2 default: access to entire root partition memory. This for the host
+ *         maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
+ *         is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
+ *         (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
+ *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
+ * Create dummy domains to correspond to hypervisor prebuilt default identity
+ * and null domains (dummy because we do not make hypercalls to create them).
+ */
+static struct hv_iommu_domain hv_def_identity_dom = {
+    .iommu_dom.type = IOMMU_DOMAIN_IDENTITY,
+    .iommu_dom.ops = &hv_special_domain_ops,
+    .iommu_dom.owner = &hv_iommu_ops,
+    .domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT, /* 0 */
+};
+
+static struct hv_iommu_domain hv_def_blocked_dom = {
+    .iommu_dom.type = IOMMU_DOMAIN_BLOCKED,
+    .iommu_dom.ops = &hv_special_domain_ops,
+    .iommu_dom.owner = &hv_iommu_ops,
+    .domid_num = HV_DEVICE_DOMAIN_ID_NULL,    /* INTMAX */
+};
+
   static struct iommu_domain_ops hv_paging_domain_ops = {
       .attach_dev = hv_iommu_attach_dev,
       .map_pages = hv_iommu_map_pages,
@@ -508,23 +521,6 @@ static struct iommu_ops hv_iommu_ops = {
       .release_domain     = &hv_def_blocked_dom.iommu_dom,
   };
   -static const struct iommu_domain_ops hv_special_domain_ops = {
-    .attach_dev = hv_iommu_attach_dev,
-};
-
-static void __init hv_initialize_special_domains(void)
-{
-    hv_def_identity_dom.iommu_dom.type = IOMMU_DOMAIN_IDENTITY;
-    hv_def_identity_dom.iommu_dom.ops = &hv_special_domain_ops;
-    hv_def_identity_dom.iommu_dom.owner = &hv_iommu_ops;
-    hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT; /* 0 */
-
-    hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
-    hv_def_blocked_dom.iommu_dom.ops = &hv_special_domain_ops;
-    hv_def_blocked_dom.iommu_dom.owner = &hv_iommu_ops;
-    hv_def_blocked_dom.domid_num = HV_DEVICE_DOMAIN_ID_NULL;    /* INTMAX */
-}
-
   /*
    * In general, the hypervisor does not support the capabilities hypercall for
    * root partitions, an exception was made for max_iova_width. For IOMMU cap,
@@ -571,11 +567,6 @@ static int __init hv_root_iommu_init(void)
         hv_iommu_max_iova = DMA_BIT_MASK(caps.max_iova_width);

  -    /* This must come before iommu_device_register() because the latter -     * calls into the hooks.
-     */
-    hv_initialize_special_domains();
-
      rc = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu"); if (rc) {
          pr_err("Hyper-V: iommu_device_sysfs_add failed: %d\n", rc);

<snip>

Thanks,
Easwar (he/him)

Reviewed-by on v4 1/5
#3 lore raw

Re: [PATCH V4 1/5] mshv: Add gfp flags to deposit memory logic

In reply to Mukesh R

Hi Mukesh,

Quoted 151 lines · On Wed, 4 Nov 2026 18:01:02 -0800 Mukesh R <[email protected]> wrote:

Currently GFP_KERNEL is hard coded in hv_alloc_dep_pages(). Add gfp_flags parameter so it can be called in atomic context from map_pages callback in struct iommu_domain_ops and other places in future. A new function hv_deposit_memory_gfp() is added to facilitate this, so hv_deposit_memory() can continue to function as is.

---
 drivers/hv/hv_proc.c           | 20 +++++++++++---------
 include/asm-generic/mshyperv.h | 14 +++++++++++---
 2 files changed, 22 insertions(+), 12 deletions(-)
drivers/hv/hv_proc.c +1 -1
index f811bb5c42a1..8380e9050f46 100644
@@ -22,7 +22,7 @@
  * Returns: number of pages allocated or -ENOMEM
  */
 static int hv_alloc_dep_pages(int node, u64 *pfna, u64 *lastpfnp,
 int num_pages,
-			      bool single)
+			      bool single, gfp_t gfp_flags)
 {
 	struct page *page;
drivers/hv/hv_proc.c +0 -1
 	int num_allocd, count = 0;
@@ -37,7 +37,6 @@ static int hv_alloc_dep_pages(int node, u64 *pfna,
 u64 *lastpfnp, int num_pages, while (num_pages) {
 		/* Find highest order we can actually allocate */
 		int order = 31 - __builtin_clz(num_pages);
-		gfp_t gfp_flags = GFP_KERNEL;
 
 		if (!single)
 			gfp_flags |= __GFP_NOWARN;
@@ -81,7 +80,8 @@ static int hv_alloc_dep_pages(int node, u64 *pfna,
 u64 *lastpfnp, int num_pages,
  * so we must split that also. Lastly,

HV_MAX_CONTIGUOUS_ALLOCATION_PAGES is
* not guaranteed to always be power of 2.
*/
-static int hv_call_deposit_pages(int node, u64 partition_id, bool contiguous) +static int hv_call_deposit_pages(int node, u64 partition_id, bool contiguous,

drivers/hv/hv_proc.c +4 -2
+				 gfp_t gfp_flags)
 {
 	struct hv_deposit_memory *hc_input;
 	int i, rc, num_pages;
@@ -98,14 +98,15 @@ static int hv_call_deposit_pages(int node, u64
 partition_id, bool contiguous) num_pages = HV_DEPOSIT_MAX;
 	}
 
-	hc_input = (struct hv_deposit_memory
 *)get_zeroed_page(GFP_KERNEL);
+	hc_input = (struct hv_deposit_memory
 *)get_zeroed_page(gfp_flags); if (hc_input == NULL)
 		return -ENOMEM;
 
 	hc_input->partition_id = partition_id;
 	pfna = hc_input->gpa_page_list;
 
-	rc = hv_alloc_dep_pages(node, pfna, &lastpfn, num_pages,
 contiguous);
+	rc = hv_alloc_dep_pages(node, pfna, &lastpfn, num_pages,
 contiguous,
+				gfp_flags);
drivers/hv/hv_proc.c +1 -1
 	if (rc < 0)
 		goto out_free;
@@ -159,7 +160,7 @@ static int hv_call_deposit_pages(int node, u64
 partition_id, bool contiguous) return rc;
 }
 
-int hv_deposit_memory_node(int node, u64 pt_id, u64 hv_status)
+int hv_deposit_memory_node(int node, u64 pt_id, u64 hv_status, gfp_t
 gfp_flags) {
 	int result = hv_result(hv_status);
 	bool contiguous = false;
@@ -179,7 +180,7 @@ int hv_deposit_memory_node(int node, u64 pt_id,

u64 hv_status) result == HV_STATUS_INSUFFICIENT_CONTIGUOUS_ROOT_MEMORY) contiguous = true;

drivers/hv/hv_proc.c +1 -1
-	return hv_call_deposit_pages(node, pt_id, contiguous);
+	return hv_call_deposit_pages(node, pt_id, contiguous,

gfp_flags); }
EXPORT_SYMBOL_GPL(hv_deposit_memory_node);

drivers/hv/hv_proc.c +1 -1
@@ -232,7 +233,7 @@ int hv_call_add_logical_proc(int node, u32
 lp_index, u32 apic_id) break;
 		}
 		ret = hv_deposit_memory_node(node,
 hv_current_partition_id,
-					     status);
+					     status, GFP_KERNEL);
 	} while (!ret);
 
drivers/hv/hv_proc.c +2 -1
 	return ret;
@@ -266,7 +267,8 @@ int hv_call_create_vp(int node, u64 partition_id,
 u32 vp_index, u32 flags) }
 			break;
 		}
-		ret = hv_deposit_memory_node(node, partition_id,
 status);
+		ret = hv_deposit_memory_node(node, partition_id,
 status,
+					     GFP_KERNEL);
 
drivers/hv/hv_proc.c +0 -0
 	} while (!ret);
(quoted diff) +0 -0

b/include/asm-generic/mshyperv.h index 4c9f3353074a..2584e693d7b3 100644 --- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h

include/asm-generic/mshyperv.h +1 -1
@@ -346,7 +346,7 @@ static inline bool hv_parent_partition(void)
 }
 
 bool hv_result_needs_memory(u64 status);
-int hv_deposit_memory_node(int node, u64 partition_id, u64 status);
+int hv_deposit_memory_node(int node, u64 partition_id, u64 status,

gfp_t gfp); int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); int hv_call_notify_all_processors_started(void);
bool hv_lp_exists(u32 lp_index);

(quoted diff) +0 -0
@@ -357,7 +357,8 @@ static inline bool hv_root_partition(void) {

return false; } static inline bool hv_l1vh_partition(void) { return false; } static inline bool hv_parent_partition(void) { return false; } static inline bool hv_result_needs_memory(u64 status) { return false; } -static inline int hv_deposit_memory_node(int node, u64 partition_id, u64 status) +static inline int hv_deposit_memory_node(int node, u64 partition_id, u64 status,

include/asm-generic/mshyperv.h +8 -1
+					 gfp_t gfp)
 {
 	return -EOPNOTSUPP;
 }
@@ -379,9 +380,16 @@ static inline int hv_call_create_vp(int node,
 u64 partition_id, u32 vp_index, u3 }
 #endif /* CONFIG_MSHV_ROOT */
 
+/* Generic deposit function with no default numa node and GFP_KERNEL
 */ static inline int hv_deposit_memory(u64 partition_id, u64 status)
 {
-	return hv_deposit_memory_node(NUMA_NO_NODE, partition_id,
 status);
+	return hv_deposit_memory_node(NUMA_NO_NODE, partition_id,
 status,
+				      GFP_KERNEL);
+}
+
+static inline int hv_deposit_memory_gfp(u64 partition_id, u64
 status, gfp_t gfp) +{
+	return hv_deposit_memory_node(NUMA_NO_NODE, partition_id,

status, gfp); }

Quoted 1 line

#if IS_ENABLED(CONFIG_HYPERV_VTL_MODE)

nit: no user for hv_deposit_memory_gfp here.
IMHO, hv_deposit_memory_gfp() should be introduced in patch 5/5, where hv_iommu_map_pages()  first calls it:
ret = hv_deposit_memory_gfp(hv_current_partition_id, status, gfp);

Other than that,

author
#16 lore raw

Re: [PATCH V4 5/5] x86/hyperv: Implement root partition IOMMU kernel only driver

In reply to Mukesh R
Quoted 10 lines · Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works on Microsoft Hyper-V hypervisor (aka MSHV) in privileged VMs aka root VMs. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, attaching of devices, mapping and unmapping of pages, etc. During boot, hypervisor automatically creates identity and blocked domains, so there is no need to do hypercalls to create them. This is a kernel only driver and only supported on baremetal root (and not L1VH root) without any guest passthru support. Support for guest device passthru will be added incrementally.

Quoted 2 lines

---

Hi Joerg and Jason,

I've addressed all your comments and suggestions. Wondering if there's anything else to get your approval/ack. Please let me know.

Thanks,
-Mukesh

arch/x86/kernel/pci-dma.c            |   2 +
>   drivers/iommu/hyperv/Kconfig         |  13 +
>   drivers/iommu/hyperv/Makefile        |   1 +
>   drivers/iommu/hyperv/hv-iommu-root.c | 613 +++++++++++++++++++++++++++
>   include/asm-generic/mshyperv.h       |   3 +
>   include/linux/hyperv.h               |   6 +
Quoted 701 lines
  6 files changed, 638 insertions(+)

create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c

arch/x86/kernel/pci-dma.c +2 -0
index 75cf8f6ae8cd..560d8a4f6e1d 100644
@@ -8,6 +8,7 @@
  #include <linux/gfp.h>
  #include <linux/pci.h>
  #include <linux/amd-iommu.h>
+#include <linux/hyperv.h>
 
  #include <asm/proto.h>
  #include <asm/dma.h>
@@ -103,6 +104,7 @@ void __init pci_iommu_alloc(void)
  	gart_iommu_hole_init();
  	amd_iommu_detect();
  	detect_intel_iommu();
+	hv_root_iommu_detect();
  	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
  }
 
drivers/iommu/hyperv/Kconfig +13 -0
index f2f993f647c3..6ba5125ed501 100644
@@ -13,3 +13,16 @@ config HYPERV_PVIOMMU
  	  Microsoft Hyper-V. Provides DMA remapping and IOTLB
  	  flush support to enable DMA isolation for PCI devices
  	  assigned to the guest.
+
+config HYPERV_ROOT_IOMMU
+	bool "Hyper-V IOMMU Device in root partition"
+	depends on HYPERV && X86 && PCI_MSI && MSHV_ROOT
+	select IOMMU_API
+	default HYPERV
+	help
+	  This enables Hyper-V pseudo IOMMU device. When running as privileged
+	  VM aka root on Microsoft Hyper-V hypervisor, this must be enabled
+	  for doing any PCI passthru of devices to guest VMs. This applies to
+	  both PFs and VFs. When enabling this, it is best to disable amd/intel
+	  iommus via: intel_iommu=off amd_iommu=off as the hypervisor really
+	  owns the iommu.
drivers/iommu/hyperv/Makefile +1 -0
index d3b2a259439c..04c66b4409a3 100644
@@ -1,3 +1,4 @@
  # SPDX-License-Identifier: GPL-2.0
  obj-$(CONFIG_IRQ_REMAP) += hv-irq-remap-x86.o
  obj-$(CONFIG_HYPERV_PVIOMMU) += hv-iommu-guest.o
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +613 -0
new file mode 100644
index 000000000000..e0e3e9d4385e
@@ -0,0 +1,613 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hyper-V root vIOMMU driver.
+ * Copyright (C) 2026, Microsoft, Inc.
+ */
+#include <linux/pci.h>
+#include <linux/dma-map-ops.h>
+#include <linux/interval_tree.h>
+#include <linux/hyperv.h>
+#include <asm/iommu.h>
+#include <asm/mshyperv.h>
+#include "../dma-iommu.h"
+
+static dma_addr_t hv_iommu_max_iova;
+static struct iommu_domain_ops hv_paging_domain_ops;
+
+/* IOMMU device that we export to the world. MSHV supports max of one */
+static struct iommu_device hv_virt_iommu;
+
+struct hv_iommu_domain {
+	struct iommu_domain iommu_dom;
+	u32 domid_num;			      /* as opposed to domain_id.type */
+	spinlock_t mappings_lock;	      /* protects mappings_tree */
+	struct rb_root_cached mappings_tree;  /* iova to pa lookup tree */
+};
+
+#define to_hv_domain(d) container_of(d, struct hv_iommu_domain, iommu_dom)
+
+struct hv_iommu_mapping {
+	phys_addr_t paddr;
+	struct interval_tree_node iova;
+	u32 flags;
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2) default
+ * domain. It has two types:
+ *   S2 default: access to entire root partition memory. This for the host
+ *		 maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
+ *		 is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
+ *		 (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
+ *   S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
+ */
+
+/*
+ * Create dummy domains to correspond to hypervisor prebuilt default identity
+ * and null domains (dummy because we do not make hypercalls to create them).
+ */
+static struct hv_iommu_domain hv_def_identity_dom;
+static struct hv_iommu_domain hv_def_blocked_dom;
+
+/*
+ * The hypervisor owns the IOMMU. As such it will choose the best page size
+ * and whenever possible use larger page sizes automatically. The map gpa
+ * hypercall to map the iova however currently uses only 4K pfns as input.
+ * So we specify 4K as the IOMMU PAGE_SIZE.
+ */
+#define HV_IOMMU_PGSIZES SZ_4K
+
+static DEFINE_IDA(hv_iommu_domids);   /* unique numeric ids for new domains */
+
+static bool hv_iommu_capable(struct device *dev, enum iommu_cap cap)
+{
+	switch (cap) {
+	case IOMMU_CAP_CACHE_COHERENCY:
+		return true;
+	default:
+		return false;
+	}
+}
+
+static int hv_iommu_add_tree_mapping(struct hv_iommu_domain *hvdom, ulong iova,
+				     phys_addr_t paddr, size_t size, u32 flags,
+				     gfp_t gfp)
+{
+	ulong irqflags;
+	struct hv_iommu_mapping *mapping;
+
+	mapping = kzalloc_obj(struct hv_iommu_mapping, gfp);
+	if (!mapping)
+		return -ENOMEM;
+
+	mapping->paddr = paddr;
+	mapping->iova.start = iova;
+	mapping->iova.last = iova + size - 1;
+	mapping->flags = flags;
+
+	spin_lock_irqsave(&hvdom->mappings_lock, irqflags);
+	interval_tree_insert(&mapping->iova, &hvdom->mappings_tree);
+	spin_unlock_irqrestore(&hvdom->mappings_lock, irqflags);
+
+	return 0;
+}
+
+/* If both size and iova are 0, then last = ULONG_MAX will remove everything */
+static size_t hv_iommu_del_tree_mappings(struct hv_iommu_domain *hvdom,
+					 ulong iova, size_t size)
+{
+	ulong flags;
+	size_t unmapped = 0;
+	ulong last = iova + size - 1;
+	struct hv_iommu_mapping *mapping = NULL;
+	struct interval_tree_node *node, *next;
+
+	spin_lock_irqsave(&hvdom->mappings_lock, flags);
+	next = interval_tree_iter_first(&hvdom->mappings_tree, iova, last);
+	while (next) {
+		node = next;
+		mapping = container_of(node, struct hv_iommu_mapping, iova);
+		next = interval_tree_iter_next(node, iova, last);
+
+		/* Splitting of a mapping is not supported at present */
+		if (mapping->iova.start < iova)
+			break;
+
+		unmapped += mapping->iova.last - mapping->iova.start + 1;
+
+		interval_tree_remove(node, &hvdom->mappings_tree);
+		kfree(mapping);
+	}
+	spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
+
+	return unmapped;
+}
+
+/* Create a new device domain in the hypervisor */
+static int hv_iommu_create_hyp_devdom(struct hv_iommu_domain *hvdom)
+{
+	u64 status;
+	struct hv_input_device_domain *ddp;
+	struct hv_input_create_device_domain *input;
+	ulong flags;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	ddp = &input->device_domain;
+	ddp->partition_id = HV_PARTITION_ID_SELF;
+	ddp->domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+	ddp->domain_id.id = hvdom->domid_num;
+
+	input->create_device_domain_flags.forward_progress_required = 1;
+	input->create_device_domain_flags.inherit_owning_vtl = 0;
+
+	status = hv_do_hypercall(HVCALL_CREATE_DEVICE_DOMAIN, input, NULL);
+
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "\n");
+
+	return hv_result_to_errno(status);
+}
+
+static struct iommu_domain *hv_iommu_domain_alloc_paging(struct device *dev)
+{
+	struct hv_iommu_domain *hvdom;
+	int rc;
+
+	if (!dev_is_pci(dev))
+		return NULL;
+
+	hvdom = kzalloc_obj(struct hv_iommu_domain);
+	if (hvdom == NULL)
+		return ERR_PTR(-ENOMEM);
+
+	spin_lock_init(&hvdom->mappings_lock);
+	hvdom->mappings_tree = RB_ROOT_CACHED;
+
+	rc = ida_alloc_range(&hv_iommu_domids,
+			     HV_DEVICE_DOMAIN_ID_DEFAULT + 1,
+			     HV_DEVICE_DOMAIN_ID_NULL - 1, GFP_KERNEL);
+	if (rc < 0)
+		goto out_err;
+
+	hvdom->domid_num = rc;
+	hvdom->iommu_dom.pgsize_bitmap = HV_IOMMU_PGSIZES;
+	hvdom->iommu_dom.geometry.aperture_start = 0;
+	hvdom->iommu_dom.geometry.aperture_end = hv_iommu_max_iova;
+	hvdom->iommu_dom.geometry.force_aperture = true;
+	hvdom->iommu_dom.ops = &hv_paging_domain_ops;
+
+	rc = hv_iommu_create_hyp_devdom(hvdom);
+	if (rc)
+		goto out_err1;
+
+	return &hvdom->iommu_dom;
+
+out_err1:
+	ida_free(&hv_iommu_domids, hvdom->domid_num);
+out_err:
+	kfree(hvdom);
+	return ERR_PTR(rc);
+}
+
+static void hv_iommu_domain_free(struct iommu_domain *immdom)
+{
+	ulong flags;
+	u64 status;
+	struct hv_input_delete_device_domain *input;
+	struct hv_input_device_domain *ddp;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+
+	/* Cleanup any remaining. 0 for size results in ULONG_MAX as the last */
+	hv_iommu_del_tree_mappings(hvdom, 0, 0);
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	ddp = &input->device_domain;
+	memset(input, 0, sizeof(*input));
+
+	ddp->partition_id = HV_PARTITION_ID_SELF;
+	ddp->domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+	ddp->domain_id.id = hvdom->domid_num;
+
+	status = hv_do_hypercall(HVCALL_DELETE_DEVICE_DOMAIN, input,
+				 NULL);
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "\n");
+
+	ida_free(&hv_iommu_domids, hvdom->domid_num);
+	kfree(hvdom);
+}
+
+/*
+ * Attach a device to the default domain, or the null domain, or to a domain
+ * previously created in the hypervisor.
+ */
+static int hv_iommu_att_dev2dom(struct hv_iommu_domain *hvdom,
+				struct pci_dev *pdev)
+{
+	ulong flags;
+	u64 status;
+	struct hv_input_attach_device_domain *input;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	/* For null domain, hvdom->domid_num == HV_DEVICE_DOMAIN_ID_NULL */
+	input->device_domain.partition_id = HV_PARTITION_ID_SELF;
+	input->device_domain.domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+	input->device_domain.domain_id.id = hvdom->domid_num;
+
+	input->device_id.as_uint64 = hv_build_devid_type_pci(pdev);
+
+	status = hv_do_hypercall(HVCALL_ATTACH_DEVICE_DOMAIN, input, NULL);
+	local_irq_restore(flags);
+
+	if (hv_result_success(status) ||
+	    hv_result(status) == HV_STATUS_DEVICE_ALREADY_IN_DOMAIN)
+		return 0;
+
+	hv_status_err(status, "\n");
+	return hv_result_to_errno(status);
+}
+
+static int hv_iommu_attach_dev(struct iommu_domain *immdom, struct device *dev,
+			       struct iommu_domain *old)
+{
+	struct pci_dev *pdev;
+	int rc;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+
+	pdev = to_pci_dev(dev);
+
+	rc = hv_iommu_att_dev2dom(hvdom, pdev);
+	if (rc)
+		WARN(1, "Failed to attach pdev:%s\n", pci_name(pdev));
+
+	return rc;
+}
+
+static u64 hv_iommu_unmap_batch(u32 domid_num, ulong iova, u16 count)
+{
+	ulong flags;
+	struct hv_input_unmap_device_gpa_pages *input;
+	u64 status;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	input->device_domain.partition_id = HV_PARTITION_ID_SELF;
+	input->device_domain.domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+	input->device_domain.domain_id.id = domid_num;
+	input->target_device_va_base = iova;
+
+	status = hv_do_rep_hypercall(HVCALL_UNMAP_DEVICE_GPA_PAGES, count,
+				     0, input, NULL);
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "iova:0x%lx count:0x%x\n", iova, count);
+
+	return status;
+}
+
+static size_t hv_iommu_hyp_unmap_pages(struct iommu_domain *immdom, ulong iova,
+				       size_t size)
+{
+	u64 status;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+	size_t tot_done = 0;
+	ulong npages = size >> HV_HYP_PAGE_SHIFT;
+
+	while (npages) {
+		int done, count = min(npages, HV_REP_COUNT_MAX);
+
+		status = hv_iommu_unmap_batch(hvdom->domid_num, iova, count);
+
+		done = hv_repcomp(status);
+		tot_done += done;
+		npages -= done;
+		iova += done << HV_HYP_PAGE_SHIFT;
+
+		if (!hv_result_success(status))
+			break;
+	}
+
+	return tot_done << HV_HYP_PAGE_SHIFT;
+}
+
+static size_t hv_iommu_unmap_pages(struct iommu_domain *immdom, ulong iova,
+				   size_t pgsize, size_t pgcount,
+				   struct iommu_iotlb_gather *gather)
+{
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+	size_t unmap_sz, size = pgsize * pgcount;
+
+	/* For valid input, the hypervisor guarantees unmap will succeed */
+	unmap_sz = hv_iommu_hyp_unmap_pages(immdom, iova, size);
+	if (unmap_sz != size)
+		WARN(1, "Failed to unmap device gpa(%lx/%lx)\n", unmap_sz,
+		     size);
+
+	if (unmap_sz == 0)
+		return 0;
+
+	size = hv_iommu_del_tree_mappings(hvdom, iova, unmap_sz);
+	if (size != unmap_sz)
+		pr_err("%s: could not delete tree mappings (%lx:%lx/%lx)\n",
+		       __func__, iova, unmap_sz, size);
+
+	return size;
+}
+
+/* Return: must return exact status from the hypercall without changes */
+static u64 hv_iommu_map_pgs(struct hv_iommu_domain *hvdom, ulong iova,
+			    phys_addr_t paddr, ulong npages, u32 map_flags)
+{
+	u64 status;
+	int i;
+	struct hv_input_map_device_gpa_pages *input;
+	unsigned long flags, pfn;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	input->device_domain.partition_id = HV_PARTITION_ID_SELF;
+	input->device_domain.domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+	input->device_domain.domain_id.id = hvdom->domid_num;
+	input->map_flags = map_flags;
+	input->target_device_va_base = iova;
+
+	pfn = paddr >> HV_HYP_PAGE_SHIFT;
+	for (i = 0; i < npages; i++, pfn++)
+		input->gpa_page_list[i] = pfn;
+
+	status = hv_do_rep_hypercall(HVCALL_MAP_DEVICE_GPA_PAGES, npages, 0,
+				     input, NULL);
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status) && !hv_result_needs_memory(status))
+		hv_status_err(status, "npgs:%lx paddr:%llx iova:%lx mf:0x%x\n",
+			      npages, paddr, iova, map_flags);
+	return status;
+}
+
+#define HV_MAP_DEVICE_GPA_BATCH_SIZE   \
+	((HV_HYP_PAGE_SIZE - sizeof(struct hv_input_map_device_gpa_pages)) \
+			/ sizeof(u64))
+
+/* NB: cond_resched() is in vfio_iommu_map() */
+static int hv_iommu_map_pages(struct iommu_domain *immdom, ulong iova,
+			      phys_addr_t paddr, size_t pgsize, size_t pgcount,
+			      int prot, gfp_t gfp, size_t *mapped)
+{
+	u32 map_flags;
+	int ret;
+	u64 status = 0;
+	ulong npages, sav_iova = iova, done = 0;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+	size_t size = pgsize * pgcount;
+
+	map_flags = HV_MAP_GPA_READABLE;	/* required */
+	map_flags |= prot & IOMMU_WRITE ? HV_MAP_GPA_WRITABLE : 0;
+
+	ret = hv_iommu_add_tree_mapping(hvdom, iova, paddr, size, map_flags,
+					gfp);
+	if (ret)
+		return ret;
+
+	npages = size >> HV_HYP_PAGE_SHIFT;
+	while (done < npages) {
+		ulong completed, remain = npages - done;
+
+		remain = min(remain, HV_MAP_DEVICE_GPA_BATCH_SIZE);
+
+		status = hv_iommu_map_pgs(hvdom, iova, paddr, remain,
+					  map_flags);
+
+		completed = hv_repcomp(status);
+		done = done + completed;
+		iova = iova + (completed << HV_HYP_PAGE_SHIFT);
+		paddr = paddr + (completed << HV_HYP_PAGE_SHIFT);
+
+		if (hv_result_needs_memory(status)) {
+			ret = hv_deposit_memory_gfp(hv_current_partition_id,
+						    status, gfp);
+			if (ret)
+				break;
+			continue;
+		}
+		if (!hv_result_success(status))
+			break;
+	}
+
+	if (ret == 0)
+		ret = hv_result_to_errno(status);
+
+	if (hv_result_success(status)) {
+		if (mapped)
+			*mapped = size;
+	} else {
+		size_t done_size = done << HV_HYP_PAGE_SHIFT;
+
+		hv_status_err(status, "pgs:%lx/%lx iova:%lx\n", done, npages,
+			      sav_iova);
+
+		hv_iommu_del_tree_mappings(hvdom, sav_iova, size);
+
+		/* Hypervisor guarantees unmap will succeed here */
+		hv_iommu_hyp_unmap_pages(immdom, sav_iova, done_size);
+
+		if (mapped)
+			*mapped = 0;
+	}
+
+	return ret;
+}
+
+static phys_addr_t hv_iommu_iova_to_phys(struct iommu_domain *immdom,
+					 dma_addr_t iova)
+{
+	unsigned long flags;
+	struct hv_iommu_mapping *mapping;
+	struct interval_tree_node *node;
+	u64 paddr = 0;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+
+	spin_lock_irqsave(&hvdom->mappings_lock, flags);
+	node = interval_tree_iter_first(&hvdom->mappings_tree, iova, iova);
+	if (node) {
+		mapping = container_of(node, struct hv_iommu_mapping, iova);
+		paddr = mapping->paddr + (iova - mapping->iova.start);
+	}
+	spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
+
+	return paddr;
+}
+
+static struct iommu_device *hv_iommu_probe_device(struct device *dev)
+{
+	if (!dev_is_pci(dev))
+		return ERR_PTR(-ENODEV);
+
+	return &hv_virt_iommu;
+}
+
+static void hv_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *head)
+{
+	iommu_add_resv_x86_msi_region(head);
+}
+
+static struct iommu_domain_ops hv_paging_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+	.map_pages = hv_iommu_map_pages,
+	.unmap_pages = hv_iommu_unmap_pages,
+	.iova_to_phys = hv_iommu_iova_to_phys,
+	.free = hv_iommu_domain_free,
+};
+
+static struct iommu_ops hv_iommu_ops = {
+	.capable	    = hv_iommu_capable,
+	.domain_alloc_paging	= hv_iommu_domain_alloc_paging,
+	.probe_device	    = hv_iommu_probe_device,
+	.device_group	    = pci_device_group,
+	.get_resv_regions   = hv_iommu_get_resv_regions,
+	.owner		    = THIS_MODULE,
+	.identity_domain    = &hv_def_identity_dom.iommu_dom,
+	.blocked_domain     = &hv_def_blocked_dom.iommu_dom,
+	.release_domain     = &hv_def_blocked_dom.iommu_dom,
+};
+
+static const struct iommu_domain_ops hv_special_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+};
+
+static void __init hv_initialize_special_domains(void)
+{
+	hv_def_identity_dom.iommu_dom.type = IOMMU_DOMAIN_IDENTITY;
+	hv_def_identity_dom.iommu_dom.ops = &hv_special_domain_ops;
+	hv_def_identity_dom.iommu_dom.owner = &hv_iommu_ops;
+	hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT; /* 0 */
+
+	hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
+	hv_def_blocked_dom.iommu_dom.ops = &hv_special_domain_ops;
+	hv_def_blocked_dom.iommu_dom.owner = &hv_iommu_ops;
+	hv_def_blocked_dom.domid_num = HV_DEVICE_DOMAIN_ID_NULL;    /* INTMAX */
+}
+
+/*
+ * In general, the hypervisor does not support the capabilities hypercall for
+ * root partitions, an exception was made for max_iova_width. For IOMMU cap,
+ * hv_root_iommu_detect() below checks for HV_DEVICE_DOMAIN_AVAILABLE which
+ * supersedes HV_IOMMU_CAP_PRESENT. As for the pgsize_bitmap cap, see comment
+ * above HV_IOMMU_PGSIZES.
+ */
+static int hv_iommu_get_caps(struct hv_output_get_iommu_capabilities *caps)
+{
+	u64 status;
+	unsigned long flags;
+	struct hv_input_get_iommu_capabilities *input;
+	struct hv_output_get_iommu_capabilities *output;
+
+	local_irq_save(flags);
+
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	output = *this_cpu_ptr(hyperv_pcpu_output_arg);
+	memset(input, 0, sizeof(*input));
+	input->partition_id = HV_PARTITION_ID_SELF;
+	status = hv_do_hypercall(HVCALL_GET_IOMMU_CAPABILITIES, input, output);
+	*caps = *output;
+
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "\n");
+
+	return hv_result_to_errno(status);
+}
+
+static int __init hv_root_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+
+	hv_iommu_max_iova = DMA_BIT_MASK(caps.max_iova_width);
+
+	/* This must come before iommu_device_register() because the latter
+	 * calls into the hooks.
+	 */
+	hv_initialize_special_domains();
+
+	rc = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu");
+	if (rc) {
+		pr_err("Hyper-V: iommu_device_sysfs_add failed: %d\n", rc);
+		return rc;
+	}
+
+	rc = iommu_device_register(iommup, &hv_iommu_ops, NULL);
+	if (rc) {
+		pr_err("Hyper-V: iommu_device_register failed: %d\n", rc);
+		goto err_sysfs_remove;
+	}
+
+	pr_info("Hyper-V IOMMU initialized\n");
+
+	return 0;
+
+err_sysfs_remove:
+	iommu_device_sysfs_remove(iommup);
+	return rc;
+}
+
+void __init hv_root_iommu_detect(void)
+{
+	if (no_iommu || iommu_detected || hv_l1vh_partition())
+		return;
+
+	/* This is set for root partitions only, it is never set for non-root */
+	if (!(ms_hyperv.misc_features & HV_DEVICE_DOMAIN_AVAILABLE))
+		return;
+
+	iommu_detected = 1;
+	x86_init.iommu.iommu_init = hv_root_iommu_init;
+
+	pci_request_acs();
+}
include/asm-generic/mshyperv.h +3 -0
index 2584e693d7b3..ad02d6956ae3 100644
@@ -28,6 +28,9 @@
 
  #define VTPM_BASE_ADDRESS 0xfed40000
 
+#define HV_REP_COUNT_MAX        \
+	(HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)
+
  enum hv_partition_type {
  	HV_PARTITION_TYPE_GUEST,
  	HV_PARTITION_TYPE_ROOT,
include/linux/hyperv.h +6 -0
index e85a1a043eac..8aa752e8c4e1 100644
@@ -1823,4 +1823,10 @@ static inline unsigned long virt_to_hvpfn(void *addr)
  #define HVPFN_DOWN(x)	((x) >> HV_HYP_PAGE_SHIFT)
  #define page_to_hvpfn(page)	(page_to_pfn(page) * NR_HV_HYP_PAGES_IN_PAGE)
 
+#ifdef CONFIG_HYPERV_ROOT_IOMMU
+void __init hv_root_iommu_detect(void);
+#else
+static inline void hv_root_iommu_detect(void) { }
+#endif /* CONFIG_HYPERV_ROOT_IOMMU */
+
  #endif /* _HYPERV_H */
Reviewed-by on v4 5/5
#17 lore raw

Re: [PATCH V4 5/5] x86/hyperv: Implement root partition IOMMU kernel only driver

In reply to Mukesh R

Hi Mukesh,

Quoted 47 lines · On Wed, 4 Nov 2026 18:01:06 -0800 Mukesh R <[email protected]> wrote:

Add a new file to implement a kernel only virtual IOMMU that works on Microsoft Hyper-V hypervisor (aka MSHV) in privileged VMs aka root VMs. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, attaching of devices, mapping and unmapping of pages, etc. During boot, hypervisor automatically creates identity and blocked domains, so there is no need to do hypercalls to create them. This is a kernel only driver and only supported on baremetal root (and not L1VH root) without any guest passthru support. Support for guest device passthru will be added incrementally.

---
 arch/x86/kernel/pci-dma.c            |   2 +
 drivers/iommu/hyperv/Kconfig         |  13 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 613
+++++++++++++++++++++++++++ include/asm-generic/mshyperv.h       |
3 + include/linux/hyperv.h               |   6 +
 6 files changed, 638 insertions(+)
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
arch/x86/kernel/pci-dma.c +2 -0
index 75cf8f6ae8cd..560d8a4f6e1d 100644
@@ -8,6 +8,7 @@
 #include <linux/gfp.h>
 #include <linux/pci.h>
 #include <linux/amd-iommu.h>
+#include <linux/hyperv.h>
 
 #include <asm/proto.h>
 #include <asm/dma.h>
@@ -103,6 +104,7 @@ void __init pci_iommu_alloc(void)
 	gart_iommu_hole_init();
 	amd_iommu_detect();
 	detect_intel_iommu();
+	hv_root_iommu_detect();
 	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
 }
 
(quoted diff) +0 -0

b/drivers/iommu/hyperv/Kconfig index f2f993f647c3..6ba5125ed501 100644

drivers/iommu/hyperv/Kconfig config HYPERV_PVIOMMU · line 19

Quoted 7 lines
drivers/iommu/hyperv/Kconfig +4 -0
@@ -13,3 +13,16 @@ config HYPERV_PVIOMMU
 	  Microsoft Hyper-V. Provides DMA remapping and IOTLB
 	  flush support to enable DMA isolation for PCI devices
 	  assigned to the guest.
+
+config HYPERV_ROOT_IOMMU
+	bool "Hyper-V IOMMU Device in root partition"
+	depends on HYPERV && X86 && PCI_MSI && MSHV_ROOT

What requires  PCI_MSI  here?

Quoted 31 lines
drivers/iommu/hyperv/Kconfig +4 -0
+	select IOMMU_API
+	default HYPERV
+	help
+	  This enables Hyper-V pseudo IOMMU device. When running as

privileged

drivers/iommu/hyperv/Kconfig +1 -0
+	  VM aka root on Microsoft Hyper-V hypervisor, this must be

enabled

drivers/iommu/hyperv/Kconfig +1 -0
+	  for doing any PCI passthru of devices to guest VMs. This

applies to

drivers/iommu/hyperv/Kconfig +1 -0
+	  both PFs and VFs. When enabling this, it is best to

disable amd/intel

drivers/iommu/hyperv/Kconfig +1 -0
+	  iommus via: intel_iommu=off amd_iommu=off as the

hypervisor really

drivers/iommu/hyperv/Kconfig +1 -0
+	  owns the iommu.
(quoted diff) +0 -0

b/drivers/iommu/hyperv/Makefile index d3b2a259439c..04c66b4409a3 100644 --- a/drivers/iommu/hyperv/Makefile
+++ b/drivers/iommu/hyperv/Makefile

drivers/iommu/hyperv/Makefile +1 -0
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_IRQ_REMAP) += hv-irq-remap-x86.o
 obj-$(CONFIG_HYPERV_PVIOMMU) += hv-iommu-guest.o
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
(quoted diff) +0 -0

b/drivers/iommu/hyperv/hv-iommu-root.c new file mode 100644 index 000000000000..e0e3e9d4385e

drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
@@ -0,0 +1,613 @@
+// SPDX-License-Identifier: GPL-2.0
+/*

drivers/iommu/hyperv/hv-iommu-root.c line 12

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+ * Hyper-V root vIOMMU driver.
+ * Copyright (C) 2026, Microsoft, Inc.
+ */
+#include <linux/pci.h>
+#include <linux/dma-map-ops.h>
+#include <linux/interval_tree.h>
+#include <linux/hyperv.h>
+#include <asm/iommu.h>
+#include <asm/mshyperv.h>
+#include "../dma-iommu.h"

I don't think we need it, virtio iommu driver calls iommu_dma_get_resv_regions() but hv does not.

Quoted 59 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+static dma_addr_t hv_iommu_max_iova;
+static struct iommu_domain_ops hv_paging_domain_ops;
+
+/* IOMMU device that we export to the world. MSHV supports max of

one */ +static struct iommu_device hv_virt_iommu;

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+
+struct hv_iommu_domain {
+	struct iommu_domain iommu_dom;
+	u32 domid_num;			      /* as opposed to

domain_id.type */

drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+	spinlock_t mappings_lock;	      /* protects

mappings_tree */

drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+	struct rb_root_cached mappings_tree;  /* iova to pa lookup

tree */ +};

drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
+
+#define to_hv_domain(d) container_of(d, struct hv_iommu_domain,

iommu_dom) +

drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+struct hv_iommu_mapping {
+	phys_addr_t paddr;
+	struct interval_tree_node iova;
+	u32 flags;
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2)

default
+ * domain. It has two types:
+ * S2 default: access to entire root partition memory. This for
the host
+ * maps to IOMMU_DOMAIN_IDENTITY in the iommu
subsystem, and
+ * is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the
hypervisor.
+ * (Linux renamed to : HV_DEVICE_DOMAIN_ID_DEFAULT)
+ * S2 NULL: Blocks everything, ie, IOMMU_DOMAIN_BLOCKED in linux.
+ */

drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+
+/*
+ * Create dummy domains to correspond to hypervisor prebuilt default

identity
+ * and null domains (dummy because we do not make hypercalls to create them).
+ */

drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+static struct hv_iommu_domain hv_def_identity_dom;
+static struct hv_iommu_domain hv_def_blocked_dom;
+
+/*
+ * The hypervisor owns the IOMMU. As such it will choose the best

page size
+ * and whenever possible use larger page sizes automatically. The map gpa
+ * hypercall to map the iova however currently uses only 4K pfns as input.
+ * So we specify 4K as the IOMMU PAGE_SIZE.
+ */

drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+#define HV_IOMMU_PGSIZES SZ_4K
+
+static DEFINE_IDA(hv_iommu_domids);   /* unique numeric ids for new

domains */ +

drivers/iommu/hyperv/hv-iommu-root.c line 66

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+static bool hv_iommu_capable(struct device *dev, enum iommu_cap cap)
+{
+	switch (cap) {
+	case IOMMU_CAP_CACHE_COHERENCY:
+		return true;

Maybe add a comment here to explain it is true for x86_64 mshv platforms, but need to be revisited if this driver expands to other architectures.

Quoted 33 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+	default:
+		return false;
+	}
+}
+
+static int hv_iommu_add_tree_mapping(struct hv_iommu_domain *hvdom,

ulong iova,

drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+				     phys_addr_t paddr, size_t size,

u32 flags,

drivers/iommu/hyperv/hv-iommu-root.c new file +22 -0
+				     gfp_t gfp)
+{
+	ulong irqflags;
+	struct hv_iommu_mapping *mapping;
+
+	mapping = kzalloc_obj(struct hv_iommu_mapping, gfp);
+	if (!mapping)
+		return -ENOMEM;
+
+	mapping->paddr = paddr;
+	mapping->iova.start = iova;
+	mapping->iova.last = iova + size - 1;
+	mapping->flags = flags;
+
+	spin_lock_irqsave(&hvdom->mappings_lock, irqflags);
+	interval_tree_insert(&mapping->iova, &hvdom->mappings_tree);
+	spin_unlock_irqrestore(&hvdom->mappings_lock, irqflags);
+
+	return 0;
+}
+
+/* If both size and iova are 0, then last = ULONG_MAX will remove

everything */ +static size_t hv_iommu_del_tree_mappings(struct hv_iommu_domain *hvdom,

drivers/iommu/hyperv/hv-iommu-root.c line 105

Quoted 9 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +9 -0
+					 ulong iova, size_t size)
+{
+	ulong flags;
+	size_t unmapped = 0;
+	ulong last = iova + size - 1;
+	struct hv_iommu_mapping *mapping = NULL;
+	struct interval_tree_node *node, *next;
+
+	spin_lock_irqsave(&hvdom->mappings_lock, flags);

nit: use scoped_guard(spinlock_irqsave, &hvdom->mappings_lock) ?

Quoted 304 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+	next = interval_tree_iter_first(&hvdom->mappings_tree, iova,

last);

drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+	while (next) {
+		node = next;
+		mapping = container_of(node, struct

hv_iommu_mapping, iova);

drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+		next = interval_tree_iter_next(node, iova, last);
+
+		/* Splitting of a mapping is not supported at

present */

drivers/iommu/hyperv/hv-iommu-root.c new file +31 -0
+		if (mapping->iova.start < iova)
+			break;
+
+		unmapped += mapping->iova.last - mapping->iova.start
+ 1; +
+		interval_tree_remove(node, &hvdom->mappings_tree);
+		kfree(mapping);
+	}
+	spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
+
+	return unmapped;
+}
+
+/* Create a new device domain in the hypervisor */
+static int hv_iommu_create_hyp_devdom(struct hv_iommu_domain *hvdom)
+{
+	u64 status;
+	struct hv_input_device_domain *ddp;
+	struct hv_input_create_device_domain *input;
+	ulong flags;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	ddp = &input->device_domain;
+	ddp->partition_id = HV_PARTITION_ID_SELF;
+	ddp->domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+	ddp->domain_id.id = hvdom->domid_num;
+
+	input->create_device_domain_flags.forward_progress_required

= 1;

drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+	input->create_device_domain_flags.inherit_owning_vtl = 0;
+
+	status = hv_do_hypercall(HVCALL_CREATE_DEVICE_DOMAIN, input,

NULL); +

drivers/iommu/hyperv/hv-iommu-root.c new file +9 -0
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "\n");
+
+	return hv_result_to_errno(status);
+}
+
+static struct iommu_domain *hv_iommu_domain_alloc_paging(struct

device *dev) +{

drivers/iommu/hyperv/hv-iommu-root.c new file +16 -0
+	struct hv_iommu_domain *hvdom;
+	int rc;
+
+	if (!dev_is_pci(dev))
+		return NULL;
+
+	hvdom = kzalloc_obj(struct hv_iommu_domain);
+	if (hvdom == NULL)
+		return ERR_PTR(-ENOMEM);
+
+	spin_lock_init(&hvdom->mappings_lock);
+	hvdom->mappings_tree = RB_ROOT_CACHED;
+
+	rc = ida_alloc_range(&hv_iommu_domids,
+			     HV_DEVICE_DOMAIN_ID_DEFAULT + 1,
+			     HV_DEVICE_DOMAIN_ID_NULL - 1,

GFP_KERNEL);

drivers/iommu/hyperv/hv-iommu-root.c new file +32 -0
+	if (rc < 0)
+		goto out_err;
+
+	hvdom->domid_num = rc;
+	hvdom->iommu_dom.pgsize_bitmap = HV_IOMMU_PGSIZES;
+	hvdom->iommu_dom.geometry.aperture_start = 0;
+	hvdom->iommu_dom.geometry.aperture_end = hv_iommu_max_iova;
+	hvdom->iommu_dom.geometry.force_aperture = true;
+	hvdom->iommu_dom.ops = &hv_paging_domain_ops;
+
+	rc = hv_iommu_create_hyp_devdom(hvdom);
+	if (rc)
+		goto out_err1;
+
+	return &hvdom->iommu_dom;
+
+out_err1:
+	ida_free(&hv_iommu_domids, hvdom->domid_num);
+out_err:
+	kfree(hvdom);
+	return ERR_PTR(rc);
+}
+
+static void hv_iommu_domain_free(struct iommu_domain *immdom)
+{
+	ulong flags;
+	u64 status;
+	struct hv_input_delete_device_domain *input;
+	struct hv_input_device_domain *ddp;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+
+	/* Cleanup any remaining. 0 for size results in ULONG_MAX as

the last */

drivers/iommu/hyperv/hv-iommu-root.c new file +24 -0
+	hv_iommu_del_tree_mappings(hvdom, 0, 0);
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	ddp = &input->device_domain;
+	memset(input, 0, sizeof(*input));
+
+	ddp->partition_id = HV_PARTITION_ID_SELF;
+	ddp->domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+	ddp->domain_id.id = hvdom->domid_num;
+
+	status = hv_do_hypercall(HVCALL_DELETE_DEVICE_DOMAIN, input,
+				 NULL);
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "\n");
+
+	ida_free(&hv_iommu_domids, hvdom->domid_num);
+	kfree(hvdom);
+}
+
+/*
+ * Attach a device to the default domain, or the null domain, or to

a domain
+ * previously created in the hypervisor.
+ */

drivers/iommu/hyperv/hv-iommu-root.c new file +12 -0
+static int hv_iommu_att_dev2dom(struct hv_iommu_domain *hvdom,
+				struct pci_dev *pdev)
+{
+	ulong flags;
+	u64 status;
+	struct hv_input_attach_device_domain *input;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	/* For null domain, hvdom->domid_num ==

HV_DEVICE_DOMAIN_ID_NULL */

drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
+	input->device_domain.partition_id = HV_PARTITION_ID_SELF;
+	input->device_domain.domain_id.type =

HV_DEVICE_DOMAIN_TYPE_S2;

drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+	input->device_domain.domain_id.id = hvdom->domid_num;
+
+	input->device_id.as_uint64 = hv_build_devid_type_pci(pdev);
+
+	status = hv_do_hypercall(HVCALL_ATTACH_DEVICE_DOMAIN, input,

NULL);

drivers/iommu/hyperv/hv-iommu-root.c new file +11 -0
+	local_irq_restore(flags);
+
+	if (hv_result_success(status) ||
+	    hv_result(status) == HV_STATUS_DEVICE_ALREADY_IN_DOMAIN)
+		return 0;
+
+	hv_status_err(status, "\n");
+	return hv_result_to_errno(status);
+}
+
+static int hv_iommu_attach_dev(struct iommu_domain *immdom, struct

device *dev,

drivers/iommu/hyperv/hv-iommu-root.c new file +11 -0
+			       struct iommu_domain *old)
+{
+	struct pci_dev *pdev;
+	int rc;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+
+	pdev = to_pci_dev(dev);
+
+	rc = hv_iommu_att_dev2dom(hvdom, pdev);
+	if (rc)
+		WARN(1, "Failed to attach pdev:%s\n",

pci_name(pdev)); +

drivers/iommu/hyperv/hv-iommu-root.c new file +15 -0
+	return rc;
+}
+
+static u64 hv_iommu_unmap_batch(u32 domid_num, ulong iova, u16 count)
+{
+	ulong flags;
+	struct hv_input_unmap_device_gpa_pages *input;
+	u64 status;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	input->device_domain.partition_id = HV_PARTITION_ID_SELF;
+	input->device_domain.domain_id.type =

HV_DEVICE_DOMAIN_TYPE_S2;

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+	input->device_domain.domain_id.id = domid_num;
+	input->target_device_va_base = iova;
+
+	status = hv_do_rep_hypercall(HVCALL_UNMAP_DEVICE_GPA_PAGES,

count,

drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+				     0, input, NULL);
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "iova:0x%lx count:0x%x\n",

iova, count); +

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+	return status;
+}
+
+static size_t hv_iommu_hyp_unmap_pages(struct iommu_domain *immdom,

ulong iova,

drivers/iommu/hyperv/hv-iommu-root.c new file +11 -0
+				       size_t size)
+{
+	u64 status;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+	size_t tot_done = 0;
+	ulong npages = size >> HV_HYP_PAGE_SHIFT;
+
+	while (npages) {
+		int done, count = min(npages, HV_REP_COUNT_MAX);
+
+		status = hv_iommu_unmap_batch(hvdom->domid_num,

iova, count); +

drivers/iommu/hyperv/hv-iommu-root.c new file +13 -0
+		done = hv_repcomp(status);
+		tot_done += done;
+		npages -= done;
+		iova += done << HV_HYP_PAGE_SHIFT;
+
+		if (!hv_result_success(status))
+			break;
+	}
+
+	return tot_done << HV_HYP_PAGE_SHIFT;
+}
+
+static size_t hv_iommu_unmap_pages(struct iommu_domain *immdom,

ulong iova,

drivers/iommu/hyperv/hv-iommu-root.c new file +7 -0
+				   size_t pgsize, size_t pgcount,
+				   struct iommu_iotlb_gather *gather)
+{
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+	size_t unmap_sz, size = pgsize * pgcount;
+
+	/* For valid input, the hypervisor guarantees unmap will

succeed */

drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+	unmap_sz = hv_iommu_hyp_unmap_pages(immdom, iova, size);
+	if (unmap_sz != size)
+		WARN(1, "Failed to unmap device gpa(%lx/%lx)\n",

unmap_sz,

drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+		     size);
+
+	if (unmap_sz == 0)
+		return 0;
+
+	size = hv_iommu_del_tree_mappings(hvdom, iova, unmap_sz);
+	if (size != unmap_sz)
+		pr_err("%s: could not delete tree mappings

(%lx:%lx/%lx)\n",

drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+		       __func__, iova, unmap_sz, size);
+
+	return size;
+}
+
+/* Return: must return exact status from the hypercall without

changes */ +static u64 hv_iommu_map_pgs(struct hv_iommu_domain *hvdom, ulong iova,

drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+			    phys_addr_t paddr, ulong npages, u32

map_flags) +{

drivers/iommu/hyperv/hv-iommu-root.c new file +11 -0
+	u64 status;
+	int i;
+	struct hv_input_map_device_gpa_pages *input;
+	unsigned long flags, pfn;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	input->device_domain.partition_id = HV_PARTITION_ID_SELF;
+	input->device_domain.domain_id.type =

HV_DEVICE_DOMAIN_TYPE_S2;

drivers/iommu/hyperv/hv-iommu-root.c new file +9 -0
+	input->device_domain.domain_id.id = hvdom->domid_num;
+	input->map_flags = map_flags;
+	input->target_device_va_base = iova;
+
+	pfn = paddr >> HV_HYP_PAGE_SHIFT;
+	for (i = 0; i < npages; i++, pfn++)
+		input->gpa_page_list[i] = pfn;
+
+	status = hv_do_rep_hypercall(HVCALL_MAP_DEVICE_GPA_PAGES,

npages, 0,

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+				     input, NULL);
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status) &&

!hv_result_needs_memory(status))

drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+		hv_status_err(status, "npgs:%lx paddr:%llx iova:%lx

mf:0x%x\n",

drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+			      npages, paddr, iova, map_flags);
+	return status;
+}
+
+#define HV_MAP_DEVICE_GPA_BATCH_SIZE   \
+	((HV_HYP_PAGE_SIZE - sizeof(struct

hv_input_map_device_gpa_pages)) \

drivers/iommu/hyperv/hv-iommu-root.c line 388

Quoted 3 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+			/ sizeof(u64))
+
+/* NB: cond_resched() is in vfio_iommu_map() */

map_pages()  is not VFIO-specific, so it cannot depend on vfio_iommu_map()  providing scheduling points. This comment is not relevant.

Quoted 4 lines

+static int hv_iommu_map_pages(struct iommu_domain *immdom, ulong iova,

drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+			      phys_addr_t paddr, size_t pgsize,

size_t pgcount,

drivers/iommu/hyperv/hv-iommu-root.c line 400

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+			      int prot, gfp_t gfp, size_t *mapped)
+{
+	u32 map_flags;
+	int ret;
+	u64 status = 0;
+	ulong npages, sav_iova = iova, done = 0;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+	size_t size = pgsize * pgcount;
+
+	map_flags = HV_MAP_GPA_READABLE;	/* required */

Why required? If the core asks for write-only, this still grant read access? VT-d S2 PTE has separate R W bits.

Maybe reject write-only if mshv cannot handle it.
if ((prot & IOMMU_WRITE) && !(prot & IOMMU_READ))

    return -EINVAL;
Quoted 59 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+	map_flags |= prot & IOMMU_WRITE ? HV_MAP_GPA_WRITABLE : 0;
+
+	ret = hv_iommu_add_tree_mapping(hvdom, iova, paddr, size,

map_flags,

drivers/iommu/hyperv/hv-iommu-root.c new file +20 -0
+					gfp);
+	if (ret)
+		return ret;
+
+	npages = size >> HV_HYP_PAGE_SHIFT;
+	while (done < npages) {
+		ulong completed, remain = npages - done;
+
+		remain = min(remain, HV_MAP_DEVICE_GPA_BATCH_SIZE);
+
+		status = hv_iommu_map_pgs(hvdom, iova, paddr, remain,
+					  map_flags);
+
+		completed = hv_repcomp(status);
+		done = done + completed;
+		iova = iova + (completed << HV_HYP_PAGE_SHIFT);
+		paddr = paddr + (completed << HV_HYP_PAGE_SHIFT);
+
+		if (hv_result_needs_memory(status)) {
+			ret =

hv_deposit_memory_gfp(hv_current_partition_id,

drivers/iommu/hyperv/hv-iommu-root.c new file +19 -0
+						    status, gfp);
+			if (ret)
+				break;
+			continue;
+		}
+		if (!hv_result_success(status))
+			break;
+	}
+
+	if (ret == 0)
+		ret = hv_result_to_errno(status);
+
+	if (hv_result_success(status)) {
+		if (mapped)
+			*mapped = size;
+	} else {
+		size_t done_size = done << HV_HYP_PAGE_SHIFT;
+
+		hv_status_err(status, "pgs:%lx/%lx iova:%lx\n",

done, npages,

drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+			      sav_iova);
+
+		hv_iommu_del_tree_mappings(hvdom, sav_iova, size);
+
+		/* Hypervisor guarantees unmap will succeed here */
+		hv_iommu_hyp_unmap_pages(immdom, sav_iova,

done_size); +

drivers/iommu/hyperv/hv-iommu-root.c new file +7 -0
+		if (mapped)
+			*mapped = 0;
+	}
+
+	return ret;
+}
+

drivers/iommu/hyperv/hv-iommu-root.c line 466

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+static phys_addr_t hv_iommu_iova_to_phys(struct iommu_domain *immdom,
+					 dma_addr_t iova)
+{
+	unsigned long flags;
+	struct hv_iommu_mapping *mapping;
+	struct interval_tree_node *node;
+	u64 paddr = 0;
+	struct hv_iommu_domain *hvdom = to_hv_domain(immdom);
+
+	spin_lock_irqsave(&hvdom->mappings_lock, flags);

guard(spinlock_irqsave)(&hvdom->mappings_lock);
some other places too.

Quoted 68 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+	node = interval_tree_iter_first(&hvdom->mappings_tree, iova,

iova);

drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
+	if (node) {
+		mapping = container_of(node, struct

hv_iommu_mapping, iova);

drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+		paddr = mapping->paddr + (iova -

mapping->iova.start);

drivers/iommu/hyperv/hv-iommu-root.c new file +50 -0
+	}
+	spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
+
+	return paddr;
+}
+
+static struct iommu_device *hv_iommu_probe_device(struct device *dev)
+{
+	if (!dev_is_pci(dev))
+		return ERR_PTR(-ENODEV);
+
+	return &hv_virt_iommu;
+}
+
+static void hv_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *head)
+{
+	iommu_add_resv_x86_msi_region(head);
+}
+
+static struct iommu_domain_ops hv_paging_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+	.map_pages = hv_iommu_map_pages,
+	.unmap_pages = hv_iommu_unmap_pages,
+	.iova_to_phys = hv_iommu_iova_to_phys,
+	.free = hv_iommu_domain_free,
+};
+
+static struct iommu_ops hv_iommu_ops = {
+	.capable	    = hv_iommu_capable,
+	.domain_alloc_paging	= hv_iommu_domain_alloc_paging,
+	.probe_device	    = hv_iommu_probe_device,
+	.device_group	    = pci_device_group,
+	.get_resv_regions   = hv_iommu_get_resv_regions,
+	.owner		    = THIS_MODULE,
+	.identity_domain    = &hv_def_identity_dom.iommu_dom,
+	.blocked_domain     = &hv_def_blocked_dom.iommu_dom,
+	.release_domain     = &hv_def_blocked_dom.iommu_dom,
+};
+
+static const struct iommu_domain_ops hv_special_domain_ops = {
+	.attach_dev = hv_iommu_attach_dev,
+};
+
+static void __init hv_initialize_special_domains(void)
+{
+	hv_def_identity_dom.iommu_dom.type = IOMMU_DOMAIN_IDENTITY;
+	hv_def_identity_dom.iommu_dom.ops = &hv_special_domain_ops;
+	hv_def_identity_dom.iommu_dom.owner = &hv_iommu_ops;
+	hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_DEFAULT;

/* 0 */ +

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+	hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
+	hv_def_blocked_dom.iommu_dom.ops = &hv_special_domain_ops;
+	hv_def_blocked_dom.iommu_dom.owner = &hv_iommu_ops;
+	hv_def_blocked_dom.domid_num = HV_DEVICE_DOMAIN_ID_NULL;

/* INTMAX */ +}

drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+
+/*
+ * In general, the hypervisor does not support the capabilities

hypercall for
+ * root partitions, an exception was made for max_iova_width. For

Quoted 8 lines

IOMMU cap,
+ * hv_root_iommu_detect() below checks for HV_DEVICE_DOMAIN_AVAILABLE which
+ * supersedes HV_IOMMU_CAP_PRESENT. As for the pgsize_bitmap cap, see comment
+ * above HV_IOMMU_PGSIZES.
+ */
+static int hv_iommu_get_caps(struct hv_output_get_iommu_capabilities

Should be __init since it is only called by a __init function?

Quoted 103 lines

*caps) +{

drivers/iommu/hyperv/hv-iommu-root.c new file +12 -0
+	u64 status;
+	unsigned long flags;
+	struct hv_input_get_iommu_capabilities *input;
+	struct hv_output_get_iommu_capabilities *output;
+
+	local_irq_save(flags);
+
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	output = *this_cpu_ptr(hyperv_pcpu_output_arg);
+	memset(input, 0, sizeof(*input));
+	input->partition_id = HV_PARTITION_ID_SELF;
+	status = hv_do_hypercall(HVCALL_GET_IOMMU_CAPABILITIES,

input, output);

drivers/iommu/hyperv/hv-iommu-root.c new file +26 -0
+	*caps = *output;
+
+	local_irq_restore(flags);
+
+	if (!hv_result_success(status))
+		hv_status_err(status, "\n");
+
+	return hv_result_to_errno(status);
+}
+
+static int __init hv_root_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+
+	hv_iommu_max_iova = DMA_BIT_MASK(caps.max_iova_width);
+
+	/* This must come before iommu_device_register() because the

latter

drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+	 * calls into the hooks.
+	 */
+	hv_initialize_special_domains();
+
+	rc = iommu_device_sysfs_add(iommup, NULL, NULL, "%s",

"hyperv-iommu");

drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
+	if (rc) {
+		pr_err("Hyper-V: iommu_device_sysfs_add failed:

%d\n", rc);

drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+		return rc;
+	}
+
+	rc = iommu_device_register(iommup, &hv_iommu_ops, NULL);
+	if (rc) {
+		pr_err("Hyper-V: iommu_device_register failed:

%d\n", rc);

drivers/iommu/hyperv/hv-iommu-root.c new file +18 -0
+		goto err_sysfs_remove;
+	}
+
+	pr_info("Hyper-V IOMMU initialized\n");
+
+	return 0;
+
+err_sysfs_remove:
+	iommu_device_sysfs_remove(iommup);
+	return rc;
+}
+
+void __init hv_root_iommu_detect(void)
+{
+	if (no_iommu || iommu_detected || hv_l1vh_partition())
+		return;
+
+	/* This is set for root partitions only, it is never set for

non-root */

drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+	if (!(ms_hyperv.misc_features & HV_DEVICE_DOMAIN_AVAILABLE))
+		return;
+
+	iommu_detected = 1;
+	x86_init.iommu.iommu_init = hv_root_iommu_init;
+
+	pci_request_acs();
+}
(quoted diff) +0 -0

b/include/asm-generic/mshyperv.h index 2584e693d7b3..ad02d6956ae3 100644 --- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h

include/asm-generic/mshyperv.h +3 -0
@@ -28,6 +28,9 @@
 
 #define VTPM_BASE_ADDRESS 0xfed40000
 
+#define HV_REP_COUNT_MAX        \
+	(HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)
+
 enum hv_partition_type {
 	HV_PARTITION_TYPE_GUEST,
 	HV_PARTITION_TYPE_ROOT,
include/linux/hyperv.h +0 -0
index e85a1a043eac..8aa752e8c4e1 100644

include/linux/hyperv.h static inline unsigned long virt_to_hvpfn(void · line 1829

Quoted 7 lines
include/linux/hyperv.h +4 -0
@@ -1823,4 +1823,10 @@ static inline unsigned long virt_to_hvpfn(void
 *addr) #define HVPFN_DOWN(x)	((x) >> HV_HYP_PAGE_SHIFT)
 #define page_to_hvpfn(page)	(page_to_pfn(page) *
 NR_HV_HYP_PAGES_IN_PAGE) 
+#ifdef CONFIG_HYPERV_ROOT_IOMMU
+void __init hv_root_iommu_detect(void);
+#else
+static inline void hv_root_iommu_detect(void) { }

nit: add __init to be consistent? also it may save memory if the stub is not empty in the future.

With above addressed,

Quoted 3 lines
include/linux/hyperv.h +2 -0
+#endif /* CONFIG_HYPERV_ROOT_IOMMU */
+
 #endif /* _HYPERV_H */