Skip to contentSkip to conversation

Hyper-V: root VM iommu kernel only driver

PATCH In review Mukesh R 3 participants 13 messages Sep 21 – Sep 24

Subsystems (5)
lore
Share & tools
lore

Mukesh R

author
#0 lore raw

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 commit and in the comments in the file implementing it.

Thanks,
-Mukesh

V0: based on upstream hyperv-next commit be0cfab740e5

Mukesh R (3):
PCI: hv: Export hv_build_devid_type_pci() and change return type
mshv: Import data structs around device domains from hyperv headers x86/hyperv: Implement Hyper-V virtual IOMMU on root VMs

 arch/x86/hyperv/irqdomain.c          |   9 +-
 arch/x86/include/asm/mshyperv.h      |   6 +
 arch/x86/kernel/pci-dma.c            |   2 +
 drivers/iommu/Kconfig                |   1 +
 drivers/iommu/hyperv/Kconfig         |  15 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 645 +++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h       |   3 +
 include/hyperv/hvgdk_mini.h          |   9 +
 include/hyperv/hvhdk_mini.h          |  90 ++++
 include/linux/hyperv.h               |   6 +
 11 files changed, 783 insertions(+), 4 deletions(-)
 create mode 100644 drivers/iommu/hyperv/Kconfig
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
Signature · 1 line
2.51.2.vfs.0.1
author
#1 lore raw

PATCH 1/3 PCI: hv: Export hv_build_devid_type_pci() and change return type

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 it's 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 f64393e853ee..8ebbd1cb7c8c 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
#2 lore raw

Re: [PATCH V0 1/3] PCI: hv: Export hv_build_devid_type_pci() and change return type

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] hv: invalid fallback device ID when CONFIG_PCI_MSI=n --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] hv: invalid fallback device ID when CONFIG_PCI_MSI=n
--

commit 92e8d721eb5c704c530977f1074dc44ee7895e0a
Author: Mukesh R <[email protected]>

PCI: hv: Export hv_build_devid_type_pci() and change return type

This commit makes hv_build_devid_type_pci() public and changes its return type to u64 to enforce its size. It also adds a fallback stub for when CONFIG_PCI_MSI is disabled.

arch/x86/include/asm/mshyperv.h void hv_crash_asm_end(void); · line 254

Quoted 12 lines
arch/x86/include/asm/mshyperv.h +4 -0
index f64393e853ee3..8ebbd1cb7c8ce 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; }

[Severity: High]
Does returning 0 here break PCI device isolation and silently misconfigure the hypervisor IOMMU?

On Hyper-V, a device ID of 0 is not an error code; it is a valid ID corresponding to HV_DEVICE_TYPE_LOGICAL (type 0) with ID 0. If CONFIG_PCI_MSI is disabled (which is permitted, as CONFIG_HYPERV_ROOT_IOMMU does not select or depend on it), the IOMMU driver will silently issue attach/map hypercalls targeting Logical Device 0 instead of the actual PCI device.

For example, when compiled with CONFIG_HYPERV_ROOT_IOMMU=y and CONFIG_PCI_MSI=n, during boot and device initialization, hv_iommu_att_dev2dom() could call this stub and pass the resulting 0 to the HVCALL_ATTACH_DEVICE_DOMAIN hypercall.

Can the actual implementation of hv_build_devid_type_pci() in arch/x86/hyperv/irqdomain.c and its dependencies (get_rid_cb, struct rid_data) be moved outside the CONFIG_PCI_MSI configuration boundary rather than being masked by a silently failing stub? They use standard PCI APIs and have no real dependency on MSI.

Quoted 4 lines
arch/x86/include/asm/mshyperv.h +2 -0
+#endif
+
 #else /* CONFIG_HYPERV */
 static inline void hyperv_init(void) {}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v0 1/3
#3 lore raw

Re: [PATCH V0 1/3] PCI: hv: Export hv_build_devid_type_pci() and change return type

In reply to Mukesh R
Quoted 4 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 it's size.

Nit: it's -> its

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

No other feedback than the Sashiko review, so assuming that's fixed:

author
#4 lore raw

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

In reply to Mukesh R

Copy/import from Hyper-V public headers, definitions and declarations that are related to creating iommu domains in the hypervisor, attaching devices to them, doing the reverse, etc.

---
 include/hyperv/hvgdk_mini.h |  9 ++++
 include/hyperv/hvhdk_mini.h | 90 +++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+)
include/hyperv/hvgdk_mini.h +9 -0
index 6a4e8b9d570f..3b74f3c0229d 100644
@@ -326,6 +326,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.
@@ -486,9 +488,15 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
 #define HVCALL_GET_VP_INDEX_FROM_APIC_ID		0x009a
 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE	0x00af
 #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
+#define HVCALL_DETACH_DEVICE_DOMAIN                     0x00c4
+#define HVCALL_DELETE_DEVICE_DOMAIN                     0x00c5
 #define HVCALL_GET_GPA_PAGES_ACCESS_STATES		0x00c9
 #define HVCALL_ACQUIRE_SPARSE_SPA_PAGE_HOST_ACCESS	0x00d7
 #define HVCALL_RELEASE_SPARSE_SPA_PAGE_HOST_ACCESS	0x00d8
@@ -502,6 +510,7 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
 #define HVCALL_MMIO_READ				0x0106
 #define HVCALL_MMIO_WRITE				0x0107
 #define HVCALL_DISABLE_HYP_EX                           0x010f
+#define HVCALL_GET_IOMMU_CAPABILITIES			0x0125
 #define HVCALL_MAP_STATS_PAGE2				0x0131
 
 /* HV_HYPERCALL_INPUT */
include/hyperv/hvhdk_mini.h +90 -0

94 lines not on this page · raw message

Signature · 1 line
2.51.2.vfs.0.1
#5 lore raw

Re: [PATCH V0 2/3] mshv: Import data structs around device domains from hyperv headers

Quoted 15 lines · Mukesh R

Copy/import from Hyper-V public headers, definitions and declarations that are related to creating iommu domains in the hypervisor, attaching devices to them, doing the reverse, etc.

---
 include/hyperv/hvgdk_mini.h |  9 ++++
 include/hyperv/hvhdk_mini.h | 90 +++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+)
include/hyperv/hvgdk_mini.h +0 -0
index 6a4e8b9d570f..3b74f3c0229d 100644

include/hyperv/hvgdk_mini.h union hv_hypervisor_version_info { · line 330

Quoted 5 lines
include/hyperv/hvgdk_mini.h +2 -0
@@ -326,6 +326,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)
+

For my understanding, how is ms_hyperv.misc_features.HV_DEVICE_DOMAIN_AVAILABLE = 1 different from HV_IOMMU_CAP_PRESENT = 1 combined with HV_IOMMU_CAP_S2 = 1? It seems like there is room to combine forces with the guest driver and just go with the latter.

