Commit bac05772 authored by Vasant Hegde's avatar Vasant Hegde Committed by Joerg Roedel
Browse files

iommu/amd: Refactor protection domain allocation code



To replace if-else with switch-case statement due to increasing number of
domain types.

No functional changes intended.

Signed-off-by: default avatarVasant Hegde <vasant.hegde@amd.com>
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Reviewed-by: default avatarJerry Snitselaar <jsnitsel@redhat.com>
Link: https://lore.kernel.org/r/20230921092147.5930-5-vasant.hegde@amd.com


Signed-off-by: default avatarJoerg Roedel <jroedel@suse.de>
parent ba7d263b
Loading
Loading
Loading
Loading
+22 −23
Original line number Diff line number Diff line
@@ -2078,24 +2078,8 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
	struct io_pgtable_ops *pgtbl_ops;
	struct protection_domain *domain;
	int pgtable;
	int mode = DEFAULT_PGTABLE_LEVEL;
	int ret;

	/*
	 * Force IOMMU v1 page table when iommu=pt and
	 * when allocating domain for pass-through devices.
	 */
	if (type == IOMMU_DOMAIN_IDENTITY) {
		pgtable = AMD_IOMMU_V1;
		mode = PAGE_MODE_NONE;
	} else if (type == IOMMU_DOMAIN_UNMANAGED) {
		pgtable = AMD_IOMMU_V1;
	} else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
		pgtable = amd_iommu_pgtable;
	} else {
		return NULL;
	}

	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
	if (!domain)
		return NULL;
@@ -2106,27 +2090,42 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)

	spin_lock_init(&domain->lock);
	INIT_LIST_HEAD(&domain->dev_list);
	domain->nid = NUMA_NO_NODE;

	switch (type) {
	/* No need to allocate io pgtable ops in passthrough mode */
	case IOMMU_DOMAIN_IDENTITY:
		return domain;
	case IOMMU_DOMAIN_DMA:
	case IOMMU_DOMAIN_DMA_FQ:
		pgtable = amd_iommu_pgtable;
		break;
	/*
	 * Force IOMMU v1 page table when allocating
	 * domain for pass-through devices.
	 */
	case IOMMU_DOMAIN_UNMANAGED:
		pgtable = AMD_IOMMU_V1;
		break;
	default:
		goto out_err;
	}

	switch (pgtable) {
	case AMD_IOMMU_V1:
		ret = protection_domain_init_v1(domain, mode);
		ret = protection_domain_init_v1(domain, DEFAULT_PGTABLE_LEVEL);
		break;
	case AMD_IOMMU_V2:
		ret = protection_domain_init_v2(domain);
		break;
	default:
		ret = -EINVAL;
		break;
	}

	if (ret)
		goto out_err;

	/* No need to allocate io pgtable ops in passthrough mode */
	if (type == IOMMU_DOMAIN_IDENTITY)
		return domain;

	domain->nid = NUMA_NO_NODE;

	pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
	if (!pgtbl_ops) {
		domain_id_free(domain->id);