@@ -854,7 +854,7 @@ static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
854
854
domain_flush_cache (domain , tmp_page , VTD_PAGE_SIZE );
855
855
pteval = ((uint64_t )virt_to_dma_pfn (tmp_page ) << VTD_PAGE_SHIFT ) | DMA_PTE_READ | DMA_PTE_WRITE ;
856
856
if (domain -> use_first_level )
857
- pteval |= DMA_FL_PTE_XD | DMA_FL_PTE_US | DMA_FL_PTE_ACCESS ;
857
+ pteval |= DMA_FL_PTE_US | DMA_FL_PTE_ACCESS ;
858
858
859
859
tmp = 0ULL ;
860
860
if (!try_cmpxchg64 (& pte -> val , & tmp , pteval ))
@@ -1359,21 +1359,6 @@ static void iommu_disable_pci_caps(struct device_domain_info *info)
1359
1359
}
1360
1360
}
1361
1361
1362
- static void __iommu_flush_dev_iotlb (struct device_domain_info * info ,
1363
- u64 addr , unsigned int mask )
1364
- {
1365
- u16 sid , qdep ;
1366
-
1367
- if (!info || !info -> ats_enabled )
1368
- return ;
1369
-
1370
- sid = info -> bus << 8 | info -> devfn ;
1371
- qdep = info -> ats_qdep ;
1372
- qi_flush_dev_iotlb (info -> iommu , sid , info -> pfsid ,
1373
- qdep , addr , mask );
1374
- quirk_extra_dev_tlb_flush (info , addr , mask , IOMMU_NO_PASID , qdep );
1375
- }
1376
-
1377
1362
static void intel_flush_iotlb_all (struct iommu_domain * domain )
1378
1363
{
1379
1364
cache_tag_flush_all (to_dmar_domain (domain ));
@@ -1872,7 +1857,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1872
1857
attr = prot & (DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP );
1873
1858
attr |= DMA_FL_PTE_PRESENT ;
1874
1859
if (domain -> use_first_level ) {
1875
- attr |= DMA_FL_PTE_XD | DMA_FL_PTE_US | DMA_FL_PTE_ACCESS ;
1860
+ attr |= DMA_FL_PTE_US | DMA_FL_PTE_ACCESS ;
1876
1861
if (prot & DMA_PTE_WRITE )
1877
1862
attr |= DMA_FL_PTE_DIRTY ;
1878
1863
}
@@ -1959,7 +1944,6 @@ static void domain_context_clear_one(struct device_domain_info *info, u8 bus, u8
1959
1944
{
1960
1945
struct intel_iommu * iommu = info -> iommu ;
1961
1946
struct context_entry * context ;
1962
- u16 did_old ;
1963
1947
1964
1948
spin_lock (& iommu -> lock );
1965
1949
context = iommu_context_addr (iommu , bus , devfn , 0 );
@@ -1968,24 +1952,10 @@ static void domain_context_clear_one(struct device_domain_info *info, u8 bus, u8
1968
1952
return ;
1969
1953
}
1970
1954
1971
- did_old = context_domain_id (context );
1972
-
1973
1955
context_clear_entry (context );
1974
1956
__iommu_flush_cache (iommu , context , sizeof (* context ));
1975
1957
spin_unlock (& iommu -> lock );
1976
- iommu -> flush .flush_context (iommu ,
1977
- did_old ,
1978
- (((u16 )bus ) << 8 ) | devfn ,
1979
- DMA_CCMD_MASK_NOBIT ,
1980
- DMA_CCMD_DEVICE_INVL );
1981
-
1982
- iommu -> flush .flush_iotlb (iommu ,
1983
- did_old ,
1984
- 0 ,
1985
- 0 ,
1986
- DMA_TLB_DSI_FLUSH );
1987
-
1988
- __iommu_flush_dev_iotlb (info , 0 , MAX_AGAW_PFN_WIDTH );
1958
+ intel_context_flush_present (info , context , true);
1989
1959
}
1990
1960
1991
1961
static int domain_setup_first_level (struct intel_iommu * iommu ,
@@ -2071,7 +2041,7 @@ static int __init si_domain_init(int hw)
2071
2041
for_each_mem_pfn_range (i , nid , & start_pfn , & end_pfn , NULL ) {
2072
2042
ret = iommu_domain_identity_map (si_domain ,
2073
2043
mm_to_dma_pfn_start (start_pfn ),
2074
- mm_to_dma_pfn_end (end_pfn ));
2044
+ mm_to_dma_pfn_end (end_pfn - 1 ));
2075
2045
if (ret )
2076
2046
return ret ;
2077
2047
}
@@ -2177,17 +2147,6 @@ static bool device_rmrr_is_relaxable(struct device *dev)
2177
2147
return false;
2178
2148
}
2179
2149
2180
- /*
2181
- * Return the required default domain type for a specific device.
2182
- *
2183
- * @dev: the device in query
2184
- * @startup: true if this is during early boot
2185
- *
2186
- * Returns:
2187
- * - IOMMU_DOMAIN_DMA: device requires a dynamic mapping domain
2188
- * - IOMMU_DOMAIN_IDENTITY: device requires an identical mapping domain
2189
- * - 0: both identity and dynamic domains work for this device
2190
- */
2191
2150
static int device_def_domain_type (struct device * dev )
2192
2151
{
2193
2152
if (dev_is_pci (dev )) {
@@ -3633,6 +3592,79 @@ static struct iommu_domain blocking_domain = {
3633
3592
}
3634
3593
};
3635
3594
3595
+ static int iommu_superpage_capability (struct intel_iommu * iommu , bool first_stage )
3596
+ {
3597
+ if (!intel_iommu_superpage )
3598
+ return 0 ;
3599
+
3600
+ if (first_stage )
3601
+ return cap_fl1gp_support (iommu -> cap ) ? 2 : 1 ;
3602
+
3603
+ return fls (cap_super_page_val (iommu -> cap ));
3604
+ }
3605
+
3606
+ static struct dmar_domain * paging_domain_alloc (struct device * dev , bool first_stage )
3607
+ {
3608
+ struct device_domain_info * info = dev_iommu_priv_get (dev );
3609
+ struct intel_iommu * iommu = info -> iommu ;
3610
+ struct dmar_domain * domain ;
3611
+ int addr_width ;
3612
+
3613
+ domain = kzalloc (sizeof (* domain ), GFP_KERNEL );
3614
+ if (!domain )
3615
+ return ERR_PTR (- ENOMEM );
3616
+
3617
+ INIT_LIST_HEAD (& domain -> devices );
3618
+ INIT_LIST_HEAD (& domain -> dev_pasids );
3619
+ INIT_LIST_HEAD (& domain -> cache_tags );
3620
+ spin_lock_init (& domain -> lock );
3621
+ spin_lock_init (& domain -> cache_lock );
3622
+ xa_init (& domain -> iommu_array );
3623
+
3624
+ domain -> nid = dev_to_node (dev );
3625
+ domain -> has_iotlb_device = info -> ats_enabled ;
3626
+ domain -> use_first_level = first_stage ;
3627
+
3628
+ /* calculate the address width */
3629
+ addr_width = agaw_to_width (iommu -> agaw );
3630
+ if (addr_width > cap_mgaw (iommu -> cap ))
3631
+ addr_width = cap_mgaw (iommu -> cap );
3632
+ domain -> gaw = addr_width ;
3633
+ domain -> agaw = iommu -> agaw ;
3634
+ domain -> max_addr = __DOMAIN_MAX_ADDR (addr_width );
3635
+
3636
+ /* iommu memory access coherency */
3637
+ domain -> iommu_coherency = iommu_paging_structure_coherency (iommu );
3638
+
3639
+ /* pagesize bitmap */
3640
+ domain -> domain .pgsize_bitmap = SZ_4K ;
3641
+ domain -> iommu_superpage = iommu_superpage_capability (iommu , first_stage );
3642
+ domain -> domain .pgsize_bitmap |= domain_super_pgsize_bitmap (domain );
3643
+
3644
+ /*
3645
+ * IOVA aperture: First-level translation restricts the input-address
3646
+ * to a canonical address (i.e., address bits 63:N have the same value
3647
+ * as address bit [N-1], where N is 48-bits with 4-level paging and
3648
+ * 57-bits with 5-level paging). Hence, skip bit [N-1].
3649
+ */
3650
+ domain -> domain .geometry .force_aperture = true;
3651
+ domain -> domain .geometry .aperture_start = 0 ;
3652
+ if (first_stage )
3653
+ domain -> domain .geometry .aperture_end = __DOMAIN_MAX_ADDR (domain -> gaw - 1 );
3654
+ else
3655
+ domain -> domain .geometry .aperture_end = __DOMAIN_MAX_ADDR (domain -> gaw );
3656
+
3657
+ /* always allocate the top pgd */
3658
+ domain -> pgd = iommu_alloc_page_node (domain -> nid , GFP_KERNEL );
3659
+ if (!domain -> pgd ) {
3660
+ kfree (domain );
3661
+ return ERR_PTR (- ENOMEM );
3662
+ }
3663
+ domain_flush_cache (domain , domain -> pgd , PAGE_SIZE );
3664
+
3665
+ return domain ;
3666
+ }
3667
+
3636
3668
static struct iommu_domain * intel_iommu_domain_alloc (unsigned type )
3637
3669
{
3638
3670
struct dmar_domain * dmar_domain ;
@@ -3695,15 +3727,14 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags,
3695
3727
if (user_data || (dirty_tracking && !ssads_supported (iommu )))
3696
3728
return ERR_PTR (- EOPNOTSUPP );
3697
3729
3698
- /*
3699
- * domain_alloc_user op needs to fully initialize a domain before
3700
- * return, so uses iommu_domain_alloc() here for simple.
3701
- */
3702
- domain = iommu_domain_alloc (dev -> bus );
3703
- if (!domain )
3704
- return ERR_PTR (- ENOMEM );
3705
-
3706
- dmar_domain = to_dmar_domain (domain );
3730
+ /* Do not use first stage for user domain translation. */
3731
+ dmar_domain = paging_domain_alloc (dev , false);
3732
+ if (IS_ERR (dmar_domain ))
3733
+ return ERR_CAST (dmar_domain );
3734
+ domain = & dmar_domain -> domain ;
3735
+ domain -> type = IOMMU_DOMAIN_UNMANAGED ;
3736
+ domain -> owner = & intel_iommu_ops ;
3737
+ domain -> ops = intel_iommu_ops .default_domain_ops ;
3707
3738
3708
3739
if (nested_parent ) {
3709
3740
dmar_domain -> nested_parent = true;
@@ -4213,6 +4244,37 @@ static int intel_iommu_enable_sva(struct device *dev)
4213
4244
return 0 ;
4214
4245
}
4215
4246
4247
+ static int context_flip_pri (struct device_domain_info * info , bool enable )
4248
+ {
4249
+ struct intel_iommu * iommu = info -> iommu ;
4250
+ u8 bus = info -> bus , devfn = info -> devfn ;
4251
+ struct context_entry * context ;
4252
+
4253
+ spin_lock (& iommu -> lock );
4254
+ if (context_copied (iommu , bus , devfn )) {
4255
+ spin_unlock (& iommu -> lock );
4256
+ return - EINVAL ;
4257
+ }
4258
+
4259
+ context = iommu_context_addr (iommu , bus , devfn , false);
4260
+ if (!context || !context_present (context )) {
4261
+ spin_unlock (& iommu -> lock );
4262
+ return - ENODEV ;
4263
+ }
4264
+
4265
+ if (enable )
4266
+ context_set_sm_pre (context );
4267
+ else
4268
+ context_clear_sm_pre (context );
4269
+
4270
+ if (!ecap_coherent (iommu -> ecap ))
4271
+ clflush_cache_range (context , sizeof (* context ));
4272
+ intel_context_flush_present (info , context , true);
4273
+ spin_unlock (& iommu -> lock );
4274
+
4275
+ return 0 ;
4276
+ }
4277
+
4216
4278
static int intel_iommu_enable_iopf (struct device * dev )
4217
4279
{
4218
4280
struct pci_dev * pdev = dev_is_pci (dev ) ? to_pci_dev (dev ) : NULL ;
@@ -4242,15 +4304,23 @@ static int intel_iommu_enable_iopf(struct device *dev)
4242
4304
if (ret )
4243
4305
return ret ;
4244
4306
4307
+ ret = context_flip_pri (info , true);
4308
+ if (ret )
4309
+ goto err_remove_device ;
4310
+
4245
4311
ret = pci_enable_pri (pdev , PRQ_DEPTH );
4246
- if (ret ) {
4247
- iopf_queue_remove_device (iommu -> iopf_queue , dev );
4248
- return ret ;
4249
- }
4312
+ if (ret )
4313
+ goto err_clear_pri ;
4250
4314
4251
4315
info -> pri_enabled = 1 ;
4252
4316
4253
4317
return 0 ;
4318
+ err_clear_pri :
4319
+ context_flip_pri (info , false);
4320
+ err_remove_device :
4321
+ iopf_queue_remove_device (iommu -> iopf_queue , dev );
4322
+
4323
+ return ret ;
4254
4324
}
4255
4325
4256
4326
static int intel_iommu_disable_iopf (struct device * dev )
@@ -4261,6 +4331,15 @@ static int intel_iommu_disable_iopf(struct device *dev)
4261
4331
if (!info -> pri_enabled )
4262
4332
return - EINVAL ;
4263
4333
4334
+ /* Disable new PRI reception: */
4335
+ context_flip_pri (info , false);
4336
+
4337
+ /*
4338
+ * Remove device from fault queue and acknowledge all outstanding
4339
+ * PRQs to the device:
4340
+ */
4341
+ iopf_queue_remove_device (iommu -> iopf_queue , dev );
4342
+
4264
4343
/*
4265
4344
* PCIe spec states that by clearing PRI enable bit, the Page
4266
4345
* Request Interface will not issue new page requests, but has
@@ -4271,7 +4350,6 @@ static int intel_iommu_disable_iopf(struct device *dev)
4271
4350
*/
4272
4351
pci_disable_pri (to_pci_dev (dev ));
4273
4352
info -> pri_enabled = 0 ;
4274
- iopf_queue_remove_device (iommu -> iopf_queue , dev );
4275
4353
4276
4354
return 0 ;
4277
4355
}
0 commit comments