Quoted 32 lines
include/hyperv/hvgdk_mini.h +7 -0
 /*
  * Implementation recommendations. Indicates which behaviors the hypervisor
  * recommends the OS implement for optimal performance.
@@ -486,9 +488,15 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
 #define HVCALL_GET_VP_INDEX_FROM_APIC_ID		0x009a
 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE	0x00af
 #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
+#define HVCALL_DETACH_DEVICE_DOMAIN                     0x00c4
+#define HVCALL_DELETE_DEVICE_DOMAIN                     0x00c5
 #define HVCALL_GET_GPA_PAGES_ACCESS_STATES		0x00c9
 #define HVCALL_ACQUIRE_SPARSE_SPA_PAGE_HOST_ACCESS	0x00d7
 #define HVCALL_RELEASE_SPARSE_SPA_PAGE_HOST_ACCESS	0x00d8
@@ -502,6 +510,7 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
 #define HVCALL_MMIO_READ				0x0106
 #define HVCALL_MMIO_WRITE				0x0107
 #define HVCALL_DISABLE_HYP_EX                           0x010f
+#define HVCALL_GET_IOMMU_CAPABILITIES			0x0125
 #define HVCALL_MAP_STATS_PAGE2				0x0131
 
 /* HV_HYPERCALL_INPUT */
include/hyperv/hvhdk_mini.h +0 -0
index 035ba20870f7..3bb052fd6e06 100644

include/hyperv/hvhdk_mini.h union hv_device_id { /* HV_DEVICE_ID */ · line 554

Quoted 9 lines
include/hyperv/hvhdk_mini.h +4 -0
@@ -548,4 +548,94 @@ union hv_device_id {		/* HV_DEVICE_ID */
 	} acpi;
 } __packed;
 
+/* 3 domain types: stage 1, stage 2, and SOC */
+#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 */
 
 
+#define HV_DEVICE_DOMAIN_TYPE_SOC 2 /* HV_DEVICE_DOMAIN_ID_TYPE_SOC */

I don't think this domain type is used in patch 3, perhaps we bring it in when needed?

