Re: [PATCH 07/22] iommu/amd: Add support for AMD vIOMMU VF MMIO region

Weinan Liu
2026-03-31T05:55:42Z

On Mon, Mar 30, 2026 at 1:43 AM Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> wrote:
>
> diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c
> index 6fba5d9b1310..5dcd3fc3ba99 100644
> --- a/drivers/iommu/amd/iommufd.c
> +++ b/drivers/iommu/amd/iommufd.c
> @@ -66,11 +69,21 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *
> return aviommu->gid;
> pr_debug("%s: gid=%#x", __func__, aviommu->gid);
>
> + page_base = amd_viommu_get_vfmmio_addr(iommu, aviommu->gid);
According to the below implementation and prototype, amd_viommu_get_vfmmio_addr() won't return negative value.

> + if (page_base <= 0)
> + return -ENODEV;
goto err_out here as well to avoid leaking the gid allocated above.

> +
> + ret = iommufd_viommu_alloc_mmap(&aviommu->core,
> + page_base, SZ_4K,
> + (unsigned long *)&data.out_vfmmio_mmap_offset);
> + if (ret)
> + goto err_out;
> +
> ret = iommu_copy_struct_to_user(user_data, &data,
> IOMMU_VIOMMU_TYPE_AMD,
> reserved);
> if (ret)
> - goto err_out;
> + goto free_mmap;
>
> viommu->ops = &amd_viommu_ops;
>
> @@ -79,6 +92,8 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *
> spin_unlock_irqrestore(&pdom->lock, flags);
>
> return 0;
> +free_mmap:
> + iommufd_viommu_destroy_mmap(&aviommu->core, data.out_vfmmio_mmap_offset);
> err_out:
> amd_iommu_gid_free(aviommu->gid);
> return ret;
> diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c

>
> +/*
> + * Returns VF MMIO BAR offset for the give guest ID which will be
> + * mapped to guest vIOMMU 3rd 4K MMIO address
> + */
> +u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid)
> +{
> + /* TODO: Add check for sVIOMMU and set gid[bit 15] */
> + return iommu->vf_base_phys + gid * VIOMMU_VF_MMIO_ENTRY_SIZE;
> +}
> +EXPORT_SYMBOL(amd_viommu_get_vfmmio_addr);