Skip to content

Commit fc0051c

Browse files
LuBaolujoergroedel
authored andcommitted
iommu/vt-d: Check domain force_snooping against attached devices
As domain->force_snooping only impacts the devices attached with the domain, there's no need to check against all IOMMU units. On the other hand, force_snooping could be set on a domain no matter whether it has been attached or not, and once set it is an immutable flag. If no device attached, the operation always succeeds. Then this empty domain can be only attached to a device of which the IOMMU supports snoop control. Signed-off-by: Lu Baolu <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent 9d6ab26 commit fc0051c

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

drivers/iommu/intel/iommu.c

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,7 +2438,7 @@ static int domain_setup_first_level(struct intel_iommu *iommu,
24382438
if (level == 5)
24392439
flags |= PASID_FLAG_FL5LP;
24402440

2441-
if (domain->domain.type == IOMMU_DOMAIN_UNMANAGED)
2441+
if (domain->force_snooping)
24422442
flags |= PASID_FLAG_PAGE_SNOOP;
24432443

24442444
return intel_pasid_setup_first_level(iommu, dev, (pgd_t *)pgd, pasid,
@@ -4410,7 +4410,7 @@ static int intel_iommu_map(struct iommu_domain *domain,
44104410
prot |= DMA_PTE_READ;
44114411
if (iommu_prot & IOMMU_WRITE)
44124412
prot |= DMA_PTE_WRITE;
4413-
if (dmar_domain->force_snooping)
4413+
if (dmar_domain->set_pte_snp)
44144414
prot |= DMA_PTE_SNP;
44154415

44164416
max_addr = iova + size;
@@ -4533,13 +4533,60 @@ static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
45334533
return phys;
45344534
}
45354535

4536+
static bool domain_support_force_snooping(struct dmar_domain *domain)
4537+
{
4538+
struct device_domain_info *info;
4539+
bool support = true;
4540+
4541+
assert_spin_locked(&device_domain_lock);
4542+
list_for_each_entry(info, &domain->devices, link) {
4543+
if (!ecap_sc_support(info->iommu->ecap)) {
4544+
support = false;
4545+
break;
4546+
}
4547+
}
4548+
4549+
return support;
4550+
}
4551+
4552+
static void domain_set_force_snooping(struct dmar_domain *domain)
4553+
{
4554+
struct device_domain_info *info;
4555+
4556+
assert_spin_locked(&device_domain_lock);
4557+
4558+
/*
4559+
* Second level page table supports per-PTE snoop control. The
4560+
* iommu_map() interface will handle this by setting SNP bit.
4561+
*/
4562+
if (!domain_use_first_level(domain)) {
4563+
domain->set_pte_snp = true;
4564+
return;
4565+
}
4566+
4567+
list_for_each_entry(info, &domain->devices, link)
4568+
intel_pasid_setup_page_snoop_control(info->iommu, info->dev,
4569+
PASID_RID2PASID);
4570+
}
4571+
45364572
static bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain)
45374573
{
45384574
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
4575+
unsigned long flags;
45394576

4540-
if (!domain_update_iommu_snooping(NULL))
4577+
if (dmar_domain->force_snooping)
4578+
return true;
4579+
4580+
spin_lock_irqsave(&device_domain_lock, flags);
4581+
if (!domain_support_force_snooping(dmar_domain)) {
4582+
spin_unlock_irqrestore(&device_domain_lock, flags);
45414583
return false;
4584+
}
4585+
4586+
domain_set_force_snooping(dmar_domain);
45424587
dmar_domain->force_snooping = true;
4588+
spin_unlock_irqrestore(&device_domain_lock, flags);
4589+
45434590
return true;
45444591
}
45454592

drivers/iommu/intel/pasid.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,45 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
762762

763763
return 0;
764764
}
765+
766+
/*
767+
* Set the page snoop control for a pasid entry which has been set up.
768+
*/
769+
void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
770+
struct device *dev, u32 pasid)
771+
{
772+
struct pasid_entry *pte;
773+
u16 did;
774+
775+
spin_lock(&iommu->lock);
776+
pte = intel_pasid_get_entry(dev, pasid);
777+
if (WARN_ON(!pte || !pasid_pte_is_present(pte))) {
778+
spin_unlock(&iommu->lock);
779+
return;
780+
}
781+
782+
pasid_set_pgsnp(pte);
783+
did = pasid_get_domain_id(pte);
784+
spin_unlock(&iommu->lock);
785+
786+
if (!ecap_coherent(iommu->ecap))
787+
clflush_cache_range(pte, sizeof(*pte));
788+
789+
/*
790+
* VT-d spec 3.4 table23 states guides for cache invalidation:
791+
*
792+
* - PASID-selective-within-Domain PASID-cache invalidation
793+
* - PASID-selective PASID-based IOTLB invalidation
794+
* - If (pasid is RID_PASID)
795+
* - Global Device-TLB invalidation to affected functions
796+
* Else
797+
* - PASID-based Device-TLB invalidation (with S=1 and
798+
* Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
799+
*/
800+
pasid_cache_invalidation_with_pasid(iommu, did, pasid);
801+
qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
802+
803+
/* Device IOTLB doesn't need to be flushed in caching mode. */
804+
if (!cap_caching_mode(iommu->cap))
805+
devtlb_invalidation_with_pasid(iommu, dev, pasid);
806+
}

drivers/iommu/intel/pasid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,6 @@ void intel_pasid_tear_down_entry(struct intel_iommu *iommu,
123123
bool fault_ignore);
124124
int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid);
125125
void vcmd_free_pasid(struct intel_iommu *iommu, u32 pasid);
126+
void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
127+
struct device *dev, u32 pasid);
126128
#endif /* __INTEL_PASID_H */

include/linux/intel-iommu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ struct dmar_domain {
540540
u8 has_iotlb_device: 1;
541541
u8 iommu_coherency: 1; /* indicate coherency of iommu access */
542542
u8 force_snooping : 1; /* Create IOPTEs with snoop control */
543+
u8 set_pte_snp:1;
543544

544545
struct list_head devices; /* all devices' list */
545546
struct iova_domain iovad; /* iova's that belong to this domain */

0 commit comments

Comments
 (0)