include/hyperv/hvhdk_mini.h union hv_device_id { /* HV_DEVICE_ID */ · line 556

Quoted 2 lines
include/hyperv/hvhdk_mini.h +2 -0
+
+/* ID for stage 2 default domain and NULL domain */

I think this comment would be more useful if it related the MSHV default and NULL domains to the Linux equivalents of identity and blocking respectively. As it stands, it just repeats the names of the defines it is a comment for.

Quoted 85 lines
include/hyperv/hvhdk_mini.h +84 -0
+#define HV_DEVICE_DOMAIN_ID_S2_DEFAULT 0
+#define HV_DEVICE_DOMAIN_ID_S2_NULL    0xFFFFFFFFULL
+
+union hv_device_domain_id {
+	u64 as_uint64;
+	struct {
+		u32 type : 4;
+		u32 reserved : 28;
+		u32 id;
+	};
+} __packed;
+
+struct hv_input_device_domain { /* HV_INPUT_DEVICE_DOMAIN */
+	u64 partition_id;
+	union hv_input_vtl owner_vtl;
+	u8 padding[7];
+	union hv_device_domain_id domain_id;
+} __packed;
+
+union hv_create_device_domain_flags {	/* HV_CREATE_DEVICE_DOMAIN_FLAGS */
+	u32 as_uint32;
+	struct {
+		u32 forward_progress_required : 1;
+		u32 inherit_owning_vtl : 1;
+		u32 reserved : 30;
+	} __packed;
+} __packed;
+
+struct hv_input_create_device_domain {	/* HV_INPUT_CREATE_DEVICE_DOMAIN */
+	struct hv_input_device_domain device_domain;
+	union hv_create_device_domain_flags create_device_domain_flags;
+} __packed;
+
+struct hv_input_delete_device_domain {	/* HV_INPUT_DELETE_DEVICE_DOMAIN */
+	struct hv_input_device_domain device_domain;
+} __packed;
+
+struct hv_input_attach_device_domain {	/* HV_INPUT_ATTACH_DEVICE_DOMAIN */
+	struct hv_input_device_domain device_domain;
+	union hv_device_id device_id;
+} __packed;
+
+struct hv_input_detach_device_domain {	/* HV_INPUT_DETACH_DEVICE_DOMAIN */
+	u64 partition_id;
+	union hv_device_id device_id;
+} __packed;
+
+/* HV_INPUT_GET_IOMMU_CAPABILITIES */
+struct hv_input_get_iommu_capabilities {
+	u64 partition_id;
+	u64 reserved;
+} __packed;
+
+#define HV_IOMMU_CAP_PRESENT    BIT_ULL(0)
+#define HV_IOMMU_CAP_S2         BIT_ULL(1)
+#define HV_IOMMU_CAP_S1         BIT_ULL(2)
+#define HV_IOMMU_CAP_S1_5LVL    BIT_ULL(3)
+#define HV_IOMMU_CAP_PASID      BIT_ULL(4)
+#define HV_IOMMU_CAP_ATS        BIT_ULL(5)
+#define HV_IOMMU_CAP_PRI        BIT_ULL(6)
+
+struct hv_output_get_iommu_capabilities { /* HV_OUTPUT_GET_IOMMU_CAPABILITIES */
+	u32 size;
+	u16 reserved;
+	u8  max_iova_width;
+	u8  max_pasid_width;
+	u64 iommu_cap;          /* HV_IOMMU_CAP_* above */
+	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;
+
 #endif /* _HV_HVHDK_MINI_H */
author
#6 lore raw

Re: [PATCH V0 2/3] mshv: Import data structs around device domains from hyperv headers

Quoted 19 lines · Easwar Hariharan
Quoted 18 lines · Mukesh R

Copy/import from Hyper-V public headers, definitions and declarations that are related to creating iommu domains in the hypervisor, attaching devices to them, doing the reverse, etc.

---
  include/hyperv/hvgdk_mini.h |  9 ++++
  include/hyperv/hvhdk_mini.h | 90 +++++++++++++++++++++++++++++++++++++
  2 files changed, 99 insertions(+)
include/hyperv/hvgdk_mini.h +0 -0
index 6a4e8b9d570f..3b74f3c0229d 100644
@@ -326,6 +326,8 @@ union hv_hypervisor_version_info {
  /* stimer Direct Mode is available */
  #define HV_STIMER_DIRECT_MODE_AVAILABLE			BIT(19)
 
Quoted 7 lines

include/hyperv/hvgdk_mini.h union hv_hypervisor_version_info { · line 330

Quoted 2 lines
include/hyperv/hvgdk_mini.h +2 -0
+#define HV_DEVICE_DOMAIN_AVAILABLE			BIT(24)
+

For my understanding, how is ms_hyperv.misc_features.HV_DEVICE_DOMAIN_AVAILABLE = 1 different from HV_IOMMU_CAP_PRESENT = 1 combined with HV_IOMMU_CAP_S2 = 1? It seems like there is room to combine forces with the guest driver and just go with the latter.

It's different. we want to make sure the version of the hypervisor supports creating device domains. see: HVCALL_CREATE_DEVICE_DOMAIN

Thanks,
-Mukesh

Quoted 136 lines
Quoted 32 lines
include/hyperv/hvgdk_mini.h +7 -0
  /*
   * Implementation recommendations. Indicates which behaviors the hypervisor
   * recommends the OS implement for optimal performance.
@@ -486,9 +488,15 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
  #define HVCALL_GET_VP_INDEX_FROM_APIC_ID		0x009a
  #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE	0x00af
  #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
+#define HVCALL_DETACH_DEVICE_DOMAIN                     0x00c4
+#define HVCALL_DELETE_DEVICE_DOMAIN                     0x00c5
  #define HVCALL_GET_GPA_PAGES_ACCESS_STATES		0x00c9
  #define HVCALL_ACQUIRE_SPARSE_SPA_PAGE_HOST_ACCESS	0x00d7
  #define HVCALL_RELEASE_SPARSE_SPA_PAGE_HOST_ACCESS	0x00d8
@@ -502,6 +510,7 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
  #define HVCALL_MMIO_READ				0x0106
  #define HVCALL_MMIO_WRITE				0x0107
  #define HVCALL_DISABLE_HYP_EX                           0x010f
+#define HVCALL_GET_IOMMU_CAPABILITIES			0x0125
  #define HVCALL_MAP_STATS_PAGE2				0x0131
 
  /* HV_HYPERCALL_INPUT */
include/hyperv/hvhdk_mini.h +0 -0
index 035ba20870f7..3bb052fd6e06 100644

include/hyperv/hvhdk_mini.h union hv_device_id { /* HV_DEVICE_ID */ · line 554

Quoted 9 lines
include/hyperv/hvhdk_mini.h +4 -0
@@ -548,4 +548,94 @@ union hv_device_id {		/* HV_DEVICE_ID */
  	} acpi;
  } __packed;
 
+/* 3 domain types: stage 1, stage 2, and SOC */
+#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 */
 
 
+#define HV_DEVICE_DOMAIN_TYPE_SOC 2 /* HV_DEVICE_DOMAIN_ID_TYPE_SOC */

I don't think this domain type is used in patch 3, perhaps we bring it in when needed?

include/hyperv/hvhdk_mini.h union hv_device_id { /* HV_DEVICE_ID */ · line 556

Quoted 2 lines
include/hyperv/hvhdk_mini.h +2 -0
+
+/* ID for stage 2 default domain and NULL domain */

I think this comment would be more useful if it related the MSHV default and NULL domains to the Linux equivalents of identity and blocking respectively. As it stands, it just repeats the names of the defines it is a comment for.

Quoted 85 lines
include/hyperv/hvhdk_mini.h +84 -0
+#define HV_DEVICE_DOMAIN_ID_S2_DEFAULT 0
+#define HV_DEVICE_DOMAIN_ID_S2_NULL    0xFFFFFFFFULL
+
+union hv_device_domain_id {
+	u64 as_uint64;
+	struct {
+		u32 type : 4;
+		u32 reserved : 28;
+		u32 id;
+	};
+} __packed;
+
+struct hv_input_device_domain { /* HV_INPUT_DEVICE_DOMAIN */
+	u64 partition_id;
+	union hv_input_vtl owner_vtl;
+	u8 padding[7];
+	union hv_device_domain_id domain_id;
+} __packed;
+
+union hv_create_device_domain_flags {	/* HV_CREATE_DEVICE_DOMAIN_FLAGS */
+	u32 as_uint32;
+	struct {
+		u32 forward_progress_required : 1;
+		u32 inherit_owning_vtl : 1;
+		u32 reserved : 30;
+	} __packed;
+} __packed;
+
+struct hv_input_create_device_domain {	/* HV_INPUT_CREATE_DEVICE_DOMAIN */
+	struct hv_input_device_domain device_domain;
+	union hv_create_device_domain_flags create_device_domain_flags;
+} __packed;
+
+struct hv_input_delete_device_domain {	/* HV_INPUT_DELETE_DEVICE_DOMAIN */
+	struct hv_input_device_domain device_domain;
+} __packed;
+
+struct hv_input_attach_device_domain {	/* HV_INPUT_ATTACH_DEVICE_DOMAIN */
+	struct hv_input_device_domain device_domain;
+	union hv_device_id device_id;
+} __packed;
+
+struct hv_input_detach_device_domain {	/* HV_INPUT_DETACH_DEVICE_DOMAIN */
+	u64 partition_id;
+	union hv_device_id device_id;
+} __packed;
+
+/* HV_INPUT_GET_IOMMU_CAPABILITIES */
+struct hv_input_get_iommu_capabilities {
+	u64 partition_id;
+	u64 reserved;
+} __packed;
+
+#define HV_IOMMU_CAP_PRESENT    BIT_ULL(0)
+#define HV_IOMMU_CAP_S2         BIT_ULL(1)
+#define HV_IOMMU_CAP_S1         BIT_ULL(2)
+#define HV_IOMMU_CAP_S1_5LVL    BIT_ULL(3)
+#define HV_IOMMU_CAP_PASID      BIT_ULL(4)
+#define HV_IOMMU_CAP_ATS        BIT_ULL(5)
+#define HV_IOMMU_CAP_PRI        BIT_ULL(6)
+
+struct hv_output_get_iommu_capabilities { /* HV_OUTPUT_GET_IOMMU_CAPABILITIES */
+	u32 size;
+	u16 reserved;
+	u8  max_iova_width;
+	u8  max_pasid_width;
+	u64 iommu_cap;          /* HV_IOMMU_CAP_* above */
+	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;
+
  #endif /* _HV_HVHDK_MINI_H */
#7 lore raw

Re: [PATCH V0 2/3] mshv: Import data structs around device domains from hyperv headers

Quoted 27 lines · Mukesh R
Quoted 11 lines · Easwar Hariharan
Quoted 10 lines · Mukesh R

Copy/import from Hyper-V public headers, definitions and declarations that are related to creating iommu domains in the hypervisor, attaching devices to them, doing the reverse, etc.

---
  include/hyperv/hvgdk_mini.h |  9 ++++ include/hyperv/hvhdk_mini.h | 90 +++++++++++++++++++++++++++++++++++++

  2 files changed, 99 insertions(+)
Quoted 14 lines

include/hyperv/hvgdk_mini.h union hv_hypervisor_version_info { · line 329

Quoted 9 lines
include/hyperv/hvgdk_mini.h +1 -0
index 6a4e8b9d570f..3b74f3c0229d 100644
@@ -326,6 +326,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)
+

For my understanding, how is ms_hyperv.misc_features.HV_DEVICE_DOMAIN_AVAILABLE = 1 different from HV_IOMMU_CAP_PRESENT = 1 combined with HV_IOMMU_CAP_S2 = 1? It seems like there is room to combine forces with the guest driver and just go with the latter.

Quoted 3 lines

It's different. we want to make sure the version of the hypervisor supports creating device domains. see: HVCALL_CREATE_DEVICE_DOMAIN

I guess current hypervisor versions use BIT(24) to identify support for creating device domains, so this is mandatory for the root IOMMU driver. Hopefully that gets deprecated and the two drivers can use the same GetIommuCapabilities with S2 and S1 capability flags to identify what roles are supported in the future.

- Easwar (he/him)

Quoted 134 lines
Quoted 134 lines
Quoted 34 lines
include/hyperv/hvgdk_mini.h +7 -0
   /*
    * Implementation recommendations. Indicates which behaviors the hypervisor
    * recommends the OS implement for optimal performance.
@@ -486,9 +488,15 @@ union hv_vp_assist_msr_contents {     /* HV_REGISTER_VP_ASSIST_PAGE */
   #define HVCALL_GET_VP_INDEX_FROM_APIC_ID        0x009a
   #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE    0x00af
   #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
+#define HVCALL_DETACH_DEVICE_DOMAIN                     0x00c4
+#define HVCALL_DELETE_DEVICE_DOMAIN                     0x00c5
   #define HVCALL_GET_GPA_PAGES_ACCESS_STATES        0x00c9
   #define HVCALL_ACQUIRE_SPARSE_SPA_PAGE_HOST_ACCESS    0x00d7
   #define HVCALL_RELEASE_SPARSE_SPA_PAGE_HOST_ACCESS    0x00d8
@@ -502,6 +510,7 @@ union hv_vp_assist_msr_contents {     /* HV_REGISTER_VP_ASSIST_PAGE */
   #define HVCALL_MMIO_READ                0x0106
   #define HVCALL_MMIO_WRITE                0x0107
   #define HVCALL_DISABLE_HYP_EX                           0x010f
+#define HVCALL_GET_IOMMU_CAPABILITIES            0x0125
   #define HVCALL_MAP_STATS_PAGE2                0x0131
     /* HV_HYPERCALL_INPUT */
include/hyperv/hvhdk_mini.h +0 -0
index 035ba20870f7..3bb052fd6e06 100644
@@ -548,4 +548,94 @@ union hv_device_id {        /* HV_DEVICE_ID */
       } acpi;
   } __packed;

  +/* 3 domain types: stage 1, stage 2, and SOC */

include/hyperv/hvhdk_mini.h union hv_device_id { /* HV_DEVICE_ID */ · line 554

Quoted 5 lines
include/hyperv/hvhdk_mini.h +3 -0
+#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 */
 
 
+#define HV_DEVICE_DOMAIN_TYPE_SOC 2 /* HV_DEVICE_DOMAIN_ID_TYPE_SOC */

I don't think this domain type is used in patch 3, perhaps we bring it in when needed?

include/hyperv/hvhdk_mini.h union hv_device_id { /* HV_DEVICE_ID */ · line 556

Quoted 2 lines
include/hyperv/hvhdk_mini.h +2 -0
+
+/* ID for stage 2 default domain and NULL domain */

I think this comment would be more useful if it related the MSHV default and NULL domains to the Linux equivalents of identity and blocking respectively. As it stands, it just repeats the names of the defines it is a comment for.

Quoted 85 lines
include/hyperv/hvhdk_mini.h +84 -0
+#define HV_DEVICE_DOMAIN_ID_S2_DEFAULT 0
+#define HV_DEVICE_DOMAIN_ID_S2_NULL    0xFFFFFFFFULL
+
+union hv_device_domain_id {
+    u64 as_uint64;
+    struct {
+        u32 type : 4;
+        u32 reserved : 28;
+        u32 id;
+    };
+} __packed;
+
+struct hv_input_device_domain { /* HV_INPUT_DEVICE_DOMAIN */
+    u64 partition_id;
+    union hv_input_vtl owner_vtl;
+    u8 padding[7];
+    union hv_device_domain_id domain_id;
+} __packed;
+
+union hv_create_device_domain_flags {    /* HV_CREATE_DEVICE_DOMAIN_FLAGS */
+    u32 as_uint32;
+    struct {
+        u32 forward_progress_required : 1;
+        u32 inherit_owning_vtl : 1;
+        u32 reserved : 30;
+    } __packed;
+} __packed;
+
+struct hv_input_create_device_domain {    /* HV_INPUT_CREATE_DEVICE_DOMAIN */
+    struct hv_input_device_domain device_domain;
+    union hv_create_device_domain_flags create_device_domain_flags;
+} __packed;
+
+struct hv_input_delete_device_domain {    /* HV_INPUT_DELETE_DEVICE_DOMAIN */
+    struct hv_input_device_domain device_domain;
+} __packed;
+
+struct hv_input_attach_device_domain {    /* HV_INPUT_ATTACH_DEVICE_DOMAIN */
+    struct hv_input_device_domain device_domain;
+    union hv_device_id device_id;
+} __packed;
+
+struct hv_input_detach_device_domain {    /* HV_INPUT_DETACH_DEVICE_DOMAIN */
+    u64 partition_id;
+    union hv_device_id device_id;
+} __packed;
+
+/* HV_INPUT_GET_IOMMU_CAPABILITIES */
+struct hv_input_get_iommu_capabilities {
+    u64 partition_id;
+    u64 reserved;
+} __packed;
+
+#define HV_IOMMU_CAP_PRESENT    BIT_ULL(0)
+#define HV_IOMMU_CAP_S2         BIT_ULL(1)
+#define HV_IOMMU_CAP_S1         BIT_ULL(2)
+#define HV_IOMMU_CAP_S1_5LVL    BIT_ULL(3)
+#define HV_IOMMU_CAP_PASID      BIT_ULL(4)
+#define HV_IOMMU_CAP_ATS        BIT_ULL(5)
+#define HV_IOMMU_CAP_PRI        BIT_ULL(6)
+
+struct hv_output_get_iommu_capabilities { /* HV_OUTPUT_GET_IOMMU_CAPABILITIES */
+    u32 size;
+    u16 reserved;
+    u8  max_iova_width;
+    u8  max_pasid_width;
+    u64 iommu_cap;          /* HV_IOMMU_CAP_* above */
+    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;
+

  #endif /* _HV_HVHDK_MINI_H */

author
#8 lore raw

PATCH 3/3 x86/hyperv: Implement root VM IOMMU kernel only driver

In reply to Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works with Microsoft Hyper-V hypervisor on 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/Kconfig                |   1 +
 drivers/iommu/hyperv/Kconfig         |  15 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 645 +++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h       |   3 +
 include/linux/hyperv.h               |   6 +
 7 files changed, 673 insertions(+)
 create mode 100644 drivers/iommu/hyperv/Kconfig
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
arch/x86/kernel/pci-dma.c +2 -0
index 6267363e0189..37fed8c7a8c2 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)
 	}
 	pci_swiotlb_detect();
 	gart_iommu_hole_init();
+	hv_iommu_detect();
 	amd_iommu_detect();
 	detect_intel_iommu();
 	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
drivers/iommu/Kconfig +1 -0
index 6e07bd69467a..3e410e0f3e1d 100644
@@ -197,6 +197,7 @@ source "drivers/iommu/arm/Kconfig"
 source "drivers/iommu/intel/Kconfig"
 source "drivers/iommu/iommufd/Kconfig"
 source "drivers/iommu/riscv/Kconfig"
+source "drivers/iommu/hyperv/Kconfig"
 
 config IRQ_REMAP
 	bool "Support for Interrupt Remapping"
drivers/iommu/hyperv/Kconfig new file +15 -0
new file mode 100644
index 000000000000..4342043443e7
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Hyper-V IOMMU support
+
+config HYPERV_ROOT_IOMMU
+	bool "Hyper-V root VM IOMMU Device"
+	depends on HYPERV && X86
+	select IOMMU_API
+	default HYPERV
+	help
+	  This enables Hyper-V pseudo IOMMU device on a root VM. 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 6ef0ef97f3dd..c7e7d0dac2a4 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_IRQ_REMAP) += hv-irq-remap-x86.o
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +645 -0

645 lines not on this page · raw message

include/asm-generic/mshyperv.h +3 -0
index bf601d67cecb..fa8331c7c3a2 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 9e109d91aa14..01a69f88cfa7 100644
@@ -1783,4 +1783,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_iommu_detect(void);
+#else
+static inline void hv_iommu_detect(void) { }
+#endif /* CONFIG_HYPERV_ROOT_IOMMU */
+
 #endif /* _HYPERV_H */
Signature · 1 line
2.51.2.vfs.0.1
#9 lore raw

Re: [PATCH V0 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] iommu: Flawed partial unmap logic causes memory leak and over-unmap in hv_iommu_map_pages - [High]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] iommu: Flawed partial unmap logic causes memory leak and over-unmap in hv_iommu_map_pages
- [High] iommu: Incorrect assignment of IOVA aperture_end restricts DMA address space --

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

x86/hyperv: Implement root VM IOMMU kernel only driver

Adds a new kernel-only virtual IOMMU driver that works with the Microsoft Hyper-V hypervisor on privileged root VMs. The driver communicates with the hypervisor for managing paging domains and device mappings.

Quoted 6 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +0 -0
new file mode 100644
index 0000000000000..a5268e0e52cc2
@@ -0,0 +1,645 @@

[ ... ]

Quoted 2 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
+static struct iommu_domain *hv_iommu_domain_alloc_paging(struct device *dev)
+{

[ ... ]

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

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+	hvdom->domid_num = unique_id;
+
+	hvdom->iommu_dom.pgsize_bitmap = HV_IOMMU_PGSIZES;
+	hvdom->iommu_dom.geometry.aperture_start = 0;
+	hvdom->iommu_dom.geometry.aperture_end = hv_max_iova_width;

[Severity: High]
Does this directly assign the bit-width value to the maximum address boundary instead of creating an address mask?

The hypervisor capability max_iova_width returns the bit-width of the supported IOVA space (e.g., 39 or 48 bits), not the maximum memory address mask. Since the IOMMU core uses aperture_end as the absolute maximum address for IOVA allocations, this seems to restrict the DMA address space to a scalar bit-width value, which could cause any DMA mapping requested above address 39 or 48 to fail.

Quoted 2 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
+	hvdom->iommu_dom.geometry.force_aperture = true;
+	hvdom->iommu_dom.ops = &hv_paging_domain_ops;

[ ... ]

Quoted 4 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+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)
+{

[ ... ]

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

Quoted 13 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +13 -0
+	if (!hv_result_success(status)) {
+		size_t done_size = done << HV_HYP_PAGE_SHIFT;
+
+		hv_status_err(status, "pgs:%lx/%lx iova:%lx\n",
+			      done, npages, iova);
+		/*
+		 * lookup tree has all mappings [0 - size-1]. Below unmap will
+		 * only remove from [0 - done], we need to remove second chunk
+		 * [done+1 - size-1].
+		 */
+		hv_iommu_del_tree_mappings(hvdom, iova, size - done_size);
+		hv_iommu_unmap_pages(immdom, iova - done_size, HV_HYP_PAGE_SIZE,
+				     done, NULL);

[Severity: High]
If a batched mapping operation fails midway, does this error path inadvertently leak mappings and over-unmap pages?

When splitting the software tree mapping, the error path calls hv_iommu_del_tree_mappings(), which blocks removing the unmapped tail because it does not support splitting:

drivers/iommu/hyperv/hv-iommu-root.c:hv_iommu_del_tree_mappings() {
    ...
    /* Splitting of a mapping is not supported at present */
    if (mapping->iova.start < iova)
        break;
    ...
}

Subsequently, does the head unmap via hv_iommu_unmap_pages() delete the entire software node and use its full original size to unmap pages from the hypervisor?

This seems like it would send out-of-bounds unmap requests for pages never successfully mapped, and abandon the correctly mapped pages since the software node is destroyed.

Quoted 3 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+		if (mapped)
+			*mapped = 0;
+	} else

[ ... ]

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

Re: [PATCH V0 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

In reply to Mukesh R
Quoted 111 lines · Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works with Microsoft Hyper-V hypervisor on 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/Kconfig                |   1 +
 drivers/iommu/hyperv/Kconfig         |  15 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 645 +++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h       |   3 +
 include/linux/hyperv.h               |   6 +
 7 files changed, 673 insertions(+)
 create mode 100644 drivers/iommu/hyperv/Kconfig
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
arch/x86/kernel/pci-dma.c +2 -0
index 6267363e0189..37fed8c7a8c2 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)
 	}
 	pci_swiotlb_detect();
 	gart_iommu_hole_init();
+	hv_iommu_detect();
 	amd_iommu_detect();
 	detect_intel_iommu();
 	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
drivers/iommu/Kconfig +1 -0
index 6e07bd69467a..3e410e0f3e1d 100644
@@ -197,6 +197,7 @@ source "drivers/iommu/arm/Kconfig"
 source "drivers/iommu/intel/Kconfig"
 source "drivers/iommu/iommufd/Kconfig"
 source "drivers/iommu/riscv/Kconfig"
+source "drivers/iommu/hyperv/Kconfig"
 
 config IRQ_REMAP
 	bool "Support for Interrupt Remapping"
drivers/iommu/hyperv/Kconfig new file +15 -0
new file mode 100644
index 000000000000..4342043443e7
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Hyper-V IOMMU support
+
+config HYPERV_ROOT_IOMMU
+	bool "Hyper-V root VM IOMMU Device"
+	depends on HYPERV && X86
+	select IOMMU_API
+	default HYPERV
+	help
+	  This enables Hyper-V pseudo IOMMU device on a root VM. 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 6ef0ef97f3dd..c7e7d0dac2a4 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_IRQ_REMAP) += hv-irq-remap-x86.o
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +21 -0
new file mode 100644
index 000000000000..a5268e0e52cc
@@ -0,0 +1,645 @@
+// 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"
+
+/*
+ * We will not claim these PCI devices. Eg hypervisor debugger is using it
+ * for a dynamic debug session. They cannot be enumerated under static ACPI
+ * device scope.
+ */
+static char *hv_skip_pci_devs;
+static int __init hv_iommu_setup_skip(char *str)
+{

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	hv_skip_pci_devs = str;
+	return 1;
+}
+/* Eg: hv_iommu_skip=(SSSS:BB:DD.F)(SSSS:BB:DD.F) */
+__setup("hv_iommu_skip=", hv_iommu_setup_skip);
+
+static dma_addr_t hv_max_iova_width;
+static struct iommu_domain_ops hv_paging_domain_ops;
+
+/* IOMMU device that we export to the world. HyperV supports max of one */

Nit: HyperV -> MSHV

AIUI, HyperV is MSHV+the Windows virtualization stack.

Quoted 2 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
+static struct iommu_device hv_virt_iommu;
+

<snip>

Quoted 155 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +155 -0
+}
+
+static size_t hv_iommu_unmap_pages(struct iommu_domain *immdom, ulong iova,
+				   size_t pgsize, size_t pgcount,
+				   struct iommu_iotlb_gather *gather)
+{
+	unsigned long npages;
+	u64 status;
+	struct hv_domain *hvdom = to_hv_domain(immdom);
+	size_t unmapped, tot_done = 0, size = pgsize * pgcount;
+
+	unmapped = hv_iommu_del_tree_mappings(hvdom, iova, size);
+	if (unmapped < size)
+		pr_err("%s: could not delete all mappings (%lx:%lx/%lx)\n",
+		       __func__, iova, unmapped, size);
+
+	npages = unmapped >> 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;
+}
+
+/* Return: must return exact status from the hypercall without changes */
+static u64 hv_iommu_map_pgs(struct hv_domain *hvdom,
+			    unsigned long iova, phys_addr_t paddr,
+			    unsigned long 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);
+
+	return status;
+}
+
+#define HV_MAP_DEVICE_GPA_BATCH_SIZE   \
+	((HV_HYP_PAGE_SIZE - sizeof(struct hv_input_map_device_gpa_pages)) \
+			/ sizeof(u64))
+
+/*
+ * The core VFIO code loops over memory ranges calling this function with the
+ * largest pgsize from HV_IOMMU_PGSIZES. 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;
+	unsigned long npages, done = 0;
+	struct hv_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);
+	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(status) == HV_STATUS_INSUFFICIENT_MEMORY) {
+			ret = hv_call_deposit_pages(NUMA_NO_NODE,
+						    hv_current_partition_id,
+						    256);
+			if (ret)
+				break;
+			continue;
+		}
+		if (!hv_result_success(status))
+			break;
+	}
+
+	if (!hv_result_success(status)) {
+		size_t done_size = done << HV_HYP_PAGE_SHIFT;
+
+		hv_status_err(status, "pgs:%lx/%lx iova:%lx\n",
+			      done, npages, iova);
+		/*
+		 * lookup tree has all mappings [0 - size-1]. Below unmap will
+		 * only remove from [0 - done], we need to remove second chunk
+		 * [done+1 - size-1].
+		 */
+		hv_iommu_del_tree_mappings(hvdom, iova, size - done_size);
+		hv_iommu_unmap_pages(immdom, iova - done_size, HV_HYP_PAGE_SIZE,
+				     done, NULL);
+		if (mapped)
+			*mapped = 0;
+	} else
+		if (mapped)
+			*mapped = size;
+
+	return hv_result_to_errno(status);
+}
+
+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_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);
+	}

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
+
+	return paddr;
+}
+
+/*
+ * Currently, hypervisor does not provide list of devices it is using
+ * dynamically. So use this to allow users to manually specify devices that
+ * should be skipped. (eg. hypervisor debugger using some network device).
+ */

