Skip to content

Commit a71730e

Browse files
committed
iommu/amd: Allocate page-table in protection_domain_init()
Consolidate the allocation of the domain page-table in one place. Signed-off-by: Joerg Roedel <[email protected]> Reviewed-by: Suravee Suthikulpanit <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 1226c37 commit a71730e

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

drivers/iommu/amd_iommu.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
*/
7272
#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
7373

74+
#define DEFAULT_PGTABLE_LEVEL PAGE_MODE_3_LEVEL
75+
7476
static DEFINE_SPINLOCK(pd_bitmap_lock);
7577

7678
/* List of all available dev_data structures */
@@ -99,7 +101,7 @@ struct iommu_cmd {
99101
struct kmem_cache *amd_iommu_irq_cache;
100102

101103
static void update_domain(struct protection_domain *domain);
102-
static int protection_domain_init(struct protection_domain *domain);
104+
static int protection_domain_init(struct protection_domain *domain, int mode);
103105
static void detach_device(struct device *dev);
104106
static void update_and_flush_device_table(struct protection_domain *domain,
105107
struct domain_pgtable *pgtable);
@@ -1847,21 +1849,14 @@ static void dma_ops_domain_free(struct protection_domain *domain)
18471849
static struct protection_domain *dma_ops_domain_alloc(void)
18481850
{
18491851
struct protection_domain *domain;
1850-
u64 *pt_root, root;
18511852

18521853
domain = kzalloc(sizeof(struct protection_domain), GFP_KERNEL);
18531854
if (!domain)
18541855
return NULL;
18551856

1856-
if (protection_domain_init(domain))
1857-
goto free_domain;
1858-
1859-
pt_root = (void *)get_zeroed_page(GFP_KERNEL);
1860-
if (!pt_root)
1857+
if (protection_domain_init(domain, DEFAULT_PGTABLE_LEVEL))
18611858
goto free_domain;
18621859

1863-
root = amd_iommu_domain_encode_pgtable(pt_root, PAGE_MODE_3_LEVEL);
1864-
atomic64_set(&domain->pt_root, root);
18651860
domain->flags = PD_DMA_OPS_MASK;
18661861

18671862
if (iommu_get_dma_cookie(&domain->domain) == -ENOMEM)
@@ -2401,26 +2396,39 @@ static void protection_domain_free(struct protection_domain *domain)
24012396
kfree(domain);
24022397
}
24032398

2404-
static int protection_domain_init(struct protection_domain *domain)
2399+
static int protection_domain_init(struct protection_domain *domain, int mode)
24052400
{
2401+
u64 *pt_root = NULL, root;
2402+
2403+
BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
2404+
24062405
spin_lock_init(&domain->lock);
24072406
domain->id = domain_id_alloc();
24082407
if (!domain->id)
24092408
return -ENOMEM;
24102409
INIT_LIST_HEAD(&domain->dev_list);
24112410

2411+
if (mode != PAGE_MODE_NONE) {
2412+
pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2413+
if (!pt_root)
2414+
return -ENOMEM;
2415+
}
2416+
2417+
root = amd_iommu_domain_encode_pgtable(pt_root, mode);
2418+
atomic64_set(&domain->pt_root, root);
2419+
24122420
return 0;
24132421
}
24142422

2415-
static struct protection_domain *protection_domain_alloc(void)
2423+
static struct protection_domain *protection_domain_alloc(int mode)
24162424
{
24172425
struct protection_domain *domain;
24182426

24192427
domain = kzalloc(sizeof(*domain), GFP_KERNEL);
24202428
if (!domain)
24212429
return NULL;
24222430

2423-
if (protection_domain_init(domain))
2431+
if (protection_domain_init(domain, mode))
24242432
goto out_err;
24252433

24262434
return domain;
@@ -2434,23 +2442,13 @@ static struct protection_domain *protection_domain_alloc(void)
24342442
static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
24352443
{
24362444
struct protection_domain *pdomain;
2437-
u64 *pt_root, root;
24382445

24392446
switch (type) {
24402447
case IOMMU_DOMAIN_UNMANAGED:
2441-
pdomain = protection_domain_alloc();
2448+
pdomain = protection_domain_alloc(DEFAULT_PGTABLE_LEVEL);
24422449
if (!pdomain)
24432450
return NULL;
24442451

2445-
pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2446-
if (!pt_root) {
2447-
protection_domain_free(pdomain);
2448-
return NULL;
2449-
}
2450-
2451-
root = amd_iommu_domain_encode_pgtable(pt_root, PAGE_MODE_3_LEVEL);
2452-
atomic64_set(&pdomain->pt_root, root);
2453-
24542452
pdomain->domain.geometry.aperture_start = 0;
24552453
pdomain->domain.geometry.aperture_end = ~0ULL;
24562454
pdomain->domain.geometry.force_aperture = true;
@@ -2464,11 +2462,9 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
24642462
}
24652463
break;
24662464
case IOMMU_DOMAIN_IDENTITY:
2467-
pdomain = protection_domain_alloc();
2465+
pdomain = protection_domain_alloc(PAGE_MODE_NONE);
24682466
if (!pdomain)
24692467
return NULL;
2470-
2471-
atomic64_set(&pdomain->pt_root, PAGE_MODE_NONE);
24722468
break;
24732469
default:
24742470
return NULL;

0 commit comments

Comments
 (0)