Skip to content

Commit 301441a

Browse files
committed
iommu/amd: Consolidate domain allocation/freeing
Merge the allocation code paths of DMA and UNMANAGED domains and remove code duplication. Signed-off-by: Joerg Roedel <[email protected]> Reviewed-by: Suravee Suthikulpanit <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 75b2774 commit 301441a

File tree

1 file changed

+27
-89
lines changed

1 file changed

+27
-89
lines changed

drivers/iommu/amd_iommu.c

Lines changed: 27 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ struct iommu_cmd {
101101
struct kmem_cache *amd_iommu_irq_cache;
102102

103103
static void update_domain(struct protection_domain *domain);
104-
static int protection_domain_init(struct protection_domain *domain, int mode);
105104
static void detach_device(struct device *dev);
106105
static void update_and_flush_device_table(struct protection_domain *domain,
107106
struct domain_pgtable *pgtable);
@@ -1818,58 +1817,6 @@ static void free_gcr3_table(struct protection_domain *domain)
18181817
free_page((unsigned long)domain->gcr3_tbl);
18191818
}
18201819

1821-
/*
1822-
* Free a domain, only used if something went wrong in the
1823-
* allocation path and we need to free an already allocated page table
1824-
*/
1825-
static void dma_ops_domain_free(struct protection_domain *domain)
1826-
{
1827-
struct domain_pgtable pgtable;
1828-
1829-
if (!domain)
1830-
return;
1831-
1832-
iommu_put_dma_cookie(&domain->domain);
1833-
1834-
amd_iommu_domain_get_pgtable(domain, &pgtable);
1835-
atomic64_set(&domain->pt_root, 0);
1836-
free_pagetable(&pgtable);
1837-
1838-
if (domain->id)
1839-
domain_id_free(domain->id);
1840-
1841-
kfree(domain);
1842-
}
1843-
1844-
/*
1845-
* Allocates a new protection domain usable for the dma_ops functions.
1846-
* It also initializes the page table and the address allocator data
1847-
* structures required for the dma_ops interface
1848-
*/
1849-
static struct protection_domain *dma_ops_domain_alloc(void)
1850-
{
1851-
struct protection_domain *domain;
1852-
1853-
domain = kzalloc(sizeof(struct protection_domain), GFP_KERNEL);
1854-
if (!domain)
1855-
return NULL;
1856-
1857-
if (protection_domain_init(domain, DEFAULT_PGTABLE_LEVEL))
1858-
goto free_domain;
1859-
1860-
domain->flags = PD_DMA_OPS_MASK;
1861-
1862-
if (iommu_get_dma_cookie(&domain->domain) == -ENOMEM)
1863-
goto free_domain;
1864-
1865-
return domain;
1866-
1867-
free_domain:
1868-
dma_ops_domain_free(domain);
1869-
1870-
return NULL;
1871-
}
1872-
18731820
/*
18741821
* little helper function to check whether a given protection domain is a
18751822
* dma_ops domain
@@ -2447,36 +2394,32 @@ static struct protection_domain *protection_domain_alloc(int mode)
24472394

24482395
static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
24492396
{
2450-
struct protection_domain *pdomain;
2451-
2452-
switch (type) {
2453-
case IOMMU_DOMAIN_UNMANAGED:
2454-
pdomain = protection_domain_alloc(DEFAULT_PGTABLE_LEVEL);
2455-
if (!pdomain)
2456-
return NULL;
2397+
struct protection_domain *domain;
2398+
int mode = DEFAULT_PGTABLE_LEVEL;
24572399

2458-
pdomain->domain.geometry.aperture_start = 0;
2459-
pdomain->domain.geometry.aperture_end = ~0ULL;
2460-
pdomain->domain.geometry.force_aperture = true;
2400+
if (type == IOMMU_DOMAIN_IDENTITY)
2401+
mode = PAGE_MODE_NONE;
24612402

2462-
break;
2463-
case IOMMU_DOMAIN_DMA:
2464-
pdomain = dma_ops_domain_alloc();
2465-
if (!pdomain) {
2466-
pr_err("Failed to allocate\n");
2467-
return NULL;
2468-
}
2469-
break;
2470-
case IOMMU_DOMAIN_IDENTITY:
2471-
pdomain = protection_domain_alloc(PAGE_MODE_NONE);
2472-
if (!pdomain)
2473-
return NULL;
2474-
break;
2475-
default:
2403+
domain = protection_domain_alloc(mode);
2404+
if (!domain)
24762405
return NULL;
2406+
2407+
domain->domain.geometry.aperture_start = 0;
2408+
domain->domain.geometry.aperture_end = ~0ULL;
2409+
domain->domain.geometry.force_aperture = true;
2410+
2411+
if (type == IOMMU_DOMAIN_DMA) {
2412+
if (iommu_get_dma_cookie(&domain->domain) == -ENOMEM)
2413+
goto free_domain;
2414+
domain->flags = PD_DMA_OPS_MASK;
24772415
}
24782416

2479-
return &pdomain->domain;
2417+
return &domain->domain;
2418+
2419+
free_domain:
2420+
protection_domain_free(domain);
2421+
2422+
return NULL;
24802423
}
24812424

24822425
static void amd_iommu_domain_free(struct iommu_domain *dom)
@@ -2493,18 +2436,13 @@ static void amd_iommu_domain_free(struct iommu_domain *dom)
24932436
if (!dom)
24942437
return;
24952438

2496-
switch (dom->type) {
2497-
case IOMMU_DOMAIN_DMA:
2498-
/* Now release the domain */
2499-
dma_ops_domain_free(domain);
2500-
break;
2501-
default:
2502-
if (domain->flags & PD_IOMMUV2_MASK)
2503-
free_gcr3_table(domain);
2439+
if (dom->type == IOMMU_DOMAIN_DMA)
2440+
iommu_put_dma_cookie(&domain->domain);
25042441

2505-
protection_domain_free(domain);
2506-
break;
2507-
}
2442+
if (domain->flags & PD_IOMMUV2_MASK)
2443+
free_gcr3_table(domain);
2444+
2445+
protection_domain_free(domain);
25082446
}
25092447

25102448
static void amd_iommu_detach_device(struct iommu_domain *dom,

0 commit comments

Comments
 (0)