Maybe a flag, say OwnedByHyp = 1 returned for the root equivalent of the guest driver's GetLogicalDeviceProperty hypercall can solve this.

Quoted 32 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +32 -0
+static struct iommu_device *hv_iommu_probe_device(struct device *dev)
+{
+	if (!dev_is_pci(dev))
+		return ERR_PTR(-ENODEV);
+
+	if (hv_skip_pci_devs && *hv_skip_pci_devs) {
+		int rc, parsed, segment, bus, slot, func;
+		int pos = 0;
+		struct pci_dev *pdev = to_pci_dev(dev);
+
+		do {
+			parsed = 0;
+
+			rc = sscanf(hv_skip_pci_devs + pos, " (%x:%x:%x.%x) %n",
+				    &segment, &bus, &slot, &func, &parsed);
+
+			if (rc != 4 || parsed <= 0)
+				break;
+
+			if (pci_domain_nr(pdev->bus) == segment &&
+			    pdev->bus->number == bus &&
+			    PCI_SLOT(pdev->devfn) == slot &&
+			    PCI_FUNC(pdev->devfn) == func) {
+
+				dev_info(dev, "skipped by Hyper-V IOMMU\n");
+				return ERR_PTR(-ENODEV);
+			}
+			pos += parsed;
+
+		} while (hv_skip_pci_devs[pos]);
+	}
+

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	return &hv_virt_iommu;
+}
+
+static struct iommu_group *hv_iommu_device_group(struct device *dev)
+{
+	if (dev_is_pci(dev))
+		return pci_device_group(dev);
+
+	return generic_device_group(dev);
+}

