On 12/8/2025 9:11 PM, Yu Zhang wrote:
> Add a para-virtualized IOMMU driver for Linux guests running on Hyper-V.
> This driver implements stage-1 IO translation within the guest OS.
> It integrates with the Linux IOMMU core, utilizing Hyper-V hypercalls
> for:
>  - Capability discovery
>  - Domain allocation, configuration, and deallocation
>  - Device attachment and detachment
>  - IOTLB invalidation
> 
> The driver constructs x86-compatible stage-1 IO page tables in the
> guest memory using consolidated IO page table helpers. This allows
> the guest to manage stage-1 translations independently of vendor-
> specific drivers (like Intel VT-d or AMD IOMMU).
> 
> Hyper-v consumes this stage-1 IO page table, when a device domain is
> created and configured, and nests it with the host's stage-2 IO page
> tables, therefore elemenating the VM exits for guest IOMMU mapping
> operations.
> 
> For guest IOMMU unmapping operations, VM exits to perform the IOTLB
> flush(and possibly the device TLB flush) is still unavoidable. For
> now, HVCALL_FLUSH_DEVICE_DOMAIN       is used to implement a domain-selective
> IOTLB flush. New hypercalls for finer-grained hypercall will be provided
> in future patches.
> 
> Co-developed-by: Wei Liu <[email protected]>
> Signed-off-by: Wei Liu <[email protected]>
> Co-developed-by: Jacob Pan <[email protected]>
> Signed-off-by: Jacob Pan <[email protected]>
> Co-developed-by: Easwar Hariharan <[email protected]>
> Signed-off-by: Easwar Hariharan <[email protected]>
> Signed-off-by: Yu Zhang <[email protected]>
> ---
>  drivers/iommu/hyperv/Kconfig  |  14 +
>  drivers/iommu/hyperv/Makefile |   1 +
>  drivers/iommu/hyperv/iommu.c  | 608 ++++++++++++++++++++++++++++++++++
>  drivers/iommu/hyperv/iommu.h  |  53 +++
>  4 files changed, 676 insertions(+)
>  create mode 100644 drivers/iommu/hyperv/iommu.c
>  create mode 100644 drivers/iommu/hyperv/iommu.h
> 

<snip>

> +
> +static int __init hv_iommu_init(void)
> +{
> +     int ret = 0;
> +     struct hv_iommu_dev *hv_iommu = NULL;
> +     struct hv_output_get_iommu_capabilities hv_iommu_cap = {0};
> +
> +     if (no_iommu || iommu_detected)
> +             return -ENODEV;
> +
> +     if (!hv_is_hyperv_initialized())
> +             return -ENODEV;
> +
> +     if (hv_iommu_detect(&hv_iommu_cap) ||
> +         !hv_iommu_present(hv_iommu_cap.iommu_cap) ||
> +         !hv_iommu_s1_domain_supported(hv_iommu_cap.iommu_cap))
> +             return -ENODEV;
> +
> +     iommu_detected = 1;
> +     pci_request_acs();
> +
> +     hv_iommu = kzalloc(sizeof(*hv_iommu), GFP_KERNEL);
> +     if (!hv_iommu)
> +             return -ENOMEM;
> +
> +     hv_init_iommu_device(hv_iommu, &hv_iommu_cap);
> +
> +     ret = hv_initialize_static_domains();
> +     if (ret) {
> +             pr_err("hv_initialize_static_domains failed: %d\n", ret);
> +             goto err_sysfs_remove;

This should be goto err_free since we haven't done the sysfs_add yet

> +     }
> +
> +     ret = iommu_device_sysfs_add(&hv_iommu->iommu, NULL, NULL, "%s", 
> "hv-iommu");
> +     if (ret) {
> +             pr_err("iommu_device_sysfs_add failed: %d\n", ret);
> +             goto err_free;

And this should be probably a goto delete_static_domains that cleans up the 
allocated static
domains...

> +     }
> +
> +
> +     ret = iommu_device_register(&hv_iommu->iommu, &hv_iommu_ops, NULL);
> +     if (ret) {
> +             pr_err("iommu_device_register failed: %d\n", ret);
> +             goto err_sysfs_remove;
> +     }
> +
> +     register_syscore_ops(&hv_iommu_syscore_ops);
> +
> +     pr_info("Microsoft Hypervisor IOMMU initialized\n");
> +     return 0;
> +
> +err_sysfs_remove:
> +     iommu_device_sysfs_remove(&hv_iommu->iommu);
> +err_free:
> +     kfree(hv_iommu);
> +     return ret;
> +}
> +
> +device_initcall(hv_iommu_init);

<snip>

Reply via email to