@@ -854,7 +854,7 @@ static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
854854 domain_flush_cache (domain , tmp_page , VTD_PAGE_SIZE );
855855 pteval = ((uint64_t )virt_to_dma_pfn (tmp_page ) << VTD_PAGE_SHIFT ) | DMA_PTE_READ | DMA_PTE_WRITE ;
856856 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 ;
858858
859859 tmp = 0ULL ;
860860 if (!try_cmpxchg64 (& pte -> val , & tmp , pteval ))
@@ -1359,21 +1359,6 @@ static void iommu_disable_pci_caps(struct device_domain_info *info)
13591359 }
13601360}
13611361
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-
13771362static void intel_flush_iotlb_all (struct iommu_domain * domain )
13781363{
13791364 cache_tag_flush_all (to_dmar_domain (domain ));
@@ -1872,7 +1857,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
18721857 attr = prot & (DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP );
18731858 attr |= DMA_FL_PTE_PRESENT ;
18741859 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 ;
18761861 if (prot & DMA_PTE_WRITE )
18771862 attr |= DMA_FL_PTE_DIRTY ;
18781863 }
@@ -1959,7 +1944,6 @@ static void domain_context_clear_one(struct device_domain_info *info, u8 bus, u8
19591944{
19601945 struct intel_iommu * iommu = info -> iommu ;
19611946 struct context_entry * context ;
1962- u16 did_old ;
19631947
19641948 spin_lock (& iommu -> lock );
19651949 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
19681952 return ;
19691953 }
19701954
1971- did_old = context_domain_id (context );
1972-
19731955 context_clear_entry (context );
19741956 __iommu_flush_cache (iommu , context , sizeof (* context ));
19751957 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);
19891959}
19901960
19911961static int domain_setup_first_level (struct intel_iommu * iommu ,
@@ -2071,7 +2041,7 @@ static int __init si_domain_init(int hw)
20712041 for_each_mem_pfn_range (i , nid , & start_pfn , & end_pfn , NULL ) {
20722042 ret = iommu_domain_identity_map (si_domain ,
20732043 mm_to_dma_pfn_start (start_pfn ),
2074- mm_to_dma_pfn_end (end_pfn ));
2044+ mm_to_dma_pfn_end (end_pfn - 1 ));
20752045 if (ret )
20762046 return ret ;
20772047 }
@@ -2177,17 +2147,6 @@ static bool device_rmrr_is_relaxable(struct device *dev)
21772147 return false;
21782148}
21792149
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- */
21912150static int device_def_domain_type (struct device * dev )
21922151{
21932152 if (dev_is_pci (dev )) {
@@ -3633,6 +3592,79 @@ static struct iommu_domain blocking_domain = {
36333592 }
36343593};
36353594
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+
36363668static struct iommu_domain * intel_iommu_domain_alloc (unsigned type )
36373669{
36383670 struct dmar_domain * dmar_domain ;
@@ -3695,15 +3727,14 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags,
36953727 if (user_data || (dirty_tracking && !ssads_supported (iommu )))
36963728 return ERR_PTR (- EOPNOTSUPP );
36973729
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 ;
37073738
37083739 if (nested_parent ) {
37093740 dmar_domain -> nested_parent = true;
@@ -4213,6 +4244,37 @@ static int intel_iommu_enable_sva(struct device *dev)
42134244 return 0 ;
42144245}
42154246
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+
42164278static int intel_iommu_enable_iopf (struct device * dev )
42174279{
42184280 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)
42424304 if (ret )
42434305 return ret ;
42444306
4307+ ret = context_flip_pri (info , true);
4308+ if (ret )
4309+ goto err_remove_device ;
4310+
42454311 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 ;
42504314
42514315 info -> pri_enabled = 1 ;
42524316
42534317 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 ;
42544324}
42554325
42564326static int intel_iommu_disable_iopf (struct device * dev )
@@ -4261,6 +4331,15 @@ static int intel_iommu_disable_iopf(struct device *dev)
42614331 if (!info -> pri_enabled )
42624332 return - EINVAL ;
42634333
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+
42644343 /*
42654344 * PCIe spec states that by clearing PRI enable bit, the Page
42664345 * Request Interface will not issue new page requests, but has
@@ -4271,7 +4350,6 @@ static int intel_iommu_disable_iopf(struct device *dev)
42714350 */
42724351 pci_disable_pri (to_pci_dev (dev ));
42734352 info -> pri_enabled = 0 ;
4274- iopf_queue_remove_device (iommu -> iopf_queue , dev );
42754353
42764354 return 0 ;
42774355}
0 commit comments