Just like the guest driver, this collapses down to return pci_device_group(dev) since you don't support non-PCI devices.

Quoted 39 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +39 -0
+
+static void hv_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *head)
+{
+	struct iommu_resv_region *reg;
+
+	/* reserve the entire LAPIC region */
+	reg = iommu_alloc_resv_region(0xfee00000, SZ_1M, 0, IOMMU_RESV_MSI,
+				      GFP_KERNEL);
+	if (reg)
+		list_add_tail(&reg->list, 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	    = hv_iommu_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,
+};
+
+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 566

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_S2_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_S2_NULL; /* INTMAX */
+}
+

Since all of these are constants, you could just initialize them with .<field> notation when you declare the static domains above.

Quoted 40 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +40 -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_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_max_iova_width = caps.max_iova_width;
+

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	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;
+	}
+
+	/* This must come before iommu_device_register() because the latter
+	 * calls into the hooks.
+	 */
+	hv_initialize_special_domains();

If statically declared as above, no need for this call at all.

Quoted 13 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +13 -0
+
+	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);

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	return rc;
+}
+
+void __init hv_iommu_detect(void)
+{
+	if (no_iommu || iommu_detected || hv_l1vh_partition())
+		return;
+
+	if (!(ms_hyperv.misc_features & HV_DEVICE_DOMAIN_AVAILABLE))
+		return;

If there's no difference between HV_DEVICE_DOMAIN_AVAILABLE = 1 and HV_IOMMU_CAP_PRESENT = 1 combined with HV_IOMMU_CAP_S2 = 1, then we can skip the detect function and just detect and init all in one like the guest driver.

Quoted 35 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+
+	iommu_detected = 1;
+	x86_init.iommu.iommu_init = hv_iommu_init;
+
+	pci_request_acs();
+}
include/asm-generic/mshyperv.h +3 -0
index bf601d67cecb..fa8331c7c3a2 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 9e109d91aa14..01a69f88cfa7 100644
@@ -1783,4 +1783,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_iommu_detect(void);
+#else
+static inline void hv_iommu_detect(void) { }
+#endif /* CONFIG_HYPERV_ROOT_IOMMU */
+
 #endif /* _HYPERV_H */
