Skip to content

Commit bac0577

Browse files
hegdevasantjoergroedel
authored andcommitted
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: Vasant Hegde <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Reviewed-by: Jerry Snitselaar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent ba7d263 commit bac0577

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

drivers/iommu/amd/iommu.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,24 +2078,8 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
20782078
struct io_pgtable_ops *pgtbl_ops;
20792079
struct protection_domain *domain;
20802080
int pgtable;
2081-
int mode = DEFAULT_PGTABLE_LEVEL;
20822081
int ret;
20832082

2084-
/*
2085-
* Force IOMMU v1 page table when iommu=pt and
2086-
* when allocating domain for pass-through devices.
2087-
*/
2088-
if (type == IOMMU_DOMAIN_IDENTITY) {
2089-
pgtable = AMD_IOMMU_V1;
2090-
mode = PAGE_MODE_NONE;
2091-
} else if (type == IOMMU_DOMAIN_UNMANAGED) {
2092-
pgtable = AMD_IOMMU_V1;
2093-
} else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
2094-
pgtable = amd_iommu_pgtable;
2095-
} else {
2096-
return NULL;
2097-
}
2098-
20992083
domain = kzalloc(sizeof(*domain), GFP_KERNEL);
21002084
if (!domain)
21012085
return NULL;
@@ -2106,27 +2090,42 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
21062090

21072091
spin_lock_init(&domain->lock);
21082092
INIT_LIST_HEAD(&domain->dev_list);
2093+
domain->nid = NUMA_NO_NODE;
2094+
2095+
switch (type) {
2096+
/* No need to allocate io pgtable ops in passthrough mode */
2097+
case IOMMU_DOMAIN_IDENTITY:
2098+
return domain;
2099+
case IOMMU_DOMAIN_DMA:
2100+
case IOMMU_DOMAIN_DMA_FQ:
2101+
pgtable = amd_iommu_pgtable;
2102+
break;
2103+
/*
2104+
* Force IOMMU v1 page table when allocating
2105+
* domain for pass-through devices.
2106+
*/
2107+
case IOMMU_DOMAIN_UNMANAGED:
2108+
pgtable = AMD_IOMMU_V1;
2109+
break;
2110+
default:
2111+
goto out_err;
2112+
}
21092113

21102114
switch (pgtable) {
21112115
case AMD_IOMMU_V1:
2112-
ret = protection_domain_init_v1(domain, mode);
2116+
ret = protection_domain_init_v1(domain, DEFAULT_PGTABLE_LEVEL);
21132117
break;
21142118
case AMD_IOMMU_V2:
21152119
ret = protection_domain_init_v2(domain);
21162120
break;
21172121
default:
21182122
ret = -EINVAL;
2123+
break;
21192124
}
21202125

21212126
if (ret)
21222127
goto out_err;
21232128

2124-
/* No need to allocate io pgtable ops in passthrough mode */
2125-
if (type == IOMMU_DOMAIN_IDENTITY)
2126-
return domain;
2127-
2128-
domain->nid = NUMA_NO_NODE;
2129-
21302129
pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
21312130
if (!pgtbl_ops) {
21322131
domain_id_free(domain->id);

0 commit comments

Comments
 (0)