Skip to content

Commit 92e2bd5

Browse files
hegdevasantjoergroedel
authored andcommitted
iommu/amd: Introduce iommu_dev_data.flags to track device capabilities
Currently we use struct iommu_dev_data.iommu_v2 to keep track of the device ATS, PRI, and PASID capabilities. But these capabilities can be enabled independently (except PRI requires ATS support). Hence, replace the iommu_v2 variable with a flags variable, which keep track of the device capabilities. From commit 9bf49e3 ("PCI/ATS: Handle sharing of PF PRI Capability with all VFs"), device PRI/PASID is shared between PF and any associated VFs. Hence use pci_pri_supported() and pci_pasid_features() instead of pci_find_ext_capability() to check device PRI/PASID support. Signed-off-by: Vasant Hegde <vasant.hegde@amd.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com> Link: https://lore.kernel.org/r/20230921092147.5930-13-vasant.hegde@amd.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 739eb25 commit 92e2bd5

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

drivers/iommu/amd/amd_iommu_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,8 @@ struct iommu_dev_data {
811811
struct protection_domain *domain; /* Domain the device is bound to */
812812
struct device *dev;
813813
u16 devid; /* PCI Device ID */
814-
bool iommu_v2; /* Device can make use of IOMMUv2 */
814+
815+
u32 flags; /* Holds AMD_IOMMU_DEVICE_FLAG_<*> */
815816
int ats_qdep;
816817
u8 ats_enabled :1; /* ATS state */
817818
u8 pri_tlp :1; /* PASID TLB required for

drivers/iommu/amd/iommu.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -319,24 +319,34 @@ static struct iommu_group *acpihid_device_group(struct device *dev)
319319
return entry->group;
320320
}
321321

322-
static bool pci_iommuv2_capable(struct pci_dev *pdev)
322+
static inline bool pdev_pasid_supported(struct iommu_dev_data *dev_data)
323323
{
324-
static const int caps[] = {
325-
PCI_EXT_CAP_ID_PRI,
326-
PCI_EXT_CAP_ID_PASID,
327-
};
328-
int i, pos;
324+
return (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP);
325+
}
329326

330-
if (!pci_ats_supported(pdev))
331-
return false;
327+
static u32 pdev_get_caps(struct pci_dev *pdev)
328+
{
329+
int features;
330+
u32 flags = 0;
331+
332+
if (pci_ats_supported(pdev))
333+
flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
334+
335+
if (pci_pri_supported(pdev))
336+
flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
332337

333-
for (i = 0; i < 2; ++i) {
334-
pos = pci_find_ext_capability(pdev, caps[i]);
335-
if (pos == 0)
336-
return false;
338+
features = pci_pasid_features(pdev);
339+
if (features >= 0) {
340+
flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
341+
342+
if (features & PCI_PASID_CAP_EXEC)
343+
flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
344+
345+
if (features & PCI_PASID_CAP_PRIV)
346+
flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
337347
}
338348

339-
return true;
349+
return flags;
340350
}
341351

342352
/*
@@ -396,8 +406,8 @@ static int iommu_init_device(struct amd_iommu *iommu, struct device *dev)
396406
* it'll be forced to go into translation mode.
397407
*/
398408
if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
399-
dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
400-
dev_data->iommu_v2 = amd_iommu_gt_ppr_supported();
409+
dev_is_pci(dev) && amd_iommu_gt_ppr_supported()) {
410+
dev_data->flags = pdev_get_caps(to_pci_dev(dev));
401411
}
402412

403413
dev_iommu_priv_set(dev, dev_data);
@@ -1850,7 +1860,7 @@ static int attach_device(struct device *dev,
18501860
goto out;
18511861
}
18521862

1853-
if (dev_data->iommu_v2) {
1863+
if (pdev_pasid_supported(dev_data)) {
18541864
if (pdev_pri_ats_enable(pdev) != 0)
18551865
goto out;
18561866

@@ -1916,7 +1926,7 @@ static void detach_device(struct device *dev)
19161926
if (!dev_is_pci(dev))
19171927
goto out;
19181928

1919-
if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
1929+
if (domain->flags & PD_IOMMUV2_MASK && pdev_pasid_supported(dev_data))
19201930
pdev_iommuv2_disable(to_pci_dev(dev));
19211931
else if (dev_data->ats_enabled)
19221932
pci_disable_ats(to_pci_dev(dev));
@@ -2471,7 +2481,7 @@ static int amd_iommu_def_domain_type(struct device *dev)
24712481
* and require remapping.
24722482
* - SNP is enabled, because it prohibits DTE[Mode]=0.
24732483
*/
2474-
if (dev_data->iommu_v2 &&
2484+
if (pdev_pasid_supported(dev_data) &&
24752485
!cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
24762486
!amd_iommu_snp_en) {
24772487
return IOMMU_DOMAIN_IDENTITY;

0 commit comments

Comments
 (0)