author
#11 lore raw

Re: [PATCH V0 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Quoted 292 lines · Easwar Hariharan
Quoted 111 lines · Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works with Microsoft Hyper-V hypervisor on 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/Kconfig                |   1 +
  drivers/iommu/hyperv/Kconfig         |  15 +
  drivers/iommu/hyperv/Makefile        |   1 +
  drivers/iommu/hyperv/hv-iommu-root.c | 645 +++++++++++++++++++++++++++
  include/asm-generic/mshyperv.h       |   3 +
  include/linux/hyperv.h               |   6 +
  7 files changed, 673 insertions(+)

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

arch/x86/kernel/pci-dma.c +2 -0
index 6267363e0189..37fed8c7a8c2 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)
  	}
  	pci_swiotlb_detect();
  	gart_iommu_hole_init();
+	hv_iommu_detect();
  	amd_iommu_detect();
  	detect_intel_iommu();
  	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
drivers/iommu/Kconfig +1 -0
index 6e07bd69467a..3e410e0f3e1d 100644
@@ -197,6 +197,7 @@ source "drivers/iommu/arm/Kconfig"
  source "drivers/iommu/intel/Kconfig"
  source "drivers/iommu/iommufd/Kconfig"
  source "drivers/iommu/riscv/Kconfig"
