71
71
*/
72
72
#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
73
73
74
+ #define DEFAULT_PGTABLE_LEVEL PAGE_MODE_3_LEVEL
75
+
74
76
static DEFINE_SPINLOCK (pd_bitmap_lock );
75
77
76
78
/* List of all available dev_data structures */
@@ -99,7 +101,7 @@ struct iommu_cmd {
99
101
struct kmem_cache * amd_iommu_irq_cache ;
100
102
101
103
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 );
103
105
static void detach_device (struct device * dev );
104
106
static void update_and_flush_device_table (struct protection_domain * domain ,
105
107
struct domain_pgtable * pgtable );
@@ -1847,21 +1849,14 @@ static void dma_ops_domain_free(struct protection_domain *domain)
1847
1849
static struct protection_domain * dma_ops_domain_alloc (void )
1848
1850
{
1849
1851
struct protection_domain * domain ;
1850
- u64 * pt_root , root ;
1851
1852
1852
1853
domain = kzalloc (sizeof (struct protection_domain ), GFP_KERNEL );
1853
1854
if (!domain )
1854
1855
return NULL ;
1855
1856
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 ))
1861
1858
goto free_domain ;
1862
1859
1863
- root = amd_iommu_domain_encode_pgtable (pt_root , PAGE_MODE_3_LEVEL );
1864
- atomic64_set (& domain -> pt_root , root );
1865
1860
domain -> flags = PD_DMA_OPS_MASK ;
1866
1861
1867
1862
if (iommu_get_dma_cookie (& domain -> domain ) == - ENOMEM )
@@ -2401,26 +2396,39 @@ static void protection_domain_free(struct protection_domain *domain)
2401
2396
kfree (domain );
2402
2397
}
2403
2398
2404
- static int protection_domain_init (struct protection_domain * domain )
2399
+ static int protection_domain_init (struct protection_domain * domain , int mode )
2405
2400
{
2401
+ u64 * pt_root = NULL , root ;
2402
+
2403
+ BUG_ON (mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL );
2404
+
2406
2405
spin_lock_init (& domain -> lock );
2407
2406
domain -> id = domain_id_alloc ();
2408
2407
if (!domain -> id )
2409
2408
return - ENOMEM ;
2410
2409
INIT_LIST_HEAD (& domain -> dev_list );
2411
2410
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
+
2412
2420
return 0 ;
2413
2421
}
2414
2422
2415
- static struct protection_domain * protection_domain_alloc (void )
2423
+ static struct protection_domain * protection_domain_alloc (int mode )
2416
2424
{
2417
2425
struct protection_domain * domain ;
2418
2426
2419
2427
domain = kzalloc (sizeof (* domain ), GFP_KERNEL );
2420
2428
if (!domain )
2421
2429
return NULL ;
2422
2430
2423
- if (protection_domain_init (domain ))
2431
+ if (protection_domain_init (domain , mode ))
2424
2432
goto out_err ;
2425
2433
2426
2434
return domain ;
@@ -2434,23 +2442,13 @@ static struct protection_domain *protection_domain_alloc(void)
2434
2442
static struct iommu_domain * amd_iommu_domain_alloc (unsigned type )
2435
2443
{
2436
2444
struct protection_domain * pdomain ;
2437
- u64 * pt_root , root ;
2438
2445
2439
2446
switch (type ) {
2440
2447
case IOMMU_DOMAIN_UNMANAGED :
2441
- pdomain = protection_domain_alloc ();
2448
+ pdomain = protection_domain_alloc (DEFAULT_PGTABLE_LEVEL );
2442
2449
if (!pdomain )
2443
2450
return NULL ;
2444
2451
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
-
2454
2452
pdomain -> domain .geometry .aperture_start = 0 ;
2455
2453
pdomain -> domain .geometry .aperture_end = ~0ULL ;
2456
2454
pdomain -> domain .geometry .force_aperture = true;
@@ -2464,11 +2462,9 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2464
2462
}
2465
2463
break ;
2466
2464
case IOMMU_DOMAIN_IDENTITY :
2467
- pdomain = protection_domain_alloc ();
2465
+ pdomain = protection_domain_alloc (PAGE_MODE_NONE );
2468
2466
if (!pdomain )
2469
2467
return NULL ;
2470
-
2471
- atomic64_set (& pdomain -> pt_root , PAGE_MODE_NONE );
2472
2468
break ;
2473
2469
default :
2474
2470
return NULL ;
0 commit comments