+source "drivers/iommu/hyperv/Kconfig"
 
  config IRQ_REMAP
  	bool "Support for Interrupt Remapping"
drivers/iommu/hyperv/Kconfig new file +15 -0
new file mode 100644
index 000000000000..4342043443e7
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Hyper-V IOMMU support
+
+config HYPERV_ROOT_IOMMU
+	bool "Hyper-V root VM IOMMU Device"
+	depends on HYPERV && X86
+	select IOMMU_API
+	default HYPERV
+	help
+	  This enables Hyper-V pseudo IOMMU device on a root VM. 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 6ef0ef97f3dd..c7e7d0dac2a4 100644
@@ -1,2 +1,3 @@
  # SPDX-License-Identifier: GPL-2.0
  obj-$(CONFIG_IRQ_REMAP) += hv-irq-remap-x86.o
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +21 -0
new file mode 100644
index 000000000000..a5268e0e52cc
@@ -0,0 +1,645 @@
+// 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"
+
+/*
+ * We will not claim these PCI devices. Eg hypervisor debugger is using it
+ * for a dynamic debug session. They cannot be enumerated under static ACPI
+ * device scope.
+ */
+static char *hv_skip_pci_devs;
+static int __init hv_iommu_setup_skip(char *str)
+{

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	hv_skip_pci_devs = str;
+	return 1;
+}
+/* Eg: hv_iommu_skip=(SSSS:BB:DD.F)(SSSS:BB:DD.F) */
+__setup("hv_iommu_skip=", hv_iommu_setup_skip);
+
+static dma_addr_t hv_max_iova_width;
+static struct iommu_domain_ops hv_paging_domain_ops;
+
+/* IOMMU device that we export to the world. HyperV supports max of one */

Nit: HyperV -> MSHV

AIUI, HyperV is MSHV+the Windows virtualization stack.

Quoted 2 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +2 -0
+static struct iommu_device hv_virt_iommu;
+

<snip>

Quoted 160 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +160 -0
+}
+
+static size_t hv_iommu_unmap_pages(struct iommu_domain *immdom, ulong iova,
+				   size_t pgsize, size_t pgcount,
+				   struct iommu_iotlb_gather *gather)
+{
+	unsigned long npages;
+	u64 status;
+	struct hv_domain *hvdom = to_hv_domain(immdom);
+	size_t unmapped, tot_done = 0, size = pgsize * pgcount;
+
+	unmapped = hv_iommu_del_tree_mappings(hvdom, iova, size);
+	if (unmapped < size)
+		pr_err("%s: could not delete all mappings (%lx:%lx/%lx)\n",
+		       __func__, iova, unmapped, size);
+
+	npages = unmapped >> 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;
+}
+
+/* Return: must return exact status from the hypercall without changes */
+static u64 hv_iommu_map_pgs(struct hv_domain *hvdom,
+			    unsigned long iova, phys_addr_t paddr,
+			    unsigned long 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);
+
+	return status;
+}
+
+#define HV_MAP_DEVICE_GPA_BATCH_SIZE   \
+	((HV_HYP_PAGE_SIZE - sizeof(struct hv_input_map_device_gpa_pages)) \
+			/ sizeof(u64))
+
+/*
+ * The core VFIO code loops over memory ranges calling this function with the
+ * largest pgsize from HV_IOMMU_PGSIZES. 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;
+	unsigned long npages, done = 0;
+	struct hv_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);
+	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(status) == HV_STATUS_INSUFFICIENT_MEMORY) {
+			ret = hv_call_deposit_pages(NUMA_NO_NODE,
+						    hv_current_partition_id,
+						    256);
+			if (ret)
+				break;
+			continue;
+		}
+		if (!hv_result_success(status))
+			break;
+	}
+
+	if (!hv_result_success(status)) {
+		size_t done_size = done << HV_HYP_PAGE_SHIFT;
+
+		hv_status_err(status, "pgs:%lx/%lx iova:%lx\n",
+			      done, npages, iova);
+		/*
+		 * lookup tree has all mappings [0 - size-1]. Below unmap will
+		 * only remove from [0 - done], we need to remove second chunk
+		 * [done+1 - size-1].
+		 */
+		hv_iommu_del_tree_mappings(hvdom, iova, size - done_size);
+		hv_iommu_unmap_pages(immdom, iova - done_size, HV_HYP_PAGE_SIZE,
+				     done, NULL);
+		if (mapped)
+			*mapped = 0;
+	} else
+		if (mapped)
+			*mapped = size;
+
+	return hv_result_to_errno(status);
+}
+
+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_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;
+}
+
Quoted 8 lines
Quoted 5 lines

+/*
+ * Currently, hypervisor does not provide list of devices it is using
+ * dynamically. So use this to allow users to manually specify devices that
+ * should be skipped. (eg. hypervisor debugger using some network device).
+ */

Maybe a flag, say OwnedByHyp = 1 returned for the root equivalent of the guest driver's GetLogicalDeviceProperty hypercall can solve this.

"hypervisor does not provide list of devices it is using dynamically"

Quoted 90 lines
Quoted 32 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +32 -0
+static struct iommu_device *hv_iommu_probe_device(struct device *dev)
+{
+	if (!dev_is_pci(dev))
+		return ERR_PTR(-ENODEV);
+
+	if (hv_skip_pci_devs && *hv_skip_pci_devs) {
+		int rc, parsed, segment, bus, slot, func;
+		int pos = 0;
+		struct pci_dev *pdev = to_pci_dev(dev);
+
+		do {
+			parsed = 0;
+
+			rc = sscanf(hv_skip_pci_devs + pos, " (%x:%x:%x.%x) %n",
+				    &segment, &bus, &slot, &func, &parsed);
+
+			if (rc != 4 || parsed <= 0)
+				break;
+
+			if (pci_domain_nr(pdev->bus) == segment &&
+			    pdev->bus->number == bus &&
+			    PCI_SLOT(pdev->devfn) == slot &&
+			    PCI_FUNC(pdev->devfn) == func) {
+
+				dev_info(dev, "skipped by Hyper-V IOMMU\n");
+				return ERR_PTR(-ENODEV);
+			}
+			pos += parsed;
+
+		} while (hv_skip_pci_devs[pos]);
+	}
+

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	return &hv_virt_iommu;
+}
+
+static struct iommu_group *hv_iommu_device_group(struct device *dev)
+{
+	if (dev_is_pci(dev))
+		return pci_device_group(dev);
+
+	return generic_device_group(dev);
+}

Just like the guest driver, this collapses down to return pci_device_group(dev) since you don't support non-PCI devices.

Quoted 44 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +44 -0
+
+static void hv_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *head)
+{
+	struct iommu_resv_region *reg;
+
+	/* reserve the entire LAPIC region */
+	reg = iommu_alloc_resv_region(0xfee00000, SZ_1M, 0, IOMMU_RESV_MSI,
+				      GFP_KERNEL);
+	if (reg)
+		list_add_tail(&reg->list, 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	    = hv_iommu_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,
+};
+
+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_S2_DEFAULT; /* 0 */
+
+	hv_def_blocked_dom.iommu_dom.type = IOMMU_DOMAIN_BLOCKED;
Quoted 8 lines

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

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+	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_S2_NULL; /* INTMAX */
+}
+

Since all of these are constants, you could just initialize them with .<field> notation when you declare the static domains above.

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

Quoted 116 lines
Quoted 40 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +40 -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_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_max_iova_width = caps.max_iova_width;
+

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	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;
+	}
+
+	/* This must come before iommu_device_register() because the latter
+	 * calls into the hooks.
+	 */
+	hv_initialize_special_domains();

If statically declared as above, no need for this call at all.

Quoted 13 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +13 -0
+
+	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);

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	return rc;
+}
+
+void __init hv_iommu_detect(void)
+{
+	if (no_iommu || iommu_detected || hv_l1vh_partition())
+		return;
+
+	if (!(ms_hyperv.misc_features & HV_DEVICE_DOMAIN_AVAILABLE))
+		return;

If there's no difference between HV_DEVICE_DOMAIN_AVAILABLE = 1 and HV_IOMMU_CAP_PRESENT = 1 combined with HV_IOMMU_CAP_S2 = 1, then we can skip the detect function and just detect and init all in one like the guest driver.

Quoted 35 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+
+	iommu_detected = 1;
+	x86_init.iommu.iommu_init = hv_iommu_init;
+
+	pci_request_acs();
+}
include/asm-generic/mshyperv.h +3 -0
index bf601d67cecb..fa8331c7c3a2 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 9e109d91aa14..01a69f88cfa7 100644
@@ -1783,4 +1783,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_iommu_detect(void);
+#else
+static inline void hv_iommu_detect(void) { }
+#endif /* CONFIG_HYPERV_ROOT_IOMMU */
+
  #endif /* _HYPERV_H */
#12 lore raw

Re: [PATCH V0 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Quoted 25 lines · Mukesh R
Quoted 24 lines · Easwar Hariharan
Quoted 23 lines · Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works with Microsoft Hyper-V hypervisor on 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/Kconfig                |   1 + drivers/iommu/hyperv/Kconfig         |  15 + drivers/iommu/hyperv/Makefile        |   1 + drivers/iommu/hyperv/hv-iommu-root.c | 645 +++++++++++++++++++++++++++ include/asm-generic/mshyperv.h       |   3 + include/linux/hyperv.h               |   6 +

  7 files changed, 673 insertions(+)

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

<snip>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
new file mode 100644
index 000000000000..a5268e0e52cc
@@ -0,0 +1,645 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hyper-V root vIOMMU driver.
+ * Copyright (C) 2026, Microsoft, Inc.
+ */

<snip>

Quoted 11 lines
Quoted 9 lines

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

Quoted 6 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+
+/*
+ * Currently, hypervisor does not provide list of devices it is using
+ * dynamically. So use this to allow users to manually specify devices that
+ * should be skipped. (eg. hypervisor debugger using some network device).
+ */

Maybe a flag, say OwnedByHyp = 1 returned for the root equivalent of the guest driver's GetLogicalDeviceProperty hypercall can solve this.

"hypervisor does not provide list of devices it is using dynamically"

Sorry, can you be more verbose? When I read this response yesterday, I thought by dynamically, you meant at runtime, but on thinking this through, the current scheme doesn't help with that either. You have to pass the kernel command-line parameter at boot because the IOMMU driver is built-in, and so if a debugger is attached to the hypervisor "at runtime", your current scheme, just like my suggestion for a flag returned by GetPhysicalDeviceProperty above, requires a reboot of the root partition.

Quoted 1 line

<snip>

Quoted 2 lines
Quoted 1 line
Quoted 1 line

  #endif /* _HYPERV_H */